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 | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 321 | cast_string_recursive(const struct lyd_node *node, ly_bool 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 | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 467 | cast_string_elem(struct lyd_node *node, ly_bool 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 | * |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 546 | * Implementation of ::values_equal_cb. |
| 547 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 548 | * @param[in] val1_p First value. |
| 549 | * @param[in] val2_p Second value. |
| 550 | * @param[in] mod Whether hash table is being modified. |
| 551 | * @param[in] cb_data Callback data. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 552 | * @return Boolean value whether values are equal or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 553 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 554 | static ly_bool |
| 555 | set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data)) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 556 | { |
| 557 | struct lyxp_set_hash_node *val1, *val2; |
| 558 | |
| 559 | val1 = (struct lyxp_set_hash_node *)val1_p; |
| 560 | val2 = (struct lyxp_set_hash_node *)val2_p; |
| 561 | |
| 562 | if ((val1->node == val2->node) && (val1->type == val2->type)) { |
| 563 | return 1; |
| 564 | } |
| 565 | |
| 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * @brief Insert node and its hash into set. |
| 571 | * |
| 572 | * @param[in] set et to insert to. |
| 573 | * @param[in] node Node with hash. |
| 574 | * @param[in] type Node type. |
| 575 | */ |
| 576 | static void |
| 577 | set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 578 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 579 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 580 | uint32_t i, hash; |
| 581 | struct lyxp_set_hash_node hnode; |
| 582 | |
| 583 | if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) { |
| 584 | /* create hash table and add all the nodes */ |
| 585 | set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1); |
| 586 | for (i = 0; i < set->used; ++i) { |
| 587 | hnode.node = set->val.nodes[i].node; |
| 588 | hnode.type = set->val.nodes[i].type; |
| 589 | |
| 590 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 591 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 592 | hash = dict_hash_multi(hash, NULL, 0); |
| 593 | |
| 594 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 595 | assert(!r); |
| 596 | (void)r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 597 | |
Michal Vasko | 9d6befd | 2019-12-11 14:56:56 +0100 | [diff] [blame] | 598 | if (hnode.node == node) { |
| 599 | /* it was just added, do not add it twice */ |
| 600 | node = NULL; |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 606 | /* add the new node into hash table */ |
| 607 | hnode.node = node; |
| 608 | hnode.type = type; |
| 609 | |
| 610 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 611 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 612 | hash = dict_hash_multi(hash, NULL, 0); |
| 613 | |
| 614 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 615 | assert(!r); |
| 616 | (void)r; |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | /** |
| 621 | * @brief Remove node and its hash from set. |
| 622 | * |
| 623 | * @param[in] set Set to remove from. |
| 624 | * @param[in] node Node to remove. |
| 625 | * @param[in] type Node type. |
| 626 | */ |
| 627 | static void |
| 628 | set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 629 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 630 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 631 | struct lyxp_set_hash_node hnode; |
| 632 | uint32_t hash; |
| 633 | |
| 634 | if (set->ht) { |
| 635 | hnode.node = node; |
| 636 | hnode.type = type; |
| 637 | |
| 638 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 639 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 640 | hash = dict_hash_multi(hash, NULL, 0); |
| 641 | |
| 642 | r = lyht_remove(set->ht, &hnode, hash); |
| 643 | assert(!r); |
| 644 | (void)r; |
| 645 | |
| 646 | if (!set->ht->used) { |
| 647 | lyht_free(set->ht); |
| 648 | set->ht = NULL; |
| 649 | } |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | /** |
| 654 | * @brief Check whether node is in set based on its hash. |
| 655 | * |
| 656 | * @param[in] set Set to search in. |
| 657 | * @param[in] node Node to search for. |
| 658 | * @param[in] type Node type. |
| 659 | * @param[in] skip_idx Index in @p set to skip. |
| 660 | * @return LY_ERR |
| 661 | */ |
| 662 | static LY_ERR |
| 663 | set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx) |
| 664 | { |
| 665 | struct lyxp_set_hash_node hnode, *match_p; |
| 666 | uint32_t hash; |
| 667 | |
| 668 | hnode.node = node; |
| 669 | hnode.type = type; |
| 670 | |
| 671 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 672 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 673 | hash = dict_hash_multi(hash, NULL, 0); |
| 674 | |
| 675 | if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) { |
| 676 | if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) { |
| 677 | /* we found it on the index that should be skipped, find another */ |
| 678 | hnode = *match_p; |
| 679 | if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) { |
| 680 | /* none other found */ |
| 681 | return LY_SUCCESS; |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | return LY_EEXIST; |
| 686 | } |
| 687 | |
| 688 | /* not found */ |
| 689 | return LY_SUCCESS; |
| 690 | } |
| 691 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 692 | void |
| 693 | lyxp_set_free_content(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 694 | { |
| 695 | if (!set) { |
| 696 | return; |
| 697 | } |
| 698 | |
| 699 | if (set->type == LYXP_SET_NODE_SET) { |
| 700 | free(set->val.nodes); |
| 701 | lyht_free(set->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 702 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 703 | free(set->val.scnodes); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 704 | lyht_free(set->ht); |
| 705 | } else { |
| 706 | if (set->type == LYXP_SET_STRING) { |
| 707 | free(set->val.str); |
| 708 | } |
| 709 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 710 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 711 | |
| 712 | set->val.nodes = NULL; |
| 713 | set->used = 0; |
| 714 | set->size = 0; |
| 715 | set->ht = NULL; |
| 716 | set->ctx_pos = 0; |
| 717 | set->ctx_pos = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 718 | } |
| 719 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 720 | /** |
| 721 | * @brief Free dynamically-allocated set. |
| 722 | * |
| 723 | * @param[in] set Set to free. |
| 724 | */ |
| 725 | static void |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 726 | lyxp_set_free(struct lyxp_set *set) |
| 727 | { |
| 728 | if (!set) { |
| 729 | return; |
| 730 | } |
| 731 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 732 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 733 | free(set); |
| 734 | } |
| 735 | |
| 736 | /** |
| 737 | * @brief Initialize set context. |
| 738 | * |
| 739 | * @param[in] new Set to initialize. |
| 740 | * @param[in] set Arbitrary initialized set. |
| 741 | */ |
| 742 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 743 | set_init(struct lyxp_set *new, const struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 744 | { |
| 745 | memset(new, 0, sizeof *new); |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 746 | if (set) { |
| 747 | new->ctx = set->ctx; |
| 748 | new->ctx_node = set->ctx_node; |
Michal Vasko | 588112f | 2019-12-10 14:51:53 +0100 | [diff] [blame] | 749 | new->root_type = set->root_type; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 750 | new->context_op = set->context_op; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 751 | new->local_mod = set->local_mod; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 752 | new->tree = set->tree; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 753 | new->format = set->format; |
| 754 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | /** |
| 758 | * @brief Create a deep copy of a set. |
| 759 | * |
| 760 | * @param[in] set Set to copy. |
| 761 | * @return Copy of @p set. |
| 762 | */ |
| 763 | static struct lyxp_set * |
| 764 | set_copy(struct lyxp_set *set) |
| 765 | { |
| 766 | struct lyxp_set *ret; |
| 767 | uint16_t i; |
| 768 | |
| 769 | if (!set) { |
| 770 | return NULL; |
| 771 | } |
| 772 | |
| 773 | ret = malloc(sizeof *ret); |
| 774 | LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL); |
| 775 | set_init(ret, set); |
| 776 | |
| 777 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 778 | ret->type = set->type; |
| 779 | |
| 780 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 781 | 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] | 782 | uint32_t idx; |
| 783 | LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx), |
| 784 | lyxp_set_free(ret), NULL); |
Michal Vasko | 3f27c52 | 2020-01-06 08:37:49 +0100 | [diff] [blame] | 785 | /* coverity seems to think scnodes can be NULL */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 786 | if (!ret->val.scnodes) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 787 | lyxp_set_free(ret); |
| 788 | return NULL; |
| 789 | } |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 790 | ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 791 | } |
| 792 | } |
| 793 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 794 | ret->type = set->type; |
| 795 | ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes); |
| 796 | LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL); |
| 797 | memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes); |
| 798 | |
| 799 | ret->used = ret->size = set->used; |
| 800 | ret->ctx_pos = set->ctx_pos; |
| 801 | ret->ctx_size = set->ctx_size; |
| 802 | ret->ht = lyht_dup(set->ht); |
| 803 | } else { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 804 | memcpy(ret, set, sizeof *ret); |
| 805 | if (set->type == LYXP_SET_STRING) { |
| 806 | ret->val.str = strdup(set->val.str); |
| 807 | LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL); |
| 808 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | return ret; |
| 812 | } |
| 813 | |
| 814 | /** |
| 815 | * @brief Fill XPath set with a string. Any current data are disposed of. |
| 816 | * |
| 817 | * @param[in] set Set to fill. |
| 818 | * @param[in] string String to fill into \p set. |
| 819 | * @param[in] str_len Length of \p string. 0 is a valid value! |
| 820 | */ |
| 821 | static void |
| 822 | set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len) |
| 823 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 824 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 825 | |
| 826 | set->type = LYXP_SET_STRING; |
| 827 | if ((str_len == 0) && (string[0] != '\0')) { |
| 828 | string = ""; |
| 829 | } |
| 830 | set->val.str = strndup(string, str_len); |
| 831 | } |
| 832 | |
| 833 | /** |
| 834 | * @brief Fill XPath set with a number. Any current data are disposed of. |
| 835 | * |
| 836 | * @param[in] set Set to fill. |
| 837 | * @param[in] number Number to fill into \p set. |
| 838 | */ |
| 839 | static void |
| 840 | set_fill_number(struct lyxp_set *set, long double number) |
| 841 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 842 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 843 | |
| 844 | set->type = LYXP_SET_NUMBER; |
| 845 | set->val.num = number; |
| 846 | } |
| 847 | |
| 848 | /** |
| 849 | * @brief Fill XPath set with a boolean. Any current data are disposed of. |
| 850 | * |
| 851 | * @param[in] set Set to fill. |
| 852 | * @param[in] boolean Boolean to fill into \p set. |
| 853 | */ |
| 854 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 855 | set_fill_boolean(struct lyxp_set *set, ly_bool boolean) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 856 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 857 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 858 | |
| 859 | set->type = LYXP_SET_BOOLEAN; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 860 | set->val.bln = boolean; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | /** |
| 864 | * @brief Fill XPath set with the value from another set (deep assign). |
| 865 | * Any current data are disposed of. |
| 866 | * |
| 867 | * @param[in] trg Set to fill. |
| 868 | * @param[in] src Source set to copy into \p trg. |
| 869 | */ |
| 870 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 871 | set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 872 | { |
| 873 | if (!trg || !src) { |
| 874 | return; |
| 875 | } |
| 876 | |
| 877 | if (trg->type == LYXP_SET_NODE_SET) { |
| 878 | free(trg->val.nodes); |
| 879 | } else if (trg->type == LYXP_SET_STRING) { |
| 880 | free(trg->val.str); |
| 881 | } |
| 882 | set_init(trg, src); |
| 883 | |
| 884 | if (src->type == LYXP_SET_SCNODE_SET) { |
| 885 | trg->type = LYXP_SET_SCNODE_SET; |
| 886 | trg->used = src->used; |
| 887 | trg->size = src->used; |
| 888 | |
| 889 | trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes); |
| 890 | LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 891 | memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes); |
| 892 | } else if (src->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 893 | set_fill_boolean(trg, src->val.bln); |
Michal Vasko | 44f3d2c | 2020-08-24 09:49:38 +0200 | [diff] [blame] | 894 | } else if (src->type == LYXP_SET_NUMBER) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 895 | set_fill_number(trg, src->val.num); |
| 896 | } else if (src->type == LYXP_SET_STRING) { |
| 897 | set_fill_string(trg, src->val.str, strlen(src->val.str)); |
| 898 | } else { |
| 899 | if (trg->type == LYXP_SET_NODE_SET) { |
| 900 | free(trg->val.nodes); |
| 901 | } else if (trg->type == LYXP_SET_STRING) { |
| 902 | free(trg->val.str); |
| 903 | } |
| 904 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 905 | assert(src->type == LYXP_SET_NODE_SET); |
| 906 | |
| 907 | trg->type = LYXP_SET_NODE_SET; |
| 908 | trg->used = src->used; |
| 909 | trg->size = src->used; |
| 910 | trg->ctx_pos = src->ctx_pos; |
| 911 | trg->ctx_size = src->ctx_size; |
| 912 | |
| 913 | trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes); |
| 914 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 915 | memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes); |
| 916 | if (src->ht) { |
| 917 | trg->ht = lyht_dup(src->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 918 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 919 | trg->ht = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 920 | } |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | /** |
| 925 | * @brief Clear context of all schema nodes. |
| 926 | * |
| 927 | * @param[in] set Set to clear. |
| 928 | */ |
| 929 | static void |
| 930 | set_scnode_clear_ctx(struct lyxp_set *set) |
| 931 | { |
| 932 | uint32_t i; |
| 933 | |
| 934 | for (i = 0; i < set->used; ++i) { |
| 935 | if (set->val.scnodes[i].in_ctx == 1) { |
| 936 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 937 | } else if (set->val.scnodes[i].in_ctx == -2) { |
| 938 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 939 | } |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | /** |
| 944 | * @brief Remove a node from a set. Removing last node changes |
| 945 | * set into LYXP_SET_EMPTY. Context position aware. |
| 946 | * |
| 947 | * @param[in] set Set to use. |
| 948 | * @param[in] idx Index from @p set of the node to be removed. |
| 949 | */ |
| 950 | static void |
| 951 | set_remove_node(struct lyxp_set *set, uint32_t idx) |
| 952 | { |
| 953 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 954 | assert(idx < set->used); |
| 955 | |
| 956 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 957 | |
| 958 | --set->used; |
| 959 | if (set->used) { |
| 960 | memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], |
| 961 | (set->used - idx) * sizeof *set->val.nodes); |
| 962 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 963 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 964 | } |
| 965 | } |
| 966 | |
| 967 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 968 | * @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] | 969 | * |
| 970 | * @param[in] set Set to use. |
| 971 | * @param[in] idx Index from @p set of the node to be removed. |
| 972 | */ |
| 973 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 974 | set_remove_node_none(struct lyxp_set *set, uint32_t idx) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 975 | { |
| 976 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 977 | assert(idx < set->used); |
| 978 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 979 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 980 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 981 | } |
| 982 | set->val.nodes[idx].type = LYXP_NODE_NONE; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 986 | * @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] | 987 | * set into LYXP_SET_EMPTY. Context position aware. |
| 988 | * |
| 989 | * @param[in] set Set to consolidate. |
| 990 | */ |
| 991 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 992 | set_remove_nodes_none(struct lyxp_set *set) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 993 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 994 | uint16_t i, orig_used, end = 0; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 995 | int32_t start; |
| 996 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 997 | assert(set); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 998 | |
| 999 | orig_used = set->used; |
| 1000 | set->used = 0; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1001 | for (i = 0; i < orig_used; ) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1002 | start = -1; |
| 1003 | do { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1004 | if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1005 | start = i; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1006 | } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1007 | end = i; |
| 1008 | ++i; |
| 1009 | break; |
| 1010 | } |
| 1011 | |
| 1012 | ++i; |
| 1013 | if (i == orig_used) { |
| 1014 | end = i; |
| 1015 | } |
| 1016 | } while (i < orig_used); |
| 1017 | |
| 1018 | if (start > -1) { |
| 1019 | /* move the whole chunk of valid nodes together */ |
| 1020 | if (set->used != (unsigned)start) { |
| 1021 | memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes); |
| 1022 | } |
| 1023 | set->used += end - start; |
| 1024 | } |
| 1025 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1029 | * @brief Check for duplicates in a node set. |
| 1030 | * |
| 1031 | * @param[in] set Set to check. |
| 1032 | * @param[in] node Node to look for in @p set. |
| 1033 | * @param[in] node_type Type of @p node. |
| 1034 | * @param[in] skip_idx Index from @p set to skip. |
| 1035 | * @return LY_ERR |
| 1036 | */ |
| 1037 | static LY_ERR |
| 1038 | set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx) |
| 1039 | { |
| 1040 | uint32_t i; |
| 1041 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1042 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1043 | return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx); |
| 1044 | } |
| 1045 | |
| 1046 | for (i = 0; i < set->used; ++i) { |
| 1047 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1048 | continue; |
| 1049 | } |
| 1050 | |
| 1051 | if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) { |
| 1052 | return LY_EEXIST; |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | return LY_SUCCESS; |
| 1057 | } |
| 1058 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1059 | ly_bool |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1060 | lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx, |
| 1061 | uint32_t *index_p) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1062 | { |
| 1063 | uint32_t i; |
| 1064 | |
| 1065 | for (i = 0; i < set->used; ++i) { |
| 1066 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1067 | continue; |
| 1068 | } |
| 1069 | |
| 1070 | 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] | 1071 | if (index_p) { |
| 1072 | *index_p = i; |
| 1073 | } |
| 1074 | return 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1078 | return 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1079 | } |
| 1080 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1081 | void |
| 1082 | lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1083 | { |
| 1084 | uint32_t orig_used, i, j; |
| 1085 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1086 | 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] | 1087 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1088 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1089 | return; |
| 1090 | } |
| 1091 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1092 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1093 | memcpy(set1, set2, sizeof *set1); |
| 1094 | return; |
| 1095 | } |
| 1096 | |
| 1097 | if (set1->used + set2->used > set1->size) { |
| 1098 | set1->size = set1->used + set2->used; |
| 1099 | set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes); |
| 1100 | LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), ); |
| 1101 | } |
| 1102 | |
| 1103 | orig_used = set1->used; |
| 1104 | |
| 1105 | for (i = 0; i < set2->used; ++i) { |
| 1106 | for (j = 0; j < orig_used; ++j) { |
| 1107 | /* detect duplicities */ |
| 1108 | if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) { |
| 1109 | break; |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | if (j == orig_used) { |
| 1114 | memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes); |
| 1115 | ++set1->used; |
| 1116 | } |
| 1117 | } |
| 1118 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1119 | lyxp_set_free_content(set2); |
| 1120 | set2->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | /** |
| 1124 | * @brief Insert a node into a set. Context position aware. |
| 1125 | * |
| 1126 | * @param[in] set Set to use. |
| 1127 | * @param[in] node Node to insert to @p set. |
| 1128 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1129 | * @param[in] node_type Node type of @p node. |
| 1130 | * @param[in] idx Index in @p set to insert into. |
| 1131 | */ |
| 1132 | static void |
| 1133 | set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1134 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1135 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1136 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1137 | if (!set->size) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1138 | /* first item */ |
| 1139 | if (idx) { |
| 1140 | /* no real harm done, but it is a bug */ |
| 1141 | LOGINT(set->ctx); |
| 1142 | idx = 0; |
| 1143 | } |
| 1144 | set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes); |
| 1145 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1146 | set->type = LYXP_SET_NODE_SET; |
| 1147 | set->used = 0; |
| 1148 | set->size = LYXP_SET_SIZE_START; |
| 1149 | set->ctx_pos = 1; |
| 1150 | set->ctx_size = 1; |
| 1151 | set->ht = NULL; |
| 1152 | } else { |
| 1153 | /* not an empty set */ |
| 1154 | if (set->used == set->size) { |
| 1155 | |
| 1156 | /* set is full */ |
| 1157 | set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes); |
| 1158 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1159 | set->size += LYXP_SET_SIZE_STEP; |
| 1160 | } |
| 1161 | |
| 1162 | if (idx > set->used) { |
| 1163 | LOGINT(set->ctx); |
| 1164 | idx = set->used; |
| 1165 | } |
| 1166 | |
| 1167 | /* make space for the new node */ |
| 1168 | if (idx < set->used) { |
| 1169 | memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes); |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | /* finally assign the value */ |
| 1174 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1175 | set->val.nodes[idx].type = node_type; |
| 1176 | set->val.nodes[idx].pos = pos; |
| 1177 | ++set->used; |
| 1178 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1179 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1180 | set_insert_node_hash(set, (struct lyd_node *)node, node_type); |
| 1181 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1182 | } |
| 1183 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1184 | LY_ERR |
| 1185 | 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] | 1186 | { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1187 | uint32_t index; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1188 | |
| 1189 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1190 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1191 | if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) { |
| 1192 | set->val.scnodes[index].in_ctx = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1193 | } else { |
| 1194 | if (set->used == set->size) { |
| 1195 | 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] | 1196 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1197 | set->size += LYXP_SET_SIZE_STEP; |
| 1198 | } |
| 1199 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1200 | index = set->used; |
| 1201 | set->val.scnodes[index].scnode = (struct lysc_node *)node; |
| 1202 | set->val.scnodes[index].type = node_type; |
| 1203 | set->val.scnodes[index].in_ctx = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1204 | ++set->used; |
| 1205 | } |
| 1206 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1207 | if (index_p) { |
| 1208 | *index_p = index; |
| 1209 | } |
| 1210 | |
| 1211 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | /** |
| 1215 | * @brief Replace a node in a set with another. Context position aware. |
| 1216 | * |
| 1217 | * @param[in] set Set to use. |
| 1218 | * @param[in] node Node to insert to @p set. |
| 1219 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1220 | * @param[in] node_type Node type of @p node. |
| 1221 | * @param[in] idx Index in @p set of the node to replace. |
| 1222 | */ |
| 1223 | static void |
| 1224 | set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1225 | { |
| 1226 | assert(set && (idx < set->used)); |
| 1227 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1228 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1229 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1230 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1231 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1232 | set->val.nodes[idx].type = node_type; |
| 1233 | set->val.nodes[idx].pos = pos; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1234 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1235 | set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1236 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | /** |
| 1240 | * @brief Set all nodes with ctx 1 to a new unique context value. |
| 1241 | * |
| 1242 | * @param[in] set Set to modify. |
| 1243 | * @return New context value. |
| 1244 | */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1245 | static int32_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1246 | set_scnode_new_in_ctx(struct lyxp_set *set) |
| 1247 | { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1248 | uint32_t i; |
| 1249 | int32_t ret_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1250 | |
| 1251 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1252 | |
| 1253 | ret_ctx = 3; |
| 1254 | retry: |
| 1255 | for (i = 0; i < set->used; ++i) { |
| 1256 | if (set->val.scnodes[i].in_ctx >= ret_ctx) { |
| 1257 | ret_ctx = set->val.scnodes[i].in_ctx + 1; |
| 1258 | goto retry; |
| 1259 | } |
| 1260 | } |
| 1261 | for (i = 0; i < set->used; ++i) { |
| 1262 | if (set->val.scnodes[i].in_ctx == 1) { |
| 1263 | set->val.scnodes[i].in_ctx = ret_ctx; |
| 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | return ret_ctx; |
| 1268 | } |
| 1269 | |
| 1270 | /** |
| 1271 | * @brief Get unique @p node position in the data. |
| 1272 | * |
| 1273 | * @param[in] node Node to find. |
| 1274 | * @param[in] node_type Node type of @p node. |
| 1275 | * @param[in] root Root node. |
| 1276 | * @param[in] root_type Type of the XPath @p root node. |
| 1277 | * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally |
| 1278 | * be used to increase efficiency and start the DFS from this node. |
| 1279 | * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set. |
| 1280 | * @return Node position. |
| 1281 | */ |
| 1282 | static uint32_t |
| 1283 | 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] | 1284 | 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] | 1285 | { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1286 | const struct lyd_node *elem = NULL, *top_sibling; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1287 | uint32_t pos = 1; |
| 1288 | |
| 1289 | assert(prev && prev_pos && !root->prev->next); |
| 1290 | |
| 1291 | if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 1292 | return 0; |
| 1293 | } |
| 1294 | |
| 1295 | if (*prev) { |
| 1296 | /* start from the previous element instead from the root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1297 | pos = *prev_pos; |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1298 | 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] | 1299 | goto dfs_search; |
| 1300 | } |
| 1301 | |
| 1302 | for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1303 | LYD_TREE_DFS_BEGIN(top_sibling, elem) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1304 | dfs_search: |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1305 | if (*prev && !elem) { |
| 1306 | /* resume previous DFS */ |
| 1307 | elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev; |
| 1308 | LYD_TREE_DFS_continue = 0; |
| 1309 | } |
| 1310 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1311 | 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] | 1312 | /* skip */ |
| 1313 | LYD_TREE_DFS_continue = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1314 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1315 | if (elem == node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1316 | break; |
| 1317 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1318 | ++pos; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1319 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1320 | |
| 1321 | LYD_TREE_DFS_END(top_sibling, elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | /* node found */ |
| 1325 | if (elem) { |
| 1326 | break; |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | if (!elem) { |
| 1331 | if (!(*prev)) { |
| 1332 | /* we went from root and failed to find it, cannot be */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1333 | LOGINT(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1334 | return 0; |
| 1335 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1336 | /* start the search again from the beginning */ |
| 1337 | *prev = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1338 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1339 | top_sibling = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1340 | pos = 1; |
| 1341 | goto dfs_search; |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | /* remember the last found node for next time */ |
| 1346 | *prev = node; |
| 1347 | *prev_pos = pos; |
| 1348 | |
| 1349 | return pos; |
| 1350 | } |
| 1351 | |
| 1352 | /** |
| 1353 | * @brief Assign (fill) missing node positions. |
| 1354 | * |
| 1355 | * @param[in] set Set to fill positions in. |
| 1356 | * @param[in] root Context root node. |
| 1357 | * @param[in] root_type Context root type. |
| 1358 | * @return LY_ERR |
| 1359 | */ |
| 1360 | static LY_ERR |
| 1361 | set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type) |
| 1362 | { |
| 1363 | const struct lyd_node *prev = NULL, *tmp_node; |
| 1364 | uint32_t i, tmp_pos = 0; |
| 1365 | |
| 1366 | for (i = 0; i < set->used; ++i) { |
| 1367 | if (!set->val.nodes[i].pos) { |
| 1368 | tmp_node = NULL; |
| 1369 | switch (set->val.nodes[i].type) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1370 | case LYXP_NODE_META: |
| 1371 | tmp_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1372 | if (!tmp_node) { |
| 1373 | LOGINT_RET(root->schema->module->ctx); |
| 1374 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1375 | /* fallthrough */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1376 | case LYXP_NODE_ELEM: |
| 1377 | case LYXP_NODE_TEXT: |
| 1378 | if (!tmp_node) { |
| 1379 | tmp_node = set->val.nodes[i].node; |
| 1380 | } |
| 1381 | set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos); |
| 1382 | break; |
| 1383 | default: |
| 1384 | /* all roots have position 0 */ |
| 1385 | break; |
| 1386 | } |
| 1387 | } |
| 1388 | } |
| 1389 | |
| 1390 | return LY_SUCCESS; |
| 1391 | } |
| 1392 | |
| 1393 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1394 | * @brief Get unique @p meta position in the parent metadata. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1395 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1396 | * @param[in] meta Metadata to use. |
| 1397 | * @return Metadata position. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1398 | */ |
| 1399 | static uint16_t |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1400 | get_meta_pos(struct lyd_meta *meta) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1401 | { |
| 1402 | uint16_t pos = 0; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1403 | struct lyd_meta *meta2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1404 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1405 | for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1406 | ++pos; |
| 1407 | } |
| 1408 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1409 | assert(meta2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1410 | return pos; |
| 1411 | } |
| 1412 | |
| 1413 | /** |
| 1414 | * @brief Compare 2 nodes in respect to XPath document order. |
| 1415 | * |
| 1416 | * @param[in] item1 1st node. |
| 1417 | * @param[in] item2 2nd node. |
| 1418 | * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1. |
| 1419 | */ |
| 1420 | static int |
| 1421 | set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2) |
| 1422 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1423 | uint32_t meta_pos1 = 0, meta_pos2 = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1424 | |
| 1425 | if (item1->pos < item2->pos) { |
| 1426 | return -1; |
| 1427 | } |
| 1428 | |
| 1429 | if (item1->pos > item2->pos) { |
| 1430 | return 1; |
| 1431 | } |
| 1432 | |
| 1433 | /* node positions are equal, the fun case */ |
| 1434 | |
| 1435 | /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */ |
| 1436 | /* special case since text nodes are actually saved as their parents */ |
| 1437 | if ((item1->node == item2->node) && (item1->type != item2->type)) { |
| 1438 | if (item1->type == LYXP_NODE_ELEM) { |
| 1439 | assert(item2->type == LYXP_NODE_TEXT); |
| 1440 | return -1; |
| 1441 | } else { |
| 1442 | assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM)); |
| 1443 | return 1; |
| 1444 | } |
| 1445 | } |
| 1446 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1447 | /* we need meta positions now */ |
| 1448 | if (item1->type == LYXP_NODE_META) { |
| 1449 | meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1450 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1451 | if (item2->type == LYXP_NODE_META) { |
| 1452 | meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1453 | } |
| 1454 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1455 | /* 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] | 1456 | /* check for duplicates */ |
| 1457 | if (item1->node == item2->node) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1458 | 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] | 1459 | return 0; |
| 1460 | } |
| 1461 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1462 | /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1463 | /* elem is always first, 2nd node is after it */ |
| 1464 | if (item1->type == LYXP_NODE_ELEM) { |
| 1465 | assert(item2->type != LYXP_NODE_ELEM); |
| 1466 | return -1; |
| 1467 | } |
| 1468 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1469 | /* 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] | 1470 | /* 2nd is before 1st */ |
| 1471 | if (((item1->type == LYXP_NODE_TEXT) |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1472 | && ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) |
| 1473 | || ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) |
| 1474 | || (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) |
| 1475 | && (meta_pos1 > meta_pos2))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1476 | return 1; |
| 1477 | } |
| 1478 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1479 | /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1480 | /* 2nd is after 1st */ |
| 1481 | return -1; |
| 1482 | } |
| 1483 | |
| 1484 | /** |
| 1485 | * @brief Set cast for comparisons. |
| 1486 | * |
| 1487 | * @param[in] trg Target set to cast source into. |
| 1488 | * @param[in] src Source set. |
| 1489 | * @param[in] type Target set type. |
| 1490 | * @param[in] src_idx Source set node index. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1491 | * @return LY_ERR |
| 1492 | */ |
| 1493 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1494 | 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] | 1495 | { |
| 1496 | assert(src->type == LYXP_SET_NODE_SET); |
| 1497 | |
| 1498 | set_init(trg, src); |
| 1499 | |
| 1500 | /* insert node into target set */ |
| 1501 | set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0); |
| 1502 | |
| 1503 | /* cast target set appropriately */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1504 | return lyxp_set_cast(trg, type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1505 | } |
| 1506 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1507 | /** |
| 1508 | * @brief Set content canonization for comparisons. |
| 1509 | * |
| 1510 | * @param[in] trg Target set to put the canononized source into. |
| 1511 | * @param[in] src Source set. |
| 1512 | * @param[in] xp_node Source XPath node/meta to use for canonization. |
| 1513 | * @return LY_ERR |
| 1514 | */ |
| 1515 | static LY_ERR |
| 1516 | set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node) |
| 1517 | { |
| 1518 | struct lysc_type *type = NULL; |
| 1519 | struct lyd_value val; |
| 1520 | struct ly_err_item *err = NULL; |
| 1521 | char *str, *ptr; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1522 | ly_bool dynamic; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1523 | LY_ERR rc; |
| 1524 | |
| 1525 | /* is there anything to canonize even? */ |
| 1526 | if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) { |
| 1527 | /* do we have a type to use for canonization? */ |
| 1528 | if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) { |
| 1529 | type = ((struct lyd_node_term *)xp_node->node)->value.realtype; |
| 1530 | } else if (xp_node->type == LYXP_NODE_META) { |
| 1531 | type = ((struct lyd_meta *)xp_node->node)->value.realtype; |
| 1532 | } |
| 1533 | } |
| 1534 | if (!type) { |
| 1535 | goto fill; |
| 1536 | } |
| 1537 | |
| 1538 | if (src->type == LYXP_SET_NUMBER) { |
| 1539 | /* canonize number */ |
| 1540 | if (asprintf(&str, "%Lf", src->val.num) == -1) { |
| 1541 | LOGMEM(src->ctx); |
| 1542 | return LY_EMEM; |
| 1543 | } |
| 1544 | } else { |
| 1545 | /* canonize string */ |
| 1546 | str = strdup(src->val.str); |
| 1547 | } |
| 1548 | |
| 1549 | /* ignore errors, the value may not satisfy schema constraints */ |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 1550 | rc = type->plugin->store(src->ctx, type, str, strlen(str), LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_DYNAMIC, |
| 1551 | LY_PREF_JSON, NULL, NULL, NULL, &val, &err); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1552 | ly_err_free(err); |
| 1553 | if (rc) { |
| 1554 | /* invalid value */ |
| 1555 | free(str); |
| 1556 | goto fill; |
| 1557 | } |
| 1558 | |
| 1559 | /* storing successful, now print the canonical value */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 1560 | str = (char *)type->plugin->print(&val, LY_PREF_JSON, NULL, &dynamic); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1561 | |
| 1562 | /* use the canonized value */ |
| 1563 | set_init(trg, src); |
| 1564 | trg->type = src->type; |
| 1565 | if (src->type == LYXP_SET_NUMBER) { |
| 1566 | trg->val.num = strtold(str, &ptr); |
| 1567 | if (dynamic) { |
| 1568 | free(str); |
| 1569 | } |
| 1570 | LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT); |
| 1571 | } else { |
| 1572 | trg->val.str = (dynamic ? str : strdup(str)); |
| 1573 | } |
| 1574 | type->plugin->free(src->ctx, &val); |
| 1575 | return LY_SUCCESS; |
| 1576 | |
| 1577 | fill: |
| 1578 | /* no canonization needed/possible */ |
| 1579 | set_fill_set(trg, src); |
| 1580 | return LY_SUCCESS; |
| 1581 | } |
| 1582 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1583 | #ifndef NDEBUG |
| 1584 | |
| 1585 | /** |
| 1586 | * @brief Bubble sort @p set into XPath document order. |
| 1587 | * Context position aware. Unused in the 'Release' build target. |
| 1588 | * |
| 1589 | * @param[in] set Set to sort. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1590 | * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0). |
| 1591 | */ |
| 1592 | static int |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1593 | set_sort(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1594 | { |
| 1595 | uint32_t i, j; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1596 | int ret = 0, cmp; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1597 | ly_bool inverted, change; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1598 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1599 | struct lyxp_set_node item; |
| 1600 | struct lyxp_set_hash_node hnode; |
| 1601 | uint64_t hash; |
| 1602 | |
Michal Vasko | 3cf8fbf | 2020-05-27 15:21:21 +0200 | [diff] [blame] | 1603 | if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1604 | return 0; |
| 1605 | } |
| 1606 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1607 | /* find first top-level node to be used as anchor for positions */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1608 | 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] | 1609 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1610 | |
| 1611 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1612 | if (set_assign_pos(set, root, set->root_type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1613 | return -1; |
| 1614 | } |
| 1615 | |
| 1616 | LOGDBG(LY_LDGXPATH, "SORT BEGIN"); |
| 1617 | print_set_debug(set); |
| 1618 | |
| 1619 | for (i = 0; i < set->used; ++i) { |
| 1620 | inverted = 0; |
| 1621 | change = 0; |
| 1622 | |
| 1623 | for (j = 1; j < set->used - i; ++j) { |
| 1624 | /* compare node positions */ |
| 1625 | if (inverted) { |
| 1626 | cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]); |
| 1627 | } else { |
| 1628 | cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]); |
| 1629 | } |
| 1630 | |
| 1631 | /* swap if needed */ |
| 1632 | if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) { |
| 1633 | change = 1; |
| 1634 | |
| 1635 | item = set->val.nodes[j - 1]; |
| 1636 | set->val.nodes[j - 1] = set->val.nodes[j]; |
| 1637 | set->val.nodes[j] = item; |
| 1638 | } else { |
| 1639 | /* whether node_pos1 should be smaller than node_pos2 or the other way around */ |
| 1640 | inverted = !inverted; |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | ++ret; |
| 1645 | |
| 1646 | if (!change) { |
| 1647 | break; |
| 1648 | } |
| 1649 | } |
| 1650 | |
| 1651 | LOGDBG(LY_LDGXPATH, "SORT END %d", ret); |
| 1652 | print_set_debug(set); |
| 1653 | |
| 1654 | /* check node hashes */ |
| 1655 | if (set->used >= LYD_HT_MIN_ITEMS) { |
| 1656 | assert(set->ht); |
| 1657 | for (i = 0; i < set->used; ++i) { |
| 1658 | hnode.node = set->val.nodes[i].node; |
| 1659 | hnode.type = set->val.nodes[i].type; |
| 1660 | |
| 1661 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 1662 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 1663 | hash = dict_hash_multi(hash, NULL, 0); |
| 1664 | |
| 1665 | assert(!lyht_find(set->ht, &hnode, hash, NULL)); |
| 1666 | } |
| 1667 | } |
| 1668 | |
| 1669 | return ret - 1; |
| 1670 | } |
| 1671 | |
| 1672 | /** |
| 1673 | * @brief Remove duplicate entries in a sorted node set. |
| 1674 | * |
| 1675 | * @param[in] set Sorted set to check. |
| 1676 | * @return LY_ERR (LY_EEXIST if some duplicates are found) |
| 1677 | */ |
| 1678 | static LY_ERR |
| 1679 | set_sorted_dup_node_clean(struct lyxp_set *set) |
| 1680 | { |
| 1681 | uint32_t i = 0; |
| 1682 | LY_ERR ret = LY_SUCCESS; |
| 1683 | |
| 1684 | if (set->used > 1) { |
| 1685 | while (i < set->used - 1) { |
| 1686 | if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) |
| 1687 | && (set->val.nodes[i].type == set->val.nodes[i + 1].type)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1688 | set_remove_node_none(set, i + 1); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1689 | ret = LY_EEXIST; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1690 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1691 | ++i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1692 | } |
| 1693 | } |
| 1694 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1695 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1696 | return ret; |
| 1697 | } |
| 1698 | |
| 1699 | #endif |
| 1700 | |
| 1701 | /** |
| 1702 | * @brief Merge 2 sorted sets into one. |
| 1703 | * |
| 1704 | * @param[in,out] trg Set to merge into. Duplicates are removed. |
| 1705 | * @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] | 1706 | * @return LY_ERR |
| 1707 | */ |
| 1708 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1709 | set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1710 | { |
| 1711 | uint32_t i, j, k, count, dup_count; |
| 1712 | int cmp; |
| 1713 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1714 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1715 | 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] | 1716 | return LY_EINVAL; |
| 1717 | } |
| 1718 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1719 | if (!src->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1720 | return LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1721 | } else if (!trg->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1722 | set_fill_set(trg, src); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1723 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1724 | return LY_SUCCESS; |
| 1725 | } |
| 1726 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1727 | /* find first top-level node to be used as anchor for positions */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1728 | 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] | 1729 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1730 | |
| 1731 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1732 | 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] | 1733 | return LY_EINT; |
| 1734 | } |
| 1735 | |
| 1736 | #ifndef NDEBUG |
| 1737 | LOGDBG(LY_LDGXPATH, "MERGE target"); |
| 1738 | print_set_debug(trg); |
| 1739 | LOGDBG(LY_LDGXPATH, "MERGE source"); |
| 1740 | print_set_debug(src); |
| 1741 | #endif |
| 1742 | |
| 1743 | /* make memory for the merge (duplicates are not detected yet, so space |
| 1744 | * will likely be wasted on them, too bad) */ |
| 1745 | if (trg->size - trg->used < src->used) { |
| 1746 | trg->size = trg->used + src->used; |
| 1747 | |
| 1748 | trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes); |
| 1749 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM); |
| 1750 | } |
| 1751 | |
| 1752 | i = 0; |
| 1753 | j = 0; |
| 1754 | count = 0; |
| 1755 | dup_count = 0; |
| 1756 | do { |
| 1757 | cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]); |
| 1758 | if (!cmp) { |
| 1759 | if (!count) { |
| 1760 | /* duplicate, just skip it */ |
| 1761 | ++i; |
| 1762 | ++j; |
| 1763 | } else { |
| 1764 | /* we are copying something already, so let's copy the duplicate too, |
| 1765 | * we are hoping that afterwards there are some more nodes to |
| 1766 | * copy and this way we can copy them all together */ |
| 1767 | ++count; |
| 1768 | ++dup_count; |
| 1769 | ++i; |
| 1770 | ++j; |
| 1771 | } |
| 1772 | } else if (cmp < 0) { |
| 1773 | /* inserting src node into trg, just remember it for now */ |
| 1774 | ++count; |
| 1775 | ++i; |
| 1776 | |
| 1777 | /* insert the hash now */ |
| 1778 | set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type); |
| 1779 | } else if (count) { |
| 1780 | copy_nodes: |
| 1781 | /* time to actually copy the nodes, we have found the largest block of nodes */ |
| 1782 | memmove(&trg->val.nodes[j + (count - dup_count)], |
| 1783 | &trg->val.nodes[j], |
| 1784 | (trg->used - j) * sizeof *trg->val.nodes); |
| 1785 | memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes); |
| 1786 | |
| 1787 | trg->used += count - dup_count; |
| 1788 | /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */ |
| 1789 | j += count - dup_count; |
| 1790 | |
| 1791 | count = 0; |
| 1792 | dup_count = 0; |
| 1793 | } else { |
| 1794 | ++j; |
| 1795 | } |
| 1796 | } while ((i < src->used) && (j < trg->used)); |
| 1797 | |
| 1798 | if ((i < src->used) || count) { |
| 1799 | /* insert all the hashes first */ |
| 1800 | for (k = i; k < src->used; ++k) { |
| 1801 | set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type); |
| 1802 | } |
| 1803 | |
| 1804 | /* loop ended, but we need to copy something at trg end */ |
| 1805 | count += src->used - i; |
| 1806 | i = src->used; |
| 1807 | goto copy_nodes; |
| 1808 | } |
| 1809 | |
| 1810 | /* we are inserting hashes before the actual node insert, which causes |
| 1811 | * situations when there were initially not enough items for a hash table, |
| 1812 | * but even after some were inserted, hash table was not created (during |
| 1813 | * insertion the number of items is not updated yet) */ |
| 1814 | if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) { |
| 1815 | set_insert_node_hash(trg, NULL, 0); |
| 1816 | } |
| 1817 | |
| 1818 | #ifndef NDEBUG |
| 1819 | LOGDBG(LY_LDGXPATH, "MERGE result"); |
| 1820 | print_set_debug(trg); |
| 1821 | #endif |
| 1822 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1823 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1824 | return LY_SUCCESS; |
| 1825 | } |
| 1826 | |
| 1827 | /* |
| 1828 | * (re)parse functions |
| 1829 | * |
| 1830 | * Parse functions parse the expression into |
| 1831 | * tokens (syntactic analysis). |
| 1832 | * |
| 1833 | * Reparse functions perform semantic analysis |
| 1834 | * (do not save the result, just a check) of |
| 1835 | * the expression and fill repeat indices. |
| 1836 | */ |
| 1837 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1838 | LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1839 | 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] | 1840 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1841 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1842 | if (ctx) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1843 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1844 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1845 | return LY_EINCOMPLETE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1846 | } |
| 1847 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1848 | if (want_tok && (exp->tokens[tok_idx] != want_tok)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1849 | if (ctx) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1850 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1851 | 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] | 1852 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1853 | return LY_ENOT; |
| 1854 | } |
| 1855 | |
| 1856 | return LY_SUCCESS; |
| 1857 | } |
| 1858 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1859 | LY_ERR |
| 1860 | lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok) |
| 1861 | { |
| 1862 | LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok)); |
| 1863 | |
| 1864 | /* skip the token */ |
| 1865 | ++(*tok_idx); |
| 1866 | |
| 1867 | return LY_SUCCESS; |
| 1868 | } |
| 1869 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1870 | /* just like lyxp_check_token() but tests for 2 tokens */ |
| 1871 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1872 | 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] | 1873 | enum lyxp_token want_tok2) |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1874 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1875 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1876 | if (ctx) { |
| 1877 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1878 | } |
| 1879 | return LY_EINCOMPLETE; |
| 1880 | } |
| 1881 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1882 | 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] | 1883 | if (ctx) { |
| 1884 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1885 | 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] | 1886 | } |
| 1887 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1888 | } |
| 1889 | |
| 1890 | return LY_SUCCESS; |
| 1891 | } |
| 1892 | |
| 1893 | /** |
| 1894 | * @brief Stack operation push on the repeat array. |
| 1895 | * |
| 1896 | * @param[in] exp Expression to use. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1897 | * @param[in] tok_idx Position in the expresion \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1898 | * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed. |
| 1899 | */ |
| 1900 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1901 | 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] | 1902 | { |
| 1903 | uint16_t i; |
| 1904 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1905 | if (exp->repeat[tok_idx]) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1906 | for (i = 0; exp->repeat[tok_idx][i]; ++i) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1907 | exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]); |
| 1908 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1909 | exp->repeat[tok_idx][i] = repeat_op_idx; |
| 1910 | exp->repeat[tok_idx][i + 1] = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1911 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1912 | exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]); |
| 1913 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1914 | exp->repeat[tok_idx][0] = repeat_op_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1915 | } |
| 1916 | } |
| 1917 | |
| 1918 | /** |
| 1919 | * @brief Reparse Predicate. Logs directly on error. |
| 1920 | * |
| 1921 | * [7] Predicate ::= '[' Expr ']' |
| 1922 | * |
| 1923 | * @param[in] ctx Context for logging. |
| 1924 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1925 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1926 | * @return LY_ERR |
| 1927 | */ |
| 1928 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1929 | 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] | 1930 | { |
| 1931 | LY_ERR rc; |
| 1932 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1933 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1934 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1935 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1936 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1937 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1938 | LY_CHECK_RET(rc); |
| 1939 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1940 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1941 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1942 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1943 | |
| 1944 | return LY_SUCCESS; |
| 1945 | } |
| 1946 | |
| 1947 | /** |
| 1948 | * @brief Reparse RelativeLocationPath. Logs directly on error. |
| 1949 | * |
| 1950 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 1951 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 1952 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 1953 | * |
| 1954 | * @param[in] ctx Context for logging. |
| 1955 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1956 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1957 | * @return LY_ERR (LY_EINCOMPLETE on forward reference) |
| 1958 | */ |
| 1959 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1960 | 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] | 1961 | { |
| 1962 | LY_ERR rc; |
| 1963 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1964 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1965 | LY_CHECK_RET(rc); |
| 1966 | |
| 1967 | goto step; |
| 1968 | do { |
| 1969 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1970 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1971 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1972 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1973 | LY_CHECK_RET(rc); |
| 1974 | step: |
| 1975 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1976 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1977 | case LYXP_TOKEN_DOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1978 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1979 | break; |
| 1980 | |
| 1981 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1982 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1983 | break; |
| 1984 | |
| 1985 | case LYXP_TOKEN_AT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1986 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1987 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1988 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1989 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1990 | 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] | 1991 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1992 | 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] | 1993 | return LY_EVALID; |
| 1994 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1995 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1996 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1997 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1998 | goto reparse_predicate; |
| 1999 | break; |
| 2000 | |
| 2001 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2002 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2003 | |
| 2004 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2005 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2006 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2007 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2008 | |
| 2009 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2010 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2011 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2012 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2013 | |
| 2014 | reparse_predicate: |
| 2015 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2016 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 2017 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2018 | LY_CHECK_RET(rc); |
| 2019 | } |
| 2020 | break; |
| 2021 | default: |
| 2022 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2023 | 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] | 2024 | return LY_EVALID; |
| 2025 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2026 | } 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] | 2027 | |
| 2028 | return LY_SUCCESS; |
| 2029 | } |
| 2030 | |
| 2031 | /** |
| 2032 | * @brief Reparse AbsoluteLocationPath. Logs directly on error. |
| 2033 | * |
| 2034 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 2035 | * |
| 2036 | * @param[in] ctx Context for logging. |
| 2037 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2038 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2039 | * @return LY_ERR |
| 2040 | */ |
| 2041 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2042 | reparse_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] | 2043 | { |
| 2044 | LY_ERR rc; |
| 2045 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2046 | 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] | 2047 | |
| 2048 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2049 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
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 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2052 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2053 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2054 | return LY_SUCCESS; |
| 2055 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2056 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2057 | case LYXP_TOKEN_DOT: |
| 2058 | case LYXP_TOKEN_DDOT: |
| 2059 | case LYXP_TOKEN_AT: |
| 2060 | case LYXP_TOKEN_NAMETEST: |
| 2061 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2062 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2063 | LY_CHECK_RET(rc); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2064 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2065 | default: |
| 2066 | break; |
| 2067 | } |
| 2068 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2069 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 2070 | /* '//' RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2071 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2072 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2073 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2074 | LY_CHECK_RET(rc); |
| 2075 | } |
| 2076 | |
| 2077 | return LY_SUCCESS; |
| 2078 | } |
| 2079 | |
| 2080 | /** |
| 2081 | * @brief Reparse FunctionCall. Logs directly on error. |
| 2082 | * |
| 2083 | * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
| 2084 | * |
| 2085 | * @param[in] ctx Context for logging. |
| 2086 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2087 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2088 | * @return LY_ERR |
| 2089 | */ |
| 2090 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2091 | 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] | 2092 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2093 | int8_t min_arg_count = -1; |
| 2094 | uint32_t arg_count, max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2095 | uint16_t func_tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2096 | LY_ERR rc; |
| 2097 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2098 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2099 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2100 | func_tok_idx = *tok_idx; |
| 2101 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2102 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2103 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2104 | min_arg_count = 1; |
| 2105 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2106 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2107 | min_arg_count = 1; |
| 2108 | max_arg_count = 1; |
| 2109 | } |
| 2110 | break; |
| 2111 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2112 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2113 | min_arg_count = 1; |
| 2114 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2115 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2116 | min_arg_count = 0; |
| 2117 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2118 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2119 | min_arg_count = 0; |
| 2120 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2121 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2122 | min_arg_count = 0; |
| 2123 | max_arg_count = 0; |
| 2124 | } |
| 2125 | break; |
| 2126 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2127 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2128 | min_arg_count = 1; |
| 2129 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2130 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2131 | min_arg_count = 0; |
| 2132 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2133 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2134 | min_arg_count = 1; |
| 2135 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2136 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2137 | min_arg_count = 1; |
| 2138 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2139 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2140 | min_arg_count = 1; |
| 2141 | max_arg_count = 1; |
| 2142 | } |
| 2143 | break; |
| 2144 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2145 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2146 | min_arg_count = 2; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2147 | max_arg_count = UINT32_MAX; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2148 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2149 | min_arg_count = 0; |
| 2150 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2151 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2152 | min_arg_count = 0; |
| 2153 | max_arg_count = 1; |
| 2154 | } |
| 2155 | break; |
| 2156 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2157 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2158 | min_arg_count = 1; |
| 2159 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2160 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2161 | min_arg_count = 1; |
| 2162 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2163 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2164 | min_arg_count = 0; |
| 2165 | max_arg_count = 0; |
| 2166 | } |
| 2167 | break; |
| 2168 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2169 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2170 | min_arg_count = 2; |
| 2171 | max_arg_count = 2; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2172 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2173 | min_arg_count = 0; |
| 2174 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2175 | } 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] | 2176 | min_arg_count = 2; |
| 2177 | max_arg_count = 2; |
| 2178 | } |
| 2179 | break; |
| 2180 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2181 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2182 | min_arg_count = 2; |
| 2183 | max_arg_count = 3; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2184 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2185 | min_arg_count = 3; |
| 2186 | max_arg_count = 3; |
| 2187 | } |
| 2188 | break; |
| 2189 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2190 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2191 | min_arg_count = 0; |
| 2192 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2193 | } 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] | 2194 | min_arg_count = 1; |
| 2195 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2196 | } 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] | 2197 | min_arg_count = 2; |
| 2198 | max_arg_count = 2; |
| 2199 | } |
| 2200 | break; |
| 2201 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2202 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2203 | min_arg_count = 2; |
| 2204 | max_arg_count = 2; |
| 2205 | } |
| 2206 | break; |
| 2207 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2208 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2209 | min_arg_count = 2; |
| 2210 | max_arg_count = 2; |
| 2211 | } |
| 2212 | break; |
| 2213 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2214 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2215 | min_arg_count = 0; |
| 2216 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2217 | } 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] | 2218 | min_arg_count = 0; |
| 2219 | max_arg_count = 1; |
| 2220 | } |
| 2221 | break; |
| 2222 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2223 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2224 | min_arg_count = 0; |
| 2225 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2226 | } 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] | 2227 | min_arg_count = 2; |
| 2228 | max_arg_count = 2; |
| 2229 | } |
| 2230 | break; |
| 2231 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2232 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2233 | min_arg_count = 2; |
| 2234 | max_arg_count = 2; |
| 2235 | } |
| 2236 | break; |
| 2237 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2238 | 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] | 2239 | min_arg_count = 2; |
| 2240 | max_arg_count = 2; |
| 2241 | } |
| 2242 | break; |
| 2243 | } |
| 2244 | if (min_arg_count == -1) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2245 | 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] | 2246 | return LY_EINVAL; |
| 2247 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2248 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2249 | |
| 2250 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2251 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2252 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2253 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2254 | |
| 2255 | /* ( Expr ( ',' Expr )* )? */ |
| 2256 | arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2257 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2258 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2259 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2260 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2261 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2262 | LY_CHECK_RET(rc); |
| 2263 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2264 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
| 2265 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2266 | |
| 2267 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2268 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2269 | LY_CHECK_RET(rc); |
| 2270 | } |
| 2271 | |
| 2272 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2273 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2274 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2275 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2276 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2277 | if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2278 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx], |
| 2279 | &exp->expr[exp->tok_pos[func_tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2280 | return LY_EVALID; |
| 2281 | } |
| 2282 | |
| 2283 | return LY_SUCCESS; |
| 2284 | } |
| 2285 | |
| 2286 | /** |
| 2287 | * @brief Reparse PathExpr. Logs directly on error. |
| 2288 | * |
| 2289 | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
| 2290 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 2291 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 2292 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
| 2293 | * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
| 2294 | * |
| 2295 | * @param[in] ctx Context for logging. |
| 2296 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2297 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2298 | * @return LY_ERR |
| 2299 | */ |
| 2300 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2301 | 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] | 2302 | { |
| 2303 | LY_ERR rc; |
| 2304 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2305 | if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2306 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2307 | } |
| 2308 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2309 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2310 | case LYXP_TOKEN_PAR1: |
| 2311 | /* '(' Expr ')' Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2312 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2313 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2314 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2315 | LY_CHECK_RET(rc); |
| 2316 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2317 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2318 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2319 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2320 | goto predicate; |
| 2321 | break; |
| 2322 | case LYXP_TOKEN_DOT: |
| 2323 | case LYXP_TOKEN_DDOT: |
| 2324 | case LYXP_TOKEN_AT: |
| 2325 | case LYXP_TOKEN_NAMETEST: |
| 2326 | case LYXP_TOKEN_NODETYPE: |
| 2327 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2328 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2329 | LY_CHECK_RET(rc); |
| 2330 | break; |
| 2331 | case LYXP_TOKEN_FUNCNAME: |
| 2332 | /* FunctionCall */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2333 | rc = reparse_function_call(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2334 | LY_CHECK_RET(rc); |
| 2335 | goto predicate; |
| 2336 | break; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2337 | case LYXP_TOKEN_OPER_PATH: |
| 2338 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2339 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2340 | rc = reparse_absolute_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2341 | LY_CHECK_RET(rc); |
| 2342 | break; |
| 2343 | case LYXP_TOKEN_LITERAL: |
| 2344 | /* Literal */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2345 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2346 | goto predicate; |
| 2347 | break; |
| 2348 | case LYXP_TOKEN_NUMBER: |
| 2349 | /* Number */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2350 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2351 | goto predicate; |
| 2352 | break; |
| 2353 | default: |
| 2354 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2355 | 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] | 2356 | return LY_EVALID; |
| 2357 | } |
| 2358 | |
| 2359 | return LY_SUCCESS; |
| 2360 | |
| 2361 | predicate: |
| 2362 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2363 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 2364 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2365 | LY_CHECK_RET(rc); |
| 2366 | } |
| 2367 | |
| 2368 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2369 | 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] | 2370 | |
| 2371 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2372 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2373 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2374 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2375 | LY_CHECK_RET(rc); |
| 2376 | } |
| 2377 | |
| 2378 | return LY_SUCCESS; |
| 2379 | } |
| 2380 | |
| 2381 | /** |
| 2382 | * @brief Reparse UnaryExpr. Logs directly on error. |
| 2383 | * |
| 2384 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 2385 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 2386 | * |
| 2387 | * @param[in] ctx Context for logging. |
| 2388 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2389 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2390 | * @return LY_ERR |
| 2391 | */ |
| 2392 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2393 | 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] | 2394 | { |
| 2395 | uint16_t prev_exp; |
| 2396 | LY_ERR rc; |
| 2397 | |
| 2398 | /* ('-')* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2399 | prev_exp = *tok_idx; |
| 2400 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2401 | && (exp->expr[exp->tok_pos[*tok_idx]] == '-')) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2402 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2403 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2404 | } |
| 2405 | |
| 2406 | /* PathExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2407 | prev_exp = *tok_idx; |
| 2408 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2409 | LY_CHECK_RET(rc); |
| 2410 | |
| 2411 | /* ('|' PathExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2412 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2413 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2414 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2415 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2416 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2417 | LY_CHECK_RET(rc); |
| 2418 | } |
| 2419 | |
| 2420 | return LY_SUCCESS; |
| 2421 | } |
| 2422 | |
| 2423 | /** |
| 2424 | * @brief Reparse AdditiveExpr. Logs directly on error. |
| 2425 | * |
| 2426 | * [15] AdditiveExpr ::= MultiplicativeExpr |
| 2427 | * | AdditiveExpr '+' MultiplicativeExpr |
| 2428 | * | AdditiveExpr '-' MultiplicativeExpr |
| 2429 | * [16] MultiplicativeExpr ::= UnaryExpr |
| 2430 | * | MultiplicativeExpr '*' UnaryExpr |
| 2431 | * | MultiplicativeExpr 'div' UnaryExpr |
| 2432 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 2433 | * |
| 2434 | * @param[in] ctx Context for logging. |
| 2435 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2436 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2437 | * @return LY_ERR |
| 2438 | */ |
| 2439 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2440 | 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] | 2441 | { |
| 2442 | uint16_t prev_add_exp, prev_mul_exp; |
| 2443 | LY_ERR rc; |
| 2444 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2445 | prev_add_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2446 | goto reparse_multiplicative_expr; |
| 2447 | |
| 2448 | /* ('+' / '-' MultiplicativeExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2449 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2450 | && ((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] | 2451 | exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2452 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2453 | |
| 2454 | reparse_multiplicative_expr: |
| 2455 | /* UnaryExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2456 | prev_mul_exp = *tok_idx; |
| 2457 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2458 | LY_CHECK_RET(rc); |
| 2459 | |
| 2460 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2461 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2462 | && ((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] | 2463 | exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2464 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2465 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2466 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2467 | LY_CHECK_RET(rc); |
| 2468 | } |
| 2469 | } |
| 2470 | |
| 2471 | return LY_SUCCESS; |
| 2472 | } |
| 2473 | |
| 2474 | /** |
| 2475 | * @brief Reparse EqualityExpr. Logs directly on error. |
| 2476 | * |
| 2477 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
| 2478 | * | EqualityExpr '!=' RelationalExpr |
| 2479 | * [14] RelationalExpr ::= AdditiveExpr |
| 2480 | * | RelationalExpr '<' AdditiveExpr |
| 2481 | * | RelationalExpr '>' AdditiveExpr |
| 2482 | * | RelationalExpr '<=' AdditiveExpr |
| 2483 | * | RelationalExpr '>=' AdditiveExpr |
| 2484 | * |
| 2485 | * @param[in] ctx Context for logging. |
| 2486 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2487 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2488 | * @return LY_ERR |
| 2489 | */ |
| 2490 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2491 | 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] | 2492 | { |
| 2493 | uint16_t prev_eq_exp, prev_rel_exp; |
| 2494 | LY_ERR rc; |
| 2495 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2496 | prev_eq_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2497 | goto reparse_additive_expr; |
| 2498 | |
| 2499 | /* ('=' / '!=' RelationalExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2500 | 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] | 2501 | exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2502 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2503 | |
| 2504 | reparse_additive_expr: |
| 2505 | /* AdditiveExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2506 | prev_rel_exp = *tok_idx; |
| 2507 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2508 | LY_CHECK_RET(rc); |
| 2509 | |
| 2510 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2511 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2512 | exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2513 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2514 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2515 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2516 | LY_CHECK_RET(rc); |
| 2517 | } |
| 2518 | } |
| 2519 | |
| 2520 | return LY_SUCCESS; |
| 2521 | } |
| 2522 | |
| 2523 | /** |
| 2524 | * @brief Reparse OrExpr. Logs directly on error. |
| 2525 | * |
| 2526 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 2527 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 2528 | * |
| 2529 | * @param[in] ctx Context for logging. |
| 2530 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2531 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2532 | * @return LY_ERR |
| 2533 | */ |
| 2534 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2535 | 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] | 2536 | { |
| 2537 | uint16_t prev_or_exp, prev_and_exp; |
| 2538 | LY_ERR rc; |
| 2539 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2540 | prev_or_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2541 | goto reparse_equality_expr; |
| 2542 | |
| 2543 | /* ('or' AndExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2544 | 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] | 2545 | exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2546 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2547 | |
| 2548 | reparse_equality_expr: |
| 2549 | /* EqualityExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2550 | prev_and_exp = *tok_idx; |
| 2551 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2552 | LY_CHECK_RET(rc); |
| 2553 | |
| 2554 | /* ('and' EqualityExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2555 | 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] | 2556 | exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2557 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2558 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2559 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2560 | LY_CHECK_RET(rc); |
| 2561 | } |
| 2562 | } |
| 2563 | |
| 2564 | return LY_SUCCESS; |
| 2565 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2566 | |
| 2567 | /** |
| 2568 | * @brief Parse NCName. |
| 2569 | * |
| 2570 | * @param[in] ncname Name to parse. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2571 | * @return Length of @p ncname valid bytes. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2572 | */ |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2573 | static long int |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2574 | parse_ncname(const char *ncname) |
| 2575 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2576 | uint32_t uc; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2577 | size_t size; |
| 2578 | long int len = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2579 | |
| 2580 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0); |
| 2581 | if (!is_xmlqnamestartchar(uc) || (uc == ':')) { |
| 2582 | return len; |
| 2583 | } |
| 2584 | |
| 2585 | do { |
| 2586 | len += size; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2587 | if (!*ncname) { |
| 2588 | break; |
| 2589 | } |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2590 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2591 | } while (is_xmlqnamechar(uc) && (uc != ':')); |
| 2592 | |
| 2593 | return len; |
| 2594 | } |
| 2595 | |
| 2596 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2597 | * @brief Add @p token into the expression @p exp. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2598 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2599 | * @param[in] ctx Context for logging. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2600 | * @param[in] exp Expression to use. |
| 2601 | * @param[in] token Token to add. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2602 | * @param[in] tok_pos Token position in the XPath expression. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2603 | * @param[in] tok_len Token length in the XPath expression. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2604 | * @return LY_ERR |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2605 | */ |
| 2606 | static LY_ERR |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2607 | 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] | 2608 | { |
| 2609 | uint32_t prev; |
| 2610 | |
| 2611 | if (exp->used == exp->size) { |
| 2612 | prev = exp->size; |
| 2613 | exp->size += LYXP_EXPR_SIZE_STEP; |
| 2614 | if (prev > exp->size) { |
| 2615 | LOGINT(ctx); |
| 2616 | return LY_EINT; |
| 2617 | } |
| 2618 | |
| 2619 | exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens); |
| 2620 | LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM); |
| 2621 | exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos); |
| 2622 | LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM); |
| 2623 | exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len); |
| 2624 | LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM); |
| 2625 | } |
| 2626 | |
| 2627 | exp->tokens[exp->used] = token; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2628 | exp->tok_pos[exp->used] = tok_pos; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2629 | exp->tok_len[exp->used] = tok_len; |
| 2630 | ++exp->used; |
| 2631 | return LY_SUCCESS; |
| 2632 | } |
| 2633 | |
| 2634 | void |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2635 | lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2636 | { |
| 2637 | uint16_t i; |
| 2638 | |
| 2639 | if (!expr) { |
| 2640 | return; |
| 2641 | } |
| 2642 | |
| 2643 | lydict_remove(ctx, expr->expr); |
| 2644 | free(expr->tokens); |
| 2645 | free(expr->tok_pos); |
| 2646 | free(expr->tok_len); |
| 2647 | if (expr->repeat) { |
| 2648 | for (i = 0; i < expr->used; ++i) { |
| 2649 | free(expr->repeat[i]); |
| 2650 | } |
| 2651 | } |
| 2652 | free(expr->repeat); |
| 2653 | free(expr); |
| 2654 | } |
| 2655 | |
| 2656 | struct lyxp_expr * |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2657 | lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr, size_t expr_len, ly_bool reparse) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2658 | { |
| 2659 | struct lyxp_expr *ret; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2660 | size_t parsed = 0, tok_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2661 | enum lyxp_token tok_type; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2662 | ly_bool prev_function_check = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2663 | uint16_t tok_idx = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2664 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2665 | if (!expr[0]) { |
| 2666 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 2667 | return NULL; |
| 2668 | } |
| 2669 | |
| 2670 | if (!expr_len) { |
| 2671 | expr_len = strlen(expr); |
| 2672 | } |
| 2673 | if (expr_len > UINT16_MAX) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2674 | LOGERR(ctx, LY_EINVAL, "XPath expression cannot be longer than %ud characters.", UINT16_MAX); |
| 2675 | return NULL; |
| 2676 | } |
| 2677 | |
| 2678 | /* init lyxp_expr structure */ |
| 2679 | ret = calloc(1, sizeof *ret); |
| 2680 | LY_CHECK_ERR_GOTO(!ret, LOGMEM(ctx), error); |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 2681 | LY_CHECK_GOTO(lydict_insert(ctx, expr, expr_len, &ret->expr), error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2682 | LY_CHECK_ERR_GOTO(!ret->expr, LOGMEM(ctx), error); |
| 2683 | ret->used = 0; |
| 2684 | ret->size = LYXP_EXPR_SIZE_START; |
| 2685 | ret->tokens = malloc(ret->size * sizeof *ret->tokens); |
| 2686 | LY_CHECK_ERR_GOTO(!ret->tokens, LOGMEM(ctx), error); |
| 2687 | |
| 2688 | ret->tok_pos = malloc(ret->size * sizeof *ret->tok_pos); |
| 2689 | LY_CHECK_ERR_GOTO(!ret->tok_pos, LOGMEM(ctx), error); |
| 2690 | |
| 2691 | ret->tok_len = malloc(ret->size * sizeof *ret->tok_len); |
| 2692 | LY_CHECK_ERR_GOTO(!ret->tok_len, LOGMEM(ctx), error); |
| 2693 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2694 | /* make expr 0-terminated */ |
| 2695 | expr = ret->expr; |
| 2696 | |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2697 | while (is_xmlws(expr[parsed])) { |
| 2698 | ++parsed; |
| 2699 | } |
| 2700 | |
| 2701 | do { |
| 2702 | if (expr[parsed] == '(') { |
| 2703 | |
| 2704 | /* '(' */ |
| 2705 | tok_len = 1; |
| 2706 | tok_type = LYXP_TOKEN_PAR1; |
| 2707 | |
| 2708 | if (prev_function_check && ret->used && (ret->tokens[ret->used - 1] == LYXP_TOKEN_NAMETEST)) { |
| 2709 | /* it is a NodeType/FunctionName after all */ |
| 2710 | if (((ret->tok_len[ret->used - 1] == 4) |
| 2711 | && (!strncmp(&expr[ret->tok_pos[ret->used - 1]], "node", 4) |
| 2712 | || !strncmp(&expr[ret->tok_pos[ret->used - 1]], "text", 4))) || |
| 2713 | ((ret->tok_len[ret->used - 1] == 7) |
| 2714 | && !strncmp(&expr[ret->tok_pos[ret->used - 1]], "comment", 7))) { |
| 2715 | ret->tokens[ret->used - 1] = LYXP_TOKEN_NODETYPE; |
| 2716 | } else { |
| 2717 | ret->tokens[ret->used - 1] = LYXP_TOKEN_FUNCNAME; |
| 2718 | } |
| 2719 | prev_function_check = 0; |
| 2720 | } |
| 2721 | |
| 2722 | } else if (expr[parsed] == ')') { |
| 2723 | |
| 2724 | /* ')' */ |
| 2725 | tok_len = 1; |
| 2726 | tok_type = LYXP_TOKEN_PAR2; |
| 2727 | |
| 2728 | } else if (expr[parsed] == '[') { |
| 2729 | |
| 2730 | /* '[' */ |
| 2731 | tok_len = 1; |
| 2732 | tok_type = LYXP_TOKEN_BRACK1; |
| 2733 | |
| 2734 | } else if (expr[parsed] == ']') { |
| 2735 | |
| 2736 | /* ']' */ |
| 2737 | tok_len = 1; |
| 2738 | tok_type = LYXP_TOKEN_BRACK2; |
| 2739 | |
| 2740 | } else if (!strncmp(&expr[parsed], "..", 2)) { |
| 2741 | |
| 2742 | /* '..' */ |
| 2743 | tok_len = 2; |
| 2744 | tok_type = LYXP_TOKEN_DDOT; |
| 2745 | |
| 2746 | } else if ((expr[parsed] == '.') && (!isdigit(expr[parsed + 1]))) { |
| 2747 | |
| 2748 | /* '.' */ |
| 2749 | tok_len = 1; |
| 2750 | tok_type = LYXP_TOKEN_DOT; |
| 2751 | |
| 2752 | } else if (expr[parsed] == '@') { |
| 2753 | |
| 2754 | /* '@' */ |
| 2755 | tok_len = 1; |
| 2756 | tok_type = LYXP_TOKEN_AT; |
| 2757 | |
| 2758 | } else if (expr[parsed] == ',') { |
| 2759 | |
| 2760 | /* ',' */ |
| 2761 | tok_len = 1; |
| 2762 | tok_type = LYXP_TOKEN_COMMA; |
| 2763 | |
| 2764 | } else if (expr[parsed] == '\'') { |
| 2765 | |
| 2766 | /* Literal with ' */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2767 | 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] | 2768 | LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0', |
| 2769 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error); |
| 2770 | ++tok_len; |
| 2771 | tok_type = LYXP_TOKEN_LITERAL; |
| 2772 | |
| 2773 | } else if (expr[parsed] == '\"') { |
| 2774 | |
| 2775 | /* Literal with " */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2776 | 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] | 2777 | LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0', |
| 2778 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error); |
| 2779 | ++tok_len; |
| 2780 | tok_type = LYXP_TOKEN_LITERAL; |
| 2781 | |
| 2782 | } else if ((expr[parsed] == '.') || (isdigit(expr[parsed]))) { |
| 2783 | |
| 2784 | /* Number */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2785 | for (tok_len = 0; isdigit(expr[parsed + tok_len]); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2786 | if (expr[parsed + tok_len] == '.') { |
| 2787 | ++tok_len; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 2788 | for ( ; isdigit(expr[parsed + tok_len]); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2789 | } |
| 2790 | tok_type = LYXP_TOKEN_NUMBER; |
| 2791 | |
| 2792 | } else if (expr[parsed] == '/') { |
| 2793 | |
| 2794 | /* Operator '/', '//' */ |
| 2795 | if (!strncmp(&expr[parsed], "//", 2)) { |
| 2796 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2797 | tok_type = LYXP_TOKEN_OPER_RPATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2798 | } else { |
| 2799 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2800 | tok_type = LYXP_TOKEN_OPER_PATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2801 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2802 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 2803 | } else if (!strncmp(&expr[parsed], "!=", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2804 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2805 | /* Operator '!=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2806 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2807 | tok_type = LYXP_TOKEN_OPER_NEQUAL; |
| 2808 | |
| 2809 | } else if (!strncmp(&expr[parsed], "<=", 2) || !strncmp(&expr[parsed], ">=", 2)) { |
| 2810 | |
| 2811 | /* Operator '<=', '>=' */ |
| 2812 | tok_len = 2; |
| 2813 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2814 | |
| 2815 | } else if (expr[parsed] == '|') { |
| 2816 | |
| 2817 | /* Operator '|' */ |
| 2818 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2819 | tok_type = LYXP_TOKEN_OPER_UNI; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2820 | |
| 2821 | } else if ((expr[parsed] == '+') || (expr[parsed] == '-')) { |
| 2822 | |
| 2823 | /* Operator '+', '-' */ |
| 2824 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2825 | tok_type = LYXP_TOKEN_OPER_MATH; |
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 | } else if (expr[parsed] == '=') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2828 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2829 | /* Operator '=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2830 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2831 | tok_type = LYXP_TOKEN_OPER_EQUAL; |
| 2832 | |
| 2833 | } else if ((expr[parsed] == '<') || (expr[parsed] == '>')) { |
| 2834 | |
| 2835 | /* Operator '<', '>' */ |
| 2836 | tok_len = 1; |
| 2837 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2838 | |
| 2839 | } else if (ret->used && (ret->tokens[ret->used - 1] != LYXP_TOKEN_AT) |
| 2840 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_PAR1) |
| 2841 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_BRACK1) |
| 2842 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_COMMA) |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2843 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_LOG) |
| 2844 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_EQUAL) |
| 2845 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_NEQUAL) |
| 2846 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_COMP) |
| 2847 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_MATH) |
| 2848 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_UNI) |
| 2849 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_PATH) |
| 2850 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_RPATH)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2851 | |
| 2852 | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
| 2853 | if (expr[parsed] == '*') { |
| 2854 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2855 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2856 | |
| 2857 | } else if (!strncmp(&expr[parsed], "or", 2)) { |
| 2858 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2859 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2860 | |
| 2861 | } else if (!strncmp(&expr[parsed], "and", 3)) { |
| 2862 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2863 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2864 | |
| 2865 | } else if (!strncmp(&expr[parsed], "mod", 3) || !strncmp(&expr[parsed], "div", 3)) { |
| 2866 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2867 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2868 | |
| 2869 | } else if (prev_function_check) { |
Michal Vasko | 5307857 | 2019-05-24 08:50:15 +0200 | [diff] [blame] | 2870 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.", |
| 2871 | 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] | 2872 | goto error; |
| 2873 | } else { |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2874 | 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] | 2875 | goto error; |
| 2876 | } |
| 2877 | } else if (expr[parsed] == '*') { |
| 2878 | |
| 2879 | /* NameTest '*' */ |
| 2880 | tok_len = 1; |
| 2881 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2882 | |
| 2883 | } else { |
| 2884 | |
| 2885 | /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2886 | long int ncname_len = parse_ncname(&expr[parsed]); |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2887 | 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] | 2888 | tok_len = ncname_len; |
| 2889 | |
| 2890 | if (expr[parsed + tok_len] == ':') { |
| 2891 | ++tok_len; |
| 2892 | if (expr[parsed + tok_len] == '*') { |
| 2893 | ++tok_len; |
| 2894 | } else { |
| 2895 | ncname_len = parse_ncname(&expr[parsed + tok_len]); |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2896 | 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] | 2897 | tok_len += ncname_len; |
| 2898 | } |
| 2899 | /* remove old flag to prevent ambiguities */ |
| 2900 | prev_function_check = 0; |
| 2901 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2902 | } else { |
| 2903 | /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */ |
| 2904 | prev_function_check = 1; |
| 2905 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2906 | } |
| 2907 | } |
| 2908 | |
| 2909 | /* store the token, move on to the next one */ |
| 2910 | LY_CHECK_GOTO(exp_add_token(ctx, ret, tok_type, parsed, tok_len), error); |
| 2911 | parsed += tok_len; |
| 2912 | while (is_xmlws(expr[parsed])) { |
| 2913 | ++parsed; |
| 2914 | } |
| 2915 | |
| 2916 | } while (expr[parsed]); |
| 2917 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2918 | if (reparse) { |
| 2919 | /* prealloc repeat */ |
| 2920 | ret->repeat = calloc(ret->size, sizeof *ret->repeat); |
| 2921 | LY_CHECK_ERR_GOTO(!ret->repeat, LOGMEM(ctx), error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2922 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2923 | /* fill repeat */ |
| 2924 | LY_CHECK_GOTO(reparse_or_expr(ctx, ret, &tok_idx), error); |
| 2925 | if (ret->used > tok_idx) { |
| 2926 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.", |
| 2927 | &ret->expr[ret->tok_pos[tok_idx]]); |
| 2928 | goto error; |
| 2929 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2930 | } |
| 2931 | |
| 2932 | print_expr_struct_debug(ret); |
| 2933 | |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2934 | return ret; |
| 2935 | |
| 2936 | error: |
| 2937 | lyxp_expr_free(ctx, ret); |
| 2938 | return NULL; |
| 2939 | } |
| 2940 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2941 | struct lyxp_expr * |
| 2942 | lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp) |
| 2943 | { |
| 2944 | struct lyxp_expr *dup; |
| 2945 | uint32_t i, j; |
| 2946 | |
| 2947 | dup = calloc(1, sizeof *dup); |
| 2948 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx), error); |
| 2949 | |
| 2950 | dup->tokens = malloc(exp->used * sizeof *dup->tokens); |
| 2951 | LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx), error); |
| 2952 | memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens); |
| 2953 | |
| 2954 | dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos); |
| 2955 | LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx), error); |
| 2956 | memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos); |
| 2957 | |
| 2958 | dup->tok_len = malloc(exp->used * sizeof *dup->tok_len); |
| 2959 | LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx), error); |
| 2960 | memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len); |
| 2961 | |
| 2962 | dup->repeat = malloc(exp->used * sizeof *dup->repeat); |
| 2963 | LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx), error); |
| 2964 | for (i = 0; i < exp->used; ++i) { |
| 2965 | if (!exp->repeat[i]) { |
| 2966 | dup->repeat[i] = NULL; |
| 2967 | } else { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2968 | for (j = 0; exp->repeat[i][j]; ++j) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2969 | /* the ending 0 as well */ |
| 2970 | ++j; |
| 2971 | |
Michal Vasko | 99c7164 | 2020-07-03 13:33:36 +0200 | [diff] [blame] | 2972 | dup->repeat[i] = malloc(j * sizeof **dup->repeat); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2973 | LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx), error); |
| 2974 | memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat); |
| 2975 | dup->repeat[i][j - 1] = 0; |
| 2976 | } |
| 2977 | } |
| 2978 | |
| 2979 | dup->used = exp->used; |
| 2980 | dup->size = exp->used; |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 2981 | LY_CHECK_GOTO(lydict_insert(ctx, exp->expr, 0, &dup->expr), error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2982 | |
| 2983 | return dup; |
| 2984 | |
| 2985 | error: |
| 2986 | lyxp_expr_free(ctx, dup); |
| 2987 | return NULL; |
| 2988 | } |
| 2989 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2990 | /* |
| 2991 | * warn functions |
| 2992 | * |
| 2993 | * Warn functions check specific reasonable conditions for schema XPath |
| 2994 | * and print a warning if they are not satisfied. |
| 2995 | */ |
| 2996 | |
| 2997 | /** |
| 2998 | * @brief Get the last-added schema node that is currently in the context. |
| 2999 | * |
| 3000 | * @param[in] set Set to search in. |
| 3001 | * @return Last-added schema context node, NULL if no node is in context. |
| 3002 | */ |
| 3003 | static struct lysc_node * |
| 3004 | warn_get_scnode_in_ctx(struct lyxp_set *set) |
| 3005 | { |
| 3006 | uint32_t i; |
| 3007 | |
| 3008 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
| 3009 | return NULL; |
| 3010 | } |
| 3011 | |
| 3012 | i = set->used; |
| 3013 | do { |
| 3014 | --i; |
| 3015 | if (set->val.scnodes[i].in_ctx == 1) { |
| 3016 | /* if there are more, simply return the first found (last added) */ |
| 3017 | return set->val.scnodes[i].scnode; |
| 3018 | } |
| 3019 | } while (i); |
| 3020 | |
| 3021 | return NULL; |
| 3022 | } |
| 3023 | |
| 3024 | /** |
| 3025 | * @brief Test whether a type is numeric - integer type or decimal64. |
| 3026 | * |
| 3027 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3028 | * @return Boolean value whether @p type is numeric type or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3029 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3030 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3031 | warn_is_numeric_type(struct lysc_type *type) |
| 3032 | { |
| 3033 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3034 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3035 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3036 | |
| 3037 | switch (type->basetype) { |
| 3038 | case LY_TYPE_DEC64: |
| 3039 | case LY_TYPE_INT8: |
| 3040 | case LY_TYPE_UINT8: |
| 3041 | case LY_TYPE_INT16: |
| 3042 | case LY_TYPE_UINT16: |
| 3043 | case LY_TYPE_INT32: |
| 3044 | case LY_TYPE_UINT32: |
| 3045 | case LY_TYPE_INT64: |
| 3046 | case LY_TYPE_UINT64: |
| 3047 | return 1; |
| 3048 | case LY_TYPE_UNION: |
| 3049 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3050 | LY_ARRAY_FOR(uni->types, u) { |
| 3051 | ret = warn_is_numeric_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3052 | if (ret) { |
| 3053 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3054 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3055 | } |
| 3056 | } |
| 3057 | /* did not find any suitable type */ |
| 3058 | return 0; |
| 3059 | case LY_TYPE_LEAFREF: |
| 3060 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
| 3061 | default: |
| 3062 | return 0; |
| 3063 | } |
| 3064 | } |
| 3065 | |
| 3066 | /** |
| 3067 | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
| 3068 | * |
| 3069 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3070 | * @return Boolean value whether @p type's basetype is string type or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3071 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3072 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3073 | warn_is_string_type(struct lysc_type *type) |
| 3074 | { |
| 3075 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3076 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3077 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3078 | |
| 3079 | switch (type->basetype) { |
| 3080 | case LY_TYPE_BITS: |
| 3081 | case LY_TYPE_ENUM: |
| 3082 | case LY_TYPE_IDENT: |
| 3083 | case LY_TYPE_INST: |
| 3084 | case LY_TYPE_STRING: |
| 3085 | return 1; |
| 3086 | case LY_TYPE_UNION: |
| 3087 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3088 | LY_ARRAY_FOR(uni->types, u) { |
| 3089 | ret = warn_is_string_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3090 | if (ret) { |
| 3091 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3092 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3093 | } |
| 3094 | } |
| 3095 | /* did not find any suitable type */ |
| 3096 | return 0; |
| 3097 | case LY_TYPE_LEAFREF: |
| 3098 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
| 3099 | default: |
| 3100 | return 0; |
| 3101 | } |
| 3102 | } |
| 3103 | |
| 3104 | /** |
| 3105 | * @brief Test whether a type is one specific type. |
| 3106 | * |
| 3107 | * @param[in] type Type to test. |
| 3108 | * @param[in] base Expected type. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3109 | * @return Boolean value whether the given @p type is of the specific basetype @p base. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3110 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3111 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3112 | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
| 3113 | { |
| 3114 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3115 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3116 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3117 | |
| 3118 | if (type->basetype == base) { |
| 3119 | return 1; |
| 3120 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3121 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3122 | LY_ARRAY_FOR(uni->types, u) { |
| 3123 | ret = warn_is_specific_type(uni->types[u], base); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3124 | if (ret) { |
| 3125 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3126 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3127 | } |
| 3128 | } |
| 3129 | /* did not find any suitable type */ |
| 3130 | return 0; |
| 3131 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
| 3132 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
| 3133 | } |
| 3134 | |
| 3135 | return 0; |
| 3136 | } |
| 3137 | |
| 3138 | /** |
| 3139 | * @brief Get next type of a (union) type. |
| 3140 | * |
| 3141 | * @param[in] type Base type. |
| 3142 | * @param[in] prev_type Previously returned type. |
| 3143 | * @return Next type or NULL. |
| 3144 | */ |
| 3145 | static struct lysc_type * |
| 3146 | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type) |
| 3147 | { |
| 3148 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3149 | ly_bool found = 0; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3150 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3151 | |
| 3152 | switch (type->basetype) { |
| 3153 | case LY_TYPE_UNION: |
| 3154 | uni = (struct lysc_type_union *)type; |
| 3155 | if (!prev_type) { |
| 3156 | return uni->types[0]; |
| 3157 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3158 | LY_ARRAY_FOR(uni->types, u) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3159 | if (found) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3160 | return uni->types[u]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3161 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3162 | if (prev_type == uni->types[u]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3163 | found = 1; |
| 3164 | } |
| 3165 | } |
| 3166 | return NULL; |
| 3167 | default: |
| 3168 | if (prev_type) { |
| 3169 | assert(type == prev_type); |
| 3170 | return NULL; |
| 3171 | } else { |
| 3172 | return type; |
| 3173 | } |
| 3174 | } |
| 3175 | } |
| 3176 | |
| 3177 | /** |
| 3178 | * @brief Test whether 2 types have a common type. |
| 3179 | * |
| 3180 | * @param[in] type1 First type. |
| 3181 | * @param[in] type2 Second type. |
| 3182 | * @return 1 if they do, 0 otherwise. |
| 3183 | */ |
| 3184 | static int |
| 3185 | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
| 3186 | { |
| 3187 | struct lysc_type *t1, *rt1, *t2, *rt2; |
| 3188 | |
| 3189 | t1 = NULL; |
| 3190 | while ((t1 = warn_is_equal_type_next_type(type1, t1))) { |
| 3191 | if (t1->basetype == LY_TYPE_LEAFREF) { |
| 3192 | rt1 = ((struct lysc_type_leafref *)t1)->realtype; |
| 3193 | } else { |
| 3194 | rt1 = t1; |
| 3195 | } |
| 3196 | |
| 3197 | t2 = NULL; |
| 3198 | while ((t2 = warn_is_equal_type_next_type(type2, t2))) { |
| 3199 | if (t2->basetype == LY_TYPE_LEAFREF) { |
| 3200 | rt2 = ((struct lysc_type_leafref *)t2)->realtype; |
| 3201 | } else { |
| 3202 | rt2 = t2; |
| 3203 | } |
| 3204 | |
| 3205 | if (rt2->basetype == rt1->basetype) { |
| 3206 | /* match found */ |
| 3207 | return 1; |
| 3208 | } |
| 3209 | } |
| 3210 | } |
| 3211 | |
| 3212 | return 0; |
| 3213 | } |
| 3214 | |
| 3215 | /** |
| 3216 | * @brief Check both operands of comparison operators. |
| 3217 | * |
| 3218 | * @param[in] ctx Context for errors. |
| 3219 | * @param[in] set1 First operand set. |
| 3220 | * @param[in] set2 Second operand set. |
| 3221 | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
| 3222 | * @param[in] expr Start of the expression to print with the warning. |
| 3223 | * @param[in] tok_pos Token position. |
| 3224 | */ |
| 3225 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3226 | warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, ly_bool numbers_only, const char *expr, uint16_t tok_pos) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3227 | { |
| 3228 | struct lysc_node_leaf *node1, *node2; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3229 | ly_bool leaves = 1, warning = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3230 | |
| 3231 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
| 3232 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
| 3233 | |
| 3234 | if (!node1 && !node2) { |
| 3235 | /* no node-sets involved, nothing to do */ |
| 3236 | return; |
| 3237 | } |
| 3238 | |
| 3239 | if (node1) { |
| 3240 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3241 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
| 3242 | warning = 1; |
| 3243 | leaves = 0; |
| 3244 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
| 3245 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
| 3246 | warning = 1; |
| 3247 | } |
| 3248 | } |
| 3249 | |
| 3250 | if (node2) { |
| 3251 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3252 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
| 3253 | warning = 1; |
| 3254 | leaves = 0; |
| 3255 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
| 3256 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
| 3257 | warning = 1; |
| 3258 | } |
| 3259 | } |
| 3260 | |
| 3261 | if (node1 && node2 && leaves && !numbers_only) { |
| 3262 | if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) |
| 3263 | || (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) |
| 3264 | || (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) |
| 3265 | && !warn_is_equal_type(node1->type, node2->type))) { |
| 3266 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
| 3267 | warning = 1; |
| 3268 | } |
| 3269 | } |
| 3270 | |
| 3271 | if (warning) { |
| 3272 | LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos); |
| 3273 | } |
| 3274 | } |
| 3275 | |
| 3276 | /** |
| 3277 | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
| 3278 | * |
| 3279 | * @param[in] exp Parsed XPath expression. |
| 3280 | * @param[in] set Set with the leaf/leaf-list. |
| 3281 | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
| 3282 | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
| 3283 | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
| 3284 | */ |
| 3285 | static void |
| 3286 | warn_equality_value(struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, uint16_t last_equal_exp) |
| 3287 | { |
| 3288 | struct lysc_node *scnode; |
| 3289 | struct lysc_type *type; |
| 3290 | char *value; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3291 | struct lyd_value storage; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3292 | LY_ERR rc; |
| 3293 | struct ly_err_item *err = NULL; |
| 3294 | |
| 3295 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 3296 | && ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) { |
| 3297 | /* check that the node can have the specified value */ |
| 3298 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
| 3299 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
| 3300 | } else { |
| 3301 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
| 3302 | } |
| 3303 | if (!value) { |
| 3304 | LOGMEM(set->ctx); |
| 3305 | return; |
| 3306 | } |
| 3307 | |
| 3308 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
| 3309 | LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding" |
| 3310 | " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value); |
| 3311 | 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] | 3312 | (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] | 3313 | exp->expr + exp->tok_pos[equal_exp]); |
| 3314 | } |
| 3315 | |
| 3316 | type = ((struct lysc_node_leaf *)scnode)->type; |
| 3317 | if (type->basetype != LY_TYPE_IDENT) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 3318 | 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] | 3319 | (void *)set->local_mod, NULL, NULL, &storage, &err); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3320 | |
| 3321 | if (err) { |
| 3322 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
| 3323 | ly_err_free(err); |
| 3324 | } else if (rc != LY_SUCCESS) { |
| 3325 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
| 3326 | } |
| 3327 | if (rc != LY_SUCCESS) { |
| 3328 | 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] | 3329 | (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] | 3330 | exp->expr + exp->tok_pos[equal_exp]); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3331 | } else { |
| 3332 | type->plugin->free(set->ctx, &storage); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3333 | } |
| 3334 | } |
| 3335 | free(value); |
| 3336 | } |
| 3337 | } |
| 3338 | |
| 3339 | /* |
| 3340 | * XPath functions |
| 3341 | */ |
| 3342 | |
| 3343 | /** |
| 3344 | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
| 3345 | * depending on whether the first node bit value from the second argument is set. |
| 3346 | * |
| 3347 | * @param[in] args Array of arguments. |
| 3348 | * @param[in] arg_count Count of elements in @p args. |
| 3349 | * @param[in,out] set Context and result set at the same time. |
| 3350 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3351 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3352 | */ |
| 3353 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3354 | 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] | 3355 | { |
| 3356 | struct lyd_node_term *leaf; |
| 3357 | struct lysc_node_leaf *sleaf; |
| 3358 | struct lysc_type_bits *bits; |
| 3359 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3360 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3361 | |
| 3362 | if (options & LYXP_SCNODE_ALL) { |
| 3363 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3364 | 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] | 3365 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3366 | 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] | 3367 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
| 3368 | 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] | 3369 | } |
| 3370 | |
| 3371 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3372 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3373 | 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] | 3374 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3375 | 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] | 3376 | } |
| 3377 | } |
| 3378 | set_scnode_clear_ctx(set); |
| 3379 | return rc; |
| 3380 | } |
| 3381 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3382 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3383 | 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)"); |
| 3384 | return LY_EVALID; |
| 3385 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3386 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3387 | LY_CHECK_RET(rc); |
| 3388 | |
| 3389 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3390 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3391 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3392 | if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 3393 | && (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) { |
| 3394 | bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3395 | LY_ARRAY_FOR(bits->bits, u) { |
| 3396 | if (!strcmp(bits->bits[u].name, args[1]->val.str)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3397 | set_fill_boolean(set, 1); |
| 3398 | break; |
| 3399 | } |
| 3400 | } |
| 3401 | } |
| 3402 | } |
| 3403 | |
| 3404 | return LY_SUCCESS; |
| 3405 | } |
| 3406 | |
| 3407 | /** |
| 3408 | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
| 3409 | * with the argument converted to boolean. |
| 3410 | * |
| 3411 | * @param[in] args Array of arguments. |
| 3412 | * @param[in] arg_count Count of elements in @p args. |
| 3413 | * @param[in,out] set Context and result set at the same time. |
| 3414 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3415 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3416 | */ |
| 3417 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3418 | 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] | 3419 | { |
| 3420 | LY_ERR rc; |
| 3421 | |
| 3422 | if (options & LYXP_SCNODE_ALL) { |
| 3423 | set_scnode_clear_ctx(set); |
| 3424 | return LY_SUCCESS; |
| 3425 | } |
| 3426 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3427 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3428 | LY_CHECK_RET(rc); |
| 3429 | set_fill_set(set, args[0]); |
| 3430 | |
| 3431 | return LY_SUCCESS; |
| 3432 | } |
| 3433 | |
| 3434 | /** |
| 3435 | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
| 3436 | * with the first argument rounded up to the nearest integer. |
| 3437 | * |
| 3438 | * @param[in] args Array of arguments. |
| 3439 | * @param[in] arg_count Count of elements in @p args. |
| 3440 | * @param[in,out] set Context and result set at the same time. |
| 3441 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3442 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3443 | */ |
| 3444 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3445 | 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] | 3446 | { |
| 3447 | struct lysc_node_leaf *sleaf; |
| 3448 | LY_ERR rc = LY_SUCCESS; |
| 3449 | |
| 3450 | if (options & LYXP_SCNODE_ALL) { |
| 3451 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3452 | 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] | 3453 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3454 | 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] | 3455 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 3456 | 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] | 3457 | } |
| 3458 | set_scnode_clear_ctx(set); |
| 3459 | return rc; |
| 3460 | } |
| 3461 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3462 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3463 | LY_CHECK_RET(rc); |
| 3464 | if ((long long)args[0]->val.num != args[0]->val.num) { |
| 3465 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
| 3466 | } else { |
| 3467 | set_fill_number(set, args[0]->val.num); |
| 3468 | } |
| 3469 | |
| 3470 | return LY_SUCCESS; |
| 3471 | } |
| 3472 | |
| 3473 | /** |
| 3474 | * @brief Execute the XPath concat(string, string, string*) function. |
| 3475 | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
| 3476 | * |
| 3477 | * @param[in] args Array of arguments. |
| 3478 | * @param[in] arg_count Count of elements in @p args. |
| 3479 | * @param[in,out] set Context and result set at the same time. |
| 3480 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3481 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3482 | */ |
| 3483 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3484 | 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] | 3485 | { |
| 3486 | uint16_t i; |
| 3487 | char *str = NULL; |
| 3488 | size_t used = 1; |
| 3489 | LY_ERR rc = LY_SUCCESS; |
| 3490 | struct lysc_node_leaf *sleaf; |
| 3491 | |
| 3492 | if (options & LYXP_SCNODE_ALL) { |
| 3493 | for (i = 0; i < arg_count; ++i) { |
| 3494 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
| 3495 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3496 | LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".", |
| 3497 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3498 | } else if (!warn_is_string_type(sleaf->type)) { |
Radek Krejci | 70124c8 | 2020-08-14 22:17:03 +0200 | [diff] [blame] | 3499 | 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] | 3500 | } |
| 3501 | } |
| 3502 | } |
| 3503 | set_scnode_clear_ctx(set); |
| 3504 | return rc; |
| 3505 | } |
| 3506 | |
| 3507 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3508 | rc = lyxp_set_cast(args[i], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3509 | if (rc != LY_SUCCESS) { |
| 3510 | free(str); |
| 3511 | return rc; |
| 3512 | } |
| 3513 | |
| 3514 | str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char)); |
| 3515 | LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM); |
| 3516 | strcpy(str + used - 1, args[i]->val.str); |
| 3517 | used += strlen(args[i]->val.str); |
| 3518 | } |
| 3519 | |
| 3520 | /* free, kind of */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3521 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3522 | set->type = LYXP_SET_STRING; |
| 3523 | set->val.str = str; |
| 3524 | |
| 3525 | return LY_SUCCESS; |
| 3526 | } |
| 3527 | |
| 3528 | /** |
| 3529 | * @brief Execute the XPath contains(string, string) function. |
| 3530 | * Returns LYXP_SET_BOOLEAN whether the second argument can |
| 3531 | * be found in the first or not. |
| 3532 | * |
| 3533 | * @param[in] args Array of arguments. |
| 3534 | * @param[in] arg_count Count of elements in @p args. |
| 3535 | * @param[in,out] set Context and result set at the same time. |
| 3536 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3537 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3538 | */ |
| 3539 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3540 | 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] | 3541 | { |
| 3542 | struct lysc_node_leaf *sleaf; |
| 3543 | LY_ERR rc = LY_SUCCESS; |
| 3544 | |
| 3545 | if (options & LYXP_SCNODE_ALL) { |
| 3546 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3547 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3548 | 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] | 3549 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3550 | 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] | 3551 | } |
| 3552 | } |
| 3553 | |
| 3554 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3555 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3556 | 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] | 3557 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3558 | 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] | 3559 | } |
| 3560 | } |
| 3561 | set_scnode_clear_ctx(set); |
| 3562 | return rc; |
| 3563 | } |
| 3564 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3565 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3566 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3567 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3568 | LY_CHECK_RET(rc); |
| 3569 | |
| 3570 | if (strstr(args[0]->val.str, args[1]->val.str)) { |
| 3571 | set_fill_boolean(set, 1); |
| 3572 | } else { |
| 3573 | set_fill_boolean(set, 0); |
| 3574 | } |
| 3575 | |
| 3576 | return LY_SUCCESS; |
| 3577 | } |
| 3578 | |
| 3579 | /** |
| 3580 | * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER |
| 3581 | * with the size of the node-set from the argument. |
| 3582 | * |
| 3583 | * @param[in] args Array of arguments. |
| 3584 | * @param[in] arg_count Count of elements in @p args. |
| 3585 | * @param[in,out] set Context and result set at the same time. |
| 3586 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3587 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3588 | */ |
| 3589 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3590 | 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] | 3591 | { |
| 3592 | struct lysc_node *scnode = NULL; |
| 3593 | LY_ERR rc = LY_SUCCESS; |
| 3594 | |
| 3595 | if (options & LYXP_SCNODE_ALL) { |
| 3596 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) { |
| 3597 | 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] | 3598 | } |
| 3599 | set_scnode_clear_ctx(set); |
| 3600 | return rc; |
| 3601 | } |
| 3602 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3603 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 3604 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)"); |
| 3605 | return LY_EVALID; |
| 3606 | } |
| 3607 | |
| 3608 | set_fill_number(set, args[0]->used); |
| 3609 | return LY_SUCCESS; |
| 3610 | } |
| 3611 | |
| 3612 | /** |
| 3613 | * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET |
| 3614 | * with the context with the intial node. |
| 3615 | * |
| 3616 | * @param[in] args Array of arguments. |
| 3617 | * @param[in] arg_count Count of elements in @p args. |
| 3618 | * @param[in,out] set Context and result set at the same time. |
| 3619 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3620 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3621 | */ |
| 3622 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3623 | 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] | 3624 | { |
| 3625 | if (arg_count || args) { |
| 3626 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()"); |
| 3627 | return LY_EVALID; |
| 3628 | } |
| 3629 | |
| 3630 | if (options & LYXP_SCNODE_ALL) { |
| 3631 | set_scnode_clear_ctx(set); |
| 3632 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 3633 | 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] | 3634 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3635 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3636 | |
| 3637 | /* position is filled later */ |
| 3638 | set_insert_node(set, set->ctx_node, 0, LYXP_NODE_ELEM, 0); |
| 3639 | } |
| 3640 | |
| 3641 | return LY_SUCCESS; |
| 3642 | } |
| 3643 | |
| 3644 | /** |
| 3645 | * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either |
| 3646 | * leafref or instance-identifier target node(s). |
| 3647 | * |
| 3648 | * @param[in] args Array of arguments. |
| 3649 | * @param[in] arg_count Count of elements in @p args. |
| 3650 | * @param[in,out] set Context and result set at the same time. |
| 3651 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3652 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3653 | */ |
| 3654 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3655 | 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] | 3656 | { |
| 3657 | struct lyd_node_term *leaf; |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3658 | struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3659 | struct lysc_type_leafref *lref; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3660 | const struct lysc_node *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3661 | struct ly_path *p; |
| 3662 | struct lyd_node *node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3663 | char *errmsg = NULL; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3664 | uint8_t oper; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3665 | LY_ERR rc = LY_SUCCESS; |
| 3666 | |
| 3667 | if (options & LYXP_SCNODE_ALL) { |
| 3668 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3669 | 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] | 3670 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3671 | 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] | 3672 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
| 3673 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".", |
| 3674 | __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3675 | } |
| 3676 | set_scnode_clear_ctx(set); |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3677 | if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3678 | lref = (struct lysc_type_leafref *)sleaf->type; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3679 | 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] | 3680 | |
| 3681 | /* it was already evaluated on schema, it must succeed */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 3682 | rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE, |
| 3683 | oper, LY_PATH_TARGET_MANY, set->format, lref->path_context, &p); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3684 | assert(!rc); |
| 3685 | |
| 3686 | /* get the target node */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3687 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3688 | ly_path_free(set->ctx, p); |
| 3689 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 3690 | 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] | 3691 | } |
| 3692 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3693 | return rc; |
| 3694 | } |
| 3695 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3696 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3697 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)"); |
| 3698 | return LY_EVALID; |
| 3699 | } |
| 3700 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3701 | lyxp_set_free_content(set); |
| 3702 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3703 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3704 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3705 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 3706 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3707 | /* find leafref target */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3708 | if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf, |
| 3709 | &leaf->value, set->tree, &node, &errmsg)) { |
| 3710 | LOGERR(set->ctx, LY_EVALID, errmsg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3711 | free(errmsg); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3712 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3713 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3714 | } else { |
| 3715 | assert(sleaf->type->basetype == LY_TYPE_INST); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3716 | if (ly_path_eval(leaf->value.target, set->tree, &node)) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3717 | 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] | 3718 | LYD_CANON_VALUE(leaf)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3719 | return LY_EVALID; |
| 3720 | } |
| 3721 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3722 | |
| 3723 | /* insert it */ |
| 3724 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3725 | } |
| 3726 | } |
| 3727 | |
| 3728 | return LY_SUCCESS; |
| 3729 | } |
| 3730 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3731 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3732 | xpath_derived_(struct lyxp_set **args, struct lyxp_set *set, uint32_t options, ly_bool self_match, const char *func) |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3733 | { |
| 3734 | uint16_t i; |
| 3735 | LY_ARRAY_COUNT_TYPE u; |
| 3736 | struct lyd_node_term *leaf; |
| 3737 | struct lysc_node_leaf *sleaf; |
| 3738 | struct lyd_meta *meta; |
| 3739 | struct lyd_value data = {0}, *val; |
| 3740 | struct ly_err_item *err = NULL; |
| 3741 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3742 | ly_bool found; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3743 | |
| 3744 | if (options & LYXP_SCNODE_ALL) { |
| 3745 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3746 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func); |
| 3747 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3748 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
| 3749 | sleaf->name); |
| 3750 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 3751 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name); |
| 3752 | } |
| 3753 | |
| 3754 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3755 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3756 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
| 3757 | sleaf->name); |
| 3758 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3759 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name); |
| 3760 | } |
| 3761 | } |
| 3762 | set_scnode_clear_ctx(set); |
| 3763 | return rc; |
| 3764 | } |
| 3765 | |
| 3766 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 3767 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 3768 | "derived-from(-or-self)(node-set, string)"); |
| 3769 | return LY_EVALID; |
| 3770 | } |
| 3771 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
| 3772 | LY_CHECK_RET(rc); |
| 3773 | |
| 3774 | set_fill_boolean(set, 0); |
| 3775 | found = 0; |
| 3776 | for (i = 0; i < args[0]->used; ++i) { |
| 3777 | if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) { |
| 3778 | continue; |
| 3779 | } |
| 3780 | |
| 3781 | if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 3782 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 3783 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3784 | val = &leaf->value; |
| 3785 | if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) { |
| 3786 | /* uninteresting */ |
| 3787 | continue; |
| 3788 | } |
| 3789 | |
| 3790 | /* store args[1] as ident */ |
| 3791 | 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] | 3792 | 0, set->format, (void *)set->local_mod, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3793 | (struct lyd_node *)leaf, set->tree, &data, &err); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3794 | } else { |
| 3795 | meta = args[0]->val.meta[i].meta; |
| 3796 | val = &meta->value; |
| 3797 | if (val->realtype->basetype != LY_TYPE_IDENT) { |
| 3798 | /* uninteresting */ |
| 3799 | continue; |
| 3800 | } |
| 3801 | |
| 3802 | /* store args[1] as ident */ |
| 3803 | 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] | 3804 | 0, set->format, (void *)meta->annotation->module, |
| 3805 | meta->parent, set->tree, &data, &err); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3806 | } |
| 3807 | |
| 3808 | if (err) { |
| 3809 | ly_err_print(err); |
| 3810 | ly_err_free(err); |
| 3811 | } |
| 3812 | LY_CHECK_RET(rc); |
| 3813 | |
| 3814 | /* finally check the identity itself */ |
| 3815 | if (self_match && (data.ident == val->ident)) { |
| 3816 | set_fill_boolean(set, 1); |
| 3817 | found = 1; |
| 3818 | } |
| 3819 | if (!found) { |
| 3820 | LY_ARRAY_FOR(data.ident->derived, u) { |
| 3821 | if (data.ident->derived[u] == val->ident) { |
| 3822 | set_fill_boolean(set, 1); |
| 3823 | found = 1; |
| 3824 | break; |
| 3825 | } |
| 3826 | } |
| 3827 | } |
| 3828 | |
| 3829 | /* free temporary value */ |
| 3830 | val->realtype->plugin->free(set->ctx, &data); |
| 3831 | if (found) { |
| 3832 | break; |
| 3833 | } |
| 3834 | } |
| 3835 | |
| 3836 | return LY_SUCCESS; |
| 3837 | } |
| 3838 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3839 | /** |
| 3840 | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3841 | * on whether the first argument nodes contain a node of an identity derived from the second |
| 3842 | * argument identity. |
| 3843 | * |
| 3844 | * @param[in] args Array of arguments. |
| 3845 | * @param[in] arg_count Count of elements in @p args. |
| 3846 | * @param[in,out] set Context and result set at the same time. |
| 3847 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3848 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3849 | */ |
| 3850 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3851 | 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] | 3852 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3853 | return xpath_derived_(args, set, options, 0, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3854 | } |
| 3855 | |
| 3856 | /** |
| 3857 | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3858 | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
| 3859 | * the second argument identity. |
| 3860 | * |
| 3861 | * @param[in] args Array of arguments. |
| 3862 | * @param[in] arg_count Count of elements in @p args. |
| 3863 | * @param[in,out] set Context and result set at the same time. |
| 3864 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3865 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3866 | */ |
| 3867 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3868 | 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] | 3869 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3870 | return xpath_derived_(args, set, options, 1, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3871 | } |
| 3872 | |
| 3873 | /** |
| 3874 | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
| 3875 | * with the integer value of the first node's enum value, otherwise NaN. |
| 3876 | * |
| 3877 | * @param[in] args Array of arguments. |
| 3878 | * @param[in] arg_count Count of elements in @p args. |
| 3879 | * @param[in,out] set Context and result set at the same time. |
| 3880 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3881 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3882 | */ |
| 3883 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3884 | 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] | 3885 | { |
| 3886 | struct lyd_node_term *leaf; |
| 3887 | struct lysc_node_leaf *sleaf; |
| 3888 | LY_ERR rc = LY_SUCCESS; |
| 3889 | |
| 3890 | if (options & LYXP_SCNODE_ALL) { |
| 3891 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3892 | 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] | 3893 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3894 | 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] | 3895 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
| 3896 | 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] | 3897 | } |
| 3898 | set_scnode_clear_ctx(set); |
| 3899 | return rc; |
| 3900 | } |
| 3901 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3902 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3903 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)"); |
| 3904 | return LY_EVALID; |
| 3905 | } |
| 3906 | |
| 3907 | set_fill_number(set, NAN); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3908 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3909 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3910 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3911 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
| 3912 | set_fill_number(set, leaf->value.enum_item->value); |
| 3913 | } |
| 3914 | } |
| 3915 | |
| 3916 | return LY_SUCCESS; |
| 3917 | } |
| 3918 | |
| 3919 | /** |
| 3920 | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
| 3921 | * with false value. |
| 3922 | * |
| 3923 | * @param[in] args Array of arguments. |
| 3924 | * @param[in] arg_count Count of elements in @p args. |
| 3925 | * @param[in,out] set Context and result set at the same time. |
| 3926 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3927 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3928 | */ |
| 3929 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3930 | 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] | 3931 | { |
| 3932 | if (options & LYXP_SCNODE_ALL) { |
| 3933 | set_scnode_clear_ctx(set); |
| 3934 | return LY_SUCCESS; |
| 3935 | } |
| 3936 | |
| 3937 | set_fill_boolean(set, 0); |
| 3938 | return LY_SUCCESS; |
| 3939 | } |
| 3940 | |
| 3941 | /** |
| 3942 | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
| 3943 | * with the first argument floored (truncated). |
| 3944 | * |
| 3945 | * @param[in] args Array of arguments. |
| 3946 | * @param[in] arg_count Count of elements in @p args. |
| 3947 | * @param[in,out] set Context and result set at the same time. |
| 3948 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3949 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3950 | */ |
| 3951 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3952 | 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] | 3953 | { |
| 3954 | LY_ERR rc; |
| 3955 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3956 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3957 | LY_CHECK_RET(rc); |
| 3958 | if (isfinite(args[0]->val.num)) { |
| 3959 | set_fill_number(set, (long long)args[0]->val.num); |
| 3960 | } |
| 3961 | |
| 3962 | return LY_SUCCESS; |
| 3963 | } |
| 3964 | |
| 3965 | /** |
| 3966 | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
| 3967 | * whether the language of the text matches the one from the argument. |
| 3968 | * |
| 3969 | * @param[in] args Array of arguments. |
| 3970 | * @param[in] arg_count Count of elements in @p args. |
| 3971 | * @param[in,out] set Context and result set at the same time. |
| 3972 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3973 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3974 | */ |
| 3975 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3976 | 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] | 3977 | { |
| 3978 | const struct lyd_node *node; |
| 3979 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 3980 | struct lyd_meta *meta = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3981 | const char *val; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3982 | LY_ERR rc = LY_SUCCESS; |
| 3983 | |
| 3984 | if (options & LYXP_SCNODE_ALL) { |
| 3985 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3986 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3987 | 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] | 3988 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3989 | 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] | 3990 | } |
| 3991 | } |
| 3992 | set_scnode_clear_ctx(set); |
| 3993 | return rc; |
| 3994 | } |
| 3995 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3996 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3997 | LY_CHECK_RET(rc); |
| 3998 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3999 | if (set->type != LYXP_SET_NODE_SET) { |
| 4000 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)"); |
| 4001 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4002 | } else if (!set->used) { |
| 4003 | set_fill_boolean(set, 0); |
| 4004 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4005 | } |
| 4006 | |
| 4007 | switch (set->val.nodes[0].type) { |
| 4008 | case LYXP_NODE_ELEM: |
| 4009 | case LYXP_NODE_TEXT: |
| 4010 | node = set->val.nodes[0].node; |
| 4011 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4012 | case LYXP_NODE_META: |
| 4013 | node = set->val.meta[0].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4014 | break; |
| 4015 | default: |
| 4016 | /* nothing to do with roots */ |
| 4017 | set_fill_boolean(set, 0); |
| 4018 | return LY_SUCCESS; |
| 4019 | } |
| 4020 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4021 | /* find lang metadata */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 4022 | for ( ; node; node = (struct lyd_node *)node->parent) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4023 | for (meta = node->meta; meta; meta = meta->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4024 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4025 | 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] | 4026 | break; |
| 4027 | } |
| 4028 | } |
| 4029 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4030 | if (meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4031 | break; |
| 4032 | } |
| 4033 | } |
| 4034 | |
| 4035 | /* compare languages */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4036 | if (!meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4037 | set_fill_boolean(set, 0); |
| 4038 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4039 | uint64_t i; |
| 4040 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4041 | val = meta->value.canonical; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4042 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 4043 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
| 4044 | set_fill_boolean(set, 0); |
| 4045 | break; |
| 4046 | } |
| 4047 | } |
| 4048 | if (!args[0]->val.str[i]) { |
| 4049 | if (!val[i] || (val[i] == '-')) { |
| 4050 | set_fill_boolean(set, 1); |
| 4051 | } else { |
| 4052 | set_fill_boolean(set, 0); |
| 4053 | } |
| 4054 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4055 | } |
| 4056 | |
| 4057 | return LY_SUCCESS; |
| 4058 | } |
| 4059 | |
| 4060 | /** |
| 4061 | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
| 4062 | * with the context size. |
| 4063 | * |
| 4064 | * @param[in] args Array of arguments. |
| 4065 | * @param[in] arg_count Count of elements in @p args. |
| 4066 | * @param[in,out] set Context and result set at the same time. |
| 4067 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4068 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4069 | */ |
| 4070 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4071 | 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] | 4072 | { |
| 4073 | if (options & LYXP_SCNODE_ALL) { |
| 4074 | set_scnode_clear_ctx(set); |
| 4075 | return LY_SUCCESS; |
| 4076 | } |
| 4077 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4078 | if (set->type != LYXP_SET_NODE_SET) { |
| 4079 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()"); |
| 4080 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4081 | } else if (!set->used) { |
| 4082 | set_fill_number(set, 0); |
| 4083 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4084 | } |
| 4085 | |
| 4086 | set_fill_number(set, set->ctx_size); |
| 4087 | return LY_SUCCESS; |
| 4088 | } |
| 4089 | |
| 4090 | /** |
| 4091 | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
| 4092 | * with the node name without namespace from the argument or the context. |
| 4093 | * |
| 4094 | * @param[in] args Array of arguments. |
| 4095 | * @param[in] arg_count Count of elements in @p args. |
| 4096 | * @param[in,out] set Context and result set at the same time. |
| 4097 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4098 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4099 | */ |
| 4100 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4101 | 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] | 4102 | { |
| 4103 | struct lyxp_set_node *item; |
| 4104 | /* suppress unused variable warning */ |
| 4105 | (void)options; |
| 4106 | |
| 4107 | if (options & LYXP_SCNODE_ALL) { |
| 4108 | set_scnode_clear_ctx(set); |
| 4109 | return LY_SUCCESS; |
| 4110 | } |
| 4111 | |
| 4112 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4113 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4114 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "local-name(node-set?)"); |
| 4115 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4116 | } else if (!args[0]->used) { |
| 4117 | set_fill_string(set, "", 0); |
| 4118 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4119 | } |
| 4120 | |
| 4121 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4122 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4123 | |
| 4124 | item = &args[0]->val.nodes[0]; |
| 4125 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4126 | if (set->type != LYXP_SET_NODE_SET) { |
| 4127 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)"); |
| 4128 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4129 | } else if (!set->used) { |
| 4130 | set_fill_string(set, "", 0); |
| 4131 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4132 | } |
| 4133 | |
| 4134 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4135 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4136 | |
| 4137 | item = &set->val.nodes[0]; |
| 4138 | } |
| 4139 | |
| 4140 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4141 | case LYXP_NODE_NONE: |
| 4142 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4143 | case LYXP_NODE_ROOT: |
| 4144 | case LYXP_NODE_ROOT_CONFIG: |
| 4145 | case LYXP_NODE_TEXT: |
| 4146 | set_fill_string(set, "", 0); |
| 4147 | break; |
| 4148 | case LYXP_NODE_ELEM: |
| 4149 | set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name)); |
| 4150 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4151 | case LYXP_NODE_META: |
| 4152 | 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] | 4153 | break; |
| 4154 | } |
| 4155 | |
| 4156 | return LY_SUCCESS; |
| 4157 | } |
| 4158 | |
| 4159 | /** |
| 4160 | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
| 4161 | * with the node name fully qualified (with namespace) from the argument or the context. |
| 4162 | * |
| 4163 | * @param[in] args Array of arguments. |
| 4164 | * @param[in] arg_count Count of elements in @p args. |
| 4165 | * @param[in,out] set Context and result set at the same time. |
| 4166 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4167 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4168 | */ |
| 4169 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4170 | 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] | 4171 | { |
| 4172 | struct lyxp_set_node *item; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4173 | struct lys_module *mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4174 | char *str; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4175 | const char *name = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4176 | |
| 4177 | if (options & LYXP_SCNODE_ALL) { |
| 4178 | set_scnode_clear_ctx(set); |
| 4179 | return LY_SUCCESS; |
| 4180 | } |
| 4181 | |
| 4182 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4183 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4184 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)"); |
| 4185 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4186 | } else if (!args[0]->used) { |
| 4187 | set_fill_string(set, "", 0); |
| 4188 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4189 | } |
| 4190 | |
| 4191 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4192 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4193 | |
| 4194 | item = &args[0]->val.nodes[0]; |
| 4195 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4196 | if (set->type != LYXP_SET_NODE_SET) { |
| 4197 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)"); |
| 4198 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4199 | } else if (!set->used) { |
| 4200 | set_fill_string(set, "", 0); |
| 4201 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4202 | } |
| 4203 | |
| 4204 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4205 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4206 | |
| 4207 | item = &set->val.nodes[0]; |
| 4208 | } |
| 4209 | |
| 4210 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4211 | case LYXP_NODE_NONE: |
| 4212 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4213 | case LYXP_NODE_ROOT: |
| 4214 | case LYXP_NODE_ROOT_CONFIG: |
| 4215 | case LYXP_NODE_TEXT: |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4216 | /* keep NULL */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4217 | break; |
| 4218 | case LYXP_NODE_ELEM: |
| 4219 | mod = item->node->schema->module; |
| 4220 | name = item->node->schema->name; |
| 4221 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4222 | case LYXP_NODE_META: |
| 4223 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
| 4224 | name = ((struct lyd_meta *)item->node)->name; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4225 | break; |
| 4226 | } |
| 4227 | |
| 4228 | if (mod && name) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4229 | int rc = -1; |
| 4230 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4231 | switch (set->format) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 4232 | case LY_PREF_SCHEMA: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4233 | rc = asprintf(&str, "%s:%s", lys_prefix_find_module(set->local_mod, mod), name); |
| 4234 | break; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 4235 | case LY_PREF_JSON: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4236 | rc = asprintf(&str, "%s:%s", mod->name, name); |
| 4237 | break; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 4238 | case LY_PREF_XML: |
Michal Vasko | 9409ef6 | 2019-09-12 11:47:17 +0200 | [diff] [blame] | 4239 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4240 | } |
| 4241 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
| 4242 | set_fill_string(set, str, strlen(str)); |
| 4243 | free(str); |
| 4244 | } else { |
| 4245 | set_fill_string(set, "", 0); |
| 4246 | } |
| 4247 | |
| 4248 | return LY_SUCCESS; |
| 4249 | } |
| 4250 | |
| 4251 | /** |
| 4252 | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
| 4253 | * with the namespace of the node from the argument or the context. |
| 4254 | * |
| 4255 | * @param[in] args Array of arguments. |
| 4256 | * @param[in] arg_count Count of elements in @p args. |
| 4257 | * @param[in,out] set Context and result set at the same time. |
| 4258 | * @param[in] options XPath options. |
| 4259 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4260 | */ |
| 4261 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4262 | 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] | 4263 | { |
| 4264 | struct lyxp_set_node *item; |
| 4265 | struct lys_module *mod; |
| 4266 | /* suppress unused variable warning */ |
| 4267 | (void)options; |
| 4268 | |
| 4269 | if (options & LYXP_SCNODE_ALL) { |
| 4270 | set_scnode_clear_ctx(set); |
| 4271 | return LY_SUCCESS; |
| 4272 | } |
| 4273 | |
| 4274 | if (arg_count) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4275 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4276 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 4277 | "namespace-uri(node-set?)"); |
| 4278 | return LY_EVALID; |
| 4279 | } else if (!args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4280 | set_fill_string(set, "", 0); |
| 4281 | return LY_SUCCESS; |
| 4282 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4283 | |
| 4284 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4285 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4286 | |
| 4287 | item = &args[0]->val.nodes[0]; |
| 4288 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4289 | if (set->type != LYXP_SET_NODE_SET) { |
| 4290 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)"); |
| 4291 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4292 | } else if (!set->used) { |
| 4293 | set_fill_string(set, "", 0); |
| 4294 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4295 | } |
| 4296 | |
| 4297 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4298 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4299 | |
| 4300 | item = &set->val.nodes[0]; |
| 4301 | } |
| 4302 | |
| 4303 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4304 | case LYXP_NODE_NONE: |
| 4305 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4306 | case LYXP_NODE_ROOT: |
| 4307 | case LYXP_NODE_ROOT_CONFIG: |
| 4308 | case LYXP_NODE_TEXT: |
| 4309 | set_fill_string(set, "", 0); |
| 4310 | break; |
| 4311 | case LYXP_NODE_ELEM: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4312 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4313 | if (item->type == LYXP_NODE_ELEM) { |
| 4314 | mod = item->node->schema->module; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4315 | } else { /* LYXP_NODE_META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4316 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4317 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4318 | } |
| 4319 | |
| 4320 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
| 4321 | break; |
| 4322 | } |
| 4323 | |
| 4324 | return LY_SUCCESS; |
| 4325 | } |
| 4326 | |
| 4327 | /** |
| 4328 | * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET |
| 4329 | * with only nodes from the context. In practice it either leaves the context |
| 4330 | * as it is or returns an empty node set. |
| 4331 | * |
| 4332 | * @param[in] args Array of arguments. |
| 4333 | * @param[in] arg_count Count of elements in @p args. |
| 4334 | * @param[in,out] set Context and result set at the same time. |
| 4335 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4336 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4337 | */ |
| 4338 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4339 | 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] | 4340 | { |
| 4341 | if (options & LYXP_SCNODE_ALL) { |
| 4342 | set_scnode_clear_ctx(set); |
| 4343 | return LY_SUCCESS; |
| 4344 | } |
| 4345 | |
| 4346 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4347 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4348 | } |
| 4349 | return LY_SUCCESS; |
| 4350 | } |
| 4351 | |
| 4352 | /** |
| 4353 | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
| 4354 | * with normalized value (no leading, trailing, double white spaces) of the node |
| 4355 | * from the argument or the context. |
| 4356 | * |
| 4357 | * @param[in] args Array of arguments. |
| 4358 | * @param[in] arg_count Count of elements in @p args. |
| 4359 | * @param[in,out] set Context and result set at the same time. |
| 4360 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4361 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4362 | */ |
| 4363 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4364 | 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] | 4365 | { |
| 4366 | uint16_t i, new_used; |
| 4367 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4368 | ly_bool have_spaces = 0, space_before = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4369 | struct lysc_node_leaf *sleaf; |
| 4370 | LY_ERR rc = LY_SUCCESS; |
| 4371 | |
| 4372 | if (options & LYXP_SCNODE_ALL) { |
| 4373 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4374 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4375 | 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] | 4376 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4377 | 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] | 4378 | } |
| 4379 | } |
| 4380 | set_scnode_clear_ctx(set); |
| 4381 | return rc; |
| 4382 | } |
| 4383 | |
| 4384 | if (arg_count) { |
| 4385 | set_fill_set(set, args[0]); |
| 4386 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4387 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4388 | LY_CHECK_RET(rc); |
| 4389 | |
| 4390 | /* is there any normalization necessary? */ |
| 4391 | for (i = 0; set->val.str[i]; ++i) { |
| 4392 | if (is_xmlws(set->val.str[i])) { |
| 4393 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
| 4394 | have_spaces = 1; |
| 4395 | break; |
| 4396 | } |
| 4397 | space_before = 1; |
| 4398 | } else { |
| 4399 | space_before = 0; |
| 4400 | } |
| 4401 | } |
| 4402 | |
| 4403 | /* yep, there is */ |
| 4404 | if (have_spaces) { |
| 4405 | /* it's enough, at least one character will go, makes space for ending '\0' */ |
| 4406 | new = malloc(strlen(set->val.str) * sizeof(char)); |
| 4407 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4408 | new_used = 0; |
| 4409 | |
| 4410 | space_before = 0; |
| 4411 | for (i = 0; set->val.str[i]; ++i) { |
| 4412 | if (is_xmlws(set->val.str[i])) { |
| 4413 | if ((i == 0) || space_before) { |
| 4414 | space_before = 1; |
| 4415 | continue; |
| 4416 | } else { |
| 4417 | space_before = 1; |
| 4418 | } |
| 4419 | } else { |
| 4420 | space_before = 0; |
| 4421 | } |
| 4422 | |
| 4423 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
| 4424 | ++new_used; |
| 4425 | } |
| 4426 | |
| 4427 | /* at worst there is one trailing space now */ |
| 4428 | if (new_used && is_xmlws(new[new_used - 1])) { |
| 4429 | --new_used; |
| 4430 | } |
| 4431 | |
| 4432 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 4433 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4434 | new[new_used] = '\0'; |
| 4435 | |
| 4436 | free(set->val.str); |
| 4437 | set->val.str = new; |
| 4438 | } |
| 4439 | |
| 4440 | return LY_SUCCESS; |
| 4441 | } |
| 4442 | |
| 4443 | /** |
| 4444 | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
| 4445 | * with the argument converted to boolean and logically inverted. |
| 4446 | * |
| 4447 | * @param[in] args Array of arguments. |
| 4448 | * @param[in] arg_count Count of elements in @p args. |
| 4449 | * @param[in,out] set Context and result set at the same time. |
| 4450 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4451 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4452 | */ |
| 4453 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4454 | 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] | 4455 | { |
| 4456 | if (options & LYXP_SCNODE_ALL) { |
| 4457 | set_scnode_clear_ctx(set); |
| 4458 | return LY_SUCCESS; |
| 4459 | } |
| 4460 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4461 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4462 | if (args[0]->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4463 | set_fill_boolean(set, 0); |
| 4464 | } else { |
| 4465 | set_fill_boolean(set, 1); |
| 4466 | } |
| 4467 | |
| 4468 | return LY_SUCCESS; |
| 4469 | } |
| 4470 | |
| 4471 | /** |
| 4472 | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
| 4473 | * with the number representation of either the argument or the context. |
| 4474 | * |
| 4475 | * @param[in] args Array of arguments. |
| 4476 | * @param[in] arg_count Count of elements in @p args. |
| 4477 | * @param[in,out] set Context and result set at the same time. |
| 4478 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4479 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4480 | */ |
| 4481 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4482 | 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] | 4483 | { |
| 4484 | LY_ERR rc; |
| 4485 | |
| 4486 | if (options & LYXP_SCNODE_ALL) { |
| 4487 | set_scnode_clear_ctx(set); |
| 4488 | return LY_SUCCESS; |
| 4489 | } |
| 4490 | |
| 4491 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4492 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4493 | LY_CHECK_RET(rc); |
| 4494 | set_fill_set(set, args[0]); |
| 4495 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4496 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4497 | LY_CHECK_RET(rc); |
| 4498 | } |
| 4499 | |
| 4500 | return LY_SUCCESS; |
| 4501 | } |
| 4502 | |
| 4503 | /** |
| 4504 | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
| 4505 | * with the context position. |
| 4506 | * |
| 4507 | * @param[in] args Array of arguments. |
| 4508 | * @param[in] arg_count Count of elements in @p args. |
| 4509 | * @param[in,out] set Context and result set at the same time. |
| 4510 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4511 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4512 | */ |
| 4513 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4514 | 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] | 4515 | { |
| 4516 | if (options & LYXP_SCNODE_ALL) { |
| 4517 | set_scnode_clear_ctx(set); |
| 4518 | return LY_SUCCESS; |
| 4519 | } |
| 4520 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4521 | if (set->type != LYXP_SET_NODE_SET) { |
| 4522 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()"); |
| 4523 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4524 | } else if (!set->used) { |
| 4525 | set_fill_number(set, 0); |
| 4526 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4527 | } |
| 4528 | |
| 4529 | set_fill_number(set, set->ctx_pos); |
| 4530 | |
| 4531 | /* UNUSED in 'Release' build type */ |
| 4532 | (void)options; |
| 4533 | return LY_SUCCESS; |
| 4534 | } |
| 4535 | |
| 4536 | /** |
| 4537 | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
| 4538 | * depending on whether the second argument regex matches the first argument string. For details refer to |
| 4539 | * YANG 1.1 RFC section 10.2.1. |
| 4540 | * |
| 4541 | * @param[in] args Array of arguments. |
| 4542 | * @param[in] arg_count Count of elements in @p args. |
| 4543 | * @param[in,out] set Context and result set at the same time. |
| 4544 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4545 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4546 | */ |
| 4547 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4548 | 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] | 4549 | { |
| 4550 | struct lysc_pattern **patterns = NULL, **pattern; |
| 4551 | struct lysc_node_leaf *sleaf; |
| 4552 | char *path; |
| 4553 | LY_ERR rc = LY_SUCCESS; |
| 4554 | struct ly_err_item *err; |
| 4555 | |
| 4556 | if (options & LYXP_SCNODE_ALL) { |
| 4557 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4558 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4559 | 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] | 4560 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4561 | 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] | 4562 | } |
| 4563 | } |
| 4564 | |
| 4565 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4566 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4567 | 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] | 4568 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4569 | 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] | 4570 | } |
| 4571 | } |
| 4572 | set_scnode_clear_ctx(set); |
| 4573 | return rc; |
| 4574 | } |
| 4575 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4576 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4577 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4578 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4579 | LY_CHECK_RET(rc); |
| 4580 | |
| 4581 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
| 4582 | *pattern = malloc(sizeof **pattern); |
| 4583 | path = lyd_path(set->ctx_node, LYD_PATH_LOG, NULL, 0); |
| 4584 | rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code); |
| 4585 | free(path); |
| 4586 | if (rc != LY_SUCCESS) { |
| 4587 | LY_ARRAY_FREE(patterns); |
| 4588 | return rc; |
| 4589 | } |
| 4590 | |
| 4591 | rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err); |
| 4592 | pcre2_code_free((*pattern)->code); |
| 4593 | free(*pattern); |
| 4594 | LY_ARRAY_FREE(patterns); |
| 4595 | if (rc && (rc != LY_EVALID)) { |
| 4596 | ly_err_print(err); |
| 4597 | ly_err_free(err); |
| 4598 | return rc; |
| 4599 | } |
| 4600 | |
| 4601 | if (rc == LY_EVALID) { |
| 4602 | ly_err_free(err); |
| 4603 | set_fill_boolean(set, 0); |
| 4604 | } else { |
| 4605 | set_fill_boolean(set, 1); |
| 4606 | } |
| 4607 | |
| 4608 | return LY_SUCCESS; |
| 4609 | } |
| 4610 | |
| 4611 | /** |
| 4612 | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
| 4613 | * with the rounded first argument. For details refer to |
| 4614 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
| 4615 | * |
| 4616 | * @param[in] args Array of arguments. |
| 4617 | * @param[in] arg_count Count of elements in @p args. |
| 4618 | * @param[in,out] set Context and result set at the same time. |
| 4619 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4620 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4621 | */ |
| 4622 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4623 | 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] | 4624 | { |
| 4625 | struct lysc_node_leaf *sleaf; |
| 4626 | LY_ERR rc = LY_SUCCESS; |
| 4627 | |
| 4628 | if (options & LYXP_SCNODE_ALL) { |
| 4629 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4630 | 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] | 4631 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4632 | 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] | 4633 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4634 | 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] | 4635 | } |
| 4636 | set_scnode_clear_ctx(set); |
| 4637 | return rc; |
| 4638 | } |
| 4639 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4640 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4641 | LY_CHECK_RET(rc); |
| 4642 | |
| 4643 | /* cover only the cases where floor can't be used */ |
| 4644 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
| 4645 | set_fill_number(set, -0.0f); |
| 4646 | } else { |
| 4647 | args[0]->val.num += 0.5; |
| 4648 | rc = xpath_floor(args, 1, args[0], options); |
| 4649 | LY_CHECK_RET(rc); |
| 4650 | set_fill_number(set, args[0]->val.num); |
| 4651 | } |
| 4652 | |
| 4653 | return LY_SUCCESS; |
| 4654 | } |
| 4655 | |
| 4656 | /** |
| 4657 | * @brief Execute the XPath starts-with(string, string) function. |
| 4658 | * Returns LYXP_SET_BOOLEAN whether the second argument is |
| 4659 | * the prefix of the first or not. |
| 4660 | * |
| 4661 | * @param[in] args Array of arguments. |
| 4662 | * @param[in] arg_count Count of elements in @p args. |
| 4663 | * @param[in,out] set Context and result set at the same time. |
| 4664 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4665 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4666 | */ |
| 4667 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4668 | 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] | 4669 | { |
| 4670 | struct lysc_node_leaf *sleaf; |
| 4671 | LY_ERR rc = LY_SUCCESS; |
| 4672 | |
| 4673 | if (options & LYXP_SCNODE_ALL) { |
| 4674 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4675 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4676 | 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] | 4677 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4678 | 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] | 4679 | } |
| 4680 | } |
| 4681 | |
| 4682 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4683 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4684 | 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] | 4685 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4686 | 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] | 4687 | } |
| 4688 | } |
| 4689 | set_scnode_clear_ctx(set); |
| 4690 | return rc; |
| 4691 | } |
| 4692 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4693 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4694 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4695 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4696 | LY_CHECK_RET(rc); |
| 4697 | |
| 4698 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
| 4699 | set_fill_boolean(set, 0); |
| 4700 | } else { |
| 4701 | set_fill_boolean(set, 1); |
| 4702 | } |
| 4703 | |
| 4704 | return LY_SUCCESS; |
| 4705 | } |
| 4706 | |
| 4707 | /** |
| 4708 | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
| 4709 | * with the string representation of either the argument or the context. |
| 4710 | * |
| 4711 | * @param[in] args Array of arguments. |
| 4712 | * @param[in] arg_count Count of elements in @p args. |
| 4713 | * @param[in,out] set Context and result set at the same time. |
| 4714 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4715 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4716 | */ |
| 4717 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4718 | 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] | 4719 | { |
| 4720 | LY_ERR rc; |
| 4721 | |
| 4722 | if (options & LYXP_SCNODE_ALL) { |
| 4723 | set_scnode_clear_ctx(set); |
| 4724 | return LY_SUCCESS; |
| 4725 | } |
| 4726 | |
| 4727 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4728 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4729 | LY_CHECK_RET(rc); |
| 4730 | set_fill_set(set, args[0]); |
| 4731 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4732 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4733 | LY_CHECK_RET(rc); |
| 4734 | } |
| 4735 | |
| 4736 | return LY_SUCCESS; |
| 4737 | } |
| 4738 | |
| 4739 | /** |
| 4740 | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
| 4741 | * with the length of the string in either the argument or the context. |
| 4742 | * |
| 4743 | * @param[in] args Array of arguments. |
| 4744 | * @param[in] arg_count Count of elements in @p args. |
| 4745 | * @param[in,out] set Context and result set at the same time. |
| 4746 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4747 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4748 | */ |
| 4749 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4750 | 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] | 4751 | { |
| 4752 | struct lysc_node_leaf *sleaf; |
| 4753 | LY_ERR rc = LY_SUCCESS; |
| 4754 | |
| 4755 | if (options & LYXP_SCNODE_ALL) { |
| 4756 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4757 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4758 | 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] | 4759 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4760 | 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] | 4761 | } |
| 4762 | } |
| 4763 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
| 4764 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4765 | 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] | 4766 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4767 | 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] | 4768 | } |
| 4769 | } |
| 4770 | set_scnode_clear_ctx(set); |
| 4771 | return rc; |
| 4772 | } |
| 4773 | |
| 4774 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4775 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4776 | LY_CHECK_RET(rc); |
| 4777 | set_fill_number(set, strlen(args[0]->val.str)); |
| 4778 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4779 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4780 | LY_CHECK_RET(rc); |
| 4781 | set_fill_number(set, strlen(set->val.str)); |
| 4782 | } |
| 4783 | |
| 4784 | return LY_SUCCESS; |
| 4785 | } |
| 4786 | |
| 4787 | /** |
| 4788 | * @brief Execute the XPath substring(string, number, number?) function. |
| 4789 | * Returns LYXP_SET_STRING substring of the first argument starting |
| 4790 | * on the second argument index ending on the third argument index, |
| 4791 | * indexed from 1. For exact definition refer to |
| 4792 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
| 4793 | * |
| 4794 | * @param[in] args Array of arguments. |
| 4795 | * @param[in] arg_count Count of elements in @p args. |
| 4796 | * @param[in,out] set Context and result set at the same time. |
| 4797 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4798 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4799 | */ |
| 4800 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4801 | 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] | 4802 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4803 | int32_t start, len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4804 | uint16_t str_start, str_len, pos; |
| 4805 | struct lysc_node_leaf *sleaf; |
| 4806 | LY_ERR rc = LY_SUCCESS; |
| 4807 | |
| 4808 | if (options & LYXP_SCNODE_ALL) { |
| 4809 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4810 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4811 | 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] | 4812 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4813 | 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] | 4814 | } |
| 4815 | } |
| 4816 | |
| 4817 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4818 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4819 | 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] | 4820 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4821 | 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] | 4822 | } |
| 4823 | } |
| 4824 | |
| 4825 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 4826 | && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4827 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4828 | 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] | 4829 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4830 | 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] | 4831 | } |
| 4832 | } |
| 4833 | set_scnode_clear_ctx(set); |
| 4834 | return rc; |
| 4835 | } |
| 4836 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4837 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4838 | LY_CHECK_RET(rc); |
| 4839 | |
| 4840 | /* start */ |
| 4841 | if (xpath_round(&args[1], 1, args[1], options)) { |
| 4842 | return -1; |
| 4843 | } |
| 4844 | if (isfinite(args[1]->val.num)) { |
| 4845 | start = args[1]->val.num - 1; |
| 4846 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4847 | start = INT32_MIN; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4848 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4849 | start = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4850 | } |
| 4851 | |
| 4852 | /* len */ |
| 4853 | if (arg_count == 3) { |
| 4854 | rc = xpath_round(&args[2], 1, args[2], options); |
| 4855 | LY_CHECK_RET(rc); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4856 | if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4857 | len = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4858 | } else if (isfinite(args[2]->val.num)) { |
| 4859 | len = args[2]->val.num; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4860 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4861 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4862 | } |
| 4863 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4864 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4865 | } |
| 4866 | |
| 4867 | /* find matching character positions */ |
| 4868 | str_start = 0; |
| 4869 | str_len = 0; |
| 4870 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
| 4871 | if (pos < start) { |
| 4872 | ++str_start; |
| 4873 | } else if (pos < start + len) { |
| 4874 | ++str_len; |
| 4875 | } else { |
| 4876 | break; |
| 4877 | } |
| 4878 | } |
| 4879 | |
| 4880 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
| 4881 | return LY_SUCCESS; |
| 4882 | } |
| 4883 | |
| 4884 | /** |
| 4885 | * @brief Execute the XPath substring-after(string, string) function. |
| 4886 | * Returns LYXP_SET_STRING with the string succeeding the occurance |
| 4887 | * of the second argument in the first or an empty string. |
| 4888 | * |
| 4889 | * @param[in] args Array of arguments. |
| 4890 | * @param[in] arg_count Count of elements in @p args. |
| 4891 | * @param[in,out] set Context and result set at the same time. |
| 4892 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4893 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4894 | */ |
| 4895 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4896 | 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] | 4897 | { |
| 4898 | char *ptr; |
| 4899 | struct lysc_node_leaf *sleaf; |
| 4900 | LY_ERR rc = LY_SUCCESS; |
| 4901 | |
| 4902 | if (options & LYXP_SCNODE_ALL) { |
| 4903 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4904 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4905 | 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] | 4906 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4907 | 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] | 4908 | } |
| 4909 | } |
| 4910 | |
| 4911 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4912 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4913 | 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] | 4914 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4915 | 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] | 4916 | } |
| 4917 | } |
| 4918 | set_scnode_clear_ctx(set); |
| 4919 | return rc; |
| 4920 | } |
| 4921 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4922 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4923 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4924 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4925 | LY_CHECK_RET(rc); |
| 4926 | |
| 4927 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4928 | if (ptr) { |
| 4929 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
| 4930 | } else { |
| 4931 | set_fill_string(set, "", 0); |
| 4932 | } |
| 4933 | |
| 4934 | return LY_SUCCESS; |
| 4935 | } |
| 4936 | |
| 4937 | /** |
| 4938 | * @brief Execute the XPath substring-before(string, string) function. |
| 4939 | * Returns LYXP_SET_STRING with the string preceding the occurance |
| 4940 | * of the second argument in the first or an empty string. |
| 4941 | * |
| 4942 | * @param[in] args Array of arguments. |
| 4943 | * @param[in] arg_count Count of elements in @p args. |
| 4944 | * @param[in,out] set Context and result set at the same time. |
| 4945 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4946 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4947 | */ |
| 4948 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4949 | 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] | 4950 | { |
| 4951 | char *ptr; |
| 4952 | struct lysc_node_leaf *sleaf; |
| 4953 | LY_ERR rc = LY_SUCCESS; |
| 4954 | |
| 4955 | if (options & LYXP_SCNODE_ALL) { |
| 4956 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4957 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4958 | 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] | 4959 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4960 | 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] | 4961 | } |
| 4962 | } |
| 4963 | |
| 4964 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4965 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4966 | 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] | 4967 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4968 | 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] | 4969 | } |
| 4970 | } |
| 4971 | set_scnode_clear_ctx(set); |
| 4972 | return rc; |
| 4973 | } |
| 4974 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4975 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4976 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4977 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4978 | LY_CHECK_RET(rc); |
| 4979 | |
| 4980 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4981 | if (ptr) { |
| 4982 | set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str); |
| 4983 | } else { |
| 4984 | set_fill_string(set, "", 0); |
| 4985 | } |
| 4986 | |
| 4987 | return LY_SUCCESS; |
| 4988 | } |
| 4989 | |
| 4990 | /** |
| 4991 | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
| 4992 | * with the sum of all the nodes in the context. |
| 4993 | * |
| 4994 | * @param[in] args Array of arguments. |
| 4995 | * @param[in] arg_count Count of elements in @p args. |
| 4996 | * @param[in,out] set Context and result set at the same time. |
| 4997 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4998 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4999 | */ |
| 5000 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5001 | 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] | 5002 | { |
| 5003 | long double num; |
| 5004 | char *str; |
| 5005 | uint16_t i; |
| 5006 | struct lyxp_set set_item; |
| 5007 | struct lysc_node_leaf *sleaf; |
| 5008 | LY_ERR rc = LY_SUCCESS; |
| 5009 | |
| 5010 | if (options & LYXP_SCNODE_ALL) { |
| 5011 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
| 5012 | for (i = 0; i < args[0]->used; ++i) { |
| 5013 | if (args[0]->val.scnodes[i].in_ctx == 1) { |
| 5014 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
| 5015 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5016 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, |
| 5017 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5018 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5019 | 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] | 5020 | } |
| 5021 | } |
| 5022 | } |
| 5023 | } |
| 5024 | set_scnode_clear_ctx(set); |
| 5025 | return rc; |
| 5026 | } |
| 5027 | |
| 5028 | set_fill_number(set, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5029 | |
| 5030 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 5031 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)"); |
| 5032 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5033 | } else if (!args[0]->used) { |
| 5034 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5035 | } |
| 5036 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5037 | set_init(&set_item, set); |
| 5038 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5039 | set_item.type = LYXP_SET_NODE_SET; |
| 5040 | set_item.val.nodes = malloc(sizeof *set_item.val.nodes); |
| 5041 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
| 5042 | |
| 5043 | set_item.used = 1; |
| 5044 | set_item.size = 1; |
| 5045 | |
| 5046 | for (i = 0; i < args[0]->used; ++i) { |
| 5047 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
| 5048 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5049 | rc = cast_node_set_to_string(&set_item, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5050 | LY_CHECK_RET(rc); |
| 5051 | num = cast_string_to_number(str); |
| 5052 | free(str); |
| 5053 | set->val.num += num; |
| 5054 | } |
| 5055 | |
| 5056 | free(set_item.val.nodes); |
| 5057 | |
| 5058 | return LY_SUCCESS; |
| 5059 | } |
| 5060 | |
| 5061 | /** |
| 5062 | * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET |
| 5063 | * with the text content of the nodes in the context. |
| 5064 | * |
| 5065 | * @param[in] args Array of arguments. |
| 5066 | * @param[in] arg_count Count of elements in @p args. |
| 5067 | * @param[in,out] set Context and result set at the same time. |
| 5068 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5069 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5070 | */ |
| 5071 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5072 | 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] | 5073 | { |
| 5074 | uint32_t i; |
| 5075 | |
| 5076 | if (options & LYXP_SCNODE_ALL) { |
| 5077 | set_scnode_clear_ctx(set); |
| 5078 | return LY_SUCCESS; |
| 5079 | } |
| 5080 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5081 | if (set->type != LYXP_SET_NODE_SET) { |
| 5082 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()"); |
| 5083 | return LY_EVALID; |
| 5084 | } |
| 5085 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 5086 | for (i = 0; i < set->used; ) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5087 | switch (set->val.nodes[i].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 5088 | case LYXP_NODE_NONE: |
| 5089 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5090 | case LYXP_NODE_ELEM: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5091 | if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 5092 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
| 5093 | ++i; |
| 5094 | break; |
| 5095 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5096 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5097 | case LYXP_NODE_ROOT: |
| 5098 | case LYXP_NODE_ROOT_CONFIG: |
| 5099 | case LYXP_NODE_TEXT: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5100 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5101 | set_remove_node(set, i); |
| 5102 | break; |
| 5103 | } |
| 5104 | } |
| 5105 | |
| 5106 | return LY_SUCCESS; |
| 5107 | } |
| 5108 | |
| 5109 | /** |
| 5110 | * @brief Execute the XPath translate(string, string, string) function. |
| 5111 | * Returns LYXP_SET_STRING with the first argument with the characters |
| 5112 | * from the second argument replaced by those on the corresponding |
| 5113 | * positions in the third argument. |
| 5114 | * |
| 5115 | * @param[in] args Array of arguments. |
| 5116 | * @param[in] arg_count Count of elements in @p args. |
| 5117 | * @param[in,out] set Context and result set at the same time. |
| 5118 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5119 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5120 | */ |
| 5121 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5122 | 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] | 5123 | { |
| 5124 | uint16_t i, j, new_used; |
| 5125 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5126 | ly_bool have_removed; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5127 | struct lysc_node_leaf *sleaf; |
| 5128 | LY_ERR rc = LY_SUCCESS; |
| 5129 | |
| 5130 | if (options & LYXP_SCNODE_ALL) { |
| 5131 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5132 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5133 | 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] | 5134 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5135 | 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] | 5136 | } |
| 5137 | } |
| 5138 | |
| 5139 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5140 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5141 | 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] | 5142 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5143 | 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] | 5144 | } |
| 5145 | } |
| 5146 | |
| 5147 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 5148 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5149 | 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] | 5150 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5151 | 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] | 5152 | } |
| 5153 | } |
| 5154 | set_scnode_clear_ctx(set); |
| 5155 | return rc; |
| 5156 | } |
| 5157 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5158 | rc = lyxp_set_cast(args[0], 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[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5161 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5162 | rc = lyxp_set_cast(args[2], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5163 | LY_CHECK_RET(rc); |
| 5164 | |
| 5165 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
| 5166 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5167 | new_used = 0; |
| 5168 | |
| 5169 | have_removed = 0; |
| 5170 | for (i = 0; args[0]->val.str[i]; ++i) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5171 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5172 | |
| 5173 | for (j = 0; args[1]->val.str[j]; ++j) { |
| 5174 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
| 5175 | /* removing this char */ |
| 5176 | if (j >= strlen(args[2]->val.str)) { |
| 5177 | have_removed = 1; |
| 5178 | found = 1; |
| 5179 | break; |
| 5180 | } |
| 5181 | /* replacing this char */ |
| 5182 | new[new_used] = args[2]->val.str[j]; |
| 5183 | ++new_used; |
| 5184 | found = 1; |
| 5185 | break; |
| 5186 | } |
| 5187 | } |
| 5188 | |
| 5189 | /* copying this char */ |
| 5190 | if (!found) { |
| 5191 | new[new_used] = args[0]->val.str[i]; |
| 5192 | ++new_used; |
| 5193 | } |
| 5194 | } |
| 5195 | |
| 5196 | if (have_removed) { |
| 5197 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 5198 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5199 | } |
| 5200 | new[new_used] = '\0'; |
| 5201 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5202 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5203 | set->type = LYXP_SET_STRING; |
| 5204 | set->val.str = new; |
| 5205 | |
| 5206 | return LY_SUCCESS; |
| 5207 | } |
| 5208 | |
| 5209 | /** |
| 5210 | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
| 5211 | * with true value. |
| 5212 | * |
| 5213 | * @param[in] args Array of arguments. |
| 5214 | * @param[in] arg_count Count of elements in @p args. |
| 5215 | * @param[in,out] set Context and result set at the same time. |
| 5216 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5217 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5218 | */ |
| 5219 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5220 | 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] | 5221 | { |
| 5222 | if (options & LYXP_SCNODE_ALL) { |
| 5223 | set_scnode_clear_ctx(set); |
| 5224 | return LY_SUCCESS; |
| 5225 | } |
| 5226 | |
| 5227 | set_fill_boolean(set, 1); |
| 5228 | return LY_SUCCESS; |
| 5229 | } |
| 5230 | |
| 5231 | /* |
| 5232 | * moveto functions |
| 5233 | * |
| 5234 | * They and only they actually change the context (set). |
| 5235 | */ |
| 5236 | |
| 5237 | /** |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5238 | * @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] | 5239 | * |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5240 | * XPath @p set is expected to be a (sc)node set! |
| 5241 | * |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5242 | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
| 5243 | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
| 5244 | * @param[in] set Set with XPath context. |
| 5245 | * @param[out] moveto_mod Expected module of a matching node. |
| 5246 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5247 | */ |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5248 | static LY_ERR |
| 5249 | 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] | 5250 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 5251 | const struct lys_module *mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5252 | const char *ptr; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5253 | size_t pref_len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5254 | |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5255 | assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET)); |
| 5256 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5257 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
| 5258 | /* specific module */ |
| 5259 | pref_len = ptr - *qname; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 5260 | 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] | 5261 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5262 | /* check for errors and non-implemented modules, as they are not valid */ |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5263 | if (!mod || !mod->implemented) { |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5264 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 5265 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INMOD, pref_len, *qname); |
| 5266 | } else { |
| 5267 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INMOD, pref_len, *qname); |
| 5268 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5269 | return LY_EVALID; |
| 5270 | } |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5271 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5272 | *qname += pref_len + 1; |
| 5273 | *qname_len -= pref_len + 1; |
| 5274 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
| 5275 | /* all modules - special case */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5276 | mod = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5277 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 5278 | /* current node module */ |
| 5279 | mod = set->ctx_scnode->module; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5280 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5281 | /* current node module */ |
| 5282 | mod = set->ctx_node->schema->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5283 | } |
| 5284 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5285 | *moveto_mod = mod; |
| 5286 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5287 | } |
| 5288 | |
| 5289 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5290 | * @brief Move context @p set to the root. Handles absolute path. |
| 5291 | * Result is LYXP_SET_NODE_SET. |
| 5292 | * |
| 5293 | * @param[in,out] set Set to use. |
| 5294 | * @param[in] options Xpath options. |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5295 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5296 | */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5297 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5298 | moveto_root(struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5299 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5300 | if (!set) { |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5301 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5302 | } |
| 5303 | |
| 5304 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5305 | set_scnode_clear_ctx(set); |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5306 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5307 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5308 | set->type = LYXP_SET_NODE_SET; |
| 5309 | set->used = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5310 | set_insert_node(set, NULL, 0, set->root_type, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5311 | } |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5312 | |
| 5313 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5314 | } |
| 5315 | |
| 5316 | /** |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5317 | * @brief Check whether a node has some unresolved "when". |
| 5318 | * |
| 5319 | * @param[in] node Node to check. |
| 5320 | * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when") |
| 5321 | */ |
| 5322 | static LY_ERR |
| 5323 | moveto_when_check(const struct lyd_node *node) |
| 5324 | { |
| 5325 | const struct lysc_node *schema; |
| 5326 | |
| 5327 | if (!node) { |
| 5328 | return LY_SUCCESS; |
| 5329 | } |
| 5330 | |
| 5331 | schema = node->schema; |
| 5332 | do { |
| 5333 | if (schema->when && !(node->flags & LYD_WHEN_TRUE)) { |
| 5334 | return LY_EINCOMPLETE; |
| 5335 | } |
| 5336 | schema = schema->parent; |
| 5337 | } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 5338 | |
| 5339 | return LY_SUCCESS; |
| 5340 | } |
| 5341 | |
| 5342 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5343 | * @brief Check @p node as a part of NameTest processing. |
| 5344 | * |
| 5345 | * @param[in] node Node to check. |
| 5346 | * @param[in] root_type XPath root node type. |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5347 | * @param[in] context_op XPath operation parent. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5348 | * @param[in] node_name Node name in the dictionary to move to, NULL for any node. |
| 5349 | * @param[in] moveto_mod Expected module of the node, NULL for any. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5350 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when, |
| 5351 | * LY_EINVAL if netither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5352 | */ |
| 5353 | static LY_ERR |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5354 | 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] | 5355 | const char *node_name, const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5356 | { |
| 5357 | /* module check */ |
| 5358 | if (moveto_mod && (node->schema->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5359 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5360 | } |
| 5361 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5362 | /* context check */ |
| 5363 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5364 | return LY_EINVAL; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5365 | } else if (context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node->schema != context_op)) { |
| 5366 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5367 | } |
| 5368 | |
| 5369 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5370 | if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5371 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5372 | } |
| 5373 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5374 | /* when check */ |
| 5375 | if (moveto_when_check(node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5376 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5377 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5378 | |
| 5379 | /* match */ |
| 5380 | return LY_SUCCESS; |
| 5381 | } |
| 5382 | |
| 5383 | /** |
| 5384 | * @brief Check @p node as a part of schema NameTest processing. |
| 5385 | * |
| 5386 | * @param[in] node Schema node to check. |
| 5387 | * @param[in] root_type XPath root node type. |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5388 | * @param[in] context_op XPath operation parent. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5389 | * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes. |
| 5390 | * @param[in] moveto_mod Expected module of the node, NULL for any. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5391 | * @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] | 5392 | */ |
| 5393 | static LY_ERR |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5394 | 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] | 5395 | const char *node_name, const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5396 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5397 | /* module check */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5398 | if (moveto_mod && (node->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5399 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5400 | } |
| 5401 | |
| 5402 | /* context check */ |
| 5403 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) { |
| 5404 | return LY_EINVAL; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5405 | } else if (context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != context_op)) { |
| 5406 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5407 | } |
| 5408 | |
| 5409 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5410 | if (node_name && strcmp(node_name, "*") && (node->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5411 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5412 | } |
| 5413 | |
| 5414 | /* match */ |
| 5415 | return LY_SUCCESS; |
| 5416 | } |
| 5417 | |
| 5418 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5419 | * @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] | 5420 | * |
| 5421 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5422 | * @param[in] mod Matching node module, NULL for any. |
| 5423 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5424 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5425 | */ |
| 5426 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5427 | 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] | 5428 | { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 5429 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5430 | const struct lyd_node *siblings, *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5431 | LY_ERR rc; |
| 5432 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5433 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5434 | return LY_SUCCESS; |
| 5435 | } |
| 5436 | |
| 5437 | if (set->type != LYXP_SET_NODE_SET) { |
| 5438 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5439 | return LY_EVALID; |
| 5440 | } |
| 5441 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5442 | for (i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5443 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5444 | |
| 5445 | 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] | 5446 | assert(!set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5447 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5448 | /* search in all the trees */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5449 | siblings = set->tree; |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5450 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5451 | /* search in children */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5452 | siblings = lyd_node_children(set->val.nodes[i].node, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5453 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5454 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5455 | for (sub = siblings; sub; sub = sub->next) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5456 | 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] | 5457 | if (rc == LY_SUCCESS) { |
| 5458 | if (!replaced) { |
| 5459 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
| 5460 | replaced = 1; |
| 5461 | } else { |
| 5462 | set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5463 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5464 | ++i; |
| 5465 | } else if (rc == LY_EINCOMPLETE) { |
| 5466 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5467 | } |
| 5468 | } |
| 5469 | |
| 5470 | if (!replaced) { |
| 5471 | /* no match */ |
| 5472 | set_remove_node(set, i); |
| 5473 | } |
| 5474 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5475 | |
| 5476 | return LY_SUCCESS; |
| 5477 | } |
| 5478 | |
| 5479 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5480 | * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). |
| 5481 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5482 | * |
| 5483 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5484 | * @param[in] scnode Matching node schema. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5485 | * @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] | 5486 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5487 | */ |
| 5488 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5489 | 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] | 5490 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5491 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5492 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5493 | const struct lyd_node *siblings; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5494 | struct lyd_node *sub, *inst = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5495 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5496 | assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5497 | |
| 5498 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5499 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5500 | } |
| 5501 | |
| 5502 | if (set->type != LYXP_SET_NODE_SET) { |
| 5503 | 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] | 5504 | ret = LY_EVALID; |
| 5505 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5506 | } |
| 5507 | |
| 5508 | /* context check for all the nodes since we have the schema node */ |
| 5509 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 5510 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5511 | goto cleanup; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5512 | } 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] | 5513 | && (scnode != set->context_op)) { |
| 5514 | lyxp_set_free_content(set); |
| 5515 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5516 | } |
| 5517 | |
| 5518 | /* create specific data instance if needed */ |
| 5519 | if (scnode->nodetype == LYS_LIST) { |
| 5520 | LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup); |
| 5521 | } else if (scnode->nodetype == LYS_LEAFLIST) { |
| 5522 | 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] | 5523 | } |
| 5524 | |
| 5525 | for (i = 0; i < set->used; ) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5526 | siblings = NULL; |
| 5527 | |
| 5528 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
| 5529 | assert(!set->val.nodes[i].node); |
| 5530 | |
| 5531 | /* search in all the trees */ |
| 5532 | siblings = set->tree; |
| 5533 | } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 5534 | /* search in children */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5535 | siblings = lyd_node_children(set->val.nodes[i].node, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5536 | } |
| 5537 | |
| 5538 | /* find the node using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5539 | if (inst) { |
| 5540 | lyd_find_sibling_first(siblings, inst, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5541 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5542 | lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5543 | } |
| 5544 | |
| 5545 | /* when check */ |
| 5546 | if (sub && moveto_when_check(sub)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5547 | ret = LY_EINCOMPLETE; |
| 5548 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5549 | } |
| 5550 | |
| 5551 | if (sub) { |
| 5552 | /* pos filled later */ |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5553 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5554 | ++i; |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5555 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5556 | /* no match */ |
| 5557 | set_remove_node(set, i); |
| 5558 | } |
| 5559 | } |
| 5560 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5561 | cleanup: |
| 5562 | lyd_free_tree(inst); |
| 5563 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5564 | } |
| 5565 | |
| 5566 | /** |
| 5567 | * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
| 5568 | * |
| 5569 | * @param[in,out] set Set to use. |
| 5570 | * @param[in] mod Matching node module, NULL for any. |
| 5571 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5572 | * @param[in] options XPath options. |
| 5573 | * @return LY_ERR |
| 5574 | */ |
| 5575 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5576 | 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] | 5577 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5578 | ly_bool temp_ctx = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5579 | uint32_t getnext_opts; |
| 5580 | uint32_t orig_used, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5581 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5582 | const struct lysc_node *iter, *start_parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5583 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5584 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5585 | return LY_SUCCESS; |
| 5586 | } |
| 5587 | |
| 5588 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 5589 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5590 | return LY_EVALID; |
| 5591 | } |
| 5592 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5593 | /* getnext opts */ |
| 5594 | getnext_opts = LYS_GETNEXT_NOSTATECHECK; |
| 5595 | if (options & LYXP_SCNODE_OUTPUT) { |
| 5596 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 5597 | } |
| 5598 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5599 | orig_used = set->used; |
| 5600 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5601 | uint32_t idx; |
| 5602 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5603 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5604 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5605 | continue; |
| 5606 | } |
| 5607 | |
| 5608 | /* remember context node */ |
| 5609 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5610 | } else { |
| 5611 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5612 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5613 | |
| 5614 | start_parent = set->val.scnodes[i].scnode; |
| 5615 | |
| 5616 | 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] | 5617 | /* 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] | 5618 | * so use it directly (root node itself is useless in this case) */ |
| 5619 | mod_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5620 | 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] | 5621 | iter = NULL; |
Michal Vasko | 509de4d | 2019-12-10 14:51:30 +0100 | [diff] [blame] | 5622 | /* module may not be implemented */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5623 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5624 | 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] | 5625 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx)); |
| 5626 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5627 | /* we need to prevent these nodes from being considered in this moveto */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5628 | if ((idx < orig_used) && (idx > i)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5629 | set->val.scnodes[idx].in_ctx = 2; |
| 5630 | temp_ctx = 1; |
| 5631 | } |
| 5632 | } |
| 5633 | } |
| 5634 | |
| 5635 | if (!mod_idx) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5636 | /* mod was specified, we are not going through the whole context */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5637 | break; |
| 5638 | } |
| 5639 | /* next iteration */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5640 | mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5641 | } |
| 5642 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5643 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 5644 | iter = NULL; |
| 5645 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5646 | 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] | 5647 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx)); |
| 5648 | |
| 5649 | if ((idx < orig_used) && (idx > i)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5650 | set->val.scnodes[idx].in_ctx = 2; |
| 5651 | temp_ctx = 1; |
| 5652 | } |
| 5653 | } |
| 5654 | } |
| 5655 | } |
| 5656 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5657 | |
| 5658 | /* correct temporary in_ctx values */ |
| 5659 | if (temp_ctx) { |
| 5660 | for (i = 0; i < orig_used; ++i) { |
| 5661 | if (set->val.scnodes[i].in_ctx == 2) { |
| 5662 | set->val.scnodes[i].in_ctx = 1; |
| 5663 | } |
| 5664 | } |
| 5665 | } |
| 5666 | |
| 5667 | return LY_SUCCESS; |
| 5668 | } |
| 5669 | |
| 5670 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5671 | * @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] | 5672 | * Context position aware. |
| 5673 | * |
| 5674 | * @param[in] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5675 | * @param[in] mod Matching node module, NULL for any. |
| 5676 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5677 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5678 | */ |
| 5679 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5680 | 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] | 5681 | { |
| 5682 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5683 | const struct lyd_node *next, *elem, *start; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5684 | struct lyxp_set ret_set; |
| 5685 | LY_ERR rc; |
| 5686 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5687 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5688 | return LY_SUCCESS; |
| 5689 | } |
| 5690 | |
| 5691 | if (set->type != LYXP_SET_NODE_SET) { |
| 5692 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5693 | return LY_EVALID; |
| 5694 | } |
| 5695 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5696 | /* 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] | 5697 | rc = moveto_node(set, NULL, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5698 | LY_CHECK_RET(rc); |
| 5699 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5700 | /* 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] | 5701 | set_init(&ret_set, set); |
| 5702 | for (i = 0; i < set->used; ++i) { |
| 5703 | |
| 5704 | /* TREE DFS */ |
| 5705 | start = set->val.nodes[i].node; |
| 5706 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5707 | 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] | 5708 | if (!rc) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5709 | /* add matching node into result set */ |
| 5710 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
| 5711 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
| 5712 | /* the node is a duplicate, we'll process it later in the set */ |
| 5713 | goto skip_children; |
| 5714 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5715 | } else if (rc == LY_EINCOMPLETE) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5716 | return rc; |
| 5717 | } else if (rc == LY_EINVAL) { |
| 5718 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5719 | } |
| 5720 | |
| 5721 | /* TREE DFS NEXT ELEM */ |
| 5722 | /* select element for the next run - children first */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5723 | next = lyd_node_children(elem, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5724 | if (!next) { |
| 5725 | skip_children: |
| 5726 | /* no children, so try siblings, but only if it's not the start, |
| 5727 | * that is considered to be the root and it's siblings are not traversed */ |
| 5728 | if (elem != start) { |
| 5729 | next = elem->next; |
| 5730 | } else { |
| 5731 | break; |
| 5732 | } |
| 5733 | } |
| 5734 | while (!next) { |
| 5735 | /* no siblings, go back through the parents */ |
| 5736 | if ((struct lyd_node *)elem->parent == start) { |
| 5737 | /* we are done, no next element to process */ |
| 5738 | break; |
| 5739 | } |
| 5740 | /* parent is already processed, go to its sibling */ |
| 5741 | elem = (struct lyd_node *)elem->parent; |
| 5742 | next = elem->next; |
| 5743 | } |
| 5744 | } |
| 5745 | } |
| 5746 | |
| 5747 | /* make the temporary set the current one */ |
| 5748 | ret_set.ctx_pos = set->ctx_pos; |
| 5749 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5750 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5751 | memcpy(set, &ret_set, sizeof *set); |
| 5752 | |
| 5753 | return LY_SUCCESS; |
| 5754 | } |
| 5755 | |
| 5756 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5757 | * @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] | 5758 | * |
| 5759 | * @param[in] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5760 | * @param[in] mod Matching node module, NULL for any. |
| 5761 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5762 | * @param[in] options XPath options. |
| 5763 | * @return LY_ERR |
| 5764 | */ |
| 5765 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5766 | 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] | 5767 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5768 | uint32_t i, orig_used; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5769 | const struct lysc_node *next, *elem, *start; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5770 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5771 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5772 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5773 | return LY_SUCCESS; |
| 5774 | } |
| 5775 | |
| 5776 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 5777 | 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] | 5778 | return LY_EVALID; |
| 5779 | } |
| 5780 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5781 | orig_used = set->used; |
| 5782 | for (i = 0; i < orig_used; ++i) { |
| 5783 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5784 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5785 | continue; |
| 5786 | } |
| 5787 | |
| 5788 | /* remember context node */ |
| 5789 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5790 | } else { |
| 5791 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5792 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5793 | |
| 5794 | /* TREE DFS */ |
| 5795 | start = set->val.scnodes[i].scnode; |
| 5796 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5797 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 5798 | /* schema-only nodes, skip root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5799 | goto next_iter; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5800 | } |
| 5801 | |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5802 | 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] | 5803 | if (!rc) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5804 | uint32_t idx; |
| 5805 | |
| 5806 | if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5807 | set->val.scnodes[idx].in_ctx = 1; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5808 | if ((uint32_t)idx > i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5809 | /* we will process it later in the set */ |
| 5810 | goto skip_children; |
| 5811 | } |
| 5812 | } else { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5813 | 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] | 5814 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5815 | } else if (rc == LY_EINVAL) { |
| 5816 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5817 | } |
| 5818 | |
| 5819 | next_iter: |
| 5820 | /* TREE DFS NEXT ELEM */ |
| 5821 | /* select element for the next run - children first */ |
| 5822 | 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] | 5823 | if (!next) { |
| 5824 | skip_children: |
| 5825 | /* no children, so try siblings, but only if it's not the start, |
| 5826 | * that is considered to be the root and it's siblings are not traversed */ |
| 5827 | if (elem != start) { |
| 5828 | next = elem->next; |
| 5829 | } else { |
| 5830 | break; |
| 5831 | } |
| 5832 | } |
| 5833 | while (!next) { |
| 5834 | /* no siblings, go back through the parents */ |
| 5835 | if (elem->parent == start) { |
| 5836 | /* we are done, no next element to process */ |
| 5837 | break; |
| 5838 | } |
| 5839 | /* parent is already processed, go to its sibling */ |
| 5840 | elem = elem->parent; |
| 5841 | next = elem->next; |
| 5842 | } |
| 5843 | } |
| 5844 | } |
| 5845 | |
| 5846 | return LY_SUCCESS; |
| 5847 | } |
| 5848 | |
| 5849 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5850 | * @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] | 5851 | * Indirectly context position aware. |
| 5852 | * |
| 5853 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5854 | * @param[in] mod Matching metadata module, NULL for any. |
| 5855 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5856 | * @return LY_ERR |
| 5857 | */ |
| 5858 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5859 | 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] | 5860 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5861 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5862 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5863 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5864 | return LY_SUCCESS; |
| 5865 | } |
| 5866 | |
| 5867 | if (set->type != LYXP_SET_NODE_SET) { |
| 5868 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5869 | return LY_EVALID; |
| 5870 | } |
| 5871 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5872 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5873 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5874 | |
| 5875 | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
| 5876 | * our attributes are always qualified */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5877 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5878 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5879 | |
| 5880 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5881 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5882 | continue; |
| 5883 | } |
| 5884 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5885 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5886 | /* match */ |
| 5887 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5888 | set->val.meta[i].meta = sub; |
| 5889 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5890 | /* pos does not change */ |
| 5891 | replaced = 1; |
| 5892 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5893 | 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] | 5894 | } |
| 5895 | ++i; |
| 5896 | } |
| 5897 | } |
| 5898 | } |
| 5899 | |
| 5900 | if (!replaced) { |
| 5901 | /* no match */ |
| 5902 | set_remove_node(set, i); |
| 5903 | } |
| 5904 | } |
| 5905 | |
| 5906 | return LY_SUCCESS; |
| 5907 | } |
| 5908 | |
| 5909 | /** |
| 5910 | * @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] | 5911 | * Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5912 | * |
| 5913 | * @param[in,out] set1 Set to use for the result. |
| 5914 | * @param[in] set2 Set that is copied to @p set1. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5915 | * @return LY_ERR |
| 5916 | */ |
| 5917 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5918 | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5919 | { |
| 5920 | LY_ERR rc; |
| 5921 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5922 | 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] | 5923 | LOGVAL(set1->ctx, LY_VLOG_LYD, set1->ctx_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2)); |
| 5924 | return LY_EVALID; |
| 5925 | } |
| 5926 | |
| 5927 | /* set2 is empty or both set1 and set2 */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5928 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5929 | return LY_SUCCESS; |
| 5930 | } |
| 5931 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5932 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5933 | memcpy(set1, set2, sizeof *set1); |
| 5934 | /* dynamic memory belongs to set1 now, do not free */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5935 | memset(set2, 0, sizeof *set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5936 | return LY_SUCCESS; |
| 5937 | } |
| 5938 | |
| 5939 | /* we assume sets are sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5940 | assert(!set_sort(set1) && !set_sort(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5941 | |
| 5942 | /* sort, remove duplicates */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5943 | rc = set_sorted_merge(set1, set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5944 | LY_CHECK_RET(rc); |
| 5945 | |
| 5946 | /* final set must be sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5947 | assert(!set_sort(set1)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5948 | |
| 5949 | return LY_SUCCESS; |
| 5950 | } |
| 5951 | |
| 5952 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5953 | * @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] | 5954 | * Context position aware. |
| 5955 | * |
| 5956 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5957 | * @param[in] mod Matching metadata module, NULL for any. |
| 5958 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5959 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5960 | */ |
| 5961 | static int |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5962 | 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] | 5963 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5964 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5965 | struct lyxp_set *set_all_desc = NULL; |
| 5966 | LY_ERR rc; |
| 5967 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5968 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5969 | return LY_SUCCESS; |
| 5970 | } |
| 5971 | |
| 5972 | if (set->type != LYXP_SET_NODE_SET) { |
| 5973 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5974 | return LY_EVALID; |
| 5975 | } |
| 5976 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5977 | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
| 5978 | * but it likely won't be used much, so it's a waste of time */ |
| 5979 | /* copy the context */ |
| 5980 | set_all_desc = set_copy(set); |
| 5981 | /* get all descendant nodes (the original context nodes are removed) */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5982 | rc = moveto_node_alldesc(set_all_desc, NULL, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5983 | if (rc != LY_SUCCESS) { |
| 5984 | lyxp_set_free(set_all_desc); |
| 5985 | return rc; |
| 5986 | } |
| 5987 | /* prepend the original context nodes */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5988 | rc = moveto_union(set, set_all_desc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5989 | if (rc != LY_SUCCESS) { |
| 5990 | lyxp_set_free(set_all_desc); |
| 5991 | return rc; |
| 5992 | } |
| 5993 | lyxp_set_free(set_all_desc); |
| 5994 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5995 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5996 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5997 | |
| 5998 | /* only attributes of an elem can be in the result, skip all the rest, |
| 5999 | * we have all attributes qualified in lyd tree */ |
| 6000 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6001 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6002 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6003 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6004 | continue; |
| 6005 | } |
| 6006 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6007 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6008 | /* match */ |
| 6009 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6010 | set->val.meta[i].meta = sub; |
| 6011 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6012 | /* pos does not change */ |
| 6013 | replaced = 1; |
| 6014 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6015 | 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] | 6016 | } |
| 6017 | ++i; |
| 6018 | } |
| 6019 | } |
| 6020 | } |
| 6021 | |
| 6022 | if (!replaced) { |
| 6023 | /* no match */ |
| 6024 | set_remove_node(set, i); |
| 6025 | } |
| 6026 | } |
| 6027 | |
| 6028 | return LY_SUCCESS; |
| 6029 | } |
| 6030 | |
| 6031 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6032 | * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET. |
| 6033 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6034 | * |
| 6035 | * @param[in] parent Current parent. |
| 6036 | * @param[in] parent_pos Position of @p parent. |
| 6037 | * @param[in] parent_type Node type of @p parent. |
| 6038 | * @param[in,out] to_set Set to use. |
| 6039 | * @param[in] dup_check_set Set for checking duplicities. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6040 | * @param[in] options XPath options. |
| 6041 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6042 | */ |
| 6043 | static LY_ERR |
| 6044 | 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] | 6045 | 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] | 6046 | { |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6047 | const struct lyd_node *iter, *first; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6048 | LY_ERR rc; |
| 6049 | |
| 6050 | switch (parent_type) { |
| 6051 | case LYXP_NODE_ROOT: |
| 6052 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6053 | assert(!parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6054 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6055 | /* add all top-level nodes as elements */ |
| 6056 | first = to_set->tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6057 | break; |
| 6058 | case LYXP_NODE_ELEM: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6059 | /* add just the text node of this term element node */ |
| 6060 | if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6061 | if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) { |
| 6062 | set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used); |
| 6063 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6064 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6065 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6066 | |
| 6067 | /* add all the children of this node */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 6068 | first = lyd_node_children(parent, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6069 | break; |
| 6070 | default: |
| 6071 | LOGINT_RET(parent->schema->module->ctx); |
| 6072 | } |
| 6073 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6074 | /* add all top-level nodes as elements */ |
| 6075 | LY_LIST_FOR(first, iter) { |
| 6076 | /* context check */ |
| 6077 | if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) { |
| 6078 | continue; |
| 6079 | } |
| 6080 | |
| 6081 | /* when check */ |
| 6082 | if (moveto_when_check(iter)) { |
| 6083 | return LY_EINCOMPLETE; |
| 6084 | } |
| 6085 | |
| 6086 | if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) { |
| 6087 | set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used); |
| 6088 | |
| 6089 | /* also add all the children of this node, recursively */ |
| 6090 | rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options); |
| 6091 | LY_CHECK_RET(rc); |
| 6092 | } |
| 6093 | } |
| 6094 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6095 | return LY_SUCCESS; |
| 6096 | } |
| 6097 | |
| 6098 | /** |
| 6099 | * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET |
| 6100 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6101 | * |
| 6102 | * @param[in,out] set Set to use. |
| 6103 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6104 | * @param[in] options XPath options. |
| 6105 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6106 | */ |
| 6107 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6108 | moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6109 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6110 | struct lyxp_set ret_set; |
| 6111 | LY_ERR rc; |
| 6112 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6113 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6114 | return LY_SUCCESS; |
| 6115 | } |
| 6116 | |
| 6117 | if (set->type != LYXP_SET_NODE_SET) { |
| 6118 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 6119 | return LY_EVALID; |
| 6120 | } |
| 6121 | |
| 6122 | /* nothing to do */ |
| 6123 | if (!all_desc) { |
| 6124 | return LY_SUCCESS; |
| 6125 | } |
| 6126 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6127 | /* add all the children, they get added recursively */ |
| 6128 | set_init(&ret_set, set); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6129 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6130 | /* copy the current node to tmp */ |
| 6131 | set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used); |
| 6132 | |
| 6133 | /* do not touch attributes and text nodes */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6134 | 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] | 6135 | continue; |
| 6136 | } |
| 6137 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6138 | /* add all the children */ |
| 6139 | 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] | 6140 | set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6141 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6142 | lyxp_set_free_content(&ret_set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6143 | return rc; |
| 6144 | } |
| 6145 | } |
| 6146 | |
| 6147 | /* use the temporary set as the current one */ |
| 6148 | ret_set.ctx_pos = set->ctx_pos; |
| 6149 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6150 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6151 | memcpy(set, &ret_set, sizeof *set); |
| 6152 | |
| 6153 | return LY_SUCCESS; |
| 6154 | } |
| 6155 | |
| 6156 | /** |
| 6157 | * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET |
| 6158 | * (or LYXP_SET_EMPTY). |
| 6159 | * |
| 6160 | * @param[in,out] set Set to use. |
| 6161 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6162 | * @param[in] options XPath options. |
| 6163 | * @return LY_ERR |
| 6164 | */ |
| 6165 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6166 | moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6167 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6168 | uint32_t getnext_opts; |
| 6169 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6170 | const struct lysc_node *iter, *start_parent; |
| 6171 | const struct lys_module *mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6172 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6173 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6174 | return LY_SUCCESS; |
| 6175 | } |
| 6176 | |
| 6177 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6178 | 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] | 6179 | return LY_EVALID; |
| 6180 | } |
| 6181 | |
| 6182 | /* nothing to do */ |
| 6183 | if (!all_desc) { |
| 6184 | return LY_SUCCESS; |
| 6185 | } |
| 6186 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6187 | /* getnext opts */ |
| 6188 | getnext_opts = LYS_GETNEXT_NOSTATECHECK; |
| 6189 | if (options & LYXP_SCNODE_OUTPUT) { |
| 6190 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 6191 | } |
| 6192 | |
| 6193 | /* 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] | 6194 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6195 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6196 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6197 | continue; |
| 6198 | } |
| 6199 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6200 | /* remember context node */ |
| 6201 | set->val.scnodes[i].in_ctx = -1; |
| 6202 | } else { |
| 6203 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6204 | } |
| 6205 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6206 | start_parent = set->val.scnodes[i].scnode; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6207 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6208 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
| 6209 | /* it can actually be in any module, it's all <running> */ |
| 6210 | mod_idx = 0; |
| 6211 | while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
| 6212 | iter = NULL; |
| 6213 | /* module may not be implemented */ |
| 6214 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
| 6215 | /* context check */ |
| 6216 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) { |
| 6217 | continue; |
| 6218 | } |
| 6219 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6220 | 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] | 6221 | /* throw away the insert index, we want to consider that node again, recursively */ |
| 6222 | } |
| 6223 | } |
| 6224 | |
| 6225 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 6226 | iter = NULL; |
| 6227 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6228 | /* context check */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6229 | 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] | 6230 | continue; |
| 6231 | } |
| 6232 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6233 | 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] | 6234 | } |
| 6235 | } |
| 6236 | } |
| 6237 | |
| 6238 | return LY_SUCCESS; |
| 6239 | } |
| 6240 | |
| 6241 | /** |
| 6242 | * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET |
| 6243 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6244 | * |
| 6245 | * @param[in] set Set to use. |
| 6246 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6247 | * @param[in] options XPath options. |
| 6248 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6249 | */ |
| 6250 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6251 | moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6252 | { |
| 6253 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6254 | struct lyd_node *node, *new_node; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6255 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6256 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6257 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6258 | return LY_SUCCESS; |
| 6259 | } |
| 6260 | |
| 6261 | if (set->type != LYXP_SET_NODE_SET) { |
| 6262 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 6263 | return LY_EVALID; |
| 6264 | } |
| 6265 | |
| 6266 | if (all_desc) { |
| 6267 | /* <path>//.. == <path>//./.. */ |
| 6268 | rc = moveto_self(set, 1, options); |
| 6269 | LY_CHECK_RET(rc); |
| 6270 | } |
| 6271 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6272 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6273 | node = set->val.nodes[i].node; |
| 6274 | |
| 6275 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 6276 | new_node = (struct lyd_node *)node->parent; |
| 6277 | } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) { |
| 6278 | new_node = node; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6279 | } else if (set->val.nodes[i].type == LYXP_NODE_META) { |
| 6280 | new_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6281 | if (!new_node) { |
| 6282 | LOGINT_RET(set->ctx); |
| 6283 | } |
| 6284 | } else { |
| 6285 | /* root does not have a parent */ |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6286 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6287 | continue; |
| 6288 | } |
| 6289 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6290 | /* when check */ |
| 6291 | if (moveto_when_check(new_node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6292 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6293 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6294 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6295 | if (!new_node) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6296 | /* node already there can also be the root */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6297 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6298 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6299 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6300 | /* 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] | 6301 | new_type = LYXP_NODE_ELEM; |
| 6302 | } |
| 6303 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6304 | if (set_dup_node_check(set, new_node, new_type, -1)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6305 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6306 | } else { |
| 6307 | set_replace_node(set, new_node, 0, new_type, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6308 | } |
| 6309 | } |
| 6310 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6311 | set_remove_nodes_none(set); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6312 | assert(!set_sort(set) && !set_sorted_dup_node_clean(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6313 | |
| 6314 | return LY_SUCCESS; |
| 6315 | } |
| 6316 | |
| 6317 | /** |
| 6318 | * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET |
| 6319 | * (or LYXP_SET_EMPTY). |
| 6320 | * |
| 6321 | * @param[in] set Set to use. |
| 6322 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6323 | * @param[in] options XPath options. |
| 6324 | * @return LY_ERR |
| 6325 | */ |
| 6326 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6327 | moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6328 | { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6329 | uint32_t i, orig_used, idx; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6330 | ly_bool temp_ctx = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6331 | const struct lysc_node *node, *new_node; |
| 6332 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6333 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6334 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6335 | return LY_SUCCESS; |
| 6336 | } |
| 6337 | |
| 6338 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6339 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6340 | return LY_EVALID; |
| 6341 | } |
| 6342 | |
| 6343 | if (all_desc) { |
| 6344 | /* <path>//.. == <path>//./.. */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6345 | LY_CHECK_RET(moveto_scnode_self(set, 1, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6346 | } |
| 6347 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6348 | orig_used = set->used; |
| 6349 | for (i = 0; i < orig_used; ++i) { |
| 6350 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6351 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6352 | continue; |
| 6353 | } |
| 6354 | |
| 6355 | /* remember context node */ |
| 6356 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 6357 | } else { |
| 6358 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6359 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6360 | |
| 6361 | node = set->val.scnodes[i].scnode; |
| 6362 | |
| 6363 | if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6364 | new_node = lysc_data_parent(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6365 | } else { |
| 6366 | /* root does not have a parent */ |
| 6367 | continue; |
| 6368 | } |
| 6369 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6370 | if (!new_node) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6371 | /* node has no parent */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6372 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6373 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6374 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6375 | /* 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] | 6376 | new_type = LYXP_NODE_ELEM; |
| 6377 | } |
| 6378 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6379 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx)); |
| 6380 | if ((idx < orig_used) && (idx > i)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6381 | set->val.scnodes[idx].in_ctx = 2; |
| 6382 | temp_ctx = 1; |
| 6383 | } |
| 6384 | } |
| 6385 | |
| 6386 | if (temp_ctx) { |
| 6387 | for (i = 0; i < orig_used; ++i) { |
| 6388 | if (set->val.scnodes[i].in_ctx == 2) { |
| 6389 | set->val.scnodes[i].in_ctx = 1; |
| 6390 | } |
| 6391 | } |
| 6392 | } |
| 6393 | |
| 6394 | return LY_SUCCESS; |
| 6395 | } |
| 6396 | |
| 6397 | /** |
| 6398 | * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'. |
| 6399 | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
| 6400 | * |
| 6401 | * @param[in,out] set1 Set to use for the result. |
| 6402 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6403 | * @param[in] op Comparison operator to process. |
| 6404 | * @param[in] options XPath options. |
| 6405 | * @return LY_ERR |
| 6406 | */ |
| 6407 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6408 | 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] | 6409 | { |
| 6410 | /* |
| 6411 | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
| 6412 | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
| 6413 | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6414 | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6415 | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
| 6416 | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6417 | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6418 | * |
| 6419 | * '=' or '!=' |
| 6420 | * BOOLEAN + BOOLEAN |
| 6421 | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6422 | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6423 | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6424 | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6425 | * NUMBER + NUMBER |
| 6426 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6427 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6428 | * STRING + STRING |
| 6429 | * |
| 6430 | * '<=', '<', '>=', '>' |
| 6431 | * NUMBER + NUMBER |
| 6432 | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6433 | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6434 | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6435 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6436 | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6437 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6438 | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6439 | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6440 | */ |
| 6441 | struct lyxp_set iter1, iter2; |
| 6442 | int result; |
| 6443 | int64_t i; |
| 6444 | LY_ERR rc; |
| 6445 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6446 | memset(&iter1, 0, sizeof iter1); |
| 6447 | memset(&iter2, 0, sizeof iter2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6448 | |
| 6449 | /* iterative evaluation with node-sets */ |
| 6450 | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
| 6451 | if (set1->type == LYXP_SET_NODE_SET) { |
| 6452 | for (i = 0; i < set1->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6453 | /* cast set1 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6454 | switch (set2->type) { |
| 6455 | case LYXP_SET_NUMBER: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6456 | rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6457 | break; |
| 6458 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6459 | rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6460 | break; |
| 6461 | default: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6462 | rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6463 | break; |
| 6464 | } |
| 6465 | LY_CHECK_RET(rc); |
| 6466 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6467 | /* canonize set2 */ |
| 6468 | LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc); |
| 6469 | |
| 6470 | /* compare recursively */ |
| 6471 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
| 6472 | lyxp_set_free_content(&iter2); |
| 6473 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6474 | |
| 6475 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6476 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6477 | set_fill_boolean(set1, 1); |
| 6478 | return LY_SUCCESS; |
| 6479 | } |
| 6480 | } |
| 6481 | } else { |
| 6482 | for (i = 0; i < set2->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6483 | /* set set2 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6484 | switch (set1->type) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6485 | case LYXP_SET_NUMBER: |
| 6486 | rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i); |
| 6487 | break; |
| 6488 | case LYXP_SET_BOOLEAN: |
| 6489 | rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i); |
| 6490 | break; |
| 6491 | default: |
| 6492 | rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i); |
| 6493 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6494 | } |
| 6495 | LY_CHECK_RET(rc); |
| 6496 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6497 | /* canonize set1 */ |
| 6498 | 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] | 6499 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6500 | /* compare recursively */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6501 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6502 | lyxp_set_free_content(&iter2); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6503 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6504 | |
| 6505 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6506 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6507 | set_fill_boolean(set1, 1); |
| 6508 | return LY_SUCCESS; |
| 6509 | } |
| 6510 | } |
| 6511 | } |
| 6512 | |
| 6513 | /* false for all nodes */ |
| 6514 | set_fill_boolean(set1, 0); |
| 6515 | return LY_SUCCESS; |
| 6516 | } |
| 6517 | |
| 6518 | /* first convert properly */ |
| 6519 | if ((op[0] == '=') || (op[0] == '!')) { |
| 6520 | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6521 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
| 6522 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6523 | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6524 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6525 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6526 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6527 | LY_CHECK_RET(rc); |
| 6528 | } /* else we have 2 strings */ |
| 6529 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6530 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6531 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6532 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6533 | LY_CHECK_RET(rc); |
| 6534 | } |
| 6535 | |
| 6536 | assert(set1->type == set2->type); |
| 6537 | |
| 6538 | /* compute result */ |
| 6539 | if (op[0] == '=') { |
| 6540 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6541 | result = (set1->val.bln == set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6542 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6543 | result = (set1->val.num == set2->val.num); |
| 6544 | } else { |
| 6545 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6546 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6547 | } |
| 6548 | } else if (op[0] == '!') { |
| 6549 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6550 | result = (set1->val.bln != set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6551 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6552 | result = (set1->val.num != set2->val.num); |
| 6553 | } else { |
| 6554 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6555 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6556 | } |
| 6557 | } else { |
| 6558 | assert(set1->type == LYXP_SET_NUMBER); |
| 6559 | if (op[0] == '<') { |
| 6560 | if (op[1] == '=') { |
| 6561 | result = (set1->val.num <= set2->val.num); |
| 6562 | } else { |
| 6563 | result = (set1->val.num < set2->val.num); |
| 6564 | } |
| 6565 | } else { |
| 6566 | if (op[1] == '=') { |
| 6567 | result = (set1->val.num >= set2->val.num); |
| 6568 | } else { |
| 6569 | result = (set1->val.num > set2->val.num); |
| 6570 | } |
| 6571 | } |
| 6572 | } |
| 6573 | |
| 6574 | /* assign result */ |
| 6575 | if (result) { |
| 6576 | set_fill_boolean(set1, 1); |
| 6577 | } else { |
| 6578 | set_fill_boolean(set1, 0); |
| 6579 | } |
| 6580 | |
| 6581 | return LY_SUCCESS; |
| 6582 | } |
| 6583 | |
| 6584 | /** |
| 6585 | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
| 6586 | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
| 6587 | * |
| 6588 | * @param[in,out] set1 Set to use for the result. |
| 6589 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6590 | * @param[in] op Operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6591 | * @return LY_ERR |
| 6592 | */ |
| 6593 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6594 | 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] | 6595 | { |
| 6596 | LY_ERR rc; |
| 6597 | |
| 6598 | /* unary '-' */ |
| 6599 | if (!set2 && (op[0] == '-')) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6600 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6601 | LY_CHECK_RET(rc); |
| 6602 | set1->val.num *= -1; |
| 6603 | lyxp_set_free(set2); |
| 6604 | return LY_SUCCESS; |
| 6605 | } |
| 6606 | |
| 6607 | assert(set1 && set2); |
| 6608 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6609 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6610 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6611 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6612 | LY_CHECK_RET(rc); |
| 6613 | |
| 6614 | switch (op[0]) { |
| 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 | /* '*' */ |
| 6626 | case '*': |
| 6627 | set1->val.num *= set2->val.num; |
| 6628 | break; |
| 6629 | |
| 6630 | /* 'div' */ |
| 6631 | case 'd': |
| 6632 | set1->val.num /= set2->val.num; |
| 6633 | break; |
| 6634 | |
| 6635 | /* 'mod' */ |
| 6636 | case 'm': |
| 6637 | set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num); |
| 6638 | break; |
| 6639 | |
| 6640 | default: |
| 6641 | LOGINT_RET(set1->ctx); |
| 6642 | } |
| 6643 | |
| 6644 | return LY_SUCCESS; |
| 6645 | } |
| 6646 | |
| 6647 | /* |
| 6648 | * eval functions |
| 6649 | * |
| 6650 | * They execute a parsed XPath expression on some data subtree. |
| 6651 | */ |
| 6652 | |
| 6653 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6654 | * @brief Evaluate Predicate. Logs directly on error. |
| 6655 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6656 | * [9] Predicate ::= '[' Expr ']' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6657 | * |
| 6658 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6659 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6660 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6661 | * @param[in] options XPath options. |
| 6662 | * @param[in] parent_pos_pred Whether parent predicate was a positional one. |
| 6663 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6664 | */ |
| 6665 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6666 | eval_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options, ly_bool parent_pos_pred) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6667 | { |
| 6668 | LY_ERR rc; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 6669 | uint16_t i, orig_exp; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6670 | uint32_t orig_pos, orig_size; |
| 6671 | int32_t pred_in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6672 | struct lyxp_set set2; |
| 6673 | struct lyd_node *orig_parent; |
| 6674 | |
| 6675 | /* '[' */ |
| 6676 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6677 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6678 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6679 | |
| 6680 | if (!set) { |
| 6681 | only_parse: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6682 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6683 | LY_CHECK_RET(rc); |
| 6684 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 6685 | /* 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] | 6686 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6687 | |
| 6688 | /* empty set, nothing to evaluate */ |
| 6689 | if (!set->used) { |
| 6690 | goto only_parse; |
| 6691 | } |
| 6692 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6693 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6694 | orig_pos = 0; |
| 6695 | orig_size = set->used; |
| 6696 | orig_parent = NULL; |
Michal Vasko | 39dbf35 | 2020-05-21 10:08:59 +0200 | [diff] [blame] | 6697 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6698 | set_init(&set2, set); |
| 6699 | set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0); |
| 6700 | /* remember the node context position for position() and context size for last(), |
| 6701 | * predicates should always be evaluated with respect to the child axis (since we do |
| 6702 | * not support explicit axes) so we assign positions based on their parents */ |
| 6703 | if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) { |
| 6704 | orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent; |
| 6705 | orig_pos = 1; |
| 6706 | } else { |
| 6707 | ++orig_pos; |
| 6708 | } |
| 6709 | |
| 6710 | set2.ctx_pos = orig_pos; |
| 6711 | set2.ctx_size = orig_size; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6712 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6713 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6714 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6715 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6716 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6717 | return rc; |
| 6718 | } |
| 6719 | |
| 6720 | /* number is a position */ |
| 6721 | if (set2.type == LYXP_SET_NUMBER) { |
| 6722 | if ((long long)set2.val.num == orig_pos) { |
| 6723 | set2.val.num = 1; |
| 6724 | } else { |
| 6725 | set2.val.num = 0; |
| 6726 | } |
| 6727 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6728 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6729 | |
| 6730 | /* predicate satisfied or not? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6731 | if (!set2.val.bln) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6732 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6733 | } |
| 6734 | } |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6735 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6736 | |
| 6737 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 6738 | for (i = 0; i < set->used; ++i) { |
| 6739 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6740 | /* there is a currently-valid node */ |
| 6741 | break; |
| 6742 | } |
| 6743 | } |
| 6744 | /* empty set, nothing to evaluate */ |
| 6745 | if (i == set->used) { |
| 6746 | goto only_parse; |
| 6747 | } |
| 6748 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6749 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6750 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6751 | /* set special in_ctx to all the valid snodes */ |
| 6752 | pred_in_ctx = set_scnode_new_in_ctx(set); |
| 6753 | |
| 6754 | /* use the valid snodes one-by-one */ |
| 6755 | for (i = 0; i < set->used; ++i) { |
| 6756 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
| 6757 | continue; |
| 6758 | } |
| 6759 | set->val.scnodes[i].in_ctx = 1; |
| 6760 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6761 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6762 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6763 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6764 | LY_CHECK_RET(rc); |
| 6765 | |
| 6766 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
| 6767 | } |
| 6768 | |
| 6769 | /* restore the state as it was before the predicate */ |
| 6770 | for (i = 0; i < set->used; ++i) { |
| 6771 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6772 | set->val.scnodes[i].in_ctx = 0; |
| 6773 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
| 6774 | set->val.scnodes[i].in_ctx = 1; |
| 6775 | } |
| 6776 | } |
| 6777 | |
| 6778 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6779 | set2.type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6780 | set_fill_set(&set2, set); |
| 6781 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6782 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6783 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6784 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6785 | return rc; |
| 6786 | } |
| 6787 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6788 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6789 | if (!set2.val.bln) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6790 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6791 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6792 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6793 | } |
| 6794 | |
| 6795 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6796 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6797 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6798 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6799 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6800 | |
| 6801 | return LY_SUCCESS; |
| 6802 | } |
| 6803 | |
| 6804 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6805 | * @brief Evaluate Literal. Logs directly on error. |
| 6806 | * |
| 6807 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6808 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6809 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6810 | */ |
| 6811 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6812 | 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] | 6813 | { |
| 6814 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6815 | if (exp->tok_len[*tok_idx] == 2) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6816 | set_fill_string(set, "", 0); |
| 6817 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6818 | 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] | 6819 | } |
| 6820 | } |
| 6821 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6822 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6823 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6824 | } |
| 6825 | |
| 6826 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6827 | * @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] | 6828 | * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6829 | * @param[in] exp Full parsed XPath expression. |
| 6830 | * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success. |
| 6831 | * @param[in] scnode Found schema node as context for the predicate. |
| 6832 | * @param[in] format Format of any prefixes in key names/values. |
| 6833 | * @param[out] predicates Parsed predicates. |
| 6834 | * @param[out] pred_type Type of @p predicates. |
| 6835 | * @return LY_SUCCESS on success, |
| 6836 | * @return LY_ERR on any error. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6837 | */ |
| 6838 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6839 | 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] | 6840 | LY_PREFIX_FORMAT format, struct ly_path_predicate **predicates, |
| 6841 | enum ly_path_pred_type *pred_type) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6842 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6843 | LY_ERR ret = LY_SUCCESS; |
| 6844 | uint16_t key_count, e_idx, pred_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6845 | const struct lysc_node *key; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6846 | size_t pred_len; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6847 | uint32_t prev_lo; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6848 | struct lyxp_expr *exp2 = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6849 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6850 | assert(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6851 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6852 | if (scnode->nodetype == LYS_LIST) { |
| 6853 | /* get key count */ |
| 6854 | if (scnode->flags & LYS_KEYLESS) { |
| 6855 | return LY_EINVAL; |
| 6856 | } |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 6857 | 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] | 6858 | assert(key_count); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6859 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6860 | /* learn where the predicates end */ |
| 6861 | e_idx = *tok_idx; |
| 6862 | while (key_count) { |
| 6863 | /* '[' */ |
| 6864 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6865 | return LY_EINVAL; |
| 6866 | } |
| 6867 | ++e_idx; |
| 6868 | |
| 6869 | /* ']' */ |
| 6870 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6871 | ++e_idx; |
| 6872 | } |
| 6873 | ++e_idx; |
| 6874 | |
| 6875 | /* another presumably key predicate parsed */ |
| 6876 | --key_count; |
| 6877 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6878 | } else { |
| 6879 | /* learn just where this single predicate ends */ |
| 6880 | e_idx = *tok_idx; |
| 6881 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6882 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6883 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6884 | return LY_EINVAL; |
| 6885 | } |
| 6886 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6887 | |
| 6888 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6889 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6890 | ++e_idx; |
| 6891 | } |
| 6892 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6893 | } |
| 6894 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6895 | /* get the joined length of all the keys predicates/of the single leaf-list predicate */ |
| 6896 | pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx]; |
| 6897 | |
| 6898 | /* turn logging off */ |
| 6899 | prev_lo = ly_log_options(0); |
| 6900 | |
| 6901 | /* parse the predicate(s) */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6902 | 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] | 6903 | LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup); |
| 6904 | |
| 6905 | /* compile */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6906 | ret = ly_path_compile_predicate(scnode->module->ctx, scnode, scnode->module, scnode, exp2, &pred_idx, |
| 6907 | format, scnode->module, predicates, pred_type); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6908 | LY_CHECK_GOTO(ret, cleanup); |
| 6909 | |
| 6910 | /* success, the predicate must include all the needed information for hash-based search */ |
| 6911 | *tok_idx = e_idx; |
| 6912 | |
| 6913 | cleanup: |
| 6914 | ly_log_options(prev_lo); |
| 6915 | lyxp_expr_free(scnode->module->ctx, exp2); |
| 6916 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6917 | } |
| 6918 | |
| 6919 | /** |
| 6920 | * @brief Evaluate NameTest and any following Predicates. Logs directly on error. |
| 6921 | * |
| 6922 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 6923 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 6924 | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
| 6925 | * |
| 6926 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6927 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6928 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 6929 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 6930 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6931 | * @param[in] options XPath options. |
| 6932 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6933 | */ |
| 6934 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6935 | eval_name_test_with_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool attr_axis, ly_bool all_desc, struct lyxp_set *set, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6936 | uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6937 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6938 | char *path; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6939 | const char *ncname, *ncname_dict = NULL; |
| 6940 | uint16_t ncname_len; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6941 | const struct lys_module *moveto_mod; |
| 6942 | const struct lysc_node *scnode = NULL, *tmp; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6943 | struct ly_path_predicate *predicates = NULL; |
| 6944 | enum ly_path_pred_type pred_type = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6945 | LY_ERR rc = LY_SUCCESS; |
| 6946 | |
| 6947 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6948 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6949 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6950 | |
| 6951 | if (!set) { |
| 6952 | goto moveto; |
| 6953 | } |
| 6954 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6955 | ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]]; |
| 6956 | ncname_len = exp->tok_len[*tok_idx - 1]; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6957 | |
| 6958 | /* parse (and skip) module name */ |
| 6959 | rc = moveto_resolve_model(&ncname, &ncname_len, set, &moveto_mod); |
| 6960 | LY_CHECK_GOTO(rc, cleanup); |
| 6961 | |
| 6962 | if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) { |
| 6963 | /* find the matching schema node in some parent in the context */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6964 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6965 | if (set->val.nodes[i].type == set->root_type) { |
| 6966 | tmp = lys_find_child(NULL, moveto_mod, ncname, ncname_len, 0, 0); |
| 6967 | } else if ((set->val.nodes[i].type == LYXP_NODE_ELEM) |
| 6968 | && (!scnode || (lysc_data_parent(scnode) != set->val.nodes[i].node->schema))) { |
| 6969 | /* do not repeat the same search */ |
| 6970 | 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] | 6971 | } else { |
| 6972 | tmp = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6973 | } |
| 6974 | |
| 6975 | /* additional context check */ |
| 6976 | if (tmp && (set->root_type == LYXP_NODE_ROOT_CONFIG) && (tmp->flags & LYS_CONFIG_R)) { |
| 6977 | tmp = NULL; |
| 6978 | } |
| 6979 | |
| 6980 | if (tmp) { |
| 6981 | if (scnode) { |
| 6982 | /* we found a schema node with the same name but at different level, give up, too complicated */ |
| 6983 | scnode = NULL; |
| 6984 | break; |
| 6985 | } else { |
| 6986 | /* remember the found schema node and continue to make sure it can be used */ |
| 6987 | scnode = tmp; |
| 6988 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6989 | } |
| 6990 | } |
| 6991 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6992 | if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 6993 | /* try to create the predicates */ |
| 6994 | if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->format, &predicates, &pred_type)) { |
| 6995 | /* hashes cannot be used */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6996 | scnode = NULL; |
| 6997 | } |
| 6998 | } |
| 6999 | } |
| 7000 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7001 | if (!scnode && moveto_mod) { |
| 7002 | /* we are not able to match based on a schema node and not all the modules match, |
| 7003 | * use dictionary for efficient comparison */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame^] | 7004 | LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7005 | } |
| 7006 | |
| 7007 | moveto: |
| 7008 | /* move to the attribute(s), data node(s), or schema node(s) */ |
| 7009 | if (attr_axis) { |
| 7010 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7011 | set_scnode_clear_ctx(set); |
| 7012 | } else { |
| 7013 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7014 | rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7015 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7016 | rc = moveto_attr(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7017 | } |
| 7018 | LY_CHECK_GOTO(rc, cleanup); |
| 7019 | } |
| 7020 | } else { |
| 7021 | if (set && (options & LYXP_SCNODE_ALL)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7022 | int64_t i; |
| 7023 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7024 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7025 | rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7026 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7027 | rc = moveto_scnode(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7028 | } |
| 7029 | LY_CHECK_GOTO(rc, cleanup); |
| 7030 | |
| 7031 | for (i = set->used - 1; i > -1; --i) { |
| 7032 | if (set->val.scnodes[i].in_ctx > 0) { |
| 7033 | break; |
| 7034 | } |
| 7035 | } |
| 7036 | if (i == -1) { |
| 7037 | path = lysc_path(set->ctx_scnode, LYSC_PATH_LOG, NULL, 0); |
| 7038 | 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] | 7039 | ncname_len, ncname, ncname - exp->expr, exp->expr, path); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7040 | free(path); |
| 7041 | } |
| 7042 | } else { |
| 7043 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7044 | rc = moveto_node_alldesc(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7045 | } else { |
| 7046 | if (scnode) { |
| 7047 | /* we can find the nodes using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7048 | rc = moveto_node_hash(set, scnode, predicates); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7049 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7050 | rc = moveto_node(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7051 | } |
| 7052 | } |
| 7053 | LY_CHECK_GOTO(rc, cleanup); |
| 7054 | } |
| 7055 | } |
| 7056 | |
| 7057 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7058 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7059 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7060 | LY_CHECK_RET(rc); |
| 7061 | } |
| 7062 | |
| 7063 | cleanup: |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7064 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7065 | lydict_remove(set->ctx, ncname_dict); |
| 7066 | ly_path_predicates_free(set->ctx, pred_type, scnode, predicates); |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7067 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7068 | return rc; |
| 7069 | } |
| 7070 | |
| 7071 | /** |
| 7072 | * @brief Evaluate NodeType and any following Predicates. Logs directly on error. |
| 7073 | * |
| 7074 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7075 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7076 | * [8] NodeType ::= 'text' | 'node' |
| 7077 | * |
| 7078 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7079 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7080 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 7081 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7082 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7083 | * @param[in] options XPath options. |
| 7084 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7085 | */ |
| 7086 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7087 | eval_node_type_with_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool attr_axis, ly_bool all_desc, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7088 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7089 | { |
| 7090 | LY_ERR rc; |
| 7091 | |
| 7092 | /* TODO */ |
| 7093 | (void)attr_axis; |
| 7094 | (void)all_desc; |
| 7095 | |
| 7096 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7097 | assert(exp->tok_len[*tok_idx] == 4); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7098 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 7099 | set_scnode_clear_ctx(set); |
| 7100 | /* just for the debug message below */ |
| 7101 | set = NULL; |
| 7102 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7103 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7104 | rc = xpath_node(NULL, 0, set, options); |
| 7105 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7106 | assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7107 | rc = xpath_text(NULL, 0, set, options); |
| 7108 | } |
| 7109 | LY_CHECK_RET(rc); |
| 7110 | } |
| 7111 | } |
| 7112 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7113 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7114 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7115 | |
| 7116 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7117 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7118 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7119 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7120 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7121 | |
| 7122 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7123 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7124 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7125 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7126 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7127 | |
| 7128 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7129 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7130 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7131 | LY_CHECK_RET(rc); |
| 7132 | } |
| 7133 | |
| 7134 | return LY_SUCCESS; |
| 7135 | } |
| 7136 | |
| 7137 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7138 | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
| 7139 | * |
| 7140 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 7141 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7142 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7143 | * |
| 7144 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7145 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7146 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7147 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7148 | * @param[in] options XPath options. |
| 7149 | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
| 7150 | */ |
| 7151 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7152 | eval_relative_location_path(struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7153 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7154 | ly_bool attr_axis; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7155 | LY_ERR rc; |
| 7156 | |
| 7157 | goto step; |
| 7158 | do { |
| 7159 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7160 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7161 | all_desc = 0; |
| 7162 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7163 | assert(exp->tok_len[*tok_idx] == 2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7164 | all_desc = 1; |
| 7165 | } |
| 7166 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7167 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7168 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7169 | |
| 7170 | step: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7171 | /* evaluate abbreviated axis '@'? if any */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7172 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7173 | attr_axis = 1; |
| 7174 | |
| 7175 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7176 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7177 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7178 | } else { |
| 7179 | attr_axis = 0; |
| 7180 | } |
| 7181 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7182 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7183 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7184 | case LYXP_TOKEN_DOT: |
| 7185 | /* evaluate '.' */ |
| 7186 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7187 | rc = moveto_scnode_self(set, all_desc, options); |
| 7188 | } else { |
| 7189 | rc = moveto_self(set, all_desc, options); |
| 7190 | } |
| 7191 | LY_CHECK_RET(rc); |
| 7192 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7193 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7194 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7195 | break; |
| 7196 | |
| 7197 | case LYXP_TOKEN_DDOT: |
| 7198 | /* evaluate '..' */ |
| 7199 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7200 | rc = moveto_scnode_parent(set, all_desc, options); |
| 7201 | } else { |
| 7202 | rc = moveto_parent(set, all_desc, options); |
| 7203 | } |
| 7204 | LY_CHECK_RET(rc); |
| 7205 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7206 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7207 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7208 | break; |
| 7209 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7210 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7211 | /* evaluate NameTest Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7212 | 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] | 7213 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7214 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7215 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7216 | case LYXP_TOKEN_NODETYPE: |
| 7217 | /* evaluate NodeType Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7218 | 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] | 7219 | LY_CHECK_RET(rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7220 | break; |
| 7221 | |
| 7222 | default: |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 7223 | LOGINT_RET(set ? set->ctx : NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7224 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7225 | } 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] | 7226 | |
| 7227 | return LY_SUCCESS; |
| 7228 | } |
| 7229 | |
| 7230 | /** |
| 7231 | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
| 7232 | * |
| 7233 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 7234 | * |
| 7235 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7236 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7237 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7238 | * @param[in] options XPath options. |
| 7239 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7240 | */ |
| 7241 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7242 | 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] | 7243 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7244 | ly_bool all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7245 | |
| 7246 | if (set) { |
| 7247 | /* no matter what tokens follow, we need to be at the root */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7248 | LY_CHECK_RET(moveto_root(set, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7249 | } |
| 7250 | |
| 7251 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7252 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7253 | /* evaluate '/' - deferred */ |
| 7254 | all_desc = 0; |
| 7255 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7256 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7257 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7258 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7259 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7260 | return LY_SUCCESS; |
| 7261 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7262 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7263 | case LYXP_TOKEN_DOT: |
| 7264 | case LYXP_TOKEN_DDOT: |
| 7265 | case LYXP_TOKEN_AT: |
| 7266 | case LYXP_TOKEN_NAMETEST: |
| 7267 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7268 | LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7269 | break; |
| 7270 | default: |
| 7271 | break; |
| 7272 | } |
| 7273 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7274 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 7275 | /* '//' RelativeLocationPath */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7276 | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
| 7277 | all_desc = 1; |
| 7278 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7279 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7280 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7281 | |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7282 | LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7283 | } |
| 7284 | |
| 7285 | return LY_SUCCESS; |
| 7286 | } |
| 7287 | |
| 7288 | /** |
| 7289 | * @brief Evaluate FunctionCall. Logs directly on error. |
| 7290 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7291 | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7292 | * |
| 7293 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7294 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7295 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7296 | * @param[in] options XPath options. |
| 7297 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7298 | */ |
| 7299 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7300 | 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] | 7301 | { |
| 7302 | LY_ERR rc; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7303 | 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] | 7304 | uint16_t arg_count = 0, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7305 | struct lyxp_set **args = NULL, **args_aux; |
| 7306 | |
| 7307 | if (set) { |
| 7308 | /* FunctionName */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7309 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7310 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7311 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7312 | xpath_func = &xpath_not; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7313 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7314 | xpath_func = &xpath_sum; |
| 7315 | } |
| 7316 | break; |
| 7317 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7318 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7319 | xpath_func = &xpath_lang; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7320 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7321 | xpath_func = &xpath_last; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7322 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7323 | xpath_func = &xpath_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7324 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7325 | xpath_func = &xpath_true; |
| 7326 | } |
| 7327 | break; |
| 7328 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7329 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7330 | xpath_func = &xpath_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7331 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7332 | xpath_func = &xpath_false; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7333 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7334 | xpath_func = &xpath_floor; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7335 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7336 | xpath_func = &xpath_round; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7337 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7338 | xpath_func = &xpath_deref; |
| 7339 | } |
| 7340 | break; |
| 7341 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7342 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7343 | xpath_func = &xpath_concat; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7344 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7345 | xpath_func = &xpath_number; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7346 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7347 | xpath_func = &xpath_string; |
| 7348 | } |
| 7349 | break; |
| 7350 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7351 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7352 | xpath_func = &xpath_boolean; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7353 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7354 | xpath_func = &xpath_ceiling; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7355 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7356 | xpath_func = &xpath_current; |
| 7357 | } |
| 7358 | break; |
| 7359 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7360 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7361 | xpath_func = &xpath_contains; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7362 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7363 | xpath_func = &xpath_position; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7364 | } 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] | 7365 | xpath_func = &xpath_re_match; |
| 7366 | } |
| 7367 | break; |
| 7368 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7369 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7370 | xpath_func = &xpath_substring; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7371 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7372 | xpath_func = &xpath_translate; |
| 7373 | } |
| 7374 | break; |
| 7375 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7376 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7377 | xpath_func = &xpath_local_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7378 | } 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] | 7379 | xpath_func = &xpath_enum_value; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7380 | } 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] | 7381 | xpath_func = &xpath_bit_is_set; |
| 7382 | } |
| 7383 | break; |
| 7384 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7385 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7386 | xpath_func = &xpath_starts_with; |
| 7387 | } |
| 7388 | break; |
| 7389 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7390 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7391 | xpath_func = &xpath_derived_from; |
| 7392 | } |
| 7393 | break; |
| 7394 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7395 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7396 | xpath_func = &xpath_namespace_uri; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7397 | } 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] | 7398 | xpath_func = &xpath_string_length; |
| 7399 | } |
| 7400 | break; |
| 7401 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7402 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7403 | xpath_func = &xpath_normalize_space; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7404 | } 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] | 7405 | xpath_func = &xpath_substring_after; |
| 7406 | } |
| 7407 | break; |
| 7408 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7409 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7410 | xpath_func = &xpath_substring_before; |
| 7411 | } |
| 7412 | break; |
| 7413 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7414 | 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] | 7415 | xpath_func = &xpath_derived_from_or_self; |
| 7416 | } |
| 7417 | break; |
| 7418 | } |
| 7419 | |
| 7420 | if (!xpath_func) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7421 | 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] | 7422 | return LY_EVALID; |
| 7423 | } |
| 7424 | } |
| 7425 | |
| 7426 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7427 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7428 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7429 | |
| 7430 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7431 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7432 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7433 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7434 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7435 | |
| 7436 | /* ( Expr ( ',' Expr )* )? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7437 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7438 | if (set) { |
| 7439 | args = malloc(sizeof *args); |
| 7440 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7441 | arg_count = 1; |
| 7442 | args[0] = set_copy(set); |
| 7443 | if (!args[0]) { |
| 7444 | rc = LY_EMEM; |
| 7445 | goto cleanup; |
| 7446 | } |
| 7447 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7448 | rc = eval_expr_select(exp, tok_idx, 0, args[0], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7449 | LY_CHECK_GOTO(rc, cleanup); |
| 7450 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7451 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7452 | LY_CHECK_GOTO(rc, cleanup); |
| 7453 | } |
| 7454 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7455 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7456 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7457 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7458 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7459 | |
| 7460 | if (set) { |
| 7461 | ++arg_count; |
| 7462 | args_aux = realloc(args, arg_count * sizeof *args); |
| 7463 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7464 | args = args_aux; |
| 7465 | args[arg_count - 1] = set_copy(set); |
| 7466 | if (!args[arg_count - 1]) { |
| 7467 | rc = LY_EMEM; |
| 7468 | goto cleanup; |
| 7469 | } |
| 7470 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7471 | 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] | 7472 | LY_CHECK_GOTO(rc, cleanup); |
| 7473 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7474 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7475 | LY_CHECK_GOTO(rc, cleanup); |
| 7476 | } |
| 7477 | } |
| 7478 | |
| 7479 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7480 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7481 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7482 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7483 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7484 | |
| 7485 | if (set) { |
| 7486 | /* evaluate function */ |
| 7487 | rc = xpath_func(args, arg_count, set, options); |
| 7488 | |
| 7489 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7490 | /* merge all nodes from arg evaluations */ |
| 7491 | for (i = 0; i < arg_count; ++i) { |
| 7492 | set_scnode_clear_ctx(args[i]); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7493 | lyxp_set_scnode_merge(set, args[i]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7494 | } |
| 7495 | } |
| 7496 | } else { |
| 7497 | rc = LY_SUCCESS; |
| 7498 | } |
| 7499 | |
| 7500 | cleanup: |
| 7501 | for (i = 0; i < arg_count; ++i) { |
| 7502 | lyxp_set_free(args[i]); |
| 7503 | } |
| 7504 | free(args); |
| 7505 | |
| 7506 | return rc; |
| 7507 | } |
| 7508 | |
| 7509 | /** |
| 7510 | * @brief Evaluate Number. Logs directly on error. |
| 7511 | * |
| 7512 | * @param[in] ctx Context for errors. |
| 7513 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7514 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7515 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7516 | * @return LY_ERR |
| 7517 | */ |
| 7518 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7519 | 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] | 7520 | { |
| 7521 | long double num; |
| 7522 | char *endptr; |
| 7523 | |
| 7524 | if (set) { |
| 7525 | errno = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7526 | num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7527 | if (errno) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7528 | 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] | 7529 | 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] | 7530 | 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] | 7531 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7532 | } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) { |
| 7533 | 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] | 7534 | 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] | 7535 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7536 | return LY_EVALID; |
| 7537 | } |
| 7538 | |
| 7539 | set_fill_number(set, num); |
| 7540 | } |
| 7541 | |
| 7542 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7543 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7544 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7545 | return LY_SUCCESS; |
| 7546 | } |
| 7547 | |
| 7548 | /** |
| 7549 | * @brief Evaluate PathExpr. Logs directly on error. |
| 7550 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7551 | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7552 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 7553 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 7554 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7555 | * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7556 | * |
| 7557 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7558 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7559 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7560 | * @param[in] options XPath options. |
| 7561 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7562 | */ |
| 7563 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7564 | 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] | 7565 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7566 | ly_bool all_desc, parent_pos_pred; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7567 | LY_ERR rc; |
| 7568 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7569 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7570 | case LYXP_TOKEN_PAR1: |
| 7571 | /* '(' Expr ')' */ |
| 7572 | |
| 7573 | /* '(' */ |
| 7574 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7575 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7576 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7577 | |
| 7578 | /* Expr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7579 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7580 | LY_CHECK_RET(rc); |
| 7581 | |
| 7582 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7583 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7584 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7585 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7586 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7587 | |
| 7588 | parent_pos_pred = 0; |
| 7589 | goto predicate; |
| 7590 | |
| 7591 | case LYXP_TOKEN_DOT: |
| 7592 | case LYXP_TOKEN_DDOT: |
| 7593 | case LYXP_TOKEN_AT: |
| 7594 | case LYXP_TOKEN_NAMETEST: |
| 7595 | case LYXP_TOKEN_NODETYPE: |
| 7596 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7597 | rc = eval_relative_location_path(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7598 | LY_CHECK_RET(rc); |
| 7599 | break; |
| 7600 | |
| 7601 | case LYXP_TOKEN_FUNCNAME: |
| 7602 | /* FunctionCall */ |
| 7603 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7604 | rc = eval_function_call(exp, tok_idx, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7605 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7606 | rc = eval_function_call(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7607 | } |
| 7608 | LY_CHECK_RET(rc); |
| 7609 | |
| 7610 | parent_pos_pred = 1; |
| 7611 | goto predicate; |
| 7612 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 7613 | case LYXP_TOKEN_OPER_PATH: |
| 7614 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7615 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7616 | rc = eval_absolute_location_path(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7617 | LY_CHECK_RET(rc); |
| 7618 | break; |
| 7619 | |
| 7620 | case LYXP_TOKEN_LITERAL: |
| 7621 | /* Literal */ |
| 7622 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7623 | if (set) { |
| 7624 | set_scnode_clear_ctx(set); |
| 7625 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7626 | eval_literal(exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7627 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7628 | eval_literal(exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7629 | } |
| 7630 | |
| 7631 | parent_pos_pred = 1; |
| 7632 | goto predicate; |
| 7633 | |
| 7634 | case LYXP_TOKEN_NUMBER: |
| 7635 | /* Number */ |
| 7636 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7637 | if (set) { |
| 7638 | set_scnode_clear_ctx(set); |
| 7639 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7640 | rc = eval_number(NULL, exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7641 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7642 | rc = eval_number(set->ctx, exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7643 | } |
| 7644 | LY_CHECK_RET(rc); |
| 7645 | |
| 7646 | parent_pos_pred = 1; |
| 7647 | goto predicate; |
| 7648 | |
| 7649 | default: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7650 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), |
| 7651 | &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7652 | return LY_EVALID; |
| 7653 | } |
| 7654 | |
| 7655 | return LY_SUCCESS; |
| 7656 | |
| 7657 | predicate: |
| 7658 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7659 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7660 | rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7661 | LY_CHECK_RET(rc); |
| 7662 | } |
| 7663 | |
| 7664 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7665 | 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] | 7666 | |
| 7667 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7668 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7669 | all_desc = 0; |
| 7670 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7671 | all_desc = 1; |
| 7672 | } |
| 7673 | |
| 7674 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7675 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7676 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7677 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7678 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7679 | LY_CHECK_RET(rc); |
| 7680 | } |
| 7681 | |
| 7682 | return LY_SUCCESS; |
| 7683 | } |
| 7684 | |
| 7685 | /** |
| 7686 | * @brief Evaluate UnionExpr. Logs directly on error. |
| 7687 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7688 | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7689 | * |
| 7690 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7691 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7692 | * @param[in] repeat How many times this expression is repeated. |
| 7693 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7694 | * @param[in] options XPath options. |
| 7695 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7696 | */ |
| 7697 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7698 | 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] | 7699 | { |
| 7700 | LY_ERR rc = LY_SUCCESS; |
| 7701 | struct lyxp_set orig_set, set2; |
| 7702 | uint16_t i; |
| 7703 | |
| 7704 | assert(repeat); |
| 7705 | |
| 7706 | set_init(&orig_set, set); |
| 7707 | set_init(&set2, set); |
| 7708 | |
| 7709 | set_fill_set(&orig_set, set); |
| 7710 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7711 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7712 | LY_CHECK_GOTO(rc, cleanup); |
| 7713 | |
| 7714 | /* ('|' PathExpr)* */ |
| 7715 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7716 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7717 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7718 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7719 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7720 | |
| 7721 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7722 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7723 | LY_CHECK_GOTO(rc, cleanup); |
| 7724 | continue; |
| 7725 | } |
| 7726 | |
| 7727 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7728 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7729 | LY_CHECK_GOTO(rc, cleanup); |
| 7730 | |
| 7731 | /* eval */ |
| 7732 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7733 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7734 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7735 | rc = moveto_union(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7736 | LY_CHECK_GOTO(rc, cleanup); |
| 7737 | } |
| 7738 | } |
| 7739 | |
| 7740 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7741 | lyxp_set_free_content(&orig_set); |
| 7742 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7743 | return rc; |
| 7744 | } |
| 7745 | |
| 7746 | /** |
| 7747 | * @brief Evaluate UnaryExpr. Logs directly on error. |
| 7748 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7749 | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7750 | * |
| 7751 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7752 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7753 | * @param[in] repeat How many times this expression is repeated. |
| 7754 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7755 | * @param[in] options XPath options. |
| 7756 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7757 | */ |
| 7758 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7759 | 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] | 7760 | { |
| 7761 | LY_ERR rc; |
| 7762 | uint16_t this_op, i; |
| 7763 | |
| 7764 | assert(repeat); |
| 7765 | |
| 7766 | /* ('-')+ */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7767 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7768 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7769 | 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] | 7770 | |
| 7771 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7772 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7773 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7774 | } |
| 7775 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7776 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7777 | LY_CHECK_RET(rc); |
| 7778 | |
| 7779 | if (set && (repeat % 2)) { |
| 7780 | if (options & LYXP_SCNODE_ALL) { |
| 7781 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
| 7782 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7783 | 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] | 7784 | LY_CHECK_RET(rc); |
| 7785 | } |
| 7786 | } |
| 7787 | |
| 7788 | return LY_SUCCESS; |
| 7789 | } |
| 7790 | |
| 7791 | /** |
| 7792 | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
| 7793 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7794 | * [18] MultiplicativeExpr ::= UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7795 | * | MultiplicativeExpr '*' UnaryExpr |
| 7796 | * | MultiplicativeExpr 'div' UnaryExpr |
| 7797 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 7798 | * |
| 7799 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7800 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7801 | * @param[in] repeat How many times this expression is repeated. |
| 7802 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7803 | * @param[in] options XPath options. |
| 7804 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7805 | */ |
| 7806 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7807 | 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] | 7808 | { |
| 7809 | LY_ERR rc; |
| 7810 | uint16_t this_op; |
| 7811 | struct lyxp_set orig_set, set2; |
| 7812 | uint16_t i; |
| 7813 | |
| 7814 | assert(repeat); |
| 7815 | |
| 7816 | set_init(&orig_set, set); |
| 7817 | set_init(&set2, set); |
| 7818 | |
| 7819 | set_fill_set(&orig_set, set); |
| 7820 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7821 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7822 | LY_CHECK_GOTO(rc, cleanup); |
| 7823 | |
| 7824 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 7825 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7826 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7827 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7828 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7829 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7830 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7831 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7832 | |
| 7833 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7834 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7835 | LY_CHECK_GOTO(rc, cleanup); |
| 7836 | continue; |
| 7837 | } |
| 7838 | |
| 7839 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7840 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7841 | LY_CHECK_GOTO(rc, cleanup); |
| 7842 | |
| 7843 | /* eval */ |
| 7844 | if (options & LYXP_SCNODE_ALL) { |
| 7845 | 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] | 7846 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7847 | set_scnode_clear_ctx(set); |
| 7848 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7849 | 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] | 7850 | LY_CHECK_GOTO(rc, cleanup); |
| 7851 | } |
| 7852 | } |
| 7853 | |
| 7854 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7855 | lyxp_set_free_content(&orig_set); |
| 7856 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7857 | return rc; |
| 7858 | } |
| 7859 | |
| 7860 | /** |
| 7861 | * @brief Evaluate AdditiveExpr. Logs directly on error. |
| 7862 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7863 | * [17] AdditiveExpr ::= MultiplicativeExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7864 | * | AdditiveExpr '+' MultiplicativeExpr |
| 7865 | * | AdditiveExpr '-' MultiplicativeExpr |
| 7866 | * |
| 7867 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7868 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7869 | * @param[in] repeat How many times this expression is repeated. |
| 7870 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7871 | * @param[in] options XPath options. |
| 7872 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7873 | */ |
| 7874 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7875 | 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] | 7876 | { |
| 7877 | LY_ERR rc; |
| 7878 | uint16_t this_op; |
| 7879 | struct lyxp_set orig_set, set2; |
| 7880 | uint16_t i; |
| 7881 | |
| 7882 | assert(repeat); |
| 7883 | |
| 7884 | set_init(&orig_set, set); |
| 7885 | set_init(&set2, set); |
| 7886 | |
| 7887 | set_fill_set(&orig_set, set); |
| 7888 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7889 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7890 | LY_CHECK_GOTO(rc, cleanup); |
| 7891 | |
| 7892 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 7893 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7894 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7895 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7896 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7897 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7898 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7899 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7900 | |
| 7901 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7902 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7903 | LY_CHECK_GOTO(rc, cleanup); |
| 7904 | continue; |
| 7905 | } |
| 7906 | |
| 7907 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7908 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7909 | LY_CHECK_GOTO(rc, cleanup); |
| 7910 | |
| 7911 | /* eval */ |
| 7912 | if (options & LYXP_SCNODE_ALL) { |
| 7913 | 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] | 7914 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7915 | set_scnode_clear_ctx(set); |
| 7916 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7917 | 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] | 7918 | LY_CHECK_GOTO(rc, cleanup); |
| 7919 | } |
| 7920 | } |
| 7921 | |
| 7922 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7923 | lyxp_set_free_content(&orig_set); |
| 7924 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7925 | return rc; |
| 7926 | } |
| 7927 | |
| 7928 | /** |
| 7929 | * @brief Evaluate RelationalExpr. Logs directly on error. |
| 7930 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7931 | * [16] RelationalExpr ::= AdditiveExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7932 | * | RelationalExpr '<' AdditiveExpr |
| 7933 | * | RelationalExpr '>' AdditiveExpr |
| 7934 | * | RelationalExpr '<=' AdditiveExpr |
| 7935 | * | RelationalExpr '>=' AdditiveExpr |
| 7936 | * |
| 7937 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7938 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7939 | * @param[in] repeat How many times this expression is repeated. |
| 7940 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7941 | * @param[in] options XPath options. |
| 7942 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7943 | */ |
| 7944 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7945 | 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] | 7946 | { |
| 7947 | LY_ERR rc; |
| 7948 | uint16_t this_op; |
| 7949 | struct lyxp_set orig_set, set2; |
| 7950 | uint16_t i; |
| 7951 | |
| 7952 | assert(repeat); |
| 7953 | |
| 7954 | set_init(&orig_set, set); |
| 7955 | set_init(&set2, set); |
| 7956 | |
| 7957 | set_fill_set(&orig_set, set); |
| 7958 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7959 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7960 | LY_CHECK_GOTO(rc, cleanup); |
| 7961 | |
| 7962 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 7963 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7964 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7965 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7966 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7967 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7968 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7969 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7970 | |
| 7971 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7972 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7973 | LY_CHECK_GOTO(rc, cleanup); |
| 7974 | continue; |
| 7975 | } |
| 7976 | |
| 7977 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7978 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7979 | LY_CHECK_GOTO(rc, cleanup); |
| 7980 | |
| 7981 | /* eval */ |
| 7982 | if (options & LYXP_SCNODE_ALL) { |
| 7983 | 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] | 7984 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7985 | set_scnode_clear_ctx(set); |
| 7986 | } else { |
| 7987 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 7988 | LY_CHECK_GOTO(rc, cleanup); |
| 7989 | } |
| 7990 | } |
| 7991 | |
| 7992 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7993 | lyxp_set_free_content(&orig_set); |
| 7994 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7995 | return rc; |
| 7996 | } |
| 7997 | |
| 7998 | /** |
| 7999 | * @brief Evaluate EqualityExpr. Logs directly on error. |
| 8000 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8001 | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8002 | * | EqualityExpr '!=' RelationalExpr |
| 8003 | * |
| 8004 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8005 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8006 | * @param[in] repeat How many times this expression is repeated. |
| 8007 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8008 | * @param[in] options XPath options. |
| 8009 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8010 | */ |
| 8011 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8012 | 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] | 8013 | { |
| 8014 | LY_ERR rc; |
| 8015 | uint16_t this_op; |
| 8016 | struct lyxp_set orig_set, set2; |
| 8017 | uint16_t i; |
| 8018 | |
| 8019 | assert(repeat); |
| 8020 | |
| 8021 | set_init(&orig_set, set); |
| 8022 | set_init(&set2, set); |
| 8023 | |
| 8024 | set_fill_set(&orig_set, set); |
| 8025 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8026 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8027 | LY_CHECK_GOTO(rc, cleanup); |
| 8028 | |
| 8029 | /* ('=' / '!=' RelationalExpr)* */ |
| 8030 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8031 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8032 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8033 | 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] | 8034 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8035 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8036 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8037 | |
| 8038 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8039 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8040 | LY_CHECK_GOTO(rc, cleanup); |
| 8041 | continue; |
| 8042 | } |
| 8043 | |
| 8044 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8045 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8046 | LY_CHECK_GOTO(rc, cleanup); |
| 8047 | |
| 8048 | /* eval */ |
| 8049 | if (options & LYXP_SCNODE_ALL) { |
| 8050 | 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] | 8051 | warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1); |
| 8052 | 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] | 8053 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8054 | set_scnode_clear_ctx(set); |
| 8055 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8056 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8057 | LY_CHECK_GOTO(rc, cleanup); |
| 8058 | } |
| 8059 | } |
| 8060 | |
| 8061 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8062 | lyxp_set_free_content(&orig_set); |
| 8063 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8064 | return rc; |
| 8065 | } |
| 8066 | |
| 8067 | /** |
| 8068 | * @brief Evaluate AndExpr. Logs directly on error. |
| 8069 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8070 | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8071 | * |
| 8072 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8073 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8074 | * @param[in] repeat How many times this expression is repeated. |
| 8075 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8076 | * @param[in] options XPath options. |
| 8077 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8078 | */ |
| 8079 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8080 | 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] | 8081 | { |
| 8082 | LY_ERR rc; |
| 8083 | struct lyxp_set orig_set, set2; |
| 8084 | uint16_t i; |
| 8085 | |
| 8086 | assert(repeat); |
| 8087 | |
| 8088 | set_init(&orig_set, set); |
| 8089 | set_init(&set2, set); |
| 8090 | |
| 8091 | set_fill_set(&orig_set, set); |
| 8092 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8093 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8094 | LY_CHECK_GOTO(rc, cleanup); |
| 8095 | |
| 8096 | /* cast to boolean, we know that will be the final result */ |
| 8097 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8098 | set_scnode_clear_ctx(set); |
| 8099 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8100 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8101 | } |
| 8102 | |
| 8103 | /* ('and' EqualityExpr)* */ |
| 8104 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8105 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8106 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"), |
| 8107 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8108 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8109 | |
| 8110 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8111 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) { |
| 8112 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8113 | LY_CHECK_GOTO(rc, cleanup); |
| 8114 | continue; |
| 8115 | } |
| 8116 | |
| 8117 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8118 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8119 | LY_CHECK_GOTO(rc, cleanup); |
| 8120 | |
| 8121 | /* eval - just get boolean value actually */ |
| 8122 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8123 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8124 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8125 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8126 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8127 | set_fill_set(set, &set2); |
| 8128 | } |
| 8129 | } |
| 8130 | |
| 8131 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8132 | lyxp_set_free_content(&orig_set); |
| 8133 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8134 | return rc; |
| 8135 | } |
| 8136 | |
| 8137 | /** |
| 8138 | * @brief Evaluate OrExpr. Logs directly on error. |
| 8139 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8140 | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8141 | * |
| 8142 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8143 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8144 | * @param[in] repeat How many times this expression is repeated. |
| 8145 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8146 | * @param[in] options XPath options. |
| 8147 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8148 | */ |
| 8149 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8150 | 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] | 8151 | { |
| 8152 | LY_ERR rc; |
| 8153 | struct lyxp_set orig_set, set2; |
| 8154 | uint16_t i; |
| 8155 | |
| 8156 | assert(repeat); |
| 8157 | |
| 8158 | set_init(&orig_set, set); |
| 8159 | set_init(&set2, set); |
| 8160 | |
| 8161 | set_fill_set(&orig_set, set); |
| 8162 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8163 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8164 | LY_CHECK_GOTO(rc, cleanup); |
| 8165 | |
| 8166 | /* cast to boolean, we know that will be the final result */ |
| 8167 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8168 | set_scnode_clear_ctx(set); |
| 8169 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8170 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8171 | } |
| 8172 | |
| 8173 | /* ('or' AndExpr)* */ |
| 8174 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8175 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8176 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"), |
| 8177 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8178 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8179 | |
| 8180 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8181 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) { |
| 8182 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8183 | LY_CHECK_GOTO(rc, cleanup); |
| 8184 | continue; |
| 8185 | } |
| 8186 | |
| 8187 | set_fill_set(&set2, &orig_set); |
| 8188 | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
| 8189 | * but it does not matter */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8190 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8191 | LY_CHECK_GOTO(rc, cleanup); |
| 8192 | |
| 8193 | /* eval - just get boolean value actually */ |
| 8194 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8195 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8196 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8197 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8198 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8199 | set_fill_set(set, &set2); |
| 8200 | } |
| 8201 | } |
| 8202 | |
| 8203 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8204 | lyxp_set_free_content(&orig_set); |
| 8205 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8206 | return rc; |
| 8207 | } |
| 8208 | |
| 8209 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8210 | * @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] | 8211 | * |
| 8212 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8213 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8214 | * @param[in] etype Expression type to evaluate. |
| 8215 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8216 | * @param[in] options XPath options. |
| 8217 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8218 | */ |
| 8219 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8220 | 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] | 8221 | { |
| 8222 | uint16_t i, count; |
| 8223 | enum lyxp_expr_type next_etype; |
| 8224 | LY_ERR rc; |
| 8225 | |
| 8226 | /* process operator repeats */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8227 | if (!exp->repeat[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8228 | next_etype = LYXP_EXPR_NONE; |
| 8229 | } else { |
| 8230 | /* find etype repeat */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8231 | for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8232 | |
| 8233 | /* select one-priority lower because etype expression called us */ |
| 8234 | if (i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8235 | next_etype = exp->repeat[*tok_idx][i - 1]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8236 | /* count repeats for that expression */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8237 | 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] | 8238 | } else { |
| 8239 | next_etype = LYXP_EXPR_NONE; |
| 8240 | } |
| 8241 | } |
| 8242 | |
| 8243 | /* decide what expression are we parsing based on the repeat */ |
| 8244 | switch (next_etype) { |
| 8245 | case LYXP_EXPR_OR: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8246 | rc = eval_or_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8247 | break; |
| 8248 | case LYXP_EXPR_AND: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8249 | rc = eval_and_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8250 | break; |
| 8251 | case LYXP_EXPR_EQUALITY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8252 | rc = eval_equality_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8253 | break; |
| 8254 | case LYXP_EXPR_RELATIONAL: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8255 | rc = eval_relational_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8256 | break; |
| 8257 | case LYXP_EXPR_ADDITIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8258 | rc = eval_additive_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8259 | break; |
| 8260 | case LYXP_EXPR_MULTIPLICATIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8261 | rc = eval_multiplicative_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8262 | break; |
| 8263 | case LYXP_EXPR_UNARY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8264 | rc = eval_unary_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8265 | break; |
| 8266 | case LYXP_EXPR_UNION: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8267 | rc = eval_union_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8268 | break; |
| 8269 | case LYXP_EXPR_NONE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8270 | rc = eval_path_expr(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8271 | break; |
| 8272 | default: |
| 8273 | LOGINT_RET(set->ctx); |
| 8274 | } |
| 8275 | |
| 8276 | return rc; |
| 8277 | } |
| 8278 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8279 | /** |
| 8280 | * @brief Get root type. |
| 8281 | * |
| 8282 | * @param[in] ctx_node Context node. |
| 8283 | * @param[in] ctx_scnode Schema context node. |
| 8284 | * @param[in] options XPath options. |
| 8285 | * @return Root type. |
| 8286 | */ |
| 8287 | static enum lyxp_node_type |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8288 | 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] | 8289 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8290 | const struct lysc_node *op; |
| 8291 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8292 | if (options & LYXP_SCNODE_ALL) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8293 | 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] | 8294 | |
| 8295 | if (op || (options & LYXP_SCNODE)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8296 | /* general root that can access everything */ |
| 8297 | return LYXP_NODE_ROOT; |
| 8298 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
| 8299 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8300 | return LYXP_NODE_ROOT_CONFIG; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8301 | } |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8302 | return LYXP_NODE_ROOT; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8303 | } |
| 8304 | |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8305 | op = ctx_node ? ctx_node->schema : NULL; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 8306 | 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] | 8307 | |
| 8308 | if (!ctx_node || (!op && (ctx_node->schema->flags & LYS_CONFIG_W))) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8309 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8310 | return LYXP_NODE_ROOT_CONFIG; |
| 8311 | } |
| 8312 | |
| 8313 | return LYXP_NODE_ROOT; |
| 8314 | } |
| 8315 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8316 | LY_ERR |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 8317 | 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] | 8318 | 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] | 8319 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8320 | uint16_t tok_idx = 0; |
| 8321 | const struct lyd_node *real_ctx_node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8322 | LY_ERR rc; |
| 8323 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8324 | LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_node, set, LY_EINVAL); |
| 8325 | |
| 8326 | if ((ctx_node_type == LYXP_NODE_ROOT) || (ctx_node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 8327 | /* we always need some context node because it is used for resolving unqualified names */ |
| 8328 | real_ctx_node = NULL; |
| 8329 | } else { |
| 8330 | real_ctx_node = ctx_node; |
| 8331 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8332 | |
| 8333 | /* prepare set for evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8334 | tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8335 | memset(set, 0, sizeof *set); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8336 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8337 | 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] | 8338 | set->ctx = local_mod->ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8339 | set->ctx_node = ctx_node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8340 | set->root_type = lyxp_get_root_type(real_ctx_node, NULL, options); |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8341 | for (set->context_op = ctx_node->schema; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 8342 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8343 | set->context_op = set->context_op->parent) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8344 | set->local_mod = local_mod; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 8345 | set->tree = tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8346 | set->format = format; |
| 8347 | |
| 8348 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8349 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8350 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8351 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8352 | } |
| 8353 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8354 | return rc; |
| 8355 | } |
| 8356 | |
| 8357 | #if 0 |
| 8358 | |
| 8359 | /* full xml printing of set elements, not used currently */ |
| 8360 | |
| 8361 | void |
| 8362 | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
| 8363 | { |
| 8364 | uint32_t i; |
| 8365 | char *str_num; |
| 8366 | struct lyout out; |
| 8367 | |
| 8368 | memset(&out, 0, sizeof out); |
| 8369 | |
| 8370 | out.type = LYOUT_STREAM; |
| 8371 | out.method.f = f; |
| 8372 | |
| 8373 | switch (set->type) { |
| 8374 | case LYXP_SET_EMPTY: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8375 | ly_print_(&out, "Empty XPath set\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8376 | break; |
| 8377 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8378 | ly_print_(&out, "Boolean XPath set:\n"); |
| 8379 | ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8380 | break; |
| 8381 | case LYXP_SET_STRING: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8382 | ly_print_(&out, "String XPath set:\n"); |
| 8383 | ly_print_(&out, "\"%s\"\n\n", set->value.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8384 | break; |
| 8385 | case LYXP_SET_NUMBER: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8386 | ly_print_(&out, "Number XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8387 | |
| 8388 | if (isnan(set->value.num)) { |
| 8389 | str_num = strdup("NaN"); |
| 8390 | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
| 8391 | str_num = strdup("0"); |
| 8392 | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
| 8393 | str_num = strdup("Infinity"); |
| 8394 | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
| 8395 | str_num = strdup("-Infinity"); |
| 8396 | } else if ((long long)set->value.num == set->value.num) { |
| 8397 | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
| 8398 | str_num = NULL; |
| 8399 | } |
| 8400 | } else { |
| 8401 | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
| 8402 | str_num = NULL; |
| 8403 | } |
| 8404 | } |
| 8405 | if (!str_num) { |
| 8406 | LOGMEM; |
| 8407 | return; |
| 8408 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8409 | ly_print_(&out, "%s\n\n", str_num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8410 | free(str_num); |
| 8411 | break; |
| 8412 | case LYXP_SET_NODE_SET: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8413 | ly_print_(&out, "Node XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8414 | |
| 8415 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8416 | ly_print_(&out, "%d. ", i + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8417 | switch (set->node_type[i]) { |
| 8418 | case LYXP_NODE_ROOT_ALL: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8419 | ly_print_(&out, "ROOT all\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8420 | break; |
| 8421 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8422 | ly_print_(&out, "ROOT config\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8423 | break; |
| 8424 | case LYXP_NODE_ROOT_STATE: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8425 | ly_print_(&out, "ROOT state\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8426 | break; |
| 8427 | case LYXP_NODE_ROOT_NOTIF: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8428 | 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] | 8429 | break; |
| 8430 | case LYXP_NODE_ROOT_RPC: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8431 | 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] | 8432 | break; |
| 8433 | case LYXP_NODE_ROOT_OUTPUT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8434 | 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] | 8435 | break; |
| 8436 | case LYXP_NODE_ELEM: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8437 | ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8438 | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8439 | ly_print_(&out, "\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8440 | break; |
| 8441 | case LYXP_NODE_TEXT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8442 | 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] | 8443 | break; |
| 8444 | case LYXP_NODE_ATTR: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8445 | 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] | 8446 | break; |
| 8447 | } |
| 8448 | } |
| 8449 | break; |
| 8450 | } |
| 8451 | } |
| 8452 | |
| 8453 | #endif |
| 8454 | |
| 8455 | LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8456 | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8457 | { |
| 8458 | long double num; |
| 8459 | char *str; |
| 8460 | LY_ERR rc; |
| 8461 | |
| 8462 | if (!set || (set->type == target)) { |
| 8463 | return LY_SUCCESS; |
| 8464 | } |
| 8465 | |
| 8466 | /* it's not possible to convert anything into a node set */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8467 | assert(target != LYXP_SET_NODE_SET); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8468 | |
| 8469 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8470 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8471 | return LY_EINVAL; |
| 8472 | } |
| 8473 | |
| 8474 | /* to STRING */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8475 | 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] | 8476 | switch (set->type) { |
| 8477 | case LYXP_SET_NUMBER: |
| 8478 | if (isnan(set->val.num)) { |
| 8479 | set->val.str = strdup("NaN"); |
| 8480 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8481 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 8482 | set->val.str = strdup("0"); |
| 8483 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8484 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 8485 | set->val.str = strdup("Infinity"); |
| 8486 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8487 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 8488 | set->val.str = strdup("-Infinity"); |
| 8489 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8490 | } else if ((long long)set->val.num == set->val.num) { |
| 8491 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 8492 | LOGMEM_RET(set->ctx); |
| 8493 | } |
| 8494 | set->val.str = str; |
| 8495 | } else { |
| 8496 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 8497 | LOGMEM_RET(set->ctx); |
| 8498 | } |
| 8499 | set->val.str = str; |
| 8500 | } |
| 8501 | break; |
| 8502 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8503 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8504 | set->val.str = strdup("true"); |
| 8505 | } else { |
| 8506 | set->val.str = strdup("false"); |
| 8507 | } |
| 8508 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 8509 | break; |
| 8510 | case LYXP_SET_NODE_SET: |
| 8511 | assert(set->used); |
| 8512 | |
| 8513 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8514 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8515 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8516 | rc = cast_node_set_to_string(set, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8517 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8518 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8519 | set->val.str = str; |
| 8520 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8521 | default: |
| 8522 | LOGINT_RET(set->ctx); |
| 8523 | } |
| 8524 | set->type = LYXP_SET_STRING; |
| 8525 | } |
| 8526 | |
| 8527 | /* to NUMBER */ |
| 8528 | if (target == LYXP_SET_NUMBER) { |
| 8529 | switch (set->type) { |
| 8530 | case LYXP_SET_STRING: |
| 8531 | num = cast_string_to_number(set->val.str); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8532 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8533 | set->val.num = num; |
| 8534 | break; |
| 8535 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8536 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8537 | set->val.num = 1; |
| 8538 | } else { |
| 8539 | set->val.num = 0; |
| 8540 | } |
| 8541 | break; |
| 8542 | default: |
| 8543 | LOGINT_RET(set->ctx); |
| 8544 | } |
| 8545 | set->type = LYXP_SET_NUMBER; |
| 8546 | } |
| 8547 | |
| 8548 | /* to BOOLEAN */ |
| 8549 | if (target == LYXP_SET_BOOLEAN) { |
| 8550 | switch (set->type) { |
| 8551 | case LYXP_SET_NUMBER: |
| 8552 | 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] | 8553 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8554 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8555 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8556 | } |
| 8557 | break; |
| 8558 | case LYXP_SET_STRING: |
| 8559 | if (set->val.str[0]) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8560 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8561 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8562 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8563 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8564 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8565 | } |
| 8566 | break; |
| 8567 | case LYXP_SET_NODE_SET: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8568 | if (set->used) { |
| 8569 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8570 | set->val.bln = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8571 | } else { |
| 8572 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8573 | set->val.bln = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8574 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8575 | break; |
| 8576 | default: |
| 8577 | LOGINT_RET(set->ctx); |
| 8578 | } |
| 8579 | set->type = LYXP_SET_BOOLEAN; |
| 8580 | } |
| 8581 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8582 | return LY_SUCCESS; |
| 8583 | } |
| 8584 | |
| 8585 | LY_ERR |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 8586 | 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] | 8587 | 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] | 8588 | { |
| 8589 | struct ly_ctx *ctx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8590 | const struct lysc_node *real_ctx_scnode; |
| 8591 | uint16_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8592 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8593 | 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] | 8594 | |
| 8595 | ctx = local_mod->ctx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8596 | if ((ctx_scnode_type == LYXP_NODE_ROOT) || (ctx_scnode_type == LYXP_NODE_ROOT_CONFIG)) { |
| 8597 | /* we always need some context node because it is used for resolving unqualified names */ |
| 8598 | real_ctx_scnode = NULL; |
| 8599 | } else { |
| 8600 | real_ctx_scnode = ctx_scnode; |
| 8601 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8602 | |
| 8603 | /* prepare set for evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8604 | tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8605 | memset(set, 0, sizeof *set); |
| 8606 | set->type = LYXP_SET_SCNODE_SET; |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 8607 | 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] | 8608 | set->val.scnodes[0].in_ctx = -2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8609 | set->ctx = ctx; |
| 8610 | set->ctx_scnode = ctx_scnode; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8611 | set->root_type = lyxp_get_root_type(NULL, real_ctx_scnode, options); |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8612 | for (set->context_op = ctx_scnode; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 8613 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8614 | set->context_op = set->context_op->parent) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8615 | set->local_mod = local_mod; |
| 8616 | set->format = format; |
| 8617 | |
| 8618 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8619 | return eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8620 | } |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 8621 | |
| 8622 | API const char * |
| 8623 | lyxp_get_expr(const struct lyxp_expr *path) |
| 8624 | { |
| 8625 | if (!path) { |
| 8626 | return NULL; |
| 8627 | } |
| 8628 | |
| 8629 | return path->expr; |
| 8630 | } |