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, |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 195 | item->node->schema->name, LYD_CANON_VALUE(lyd_child(item->node))); |
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 | |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 354 | for (child = lyd_child(node); 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 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1518 | const struct lysc_type *type = NULL; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 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 | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1550 | rc = type->plugin->store(src->ctx, type, str, strlen(str), LY_TYPE_OPTS_DYNAMIC, LY_PREF_JSON, NULL, LYD_HINT_DATA, |
| 1551 | xp_node->node->schema, &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 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2656 | LY_ERR |
| 2657 | lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr_str, size_t expr_len, ly_bool reparse, struct lyxp_expr **expr_p) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2658 | { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2659 | LY_ERR ret = LY_SUCCESS; |
| 2660 | struct lyxp_expr *expr; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2661 | size_t parsed = 0, tok_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2662 | enum lyxp_token tok_type; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2663 | ly_bool prev_function_check = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2664 | uint16_t tok_idx = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2665 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2666 | assert(expr_p); |
| 2667 | |
| 2668 | if (!expr_str[0]) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2669 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2670 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2671 | } |
| 2672 | |
| 2673 | if (!expr_len) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2674 | expr_len = strlen(expr_str); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2675 | } |
| 2676 | if (expr_len > UINT16_MAX) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2677 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "XPath expression cannot be longer than %ud characters.", UINT16_MAX); |
| 2678 | return LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2679 | } |
| 2680 | |
| 2681 | /* init lyxp_expr structure */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2682 | expr = calloc(1, sizeof *expr); |
| 2683 | LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error); |
| 2684 | LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error); |
| 2685 | expr->used = 0; |
| 2686 | expr->size = LYXP_EXPR_SIZE_START; |
| 2687 | expr->tokens = malloc(expr->size * sizeof *expr->tokens); |
| 2688 | LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2689 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2690 | expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos); |
| 2691 | LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2692 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2693 | expr->tok_len = malloc(expr->size * sizeof *expr->tok_len); |
| 2694 | LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2695 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2696 | /* make expr 0-terminated */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2697 | expr_str = expr->expr; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2698 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2699 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2700 | ++parsed; |
| 2701 | } |
| 2702 | |
| 2703 | do { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2704 | if (expr_str[parsed] == '(') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2705 | |
| 2706 | /* '(' */ |
| 2707 | tok_len = 1; |
| 2708 | tok_type = LYXP_TOKEN_PAR1; |
| 2709 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2710 | if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2711 | /* it is a NodeType/FunctionName after all */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2712 | if (((expr->tok_len[expr->used - 1] == 4) |
| 2713 | && (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) |
| 2714 | || !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) || |
| 2715 | ((expr->tok_len[expr->used - 1] == 7) |
| 2716 | && !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) { |
| 2717 | expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2718 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2719 | expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2720 | } |
| 2721 | prev_function_check = 0; |
| 2722 | } |
| 2723 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2724 | } else if (expr_str[parsed] == ')') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2725 | |
| 2726 | /* ')' */ |
| 2727 | tok_len = 1; |
| 2728 | tok_type = LYXP_TOKEN_PAR2; |
| 2729 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2730 | } else if (expr_str[parsed] == '[') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2731 | |
| 2732 | /* '[' */ |
| 2733 | tok_len = 1; |
| 2734 | tok_type = LYXP_TOKEN_BRACK1; |
| 2735 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2736 | } else if (expr_str[parsed] == ']') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2737 | |
| 2738 | /* ']' */ |
| 2739 | tok_len = 1; |
| 2740 | tok_type = LYXP_TOKEN_BRACK2; |
| 2741 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2742 | } else if (!strncmp(&expr_str[parsed], "..", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2743 | |
| 2744 | /* '..' */ |
| 2745 | tok_len = 2; |
| 2746 | tok_type = LYXP_TOKEN_DDOT; |
| 2747 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2748 | } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2749 | |
| 2750 | /* '.' */ |
| 2751 | tok_len = 1; |
| 2752 | tok_type = LYXP_TOKEN_DOT; |
| 2753 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2754 | } else if (expr_str[parsed] == '@') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2755 | |
| 2756 | /* '@' */ |
| 2757 | tok_len = 1; |
| 2758 | tok_type = LYXP_TOKEN_AT; |
| 2759 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2760 | } else if (expr_str[parsed] == ',') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2761 | |
| 2762 | /* ',' */ |
| 2763 | tok_len = 1; |
| 2764 | tok_type = LYXP_TOKEN_COMMA; |
| 2765 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2766 | } else if (expr_str[parsed] == '\'') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2767 | |
| 2768 | /* Literal with ' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2769 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {} |
| 2770 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
| 2771 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID, |
| 2772 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2773 | ++tok_len; |
| 2774 | tok_type = LYXP_TOKEN_LITERAL; |
| 2775 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2776 | } else if (expr_str[parsed] == '\"') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2777 | |
| 2778 | /* Literal with " */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2779 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {} |
| 2780 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
| 2781 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID, |
| 2782 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2783 | ++tok_len; |
| 2784 | tok_type = LYXP_TOKEN_LITERAL; |
| 2785 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2786 | } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2787 | |
| 2788 | /* Number */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2789 | for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
| 2790 | if (expr_str[parsed + tok_len] == '.') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2791 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2792 | for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2793 | } |
| 2794 | tok_type = LYXP_TOKEN_NUMBER; |
| 2795 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2796 | } else if (expr_str[parsed] == '/') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2797 | |
| 2798 | /* Operator '/', '//' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2799 | if (!strncmp(&expr_str[parsed], "//", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2800 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2801 | tok_type = LYXP_TOKEN_OPER_RPATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2802 | } else { |
| 2803 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2804 | tok_type = LYXP_TOKEN_OPER_PATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2805 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2806 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2807 | } else if (!strncmp(&expr_str[parsed], "!=", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2808 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2809 | /* Operator '!=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2810 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2811 | tok_type = LYXP_TOKEN_OPER_NEQUAL; |
| 2812 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2813 | } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2814 | |
| 2815 | /* Operator '<=', '>=' */ |
| 2816 | tok_len = 2; |
| 2817 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2818 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2819 | } else if (expr_str[parsed] == '|') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2820 | |
| 2821 | /* Operator '|' */ |
| 2822 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2823 | tok_type = LYXP_TOKEN_OPER_UNI; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2824 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2825 | } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2826 | |
| 2827 | /* Operator '+', '-' */ |
| 2828 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2829 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2830 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2831 | } else if (expr_str[parsed] == '=') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2832 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2833 | /* Operator '=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2834 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2835 | tok_type = LYXP_TOKEN_OPER_EQUAL; |
| 2836 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2837 | } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2838 | |
| 2839 | /* Operator '<', '>' */ |
| 2840 | tok_len = 1; |
| 2841 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2842 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2843 | } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) |
| 2844 | && (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) |
| 2845 | && (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) |
| 2846 | && (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) |
| 2847 | && (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) |
| 2848 | && (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) |
| 2849 | && (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) |
| 2850 | && (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) |
| 2851 | && (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) |
| 2852 | && (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) |
| 2853 | && (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) |
| 2854 | && (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2855 | |
| 2856 | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2857 | if (expr_str[parsed] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2858 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2859 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2860 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2861 | } else if (!strncmp(&expr_str[parsed], "or", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2862 | tok_len = 2; |
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 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2865 | } else if (!strncmp(&expr_str[parsed], "and", 3)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2866 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2867 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2868 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2869 | } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2870 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2871 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2872 | |
| 2873 | } else if (prev_function_check) { |
Michal Vasko | 5307857 | 2019-05-24 08:50:15 +0200 | [diff] [blame] | 2874 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.", |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2875 | expr_str[parsed], expr_str[parsed], expr->tok_len[expr->used - 1], &expr->expr[expr->tok_pos[expr->used - 1]]); |
| 2876 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2877 | goto error; |
| 2878 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2879 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed + 1, expr_str); |
| 2880 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2881 | goto error; |
| 2882 | } |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2883 | } else if (expr_str[parsed] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2884 | |
| 2885 | /* NameTest '*' */ |
| 2886 | tok_len = 1; |
| 2887 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2888 | |
| 2889 | } else { |
| 2890 | |
| 2891 | /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2892 | long int ncname_len = parse_ncname(&expr_str[parsed]); |
| 2893 | LY_CHECK_ERR_GOTO(ncname_len < 0, |
| 2894 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID, |
| 2895 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2896 | tok_len = ncname_len; |
| 2897 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2898 | if (expr_str[parsed + tok_len] == ':') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2899 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2900 | if (expr_str[parsed + tok_len] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2901 | ++tok_len; |
| 2902 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2903 | ncname_len = parse_ncname(&expr_str[parsed + tok_len]); |
| 2904 | LY_CHECK_ERR_GOTO(ncname_len < 0, |
| 2905 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID, |
| 2906 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2907 | tok_len += ncname_len; |
| 2908 | } |
| 2909 | /* remove old flag to prevent ambiguities */ |
| 2910 | prev_function_check = 0; |
| 2911 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2912 | } else { |
| 2913 | /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */ |
| 2914 | prev_function_check = 1; |
| 2915 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2916 | } |
| 2917 | } |
| 2918 | |
| 2919 | /* store the token, move on to the next one */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2920 | LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2921 | parsed += tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2922 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2923 | ++parsed; |
| 2924 | } |
| 2925 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2926 | } while (expr_str[parsed]); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2927 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2928 | if (reparse) { |
| 2929 | /* prealloc repeat */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2930 | expr->repeat = calloc(expr->size, sizeof *expr->repeat); |
| 2931 | LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2932 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2933 | /* fill repeat */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2934 | LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx), ret = LY_EVALID, error); |
| 2935 | if (expr->used > tok_idx) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2936 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.", |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2937 | &expr->expr[expr->tok_pos[tok_idx]]); |
| 2938 | ret = LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2939 | goto error; |
| 2940 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2941 | } |
| 2942 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2943 | print_expr_struct_debug(expr); |
| 2944 | *expr_p = expr; |
| 2945 | return LY_SUCCESS; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2946 | |
| 2947 | error: |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2948 | lyxp_expr_free(ctx, expr); |
| 2949 | return ret; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2950 | } |
| 2951 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2952 | LY_ERR |
| 2953 | lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2954 | { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2955 | LY_ERR ret = LY_SUCCESS; |
| 2956 | struct lyxp_expr *dup = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2957 | uint32_t i, j; |
| 2958 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2959 | if (!exp) { |
| 2960 | goto cleanup; |
| 2961 | } |
| 2962 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2963 | dup = calloc(1, sizeof *dup); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2964 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2965 | |
| 2966 | dup->tokens = malloc(exp->used * sizeof *dup->tokens); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2967 | LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2968 | memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens); |
| 2969 | |
| 2970 | dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2971 | LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2972 | memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos); |
| 2973 | |
| 2974 | dup->tok_len = malloc(exp->used * sizeof *dup->tok_len); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2975 | LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2976 | memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len); |
| 2977 | |
| 2978 | dup->repeat = malloc(exp->used * sizeof *dup->repeat); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2979 | LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2980 | for (i = 0; i < exp->used; ++i) { |
| 2981 | if (!exp->repeat[i]) { |
| 2982 | dup->repeat[i] = NULL; |
| 2983 | } else { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2984 | for (j = 0; exp->repeat[i][j]; ++j) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2985 | /* the ending 0 as well */ |
| 2986 | ++j; |
| 2987 | |
Michal Vasko | 99c7164 | 2020-07-03 13:33:36 +0200 | [diff] [blame] | 2988 | dup->repeat[i] = malloc(j * sizeof **dup->repeat); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2989 | LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2990 | memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat); |
| 2991 | dup->repeat[i][j - 1] = 0; |
| 2992 | } |
| 2993 | } |
| 2994 | |
| 2995 | dup->used = exp->used; |
| 2996 | dup->size = exp->used; |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2997 | LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2998 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2999 | cleanup: |
| 3000 | if (ret) { |
| 3001 | lyxp_expr_free(ctx, dup); |
| 3002 | } else { |
| 3003 | *dup_p = dup; |
| 3004 | } |
| 3005 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3006 | } |
| 3007 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3008 | /* |
| 3009 | * warn functions |
| 3010 | * |
| 3011 | * Warn functions check specific reasonable conditions for schema XPath |
| 3012 | * and print a warning if they are not satisfied. |
| 3013 | */ |
| 3014 | |
| 3015 | /** |
| 3016 | * @brief Get the last-added schema node that is currently in the context. |
| 3017 | * |
| 3018 | * @param[in] set Set to search in. |
| 3019 | * @return Last-added schema context node, NULL if no node is in context. |
| 3020 | */ |
| 3021 | static struct lysc_node * |
| 3022 | warn_get_scnode_in_ctx(struct lyxp_set *set) |
| 3023 | { |
| 3024 | uint32_t i; |
| 3025 | |
| 3026 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
| 3027 | return NULL; |
| 3028 | } |
| 3029 | |
| 3030 | i = set->used; |
| 3031 | do { |
| 3032 | --i; |
| 3033 | if (set->val.scnodes[i].in_ctx == 1) { |
| 3034 | /* if there are more, simply return the first found (last added) */ |
| 3035 | return set->val.scnodes[i].scnode; |
| 3036 | } |
| 3037 | } while (i); |
| 3038 | |
| 3039 | return NULL; |
| 3040 | } |
| 3041 | |
| 3042 | /** |
| 3043 | * @brief Test whether a type is numeric - integer type or decimal64. |
| 3044 | * |
| 3045 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3046 | * @return Boolean value whether @p type is numeric type or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3047 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3048 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3049 | warn_is_numeric_type(struct lysc_type *type) |
| 3050 | { |
| 3051 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3052 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3053 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3054 | |
| 3055 | switch (type->basetype) { |
| 3056 | case LY_TYPE_DEC64: |
| 3057 | case LY_TYPE_INT8: |
| 3058 | case LY_TYPE_UINT8: |
| 3059 | case LY_TYPE_INT16: |
| 3060 | case LY_TYPE_UINT16: |
| 3061 | case LY_TYPE_INT32: |
| 3062 | case LY_TYPE_UINT32: |
| 3063 | case LY_TYPE_INT64: |
| 3064 | case LY_TYPE_UINT64: |
| 3065 | return 1; |
| 3066 | case LY_TYPE_UNION: |
| 3067 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3068 | LY_ARRAY_FOR(uni->types, u) { |
| 3069 | ret = warn_is_numeric_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3070 | if (ret) { |
| 3071 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3072 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3073 | } |
| 3074 | } |
| 3075 | /* did not find any suitable type */ |
| 3076 | return 0; |
| 3077 | case LY_TYPE_LEAFREF: |
| 3078 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
| 3079 | default: |
| 3080 | return 0; |
| 3081 | } |
| 3082 | } |
| 3083 | |
| 3084 | /** |
| 3085 | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
| 3086 | * |
| 3087 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3088 | * @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] | 3089 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3090 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3091 | warn_is_string_type(struct lysc_type *type) |
| 3092 | { |
| 3093 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3094 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3095 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3096 | |
| 3097 | switch (type->basetype) { |
| 3098 | case LY_TYPE_BITS: |
| 3099 | case LY_TYPE_ENUM: |
| 3100 | case LY_TYPE_IDENT: |
| 3101 | case LY_TYPE_INST: |
| 3102 | case LY_TYPE_STRING: |
| 3103 | return 1; |
| 3104 | case LY_TYPE_UNION: |
| 3105 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3106 | LY_ARRAY_FOR(uni->types, u) { |
| 3107 | ret = warn_is_string_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3108 | if (ret) { |
| 3109 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3110 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3111 | } |
| 3112 | } |
| 3113 | /* did not find any suitable type */ |
| 3114 | return 0; |
| 3115 | case LY_TYPE_LEAFREF: |
| 3116 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
| 3117 | default: |
| 3118 | return 0; |
| 3119 | } |
| 3120 | } |
| 3121 | |
| 3122 | /** |
| 3123 | * @brief Test whether a type is one specific type. |
| 3124 | * |
| 3125 | * @param[in] type Type to test. |
| 3126 | * @param[in] base Expected type. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3127 | * @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] | 3128 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3129 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3130 | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
| 3131 | { |
| 3132 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3133 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3134 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3135 | |
| 3136 | if (type->basetype == base) { |
| 3137 | return 1; |
| 3138 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3139 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3140 | LY_ARRAY_FOR(uni->types, u) { |
| 3141 | ret = warn_is_specific_type(uni->types[u], base); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3142 | if (ret) { |
| 3143 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3144 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3145 | } |
| 3146 | } |
| 3147 | /* did not find any suitable type */ |
| 3148 | return 0; |
| 3149 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
| 3150 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
| 3151 | } |
| 3152 | |
| 3153 | return 0; |
| 3154 | } |
| 3155 | |
| 3156 | /** |
| 3157 | * @brief Get next type of a (union) type. |
| 3158 | * |
| 3159 | * @param[in] type Base type. |
| 3160 | * @param[in] prev_type Previously returned type. |
| 3161 | * @return Next type or NULL. |
| 3162 | */ |
| 3163 | static struct lysc_type * |
| 3164 | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type) |
| 3165 | { |
| 3166 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3167 | ly_bool found = 0; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3168 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3169 | |
| 3170 | switch (type->basetype) { |
| 3171 | case LY_TYPE_UNION: |
| 3172 | uni = (struct lysc_type_union *)type; |
| 3173 | if (!prev_type) { |
| 3174 | return uni->types[0]; |
| 3175 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3176 | LY_ARRAY_FOR(uni->types, u) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3177 | if (found) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3178 | return uni->types[u]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3179 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3180 | if (prev_type == uni->types[u]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3181 | found = 1; |
| 3182 | } |
| 3183 | } |
| 3184 | return NULL; |
| 3185 | default: |
| 3186 | if (prev_type) { |
| 3187 | assert(type == prev_type); |
| 3188 | return NULL; |
| 3189 | } else { |
| 3190 | return type; |
| 3191 | } |
| 3192 | } |
| 3193 | } |
| 3194 | |
| 3195 | /** |
| 3196 | * @brief Test whether 2 types have a common type. |
| 3197 | * |
| 3198 | * @param[in] type1 First type. |
| 3199 | * @param[in] type2 Second type. |
| 3200 | * @return 1 if they do, 0 otherwise. |
| 3201 | */ |
| 3202 | static int |
| 3203 | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
| 3204 | { |
| 3205 | struct lysc_type *t1, *rt1, *t2, *rt2; |
| 3206 | |
| 3207 | t1 = NULL; |
| 3208 | while ((t1 = warn_is_equal_type_next_type(type1, t1))) { |
| 3209 | if (t1->basetype == LY_TYPE_LEAFREF) { |
| 3210 | rt1 = ((struct lysc_type_leafref *)t1)->realtype; |
| 3211 | } else { |
| 3212 | rt1 = t1; |
| 3213 | } |
| 3214 | |
| 3215 | t2 = NULL; |
| 3216 | while ((t2 = warn_is_equal_type_next_type(type2, t2))) { |
| 3217 | if (t2->basetype == LY_TYPE_LEAFREF) { |
| 3218 | rt2 = ((struct lysc_type_leafref *)t2)->realtype; |
| 3219 | } else { |
| 3220 | rt2 = t2; |
| 3221 | } |
| 3222 | |
| 3223 | if (rt2->basetype == rt1->basetype) { |
| 3224 | /* match found */ |
| 3225 | return 1; |
| 3226 | } |
| 3227 | } |
| 3228 | } |
| 3229 | |
| 3230 | return 0; |
| 3231 | } |
| 3232 | |
| 3233 | /** |
| 3234 | * @brief Check both operands of comparison operators. |
| 3235 | * |
| 3236 | * @param[in] ctx Context for errors. |
| 3237 | * @param[in] set1 First operand set. |
| 3238 | * @param[in] set2 Second operand set. |
| 3239 | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
| 3240 | * @param[in] expr Start of the expression to print with the warning. |
| 3241 | * @param[in] tok_pos Token position. |
| 3242 | */ |
| 3243 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3244 | 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] | 3245 | { |
| 3246 | struct lysc_node_leaf *node1, *node2; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3247 | ly_bool leaves = 1, warning = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3248 | |
| 3249 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
| 3250 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
| 3251 | |
| 3252 | if (!node1 && !node2) { |
| 3253 | /* no node-sets involved, nothing to do */ |
| 3254 | return; |
| 3255 | } |
| 3256 | |
| 3257 | if (node1) { |
| 3258 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3259 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
| 3260 | warning = 1; |
| 3261 | leaves = 0; |
| 3262 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
| 3263 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
| 3264 | warning = 1; |
| 3265 | } |
| 3266 | } |
| 3267 | |
| 3268 | if (node2) { |
| 3269 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3270 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
| 3271 | warning = 1; |
| 3272 | leaves = 0; |
| 3273 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
| 3274 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
| 3275 | warning = 1; |
| 3276 | } |
| 3277 | } |
| 3278 | |
| 3279 | if (node1 && node2 && leaves && !numbers_only) { |
| 3280 | if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) |
| 3281 | || (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) |
| 3282 | || (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) |
| 3283 | && !warn_is_equal_type(node1->type, node2->type))) { |
| 3284 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
| 3285 | warning = 1; |
| 3286 | } |
| 3287 | } |
| 3288 | |
| 3289 | if (warning) { |
| 3290 | LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos); |
| 3291 | } |
| 3292 | } |
| 3293 | |
| 3294 | /** |
| 3295 | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
| 3296 | * |
| 3297 | * @param[in] exp Parsed XPath expression. |
| 3298 | * @param[in] set Set with the leaf/leaf-list. |
| 3299 | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
| 3300 | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
| 3301 | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
| 3302 | */ |
| 3303 | static void |
| 3304 | warn_equality_value(struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, uint16_t last_equal_exp) |
| 3305 | { |
| 3306 | struct lysc_node *scnode; |
| 3307 | struct lysc_type *type; |
| 3308 | char *value; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3309 | struct lyd_value storage; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3310 | LY_ERR rc; |
| 3311 | struct ly_err_item *err = NULL; |
| 3312 | |
| 3313 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 3314 | && ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) { |
| 3315 | /* check that the node can have the specified value */ |
| 3316 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
| 3317 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
| 3318 | } else { |
| 3319 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
| 3320 | } |
| 3321 | if (!value) { |
| 3322 | LOGMEM(set->ctx); |
| 3323 | return; |
| 3324 | } |
| 3325 | |
| 3326 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
| 3327 | LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding" |
| 3328 | " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value); |
| 3329 | 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] | 3330 | (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] | 3331 | exp->expr + exp->tok_pos[equal_exp]); |
| 3332 | } |
| 3333 | |
| 3334 | type = ((struct lysc_node_leaf *)scnode)->type; |
| 3335 | if (type->basetype != LY_TYPE_IDENT) { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 3336 | rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, LY_PREF_SCHEMA, (void *)set->local_mod, |
| 3337 | LYD_HINT_DATA, scnode, &storage, &err); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3338 | |
| 3339 | if (err) { |
| 3340 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
| 3341 | ly_err_free(err); |
| 3342 | } else if (rc != LY_SUCCESS) { |
| 3343 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
| 3344 | } |
| 3345 | if (rc != LY_SUCCESS) { |
| 3346 | 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] | 3347 | (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] | 3348 | exp->expr + exp->tok_pos[equal_exp]); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3349 | } else { |
| 3350 | type->plugin->free(set->ctx, &storage); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3351 | } |
| 3352 | } |
| 3353 | free(value); |
| 3354 | } |
| 3355 | } |
| 3356 | |
| 3357 | /* |
| 3358 | * XPath functions |
| 3359 | */ |
| 3360 | |
| 3361 | /** |
| 3362 | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
| 3363 | * depending on whether the first node bit value from the second argument is set. |
| 3364 | * |
| 3365 | * @param[in] args Array of arguments. |
| 3366 | * @param[in] arg_count Count of elements in @p args. |
| 3367 | * @param[in,out] set Context and result set at the same time. |
| 3368 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3369 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3370 | */ |
| 3371 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3372 | 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] | 3373 | { |
| 3374 | struct lyd_node_term *leaf; |
| 3375 | struct lysc_node_leaf *sleaf; |
| 3376 | struct lysc_type_bits *bits; |
| 3377 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3378 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3379 | |
| 3380 | if (options & LYXP_SCNODE_ALL) { |
| 3381 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3382 | 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] | 3383 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3384 | 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] | 3385 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
| 3386 | 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] | 3387 | } |
| 3388 | |
| 3389 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3390 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3391 | 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] | 3392 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3393 | 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] | 3394 | } |
| 3395 | } |
| 3396 | set_scnode_clear_ctx(set); |
| 3397 | return rc; |
| 3398 | } |
| 3399 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3400 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3401 | 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)"); |
| 3402 | return LY_EVALID; |
| 3403 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3404 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3405 | LY_CHECK_RET(rc); |
| 3406 | |
| 3407 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3408 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3409 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3410 | if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 3411 | && (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) { |
| 3412 | bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3413 | LY_ARRAY_FOR(bits->bits, u) { |
| 3414 | if (!strcmp(bits->bits[u].name, args[1]->val.str)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3415 | set_fill_boolean(set, 1); |
| 3416 | break; |
| 3417 | } |
| 3418 | } |
| 3419 | } |
| 3420 | } |
| 3421 | |
| 3422 | return LY_SUCCESS; |
| 3423 | } |
| 3424 | |
| 3425 | /** |
| 3426 | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
| 3427 | * with the argument converted to boolean. |
| 3428 | * |
| 3429 | * @param[in] args Array of arguments. |
| 3430 | * @param[in] arg_count Count of elements in @p args. |
| 3431 | * @param[in,out] set Context and result set at the same time. |
| 3432 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3433 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3434 | */ |
| 3435 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3436 | 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] | 3437 | { |
| 3438 | LY_ERR rc; |
| 3439 | |
| 3440 | if (options & LYXP_SCNODE_ALL) { |
| 3441 | set_scnode_clear_ctx(set); |
| 3442 | return LY_SUCCESS; |
| 3443 | } |
| 3444 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3445 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3446 | LY_CHECK_RET(rc); |
| 3447 | set_fill_set(set, args[0]); |
| 3448 | |
| 3449 | return LY_SUCCESS; |
| 3450 | } |
| 3451 | |
| 3452 | /** |
| 3453 | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
| 3454 | * with the first argument rounded up to the nearest integer. |
| 3455 | * |
| 3456 | * @param[in] args Array of arguments. |
| 3457 | * @param[in] arg_count Count of elements in @p args. |
| 3458 | * @param[in,out] set Context and result set at the same time. |
| 3459 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3460 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3461 | */ |
| 3462 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3463 | 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] | 3464 | { |
| 3465 | struct lysc_node_leaf *sleaf; |
| 3466 | LY_ERR rc = LY_SUCCESS; |
| 3467 | |
| 3468 | if (options & LYXP_SCNODE_ALL) { |
| 3469 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3470 | 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] | 3471 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3472 | 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] | 3473 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 3474 | 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] | 3475 | } |
| 3476 | set_scnode_clear_ctx(set); |
| 3477 | return rc; |
| 3478 | } |
| 3479 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3480 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3481 | LY_CHECK_RET(rc); |
| 3482 | if ((long long)args[0]->val.num != args[0]->val.num) { |
| 3483 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
| 3484 | } else { |
| 3485 | set_fill_number(set, args[0]->val.num); |
| 3486 | } |
| 3487 | |
| 3488 | return LY_SUCCESS; |
| 3489 | } |
| 3490 | |
| 3491 | /** |
| 3492 | * @brief Execute the XPath concat(string, string, string*) function. |
| 3493 | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
| 3494 | * |
| 3495 | * @param[in] args Array of arguments. |
| 3496 | * @param[in] arg_count Count of elements in @p args. |
| 3497 | * @param[in,out] set Context and result set at the same time. |
| 3498 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3499 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3500 | */ |
| 3501 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3502 | 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] | 3503 | { |
| 3504 | uint16_t i; |
| 3505 | char *str = NULL; |
| 3506 | size_t used = 1; |
| 3507 | LY_ERR rc = LY_SUCCESS; |
| 3508 | struct lysc_node_leaf *sleaf; |
| 3509 | |
| 3510 | if (options & LYXP_SCNODE_ALL) { |
| 3511 | for (i = 0; i < arg_count; ++i) { |
| 3512 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
| 3513 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3514 | LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".", |
| 3515 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3516 | } else if (!warn_is_string_type(sleaf->type)) { |
Radek Krejci | 70124c8 | 2020-08-14 22:17:03 +0200 | [diff] [blame] | 3517 | 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] | 3518 | } |
| 3519 | } |
| 3520 | } |
| 3521 | set_scnode_clear_ctx(set); |
| 3522 | return rc; |
| 3523 | } |
| 3524 | |
| 3525 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3526 | rc = lyxp_set_cast(args[i], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3527 | if (rc != LY_SUCCESS) { |
| 3528 | free(str); |
| 3529 | return rc; |
| 3530 | } |
| 3531 | |
| 3532 | str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char)); |
| 3533 | LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM); |
| 3534 | strcpy(str + used - 1, args[i]->val.str); |
| 3535 | used += strlen(args[i]->val.str); |
| 3536 | } |
| 3537 | |
| 3538 | /* free, kind of */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3539 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3540 | set->type = LYXP_SET_STRING; |
| 3541 | set->val.str = str; |
| 3542 | |
| 3543 | return LY_SUCCESS; |
| 3544 | } |
| 3545 | |
| 3546 | /** |
| 3547 | * @brief Execute the XPath contains(string, string) function. |
| 3548 | * Returns LYXP_SET_BOOLEAN whether the second argument can |
| 3549 | * be found in the first or not. |
| 3550 | * |
| 3551 | * @param[in] args Array of arguments. |
| 3552 | * @param[in] arg_count Count of elements in @p args. |
| 3553 | * @param[in,out] set Context and result set at the same time. |
| 3554 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3555 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3556 | */ |
| 3557 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3558 | 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] | 3559 | { |
| 3560 | struct lysc_node_leaf *sleaf; |
| 3561 | LY_ERR rc = LY_SUCCESS; |
| 3562 | |
| 3563 | if (options & LYXP_SCNODE_ALL) { |
| 3564 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3565 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3566 | 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] | 3567 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3568 | 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] | 3569 | } |
| 3570 | } |
| 3571 | |
| 3572 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3573 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3574 | 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] | 3575 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3576 | 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] | 3577 | } |
| 3578 | } |
| 3579 | set_scnode_clear_ctx(set); |
| 3580 | return rc; |
| 3581 | } |
| 3582 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3583 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3584 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3585 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3586 | LY_CHECK_RET(rc); |
| 3587 | |
| 3588 | if (strstr(args[0]->val.str, args[1]->val.str)) { |
| 3589 | set_fill_boolean(set, 1); |
| 3590 | } else { |
| 3591 | set_fill_boolean(set, 0); |
| 3592 | } |
| 3593 | |
| 3594 | return LY_SUCCESS; |
| 3595 | } |
| 3596 | |
| 3597 | /** |
| 3598 | * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER |
| 3599 | * with the size of the node-set from the argument. |
| 3600 | * |
| 3601 | * @param[in] args Array of arguments. |
| 3602 | * @param[in] arg_count Count of elements in @p args. |
| 3603 | * @param[in,out] set Context and result set at the same time. |
| 3604 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3605 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3606 | */ |
| 3607 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3608 | 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] | 3609 | { |
| 3610 | struct lysc_node *scnode = NULL; |
| 3611 | LY_ERR rc = LY_SUCCESS; |
| 3612 | |
| 3613 | if (options & LYXP_SCNODE_ALL) { |
| 3614 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) { |
| 3615 | 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] | 3616 | } |
| 3617 | set_scnode_clear_ctx(set); |
| 3618 | return rc; |
| 3619 | } |
| 3620 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3621 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 3622 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)"); |
| 3623 | return LY_EVALID; |
| 3624 | } |
| 3625 | |
| 3626 | set_fill_number(set, args[0]->used); |
| 3627 | return LY_SUCCESS; |
| 3628 | } |
| 3629 | |
| 3630 | /** |
| 3631 | * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET |
| 3632 | * with the context with the intial node. |
| 3633 | * |
| 3634 | * @param[in] args Array of arguments. |
| 3635 | * @param[in] arg_count Count of elements in @p args. |
| 3636 | * @param[in,out] set Context and result set at the same time. |
| 3637 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3638 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3639 | */ |
| 3640 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3641 | 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] | 3642 | { |
| 3643 | if (arg_count || args) { |
| 3644 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()"); |
| 3645 | return LY_EVALID; |
| 3646 | } |
| 3647 | |
| 3648 | if (options & LYXP_SCNODE_ALL) { |
| 3649 | set_scnode_clear_ctx(set); |
| 3650 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 3651 | 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] | 3652 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3653 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3654 | |
| 3655 | /* position is filled later */ |
| 3656 | set_insert_node(set, set->ctx_node, 0, LYXP_NODE_ELEM, 0); |
| 3657 | } |
| 3658 | |
| 3659 | return LY_SUCCESS; |
| 3660 | } |
| 3661 | |
| 3662 | /** |
| 3663 | * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either |
| 3664 | * leafref or instance-identifier target node(s). |
| 3665 | * |
| 3666 | * @param[in] args Array of arguments. |
| 3667 | * @param[in] arg_count Count of elements in @p args. |
| 3668 | * @param[in,out] set Context and result set at the same time. |
| 3669 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3670 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3671 | */ |
| 3672 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3673 | 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] | 3674 | { |
| 3675 | struct lyd_node_term *leaf; |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3676 | struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3677 | struct lysc_type_leafref *lref; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3678 | const struct lysc_node *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3679 | struct ly_path *p; |
| 3680 | struct lyd_node *node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3681 | char *errmsg = NULL; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3682 | uint8_t oper; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3683 | LY_ERR rc = LY_SUCCESS; |
| 3684 | |
| 3685 | if (options & LYXP_SCNODE_ALL) { |
| 3686 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3687 | 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] | 3688 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3689 | 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] | 3690 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
| 3691 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".", |
| 3692 | __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3693 | } |
| 3694 | set_scnode_clear_ctx(set); |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3695 | if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3696 | lref = (struct lysc_type_leafref *)sleaf->type; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3697 | 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] | 3698 | |
| 3699 | /* it was already evaluated on schema, it must succeed */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 3700 | rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE, |
Michal Vasko | 72619ce | 2020-10-06 14:05:32 +0200 | [diff] [blame] | 3701 | oper, LY_PATH_TARGET_MANY, set->format, (void *)lref->path_mod, &p); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3702 | assert(!rc); |
| 3703 | |
| 3704 | /* get the target node */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3705 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3706 | ly_path_free(set->ctx, p); |
| 3707 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 3708 | 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] | 3709 | } |
| 3710 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3711 | return rc; |
| 3712 | } |
| 3713 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3714 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3715 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)"); |
| 3716 | return LY_EVALID; |
| 3717 | } |
| 3718 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3719 | lyxp_set_free_content(set); |
| 3720 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3721 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3722 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3723 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 3724 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3725 | /* find leafref target */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3726 | if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf, |
| 3727 | &leaf->value, set->tree, &node, &errmsg)) { |
| 3728 | LOGERR(set->ctx, LY_EVALID, errmsg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3729 | free(errmsg); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3730 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3731 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3732 | } else { |
| 3733 | assert(sleaf->type->basetype == LY_TYPE_INST); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3734 | if (ly_path_eval(leaf->value.target, set->tree, &node)) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3735 | 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] | 3736 | LYD_CANON_VALUE(leaf)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3737 | return LY_EVALID; |
| 3738 | } |
| 3739 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3740 | |
| 3741 | /* insert it */ |
| 3742 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3743 | } |
| 3744 | } |
| 3745 | |
| 3746 | return LY_SUCCESS; |
| 3747 | } |
| 3748 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3749 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3750 | 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] | 3751 | { |
| 3752 | uint16_t i; |
| 3753 | LY_ARRAY_COUNT_TYPE u; |
| 3754 | struct lyd_node_term *leaf; |
| 3755 | struct lysc_node_leaf *sleaf; |
| 3756 | struct lyd_meta *meta; |
| 3757 | struct lyd_value data = {0}, *val; |
| 3758 | struct ly_err_item *err = NULL; |
| 3759 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3760 | ly_bool found; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3761 | |
| 3762 | if (options & LYXP_SCNODE_ALL) { |
| 3763 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3764 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func); |
| 3765 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3766 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
| 3767 | sleaf->name); |
| 3768 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 3769 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name); |
| 3770 | } |
| 3771 | |
| 3772 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3773 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3774 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
| 3775 | sleaf->name); |
| 3776 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3777 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name); |
| 3778 | } |
| 3779 | } |
| 3780 | set_scnode_clear_ctx(set); |
| 3781 | return rc; |
| 3782 | } |
| 3783 | |
| 3784 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 3785 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 3786 | "derived-from(-or-self)(node-set, string)"); |
| 3787 | return LY_EVALID; |
| 3788 | } |
| 3789 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
| 3790 | LY_CHECK_RET(rc); |
| 3791 | |
| 3792 | set_fill_boolean(set, 0); |
| 3793 | found = 0; |
| 3794 | for (i = 0; i < args[0]->used; ++i) { |
| 3795 | if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) { |
| 3796 | continue; |
| 3797 | } |
| 3798 | |
| 3799 | if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 3800 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 3801 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3802 | val = &leaf->value; |
| 3803 | if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) { |
| 3804 | /* uninteresting */ |
| 3805 | continue; |
| 3806 | } |
| 3807 | |
| 3808 | /* store args[1] as ident */ |
| 3809 | rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str), |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 3810 | 0, set->format, (void *)set->local_mod, LYD_HINT_DATA, leaf->schema, &data, &err); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3811 | } else { |
| 3812 | meta = args[0]->val.meta[i].meta; |
| 3813 | val = &meta->value; |
| 3814 | if (val->realtype->basetype != LY_TYPE_IDENT) { |
| 3815 | /* uninteresting */ |
| 3816 | continue; |
| 3817 | } |
| 3818 | |
| 3819 | /* store args[1] as ident */ |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 3820 | rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str), 0, |
| 3821 | set->format, (void *)meta->annotation->module, LYD_HINT_DATA, meta->parent->schema, &data, &err); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3822 | } |
| 3823 | |
| 3824 | if (err) { |
| 3825 | ly_err_print(err); |
| 3826 | ly_err_free(err); |
| 3827 | } |
| 3828 | LY_CHECK_RET(rc); |
| 3829 | |
| 3830 | /* finally check the identity itself */ |
| 3831 | if (self_match && (data.ident == val->ident)) { |
| 3832 | set_fill_boolean(set, 1); |
| 3833 | found = 1; |
| 3834 | } |
| 3835 | if (!found) { |
| 3836 | LY_ARRAY_FOR(data.ident->derived, u) { |
| 3837 | if (data.ident->derived[u] == val->ident) { |
| 3838 | set_fill_boolean(set, 1); |
| 3839 | found = 1; |
| 3840 | break; |
| 3841 | } |
| 3842 | } |
| 3843 | } |
| 3844 | |
| 3845 | /* free temporary value */ |
| 3846 | val->realtype->plugin->free(set->ctx, &data); |
| 3847 | if (found) { |
| 3848 | break; |
| 3849 | } |
| 3850 | } |
| 3851 | |
| 3852 | return LY_SUCCESS; |
| 3853 | } |
| 3854 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3855 | /** |
| 3856 | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3857 | * on whether the first argument nodes contain a node of an identity derived from the second |
| 3858 | * argument identity. |
| 3859 | * |
| 3860 | * @param[in] args Array of arguments. |
| 3861 | * @param[in] arg_count Count of elements in @p args. |
| 3862 | * @param[in,out] set Context and result set at the same time. |
| 3863 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3864 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3865 | */ |
| 3866 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3867 | 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] | 3868 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3869 | return xpath_derived_(args, set, options, 0, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3870 | } |
| 3871 | |
| 3872 | /** |
| 3873 | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3874 | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
| 3875 | * the second argument identity. |
| 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_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] | 3885 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3886 | return xpath_derived_(args, set, options, 1, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3887 | } |
| 3888 | |
| 3889 | /** |
| 3890 | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
| 3891 | * with the integer value of the first node's enum value, otherwise NaN. |
| 3892 | * |
| 3893 | * @param[in] args Array of arguments. |
| 3894 | * @param[in] arg_count Count of elements in @p args. |
| 3895 | * @param[in,out] set Context and result set at the same time. |
| 3896 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3897 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3898 | */ |
| 3899 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3900 | 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] | 3901 | { |
| 3902 | struct lyd_node_term *leaf; |
| 3903 | struct lysc_node_leaf *sleaf; |
| 3904 | LY_ERR rc = LY_SUCCESS; |
| 3905 | |
| 3906 | if (options & LYXP_SCNODE_ALL) { |
| 3907 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3908 | 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] | 3909 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3910 | 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] | 3911 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
| 3912 | 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] | 3913 | } |
| 3914 | set_scnode_clear_ctx(set); |
| 3915 | return rc; |
| 3916 | } |
| 3917 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3918 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3919 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)"); |
| 3920 | return LY_EVALID; |
| 3921 | } |
| 3922 | |
| 3923 | set_fill_number(set, NAN); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3924 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3925 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3926 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3927 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
| 3928 | set_fill_number(set, leaf->value.enum_item->value); |
| 3929 | } |
| 3930 | } |
| 3931 | |
| 3932 | return LY_SUCCESS; |
| 3933 | } |
| 3934 | |
| 3935 | /** |
| 3936 | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
| 3937 | * with false value. |
| 3938 | * |
| 3939 | * @param[in] args Array of arguments. |
| 3940 | * @param[in] arg_count Count of elements in @p args. |
| 3941 | * @param[in,out] set Context and result set at the same time. |
| 3942 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3943 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3944 | */ |
| 3945 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3946 | 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] | 3947 | { |
| 3948 | if (options & LYXP_SCNODE_ALL) { |
| 3949 | set_scnode_clear_ctx(set); |
| 3950 | return LY_SUCCESS; |
| 3951 | } |
| 3952 | |
| 3953 | set_fill_boolean(set, 0); |
| 3954 | return LY_SUCCESS; |
| 3955 | } |
| 3956 | |
| 3957 | /** |
| 3958 | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
| 3959 | * with the first argument floored (truncated). |
| 3960 | * |
| 3961 | * @param[in] args Array of arguments. |
| 3962 | * @param[in] arg_count Count of elements in @p args. |
| 3963 | * @param[in,out] set Context and result set at the same time. |
| 3964 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3965 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3966 | */ |
| 3967 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3968 | 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] | 3969 | { |
| 3970 | LY_ERR rc; |
| 3971 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3972 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3973 | LY_CHECK_RET(rc); |
| 3974 | if (isfinite(args[0]->val.num)) { |
| 3975 | set_fill_number(set, (long long)args[0]->val.num); |
| 3976 | } |
| 3977 | |
| 3978 | return LY_SUCCESS; |
| 3979 | } |
| 3980 | |
| 3981 | /** |
| 3982 | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
| 3983 | * whether the language of the text matches the one from the argument. |
| 3984 | * |
| 3985 | * @param[in] args Array of arguments. |
| 3986 | * @param[in] arg_count Count of elements in @p args. |
| 3987 | * @param[in,out] set Context and result set at the same time. |
| 3988 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3989 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3990 | */ |
| 3991 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3992 | 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] | 3993 | { |
| 3994 | const struct lyd_node *node; |
| 3995 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 3996 | struct lyd_meta *meta = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3997 | const char *val; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3998 | LY_ERR rc = LY_SUCCESS; |
| 3999 | |
| 4000 | if (options & LYXP_SCNODE_ALL) { |
| 4001 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4002 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4003 | 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] | 4004 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4005 | 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] | 4006 | } |
| 4007 | } |
| 4008 | set_scnode_clear_ctx(set); |
| 4009 | return rc; |
| 4010 | } |
| 4011 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4012 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4013 | LY_CHECK_RET(rc); |
| 4014 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4015 | if (set->type != LYXP_SET_NODE_SET) { |
| 4016 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)"); |
| 4017 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4018 | } else if (!set->used) { |
| 4019 | set_fill_boolean(set, 0); |
| 4020 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4021 | } |
| 4022 | |
| 4023 | switch (set->val.nodes[0].type) { |
| 4024 | case LYXP_NODE_ELEM: |
| 4025 | case LYXP_NODE_TEXT: |
| 4026 | node = set->val.nodes[0].node; |
| 4027 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4028 | case LYXP_NODE_META: |
| 4029 | node = set->val.meta[0].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4030 | break; |
| 4031 | default: |
| 4032 | /* nothing to do with roots */ |
| 4033 | set_fill_boolean(set, 0); |
| 4034 | return LY_SUCCESS; |
| 4035 | } |
| 4036 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4037 | /* find lang metadata */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 4038 | for ( ; node; node = (struct lyd_node *)node->parent) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4039 | for (meta = node->meta; meta; meta = meta->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4040 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4041 | 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] | 4042 | break; |
| 4043 | } |
| 4044 | } |
| 4045 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4046 | if (meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4047 | break; |
| 4048 | } |
| 4049 | } |
| 4050 | |
| 4051 | /* compare languages */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4052 | if (!meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4053 | set_fill_boolean(set, 0); |
| 4054 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4055 | uint64_t i; |
| 4056 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4057 | val = meta->value.canonical; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4058 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 4059 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
| 4060 | set_fill_boolean(set, 0); |
| 4061 | break; |
| 4062 | } |
| 4063 | } |
| 4064 | if (!args[0]->val.str[i]) { |
| 4065 | if (!val[i] || (val[i] == '-')) { |
| 4066 | set_fill_boolean(set, 1); |
| 4067 | } else { |
| 4068 | set_fill_boolean(set, 0); |
| 4069 | } |
| 4070 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4071 | } |
| 4072 | |
| 4073 | return LY_SUCCESS; |
| 4074 | } |
| 4075 | |
| 4076 | /** |
| 4077 | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
| 4078 | * with the context size. |
| 4079 | * |
| 4080 | * @param[in] args Array of arguments. |
| 4081 | * @param[in] arg_count Count of elements in @p args. |
| 4082 | * @param[in,out] set Context and result set at the same time. |
| 4083 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4084 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4085 | */ |
| 4086 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4087 | 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] | 4088 | { |
| 4089 | if (options & LYXP_SCNODE_ALL) { |
| 4090 | set_scnode_clear_ctx(set); |
| 4091 | return LY_SUCCESS; |
| 4092 | } |
| 4093 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4094 | if (set->type != LYXP_SET_NODE_SET) { |
| 4095 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()"); |
| 4096 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4097 | } else if (!set->used) { |
| 4098 | set_fill_number(set, 0); |
| 4099 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4100 | } |
| 4101 | |
| 4102 | set_fill_number(set, set->ctx_size); |
| 4103 | return LY_SUCCESS; |
| 4104 | } |
| 4105 | |
| 4106 | /** |
| 4107 | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
| 4108 | * with the node name without namespace from the argument or the context. |
| 4109 | * |
| 4110 | * @param[in] args Array of arguments. |
| 4111 | * @param[in] arg_count Count of elements in @p args. |
| 4112 | * @param[in,out] set Context and result set at the same time. |
| 4113 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4114 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4115 | */ |
| 4116 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4117 | 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] | 4118 | { |
| 4119 | struct lyxp_set_node *item; |
| 4120 | /* suppress unused variable warning */ |
| 4121 | (void)options; |
| 4122 | |
| 4123 | if (options & LYXP_SCNODE_ALL) { |
| 4124 | set_scnode_clear_ctx(set); |
| 4125 | return LY_SUCCESS; |
| 4126 | } |
| 4127 | |
| 4128 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4129 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4130 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "local-name(node-set?)"); |
| 4131 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4132 | } else if (!args[0]->used) { |
| 4133 | set_fill_string(set, "", 0); |
| 4134 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4135 | } |
| 4136 | |
| 4137 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4138 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4139 | |
| 4140 | item = &args[0]->val.nodes[0]; |
| 4141 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4142 | if (set->type != LYXP_SET_NODE_SET) { |
| 4143 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)"); |
| 4144 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4145 | } else if (!set->used) { |
| 4146 | set_fill_string(set, "", 0); |
| 4147 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4148 | } |
| 4149 | |
| 4150 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4151 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4152 | |
| 4153 | item = &set->val.nodes[0]; |
| 4154 | } |
| 4155 | |
| 4156 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4157 | case LYXP_NODE_NONE: |
| 4158 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4159 | case LYXP_NODE_ROOT: |
| 4160 | case LYXP_NODE_ROOT_CONFIG: |
| 4161 | case LYXP_NODE_TEXT: |
| 4162 | set_fill_string(set, "", 0); |
| 4163 | break; |
| 4164 | case LYXP_NODE_ELEM: |
| 4165 | set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name)); |
| 4166 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4167 | case LYXP_NODE_META: |
| 4168 | 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] | 4169 | break; |
| 4170 | } |
| 4171 | |
| 4172 | return LY_SUCCESS; |
| 4173 | } |
| 4174 | |
| 4175 | /** |
| 4176 | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
| 4177 | * with the node name fully qualified (with namespace) from the argument or the context. |
| 4178 | * |
| 4179 | * @param[in] args Array of arguments. |
| 4180 | * @param[in] arg_count Count of elements in @p args. |
| 4181 | * @param[in,out] set Context and result set at the same time. |
| 4182 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4183 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4184 | */ |
| 4185 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4186 | 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] | 4187 | { |
| 4188 | struct lyxp_set_node *item; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4189 | struct lys_module *mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4190 | char *str; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4191 | const char *name = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4192 | |
| 4193 | if (options & LYXP_SCNODE_ALL) { |
| 4194 | set_scnode_clear_ctx(set); |
| 4195 | return LY_SUCCESS; |
| 4196 | } |
| 4197 | |
| 4198 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4199 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4200 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)"); |
| 4201 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4202 | } else if (!args[0]->used) { |
| 4203 | set_fill_string(set, "", 0); |
| 4204 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4205 | } |
| 4206 | |
| 4207 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4208 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4209 | |
| 4210 | item = &args[0]->val.nodes[0]; |
| 4211 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4212 | if (set->type != LYXP_SET_NODE_SET) { |
| 4213 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)"); |
| 4214 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4215 | } else if (!set->used) { |
| 4216 | set_fill_string(set, "", 0); |
| 4217 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4218 | } |
| 4219 | |
| 4220 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4221 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4222 | |
| 4223 | item = &set->val.nodes[0]; |
| 4224 | } |
| 4225 | |
| 4226 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4227 | case LYXP_NODE_NONE: |
| 4228 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4229 | case LYXP_NODE_ROOT: |
| 4230 | case LYXP_NODE_ROOT_CONFIG: |
| 4231 | case LYXP_NODE_TEXT: |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4232 | /* keep NULL */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4233 | break; |
| 4234 | case LYXP_NODE_ELEM: |
| 4235 | mod = item->node->schema->module; |
| 4236 | name = item->node->schema->name; |
| 4237 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4238 | case LYXP_NODE_META: |
| 4239 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
| 4240 | name = ((struct lyd_meta *)item->node)->name; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4241 | break; |
| 4242 | } |
| 4243 | |
| 4244 | if (mod && name) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4245 | int rc = -1; |
| 4246 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4247 | switch (set->format) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 4248 | case LY_PREF_SCHEMA: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4249 | rc = asprintf(&str, "%s:%s", lys_prefix_find_module(set->local_mod, mod), name); |
| 4250 | break; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 4251 | case LY_PREF_JSON: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4252 | rc = asprintf(&str, "%s:%s", mod->name, name); |
| 4253 | break; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 4254 | case LY_PREF_XML: |
Michal Vasko | 9409ef6 | 2019-09-12 11:47:17 +0200 | [diff] [blame] | 4255 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4256 | } |
| 4257 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
| 4258 | set_fill_string(set, str, strlen(str)); |
| 4259 | free(str); |
| 4260 | } else { |
| 4261 | set_fill_string(set, "", 0); |
| 4262 | } |
| 4263 | |
| 4264 | return LY_SUCCESS; |
| 4265 | } |
| 4266 | |
| 4267 | /** |
| 4268 | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
| 4269 | * with the namespace of the node from the argument or the context. |
| 4270 | * |
| 4271 | * @param[in] args Array of arguments. |
| 4272 | * @param[in] arg_count Count of elements in @p args. |
| 4273 | * @param[in,out] set Context and result set at the same time. |
| 4274 | * @param[in] options XPath options. |
| 4275 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4276 | */ |
| 4277 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4278 | 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] | 4279 | { |
| 4280 | struct lyxp_set_node *item; |
| 4281 | struct lys_module *mod; |
| 4282 | /* suppress unused variable warning */ |
| 4283 | (void)options; |
| 4284 | |
| 4285 | if (options & LYXP_SCNODE_ALL) { |
| 4286 | set_scnode_clear_ctx(set); |
| 4287 | return LY_SUCCESS; |
| 4288 | } |
| 4289 | |
| 4290 | if (arg_count) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4291 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4292 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 4293 | "namespace-uri(node-set?)"); |
| 4294 | return LY_EVALID; |
| 4295 | } else if (!args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4296 | set_fill_string(set, "", 0); |
| 4297 | return LY_SUCCESS; |
| 4298 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4299 | |
| 4300 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4301 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4302 | |
| 4303 | item = &args[0]->val.nodes[0]; |
| 4304 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4305 | if (set->type != LYXP_SET_NODE_SET) { |
| 4306 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)"); |
| 4307 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4308 | } else if (!set->used) { |
| 4309 | set_fill_string(set, "", 0); |
| 4310 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4311 | } |
| 4312 | |
| 4313 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4314 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4315 | |
| 4316 | item = &set->val.nodes[0]; |
| 4317 | } |
| 4318 | |
| 4319 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4320 | case LYXP_NODE_NONE: |
| 4321 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4322 | case LYXP_NODE_ROOT: |
| 4323 | case LYXP_NODE_ROOT_CONFIG: |
| 4324 | case LYXP_NODE_TEXT: |
| 4325 | set_fill_string(set, "", 0); |
| 4326 | break; |
| 4327 | case LYXP_NODE_ELEM: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4328 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4329 | if (item->type == LYXP_NODE_ELEM) { |
| 4330 | mod = item->node->schema->module; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4331 | } else { /* LYXP_NODE_META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4332 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4333 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4334 | } |
| 4335 | |
| 4336 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
| 4337 | break; |
| 4338 | } |
| 4339 | |
| 4340 | return LY_SUCCESS; |
| 4341 | } |
| 4342 | |
| 4343 | /** |
| 4344 | * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET |
| 4345 | * with only nodes from the context. In practice it either leaves the context |
| 4346 | * as it is or returns an empty node set. |
| 4347 | * |
| 4348 | * @param[in] args Array of arguments. |
| 4349 | * @param[in] arg_count Count of elements in @p args. |
| 4350 | * @param[in,out] set Context and result set at the same time. |
| 4351 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4352 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4353 | */ |
| 4354 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4355 | 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] | 4356 | { |
| 4357 | if (options & LYXP_SCNODE_ALL) { |
| 4358 | set_scnode_clear_ctx(set); |
| 4359 | return LY_SUCCESS; |
| 4360 | } |
| 4361 | |
| 4362 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4363 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4364 | } |
| 4365 | return LY_SUCCESS; |
| 4366 | } |
| 4367 | |
| 4368 | /** |
| 4369 | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
| 4370 | * with normalized value (no leading, trailing, double white spaces) of the node |
| 4371 | * from the argument or the context. |
| 4372 | * |
| 4373 | * @param[in] args Array of arguments. |
| 4374 | * @param[in] arg_count Count of elements in @p args. |
| 4375 | * @param[in,out] set Context and result set at the same time. |
| 4376 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4377 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4378 | */ |
| 4379 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4380 | 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] | 4381 | { |
| 4382 | uint16_t i, new_used; |
| 4383 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4384 | ly_bool have_spaces = 0, space_before = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4385 | struct lysc_node_leaf *sleaf; |
| 4386 | LY_ERR rc = LY_SUCCESS; |
| 4387 | |
| 4388 | if (options & LYXP_SCNODE_ALL) { |
| 4389 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4390 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4391 | 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] | 4392 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4393 | 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] | 4394 | } |
| 4395 | } |
| 4396 | set_scnode_clear_ctx(set); |
| 4397 | return rc; |
| 4398 | } |
| 4399 | |
| 4400 | if (arg_count) { |
| 4401 | set_fill_set(set, args[0]); |
| 4402 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4403 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4404 | LY_CHECK_RET(rc); |
| 4405 | |
| 4406 | /* is there any normalization necessary? */ |
| 4407 | for (i = 0; set->val.str[i]; ++i) { |
| 4408 | if (is_xmlws(set->val.str[i])) { |
| 4409 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
| 4410 | have_spaces = 1; |
| 4411 | break; |
| 4412 | } |
| 4413 | space_before = 1; |
| 4414 | } else { |
| 4415 | space_before = 0; |
| 4416 | } |
| 4417 | } |
| 4418 | |
| 4419 | /* yep, there is */ |
| 4420 | if (have_spaces) { |
| 4421 | /* it's enough, at least one character will go, makes space for ending '\0' */ |
| 4422 | new = malloc(strlen(set->val.str) * sizeof(char)); |
| 4423 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4424 | new_used = 0; |
| 4425 | |
| 4426 | space_before = 0; |
| 4427 | for (i = 0; set->val.str[i]; ++i) { |
| 4428 | if (is_xmlws(set->val.str[i])) { |
| 4429 | if ((i == 0) || space_before) { |
| 4430 | space_before = 1; |
| 4431 | continue; |
| 4432 | } else { |
| 4433 | space_before = 1; |
| 4434 | } |
| 4435 | } else { |
| 4436 | space_before = 0; |
| 4437 | } |
| 4438 | |
| 4439 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
| 4440 | ++new_used; |
| 4441 | } |
| 4442 | |
| 4443 | /* at worst there is one trailing space now */ |
| 4444 | if (new_used && is_xmlws(new[new_used - 1])) { |
| 4445 | --new_used; |
| 4446 | } |
| 4447 | |
| 4448 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 4449 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4450 | new[new_used] = '\0'; |
| 4451 | |
| 4452 | free(set->val.str); |
| 4453 | set->val.str = new; |
| 4454 | } |
| 4455 | |
| 4456 | return LY_SUCCESS; |
| 4457 | } |
| 4458 | |
| 4459 | /** |
| 4460 | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
| 4461 | * with the argument converted to boolean and logically inverted. |
| 4462 | * |
| 4463 | * @param[in] args Array of arguments. |
| 4464 | * @param[in] arg_count Count of elements in @p args. |
| 4465 | * @param[in,out] set Context and result set at the same time. |
| 4466 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4467 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4468 | */ |
| 4469 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4470 | 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] | 4471 | { |
| 4472 | if (options & LYXP_SCNODE_ALL) { |
| 4473 | set_scnode_clear_ctx(set); |
| 4474 | return LY_SUCCESS; |
| 4475 | } |
| 4476 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4477 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4478 | if (args[0]->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4479 | set_fill_boolean(set, 0); |
| 4480 | } else { |
| 4481 | set_fill_boolean(set, 1); |
| 4482 | } |
| 4483 | |
| 4484 | return LY_SUCCESS; |
| 4485 | } |
| 4486 | |
| 4487 | /** |
| 4488 | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
| 4489 | * with the number representation of either the argument or the context. |
| 4490 | * |
| 4491 | * @param[in] args Array of arguments. |
| 4492 | * @param[in] arg_count Count of elements in @p args. |
| 4493 | * @param[in,out] set Context and result set at the same time. |
| 4494 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4495 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4496 | */ |
| 4497 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4498 | 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] | 4499 | { |
| 4500 | LY_ERR rc; |
| 4501 | |
| 4502 | if (options & LYXP_SCNODE_ALL) { |
| 4503 | set_scnode_clear_ctx(set); |
| 4504 | return LY_SUCCESS; |
| 4505 | } |
| 4506 | |
| 4507 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4508 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4509 | LY_CHECK_RET(rc); |
| 4510 | set_fill_set(set, args[0]); |
| 4511 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4512 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4513 | LY_CHECK_RET(rc); |
| 4514 | } |
| 4515 | |
| 4516 | return LY_SUCCESS; |
| 4517 | } |
| 4518 | |
| 4519 | /** |
| 4520 | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
| 4521 | * with the context position. |
| 4522 | * |
| 4523 | * @param[in] args Array of arguments. |
| 4524 | * @param[in] arg_count Count of elements in @p args. |
| 4525 | * @param[in,out] set Context and result set at the same time. |
| 4526 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4527 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4528 | */ |
| 4529 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4530 | 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] | 4531 | { |
| 4532 | if (options & LYXP_SCNODE_ALL) { |
| 4533 | set_scnode_clear_ctx(set); |
| 4534 | return LY_SUCCESS; |
| 4535 | } |
| 4536 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4537 | if (set->type != LYXP_SET_NODE_SET) { |
| 4538 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()"); |
| 4539 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4540 | } else if (!set->used) { |
| 4541 | set_fill_number(set, 0); |
| 4542 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4543 | } |
| 4544 | |
| 4545 | set_fill_number(set, set->ctx_pos); |
| 4546 | |
| 4547 | /* UNUSED in 'Release' build type */ |
| 4548 | (void)options; |
| 4549 | return LY_SUCCESS; |
| 4550 | } |
| 4551 | |
| 4552 | /** |
| 4553 | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
| 4554 | * depending on whether the second argument regex matches the first argument string. For details refer to |
| 4555 | * YANG 1.1 RFC section 10.2.1. |
| 4556 | * |
| 4557 | * @param[in] args Array of arguments. |
| 4558 | * @param[in] arg_count Count of elements in @p args. |
| 4559 | * @param[in,out] set Context and result set at the same time. |
| 4560 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4561 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4562 | */ |
| 4563 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4564 | 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] | 4565 | { |
| 4566 | struct lysc_pattern **patterns = NULL, **pattern; |
| 4567 | struct lysc_node_leaf *sleaf; |
| 4568 | char *path; |
| 4569 | LY_ERR rc = LY_SUCCESS; |
| 4570 | struct ly_err_item *err; |
| 4571 | |
| 4572 | if (options & LYXP_SCNODE_ALL) { |
| 4573 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4574 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4575 | 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] | 4576 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4577 | 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] | 4578 | } |
| 4579 | } |
| 4580 | |
| 4581 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4582 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4583 | 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] | 4584 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4585 | 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] | 4586 | } |
| 4587 | } |
| 4588 | set_scnode_clear_ctx(set); |
| 4589 | return rc; |
| 4590 | } |
| 4591 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4592 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4593 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4594 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4595 | LY_CHECK_RET(rc); |
| 4596 | |
| 4597 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
| 4598 | *pattern = malloc(sizeof **pattern); |
| 4599 | path = lyd_path(set->ctx_node, LYD_PATH_LOG, NULL, 0); |
| 4600 | rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code); |
| 4601 | free(path); |
| 4602 | if (rc != LY_SUCCESS) { |
| 4603 | LY_ARRAY_FREE(patterns); |
| 4604 | return rc; |
| 4605 | } |
| 4606 | |
| 4607 | rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err); |
| 4608 | pcre2_code_free((*pattern)->code); |
| 4609 | free(*pattern); |
| 4610 | LY_ARRAY_FREE(patterns); |
| 4611 | if (rc && (rc != LY_EVALID)) { |
| 4612 | ly_err_print(err); |
| 4613 | ly_err_free(err); |
| 4614 | return rc; |
| 4615 | } |
| 4616 | |
| 4617 | if (rc == LY_EVALID) { |
| 4618 | ly_err_free(err); |
| 4619 | set_fill_boolean(set, 0); |
| 4620 | } else { |
| 4621 | set_fill_boolean(set, 1); |
| 4622 | } |
| 4623 | |
| 4624 | return LY_SUCCESS; |
| 4625 | } |
| 4626 | |
| 4627 | /** |
| 4628 | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
| 4629 | * with the rounded first argument. For details refer to |
| 4630 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
| 4631 | * |
| 4632 | * @param[in] args Array of arguments. |
| 4633 | * @param[in] arg_count Count of elements in @p args. |
| 4634 | * @param[in,out] set Context and result set at the same time. |
| 4635 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4636 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4637 | */ |
| 4638 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4639 | 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] | 4640 | { |
| 4641 | struct lysc_node_leaf *sleaf; |
| 4642 | LY_ERR rc = LY_SUCCESS; |
| 4643 | |
| 4644 | if (options & LYXP_SCNODE_ALL) { |
| 4645 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4646 | 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] | 4647 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4648 | 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] | 4649 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4650 | 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] | 4651 | } |
| 4652 | set_scnode_clear_ctx(set); |
| 4653 | return rc; |
| 4654 | } |
| 4655 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4656 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4657 | LY_CHECK_RET(rc); |
| 4658 | |
| 4659 | /* cover only the cases where floor can't be used */ |
| 4660 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
| 4661 | set_fill_number(set, -0.0f); |
| 4662 | } else { |
| 4663 | args[0]->val.num += 0.5; |
| 4664 | rc = xpath_floor(args, 1, args[0], options); |
| 4665 | LY_CHECK_RET(rc); |
| 4666 | set_fill_number(set, args[0]->val.num); |
| 4667 | } |
| 4668 | |
| 4669 | return LY_SUCCESS; |
| 4670 | } |
| 4671 | |
| 4672 | /** |
| 4673 | * @brief Execute the XPath starts-with(string, string) function. |
| 4674 | * Returns LYXP_SET_BOOLEAN whether the second argument is |
| 4675 | * the prefix of the first or not. |
| 4676 | * |
| 4677 | * @param[in] args Array of arguments. |
| 4678 | * @param[in] arg_count Count of elements in @p args. |
| 4679 | * @param[in,out] set Context and result set at the same time. |
| 4680 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4681 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4682 | */ |
| 4683 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4684 | 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] | 4685 | { |
| 4686 | struct lysc_node_leaf *sleaf; |
| 4687 | LY_ERR rc = LY_SUCCESS; |
| 4688 | |
| 4689 | if (options & LYXP_SCNODE_ALL) { |
| 4690 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4691 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4692 | 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] | 4693 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4694 | 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] | 4695 | } |
| 4696 | } |
| 4697 | |
| 4698 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4699 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4700 | 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] | 4701 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4702 | 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] | 4703 | } |
| 4704 | } |
| 4705 | set_scnode_clear_ctx(set); |
| 4706 | return rc; |
| 4707 | } |
| 4708 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4709 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4710 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4711 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4712 | LY_CHECK_RET(rc); |
| 4713 | |
| 4714 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
| 4715 | set_fill_boolean(set, 0); |
| 4716 | } else { |
| 4717 | set_fill_boolean(set, 1); |
| 4718 | } |
| 4719 | |
| 4720 | return LY_SUCCESS; |
| 4721 | } |
| 4722 | |
| 4723 | /** |
| 4724 | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
| 4725 | * with the string representation of either the argument or the context. |
| 4726 | * |
| 4727 | * @param[in] args Array of arguments. |
| 4728 | * @param[in] arg_count Count of elements in @p args. |
| 4729 | * @param[in,out] set Context and result set at the same time. |
| 4730 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4731 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4732 | */ |
| 4733 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4734 | 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] | 4735 | { |
| 4736 | LY_ERR rc; |
| 4737 | |
| 4738 | if (options & LYXP_SCNODE_ALL) { |
| 4739 | set_scnode_clear_ctx(set); |
| 4740 | return LY_SUCCESS; |
| 4741 | } |
| 4742 | |
| 4743 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4744 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4745 | LY_CHECK_RET(rc); |
| 4746 | set_fill_set(set, args[0]); |
| 4747 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4748 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4749 | LY_CHECK_RET(rc); |
| 4750 | } |
| 4751 | |
| 4752 | return LY_SUCCESS; |
| 4753 | } |
| 4754 | |
| 4755 | /** |
| 4756 | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
| 4757 | * with the length of the string in either the argument or the context. |
| 4758 | * |
| 4759 | * @param[in] args Array of arguments. |
| 4760 | * @param[in] arg_count Count of elements in @p args. |
| 4761 | * @param[in,out] set Context and result set at the same time. |
| 4762 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4763 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4764 | */ |
| 4765 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4766 | 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] | 4767 | { |
| 4768 | struct lysc_node_leaf *sleaf; |
| 4769 | LY_ERR rc = LY_SUCCESS; |
| 4770 | |
| 4771 | if (options & LYXP_SCNODE_ALL) { |
| 4772 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4773 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4774 | 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] | 4775 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4776 | 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] | 4777 | } |
| 4778 | } |
| 4779 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
| 4780 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4781 | 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] | 4782 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4783 | 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] | 4784 | } |
| 4785 | } |
| 4786 | set_scnode_clear_ctx(set); |
| 4787 | return rc; |
| 4788 | } |
| 4789 | |
| 4790 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4791 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4792 | LY_CHECK_RET(rc); |
| 4793 | set_fill_number(set, strlen(args[0]->val.str)); |
| 4794 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4795 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4796 | LY_CHECK_RET(rc); |
| 4797 | set_fill_number(set, strlen(set->val.str)); |
| 4798 | } |
| 4799 | |
| 4800 | return LY_SUCCESS; |
| 4801 | } |
| 4802 | |
| 4803 | /** |
| 4804 | * @brief Execute the XPath substring(string, number, number?) function. |
| 4805 | * Returns LYXP_SET_STRING substring of the first argument starting |
| 4806 | * on the second argument index ending on the third argument index, |
| 4807 | * indexed from 1. For exact definition refer to |
| 4808 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
| 4809 | * |
| 4810 | * @param[in] args Array of arguments. |
| 4811 | * @param[in] arg_count Count of elements in @p args. |
| 4812 | * @param[in,out] set Context and result set at the same time. |
| 4813 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4814 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4815 | */ |
| 4816 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4817 | 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] | 4818 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4819 | int32_t start, len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4820 | uint16_t str_start, str_len, pos; |
| 4821 | struct lysc_node_leaf *sleaf; |
| 4822 | LY_ERR rc = LY_SUCCESS; |
| 4823 | |
| 4824 | if (options & LYXP_SCNODE_ALL) { |
| 4825 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4826 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4827 | 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] | 4828 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4829 | 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] | 4830 | } |
| 4831 | } |
| 4832 | |
| 4833 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4834 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4835 | 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] | 4836 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4837 | 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] | 4838 | } |
| 4839 | } |
| 4840 | |
| 4841 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 4842 | && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4843 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4844 | 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] | 4845 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4846 | 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] | 4847 | } |
| 4848 | } |
| 4849 | set_scnode_clear_ctx(set); |
| 4850 | return rc; |
| 4851 | } |
| 4852 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4853 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4854 | LY_CHECK_RET(rc); |
| 4855 | |
| 4856 | /* start */ |
| 4857 | if (xpath_round(&args[1], 1, args[1], options)) { |
| 4858 | return -1; |
| 4859 | } |
| 4860 | if (isfinite(args[1]->val.num)) { |
| 4861 | start = args[1]->val.num - 1; |
| 4862 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4863 | start = INT32_MIN; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4864 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4865 | start = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4866 | } |
| 4867 | |
| 4868 | /* len */ |
| 4869 | if (arg_count == 3) { |
| 4870 | rc = xpath_round(&args[2], 1, args[2], options); |
| 4871 | LY_CHECK_RET(rc); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4872 | if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4873 | len = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4874 | } else if (isfinite(args[2]->val.num)) { |
| 4875 | len = args[2]->val.num; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4876 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4877 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4878 | } |
| 4879 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4880 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4881 | } |
| 4882 | |
| 4883 | /* find matching character positions */ |
| 4884 | str_start = 0; |
| 4885 | str_len = 0; |
| 4886 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
| 4887 | if (pos < start) { |
| 4888 | ++str_start; |
| 4889 | } else if (pos < start + len) { |
| 4890 | ++str_len; |
| 4891 | } else { |
| 4892 | break; |
| 4893 | } |
| 4894 | } |
| 4895 | |
| 4896 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
| 4897 | return LY_SUCCESS; |
| 4898 | } |
| 4899 | |
| 4900 | /** |
| 4901 | * @brief Execute the XPath substring-after(string, string) function. |
| 4902 | * Returns LYXP_SET_STRING with the string succeeding the occurance |
| 4903 | * of the second argument in the first or an empty string. |
| 4904 | * |
| 4905 | * @param[in] args Array of arguments. |
| 4906 | * @param[in] arg_count Count of elements in @p args. |
| 4907 | * @param[in,out] set Context and result set at the same time. |
| 4908 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4909 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4910 | */ |
| 4911 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4912 | 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] | 4913 | { |
| 4914 | char *ptr; |
| 4915 | struct lysc_node_leaf *sleaf; |
| 4916 | LY_ERR rc = LY_SUCCESS; |
| 4917 | |
| 4918 | if (options & LYXP_SCNODE_ALL) { |
| 4919 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4920 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4921 | 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] | 4922 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4923 | 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] | 4924 | } |
| 4925 | } |
| 4926 | |
| 4927 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4928 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4929 | 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] | 4930 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4931 | 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] | 4932 | } |
| 4933 | } |
| 4934 | set_scnode_clear_ctx(set); |
| 4935 | return rc; |
| 4936 | } |
| 4937 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4938 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4939 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4940 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4941 | LY_CHECK_RET(rc); |
| 4942 | |
| 4943 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4944 | if (ptr) { |
| 4945 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
| 4946 | } else { |
| 4947 | set_fill_string(set, "", 0); |
| 4948 | } |
| 4949 | |
| 4950 | return LY_SUCCESS; |
| 4951 | } |
| 4952 | |
| 4953 | /** |
| 4954 | * @brief Execute the XPath substring-before(string, string) function. |
| 4955 | * Returns LYXP_SET_STRING with the string preceding the occurance |
| 4956 | * of the second argument in the first or an empty string. |
| 4957 | * |
| 4958 | * @param[in] args Array of arguments. |
| 4959 | * @param[in] arg_count Count of elements in @p args. |
| 4960 | * @param[in,out] set Context and result set at the same time. |
| 4961 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4962 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4963 | */ |
| 4964 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4965 | 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] | 4966 | { |
| 4967 | char *ptr; |
| 4968 | struct lysc_node_leaf *sleaf; |
| 4969 | LY_ERR rc = LY_SUCCESS; |
| 4970 | |
| 4971 | if (options & LYXP_SCNODE_ALL) { |
| 4972 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4973 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4974 | 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] | 4975 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4976 | 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] | 4977 | } |
| 4978 | } |
| 4979 | |
| 4980 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4981 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4982 | 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] | 4983 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4984 | 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] | 4985 | } |
| 4986 | } |
| 4987 | set_scnode_clear_ctx(set); |
| 4988 | return rc; |
| 4989 | } |
| 4990 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4991 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4992 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4993 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4994 | LY_CHECK_RET(rc); |
| 4995 | |
| 4996 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4997 | if (ptr) { |
| 4998 | set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str); |
| 4999 | } else { |
| 5000 | set_fill_string(set, "", 0); |
| 5001 | } |
| 5002 | |
| 5003 | return LY_SUCCESS; |
| 5004 | } |
| 5005 | |
| 5006 | /** |
| 5007 | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
| 5008 | * with the sum of all the nodes in the context. |
| 5009 | * |
| 5010 | * @param[in] args Array of arguments. |
| 5011 | * @param[in] arg_count Count of elements in @p args. |
| 5012 | * @param[in,out] set Context and result set at the same time. |
| 5013 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5014 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5015 | */ |
| 5016 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5017 | 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] | 5018 | { |
| 5019 | long double num; |
| 5020 | char *str; |
| 5021 | uint16_t i; |
| 5022 | struct lyxp_set set_item; |
| 5023 | struct lysc_node_leaf *sleaf; |
| 5024 | LY_ERR rc = LY_SUCCESS; |
| 5025 | |
| 5026 | if (options & LYXP_SCNODE_ALL) { |
| 5027 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
| 5028 | for (i = 0; i < args[0]->used; ++i) { |
| 5029 | if (args[0]->val.scnodes[i].in_ctx == 1) { |
| 5030 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
| 5031 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5032 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, |
| 5033 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5034 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5035 | 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] | 5036 | } |
| 5037 | } |
| 5038 | } |
| 5039 | } |
| 5040 | set_scnode_clear_ctx(set); |
| 5041 | return rc; |
| 5042 | } |
| 5043 | |
| 5044 | set_fill_number(set, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5045 | |
| 5046 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 5047 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)"); |
| 5048 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5049 | } else if (!args[0]->used) { |
| 5050 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5051 | } |
| 5052 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5053 | set_init(&set_item, set); |
| 5054 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5055 | set_item.type = LYXP_SET_NODE_SET; |
| 5056 | set_item.val.nodes = malloc(sizeof *set_item.val.nodes); |
| 5057 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
| 5058 | |
| 5059 | set_item.used = 1; |
| 5060 | set_item.size = 1; |
| 5061 | |
| 5062 | for (i = 0; i < args[0]->used; ++i) { |
| 5063 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
| 5064 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5065 | rc = cast_node_set_to_string(&set_item, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5066 | LY_CHECK_RET(rc); |
| 5067 | num = cast_string_to_number(str); |
| 5068 | free(str); |
| 5069 | set->val.num += num; |
| 5070 | } |
| 5071 | |
| 5072 | free(set_item.val.nodes); |
| 5073 | |
| 5074 | return LY_SUCCESS; |
| 5075 | } |
| 5076 | |
| 5077 | /** |
| 5078 | * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET |
| 5079 | * with the text content of the nodes in the context. |
| 5080 | * |
| 5081 | * @param[in] args Array of arguments. |
| 5082 | * @param[in] arg_count Count of elements in @p args. |
| 5083 | * @param[in,out] set Context and result set at the same time. |
| 5084 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5085 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5086 | */ |
| 5087 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5088 | 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] | 5089 | { |
| 5090 | uint32_t i; |
| 5091 | |
| 5092 | if (options & LYXP_SCNODE_ALL) { |
| 5093 | set_scnode_clear_ctx(set); |
| 5094 | return LY_SUCCESS; |
| 5095 | } |
| 5096 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5097 | if (set->type != LYXP_SET_NODE_SET) { |
| 5098 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()"); |
| 5099 | return LY_EVALID; |
| 5100 | } |
| 5101 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 5102 | for (i = 0; i < set->used; ) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5103 | switch (set->val.nodes[i].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 5104 | case LYXP_NODE_NONE: |
| 5105 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5106 | case LYXP_NODE_ELEM: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5107 | if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 5108 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
| 5109 | ++i; |
| 5110 | break; |
| 5111 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5112 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5113 | case LYXP_NODE_ROOT: |
| 5114 | case LYXP_NODE_ROOT_CONFIG: |
| 5115 | case LYXP_NODE_TEXT: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5116 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5117 | set_remove_node(set, i); |
| 5118 | break; |
| 5119 | } |
| 5120 | } |
| 5121 | |
| 5122 | return LY_SUCCESS; |
| 5123 | } |
| 5124 | |
| 5125 | /** |
| 5126 | * @brief Execute the XPath translate(string, string, string) function. |
| 5127 | * Returns LYXP_SET_STRING with the first argument with the characters |
| 5128 | * from the second argument replaced by those on the corresponding |
| 5129 | * positions in the third argument. |
| 5130 | * |
| 5131 | * @param[in] args Array of arguments. |
| 5132 | * @param[in] arg_count Count of elements in @p args. |
| 5133 | * @param[in,out] set Context and result set at the same time. |
| 5134 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5135 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5136 | */ |
| 5137 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5138 | 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] | 5139 | { |
| 5140 | uint16_t i, j, new_used; |
| 5141 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5142 | ly_bool have_removed; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5143 | struct lysc_node_leaf *sleaf; |
| 5144 | LY_ERR rc = LY_SUCCESS; |
| 5145 | |
| 5146 | if (options & LYXP_SCNODE_ALL) { |
| 5147 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5148 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5149 | 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] | 5150 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5151 | 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] | 5152 | } |
| 5153 | } |
| 5154 | |
| 5155 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5156 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5157 | 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] | 5158 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5159 | 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] | 5160 | } |
| 5161 | } |
| 5162 | |
| 5163 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 5164 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5165 | 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] | 5166 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5167 | 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] | 5168 | } |
| 5169 | } |
| 5170 | set_scnode_clear_ctx(set); |
| 5171 | return rc; |
| 5172 | } |
| 5173 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5174 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5175 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5176 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5177 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5178 | rc = lyxp_set_cast(args[2], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5179 | LY_CHECK_RET(rc); |
| 5180 | |
| 5181 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
| 5182 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5183 | new_used = 0; |
| 5184 | |
| 5185 | have_removed = 0; |
| 5186 | for (i = 0; args[0]->val.str[i]; ++i) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5187 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5188 | |
| 5189 | for (j = 0; args[1]->val.str[j]; ++j) { |
| 5190 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
| 5191 | /* removing this char */ |
| 5192 | if (j >= strlen(args[2]->val.str)) { |
| 5193 | have_removed = 1; |
| 5194 | found = 1; |
| 5195 | break; |
| 5196 | } |
| 5197 | /* replacing this char */ |
| 5198 | new[new_used] = args[2]->val.str[j]; |
| 5199 | ++new_used; |
| 5200 | found = 1; |
| 5201 | break; |
| 5202 | } |
| 5203 | } |
| 5204 | |
| 5205 | /* copying this char */ |
| 5206 | if (!found) { |
| 5207 | new[new_used] = args[0]->val.str[i]; |
| 5208 | ++new_used; |
| 5209 | } |
| 5210 | } |
| 5211 | |
| 5212 | if (have_removed) { |
| 5213 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 5214 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5215 | } |
| 5216 | new[new_used] = '\0'; |
| 5217 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5218 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5219 | set->type = LYXP_SET_STRING; |
| 5220 | set->val.str = new; |
| 5221 | |
| 5222 | return LY_SUCCESS; |
| 5223 | } |
| 5224 | |
| 5225 | /** |
| 5226 | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
| 5227 | * with true value. |
| 5228 | * |
| 5229 | * @param[in] args Array of arguments. |
| 5230 | * @param[in] arg_count Count of elements in @p args. |
| 5231 | * @param[in,out] set Context and result set at the same time. |
| 5232 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5233 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5234 | */ |
| 5235 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5236 | 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] | 5237 | { |
| 5238 | if (options & LYXP_SCNODE_ALL) { |
| 5239 | set_scnode_clear_ctx(set); |
| 5240 | return LY_SUCCESS; |
| 5241 | } |
| 5242 | |
| 5243 | set_fill_boolean(set, 1); |
| 5244 | return LY_SUCCESS; |
| 5245 | } |
| 5246 | |
| 5247 | /* |
| 5248 | * moveto functions |
| 5249 | * |
| 5250 | * They and only they actually change the context (set). |
| 5251 | */ |
| 5252 | |
| 5253 | /** |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5254 | * @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] | 5255 | * |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5256 | * XPath @p set is expected to be a (sc)node set! |
| 5257 | * |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5258 | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
| 5259 | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
| 5260 | * @param[in] set Set with XPath context. |
| 5261 | * @param[out] moveto_mod Expected module of a matching node. |
| 5262 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5263 | */ |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5264 | static LY_ERR |
| 5265 | 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] | 5266 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 5267 | const struct lys_module *mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5268 | const char *ptr; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5269 | size_t pref_len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5270 | |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5271 | assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET)); |
| 5272 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5273 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
| 5274 | /* specific module */ |
| 5275 | pref_len = ptr - *qname; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 5276 | 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] | 5277 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5278 | /* check for errors and non-implemented modules, as they are not valid */ |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5279 | if (!mod || !mod->implemented) { |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5280 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 5281 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INMOD, pref_len, *qname); |
| 5282 | } else { |
| 5283 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INMOD, pref_len, *qname); |
| 5284 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5285 | return LY_EVALID; |
| 5286 | } |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5287 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5288 | *qname += pref_len + 1; |
| 5289 | *qname_len -= pref_len + 1; |
| 5290 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
| 5291 | /* all modules - special case */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5292 | mod = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5293 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 5294 | /* current node module */ |
| 5295 | mod = set->ctx_scnode->module; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5296 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5297 | /* current node module */ |
| 5298 | mod = set->ctx_node->schema->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5299 | } |
| 5300 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5301 | *moveto_mod = mod; |
| 5302 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5303 | } |
| 5304 | |
| 5305 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5306 | * @brief Move context @p set to the root. Handles absolute path. |
| 5307 | * Result is LYXP_SET_NODE_SET. |
| 5308 | * |
| 5309 | * @param[in,out] set Set to use. |
| 5310 | * @param[in] options Xpath options. |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5311 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5312 | */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5313 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5314 | moveto_root(struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5315 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5316 | if (!set) { |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5317 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5318 | } |
| 5319 | |
| 5320 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5321 | set_scnode_clear_ctx(set); |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5322 | 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] | 5323 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5324 | set->type = LYXP_SET_NODE_SET; |
| 5325 | set->used = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5326 | set_insert_node(set, NULL, 0, set->root_type, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5327 | } |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5328 | |
| 5329 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5330 | } |
| 5331 | |
| 5332 | /** |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5333 | * @brief Check whether a node has some unresolved "when". |
| 5334 | * |
| 5335 | * @param[in] node Node to check. |
| 5336 | * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when") |
| 5337 | */ |
| 5338 | static LY_ERR |
| 5339 | moveto_when_check(const struct lyd_node *node) |
| 5340 | { |
| 5341 | const struct lysc_node *schema; |
| 5342 | |
| 5343 | if (!node) { |
| 5344 | return LY_SUCCESS; |
| 5345 | } |
| 5346 | |
| 5347 | schema = node->schema; |
| 5348 | do { |
| 5349 | if (schema->when && !(node->flags & LYD_WHEN_TRUE)) { |
| 5350 | return LY_EINCOMPLETE; |
| 5351 | } |
| 5352 | schema = schema->parent; |
| 5353 | } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 5354 | |
| 5355 | return LY_SUCCESS; |
| 5356 | } |
| 5357 | |
| 5358 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5359 | * @brief Check @p node as a part of NameTest processing. |
| 5360 | * |
| 5361 | * @param[in] node Node to check. |
| 5362 | * @param[in] root_type XPath root node type. |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5363 | * @param[in] context_op XPath operation parent. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5364 | * @param[in] node_name Node name in the dictionary to move to, NULL for any node. |
| 5365 | * @param[in] moveto_mod Expected module of the node, NULL for any. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5366 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when, |
| 5367 | * LY_EINVAL if netither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5368 | */ |
| 5369 | static LY_ERR |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5370 | 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] | 5371 | const char *node_name, const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5372 | { |
| 5373 | /* module check */ |
| 5374 | if (moveto_mod && (node->schema->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5375 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5376 | } |
| 5377 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5378 | /* context check */ |
| 5379 | 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] | 5380 | return LY_EINVAL; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5381 | } else if (context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node->schema != context_op)) { |
| 5382 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5383 | } |
| 5384 | |
| 5385 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5386 | if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5387 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5388 | } |
| 5389 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5390 | /* when check */ |
| 5391 | if (moveto_when_check(node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5392 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5393 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5394 | |
| 5395 | /* match */ |
| 5396 | return LY_SUCCESS; |
| 5397 | } |
| 5398 | |
| 5399 | /** |
| 5400 | * @brief Check @p node as a part of schema NameTest processing. |
| 5401 | * |
| 5402 | * @param[in] node Schema node to check. |
| 5403 | * @param[in] root_type XPath root node type. |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5404 | * @param[in] context_op XPath operation parent. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5405 | * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes. |
| 5406 | * @param[in] moveto_mod Expected module of the node, NULL for any. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5407 | * @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] | 5408 | */ |
| 5409 | static LY_ERR |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5410 | 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] | 5411 | const char *node_name, const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5412 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5413 | /* module check */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5414 | if (moveto_mod && (node->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5415 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5416 | } |
| 5417 | |
| 5418 | /* context check */ |
| 5419 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) { |
| 5420 | return LY_EINVAL; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5421 | } else if (context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != context_op)) { |
| 5422 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5423 | } |
| 5424 | |
| 5425 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5426 | if (node_name && strcmp(node_name, "*") && (node->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5427 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5428 | } |
| 5429 | |
| 5430 | /* match */ |
| 5431 | return LY_SUCCESS; |
| 5432 | } |
| 5433 | |
| 5434 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5435 | * @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] | 5436 | * |
| 5437 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5438 | * @param[in] mod Matching node module, NULL for any. |
| 5439 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5440 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5441 | */ |
| 5442 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5443 | 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] | 5444 | { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 5445 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5446 | const struct lyd_node *siblings, *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5447 | LY_ERR rc; |
| 5448 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5449 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5450 | return LY_SUCCESS; |
| 5451 | } |
| 5452 | |
| 5453 | if (set->type != LYXP_SET_NODE_SET) { |
| 5454 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5455 | return LY_EVALID; |
| 5456 | } |
| 5457 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5458 | for (i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5459 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5460 | |
| 5461 | 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] | 5462 | assert(!set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5463 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5464 | /* search in all the trees */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5465 | siblings = set->tree; |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5466 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5467 | /* search in children */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 5468 | siblings = lyd_child(set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5469 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5470 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5471 | for (sub = siblings; sub; sub = sub->next) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5472 | 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] | 5473 | if (rc == LY_SUCCESS) { |
| 5474 | if (!replaced) { |
| 5475 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
| 5476 | replaced = 1; |
| 5477 | } else { |
| 5478 | set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5479 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5480 | ++i; |
| 5481 | } else if (rc == LY_EINCOMPLETE) { |
| 5482 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5483 | } |
| 5484 | } |
| 5485 | |
| 5486 | if (!replaced) { |
| 5487 | /* no match */ |
| 5488 | set_remove_node(set, i); |
| 5489 | } |
| 5490 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5491 | |
| 5492 | return LY_SUCCESS; |
| 5493 | } |
| 5494 | |
| 5495 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5496 | * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). |
| 5497 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5498 | * |
| 5499 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5500 | * @param[in] scnode Matching node schema. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5501 | * @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] | 5502 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5503 | */ |
| 5504 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5505 | 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] | 5506 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5507 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5508 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5509 | const struct lyd_node *siblings; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5510 | struct lyd_node *sub, *inst = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5511 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5512 | assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5513 | |
| 5514 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5515 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5516 | } |
| 5517 | |
| 5518 | if (set->type != LYXP_SET_NODE_SET) { |
| 5519 | 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] | 5520 | ret = LY_EVALID; |
| 5521 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5522 | } |
| 5523 | |
| 5524 | /* context check for all the nodes since we have the schema node */ |
| 5525 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 5526 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5527 | goto cleanup; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5528 | } 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] | 5529 | && (scnode != set->context_op)) { |
| 5530 | lyxp_set_free_content(set); |
| 5531 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5532 | } |
| 5533 | |
| 5534 | /* create specific data instance if needed */ |
| 5535 | if (scnode->nodetype == LYS_LIST) { |
| 5536 | LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup); |
| 5537 | } else if (scnode->nodetype == LYS_LEAFLIST) { |
| 5538 | 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] | 5539 | } |
| 5540 | |
| 5541 | for (i = 0; i < set->used; ) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5542 | siblings = NULL; |
| 5543 | |
| 5544 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
| 5545 | assert(!set->val.nodes[i].node); |
| 5546 | |
| 5547 | /* search in all the trees */ |
| 5548 | siblings = set->tree; |
| 5549 | } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 5550 | /* search in children */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 5551 | siblings = lyd_child(set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5552 | } |
| 5553 | |
| 5554 | /* find the node using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5555 | if (inst) { |
| 5556 | lyd_find_sibling_first(siblings, inst, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5557 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5558 | lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5559 | } |
| 5560 | |
| 5561 | /* when check */ |
| 5562 | if (sub && moveto_when_check(sub)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5563 | ret = LY_EINCOMPLETE; |
| 5564 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5565 | } |
| 5566 | |
| 5567 | if (sub) { |
| 5568 | /* pos filled later */ |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5569 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5570 | ++i; |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5571 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5572 | /* no match */ |
| 5573 | set_remove_node(set, i); |
| 5574 | } |
| 5575 | } |
| 5576 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5577 | cleanup: |
| 5578 | lyd_free_tree(inst); |
| 5579 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5580 | } |
| 5581 | |
| 5582 | /** |
| 5583 | * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
| 5584 | * |
| 5585 | * @param[in,out] set Set to use. |
| 5586 | * @param[in] mod Matching node module, NULL for any. |
| 5587 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5588 | * @param[in] options XPath options. |
| 5589 | * @return LY_ERR |
| 5590 | */ |
| 5591 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5592 | 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] | 5593 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5594 | ly_bool temp_ctx = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5595 | uint32_t getnext_opts; |
| 5596 | uint32_t orig_used, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5597 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5598 | const struct lysc_node *iter, *start_parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5599 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5600 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5601 | return LY_SUCCESS; |
| 5602 | } |
| 5603 | |
| 5604 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 5605 | 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] | 5606 | return LY_EVALID; |
| 5607 | } |
| 5608 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5609 | /* getnext opts */ |
| 5610 | getnext_opts = LYS_GETNEXT_NOSTATECHECK; |
| 5611 | if (options & LYXP_SCNODE_OUTPUT) { |
| 5612 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 5613 | } |
| 5614 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5615 | orig_used = set->used; |
| 5616 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5617 | uint32_t idx; |
| 5618 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5619 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5620 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5621 | continue; |
| 5622 | } |
| 5623 | |
| 5624 | /* remember context node */ |
| 5625 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5626 | } else { |
| 5627 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5628 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5629 | |
| 5630 | start_parent = set->val.scnodes[i].scnode; |
| 5631 | |
| 5632 | 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] | 5633 | /* 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] | 5634 | * so use it directly (root node itself is useless in this case) */ |
| 5635 | mod_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5636 | 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] | 5637 | iter = NULL; |
Michal Vasko | 509de4d | 2019-12-10 14:51:30 +0100 | [diff] [blame] | 5638 | /* module may not be implemented */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5639 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5640 | 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] | 5641 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx)); |
| 5642 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5643 | /* we need to prevent these nodes from being considered in this moveto */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5644 | if ((idx < orig_used) && (idx > i)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5645 | set->val.scnodes[idx].in_ctx = 2; |
| 5646 | temp_ctx = 1; |
| 5647 | } |
| 5648 | } |
| 5649 | } |
| 5650 | |
| 5651 | if (!mod_idx) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5652 | /* mod was specified, we are not going through the whole context */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5653 | break; |
| 5654 | } |
| 5655 | /* next iteration */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5656 | mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5657 | } |
| 5658 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5659 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 5660 | iter = NULL; |
| 5661 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5662 | 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] | 5663 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx)); |
| 5664 | |
| 5665 | if ((idx < orig_used) && (idx > i)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5666 | set->val.scnodes[idx].in_ctx = 2; |
| 5667 | temp_ctx = 1; |
| 5668 | } |
| 5669 | } |
| 5670 | } |
| 5671 | } |
| 5672 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5673 | |
| 5674 | /* correct temporary in_ctx values */ |
| 5675 | if (temp_ctx) { |
| 5676 | for (i = 0; i < orig_used; ++i) { |
| 5677 | if (set->val.scnodes[i].in_ctx == 2) { |
| 5678 | set->val.scnodes[i].in_ctx = 1; |
| 5679 | } |
| 5680 | } |
| 5681 | } |
| 5682 | |
| 5683 | return LY_SUCCESS; |
| 5684 | } |
| 5685 | |
| 5686 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5687 | * @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] | 5688 | * Context position aware. |
| 5689 | * |
| 5690 | * @param[in] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5691 | * @param[in] mod Matching node module, NULL for any. |
| 5692 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5693 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5694 | */ |
| 5695 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5696 | 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] | 5697 | { |
| 5698 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5699 | const struct lyd_node *next, *elem, *start; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5700 | struct lyxp_set ret_set; |
| 5701 | LY_ERR rc; |
| 5702 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5703 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5704 | return LY_SUCCESS; |
| 5705 | } |
| 5706 | |
| 5707 | if (set->type != LYXP_SET_NODE_SET) { |
| 5708 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5709 | return LY_EVALID; |
| 5710 | } |
| 5711 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5712 | /* 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] | 5713 | rc = moveto_node(set, NULL, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5714 | LY_CHECK_RET(rc); |
| 5715 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5716 | /* 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] | 5717 | set_init(&ret_set, set); |
| 5718 | for (i = 0; i < set->used; ++i) { |
| 5719 | |
| 5720 | /* TREE DFS */ |
| 5721 | start = set->val.nodes[i].node; |
| 5722 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5723 | 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] | 5724 | if (!rc) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5725 | /* add matching node into result set */ |
| 5726 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
| 5727 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
| 5728 | /* the node is a duplicate, we'll process it later in the set */ |
| 5729 | goto skip_children; |
| 5730 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5731 | } else if (rc == LY_EINCOMPLETE) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5732 | return rc; |
| 5733 | } else if (rc == LY_EINVAL) { |
| 5734 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5735 | } |
| 5736 | |
| 5737 | /* TREE DFS NEXT ELEM */ |
| 5738 | /* select element for the next run - children first */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 5739 | next = lyd_child(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5740 | if (!next) { |
| 5741 | skip_children: |
| 5742 | /* no children, so try siblings, but only if it's not the start, |
| 5743 | * that is considered to be the root and it's siblings are not traversed */ |
| 5744 | if (elem != start) { |
| 5745 | next = elem->next; |
| 5746 | } else { |
| 5747 | break; |
| 5748 | } |
| 5749 | } |
| 5750 | while (!next) { |
| 5751 | /* no siblings, go back through the parents */ |
| 5752 | if ((struct lyd_node *)elem->parent == start) { |
| 5753 | /* we are done, no next element to process */ |
| 5754 | break; |
| 5755 | } |
| 5756 | /* parent is already processed, go to its sibling */ |
| 5757 | elem = (struct lyd_node *)elem->parent; |
| 5758 | next = elem->next; |
| 5759 | } |
| 5760 | } |
| 5761 | } |
| 5762 | |
| 5763 | /* make the temporary set the current one */ |
| 5764 | ret_set.ctx_pos = set->ctx_pos; |
| 5765 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5766 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5767 | memcpy(set, &ret_set, sizeof *set); |
| 5768 | |
| 5769 | return LY_SUCCESS; |
| 5770 | } |
| 5771 | |
| 5772 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5773 | * @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] | 5774 | * |
| 5775 | * @param[in] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5776 | * @param[in] mod Matching node module, NULL for any. |
| 5777 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5778 | * @param[in] options XPath options. |
| 5779 | * @return LY_ERR |
| 5780 | */ |
| 5781 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5782 | 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] | 5783 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5784 | uint32_t i, orig_used; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5785 | const struct lysc_node *next, *elem, *start; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5786 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5787 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5788 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5789 | return LY_SUCCESS; |
| 5790 | } |
| 5791 | |
| 5792 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 5793 | 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] | 5794 | return LY_EVALID; |
| 5795 | } |
| 5796 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5797 | orig_used = set->used; |
| 5798 | for (i = 0; i < orig_used; ++i) { |
| 5799 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5800 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5801 | continue; |
| 5802 | } |
| 5803 | |
| 5804 | /* remember context node */ |
| 5805 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5806 | } else { |
| 5807 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5808 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5809 | |
| 5810 | /* TREE DFS */ |
| 5811 | start = set->val.scnodes[i].scnode; |
| 5812 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5813 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 5814 | /* schema-only nodes, skip root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5815 | goto next_iter; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5816 | } |
| 5817 | |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5818 | 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] | 5819 | if (!rc) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5820 | uint32_t idx; |
| 5821 | |
| 5822 | if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5823 | set->val.scnodes[idx].in_ctx = 1; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5824 | if ((uint32_t)idx > i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5825 | /* we will process it later in the set */ |
| 5826 | goto skip_children; |
| 5827 | } |
| 5828 | } else { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5829 | 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] | 5830 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5831 | } else if (rc == LY_EINVAL) { |
| 5832 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5833 | } |
| 5834 | |
| 5835 | next_iter: |
| 5836 | /* TREE DFS NEXT ELEM */ |
| 5837 | /* select element for the next run - children first */ |
| 5838 | 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] | 5839 | if (!next) { |
| 5840 | skip_children: |
| 5841 | /* no children, so try siblings, but only if it's not the start, |
| 5842 | * that is considered to be the root and it's siblings are not traversed */ |
| 5843 | if (elem != start) { |
| 5844 | next = elem->next; |
| 5845 | } else { |
| 5846 | break; |
| 5847 | } |
| 5848 | } |
| 5849 | while (!next) { |
| 5850 | /* no siblings, go back through the parents */ |
| 5851 | if (elem->parent == start) { |
| 5852 | /* we are done, no next element to process */ |
| 5853 | break; |
| 5854 | } |
| 5855 | /* parent is already processed, go to its sibling */ |
| 5856 | elem = elem->parent; |
| 5857 | next = elem->next; |
| 5858 | } |
| 5859 | } |
| 5860 | } |
| 5861 | |
| 5862 | return LY_SUCCESS; |
| 5863 | } |
| 5864 | |
| 5865 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5866 | * @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] | 5867 | * Indirectly context position aware. |
| 5868 | * |
| 5869 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5870 | * @param[in] mod Matching metadata module, NULL for any. |
| 5871 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5872 | * @return LY_ERR |
| 5873 | */ |
| 5874 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5875 | 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] | 5876 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5877 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5878 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5879 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5880 | return LY_SUCCESS; |
| 5881 | } |
| 5882 | |
| 5883 | if (set->type != LYXP_SET_NODE_SET) { |
| 5884 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5885 | return LY_EVALID; |
| 5886 | } |
| 5887 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5888 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5889 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5890 | |
| 5891 | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
| 5892 | * our attributes are always qualified */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5893 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5894 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5895 | |
| 5896 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5897 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5898 | continue; |
| 5899 | } |
| 5900 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5901 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5902 | /* match */ |
| 5903 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5904 | set->val.meta[i].meta = sub; |
| 5905 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5906 | /* pos does not change */ |
| 5907 | replaced = 1; |
| 5908 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5909 | 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] | 5910 | } |
| 5911 | ++i; |
| 5912 | } |
| 5913 | } |
| 5914 | } |
| 5915 | |
| 5916 | if (!replaced) { |
| 5917 | /* no match */ |
| 5918 | set_remove_node(set, i); |
| 5919 | } |
| 5920 | } |
| 5921 | |
| 5922 | return LY_SUCCESS; |
| 5923 | } |
| 5924 | |
| 5925 | /** |
| 5926 | * @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] | 5927 | * Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5928 | * |
| 5929 | * @param[in,out] set1 Set to use for the result. |
| 5930 | * @param[in] set2 Set that is copied to @p set1. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5931 | * @return LY_ERR |
| 5932 | */ |
| 5933 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5934 | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5935 | { |
| 5936 | LY_ERR rc; |
| 5937 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5938 | 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] | 5939 | LOGVAL(set1->ctx, LY_VLOG_LYD, set1->ctx_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2)); |
| 5940 | return LY_EVALID; |
| 5941 | } |
| 5942 | |
| 5943 | /* set2 is empty or both set1 and set2 */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5944 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5945 | return LY_SUCCESS; |
| 5946 | } |
| 5947 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5948 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5949 | memcpy(set1, set2, sizeof *set1); |
| 5950 | /* dynamic memory belongs to set1 now, do not free */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5951 | memset(set2, 0, sizeof *set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5952 | return LY_SUCCESS; |
| 5953 | } |
| 5954 | |
| 5955 | /* we assume sets are sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5956 | assert(!set_sort(set1) && !set_sort(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5957 | |
| 5958 | /* sort, remove duplicates */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5959 | rc = set_sorted_merge(set1, set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5960 | LY_CHECK_RET(rc); |
| 5961 | |
| 5962 | /* final set must be sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5963 | assert(!set_sort(set1)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5964 | |
| 5965 | return LY_SUCCESS; |
| 5966 | } |
| 5967 | |
| 5968 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5969 | * @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] | 5970 | * Context position aware. |
| 5971 | * |
| 5972 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5973 | * @param[in] mod Matching metadata module, NULL for any. |
| 5974 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5975 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5976 | */ |
| 5977 | static int |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5978 | 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] | 5979 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5980 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5981 | struct lyxp_set *set_all_desc = NULL; |
| 5982 | LY_ERR rc; |
| 5983 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5984 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5985 | return LY_SUCCESS; |
| 5986 | } |
| 5987 | |
| 5988 | if (set->type != LYXP_SET_NODE_SET) { |
| 5989 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5990 | return LY_EVALID; |
| 5991 | } |
| 5992 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5993 | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
| 5994 | * but it likely won't be used much, so it's a waste of time */ |
| 5995 | /* copy the context */ |
| 5996 | set_all_desc = set_copy(set); |
| 5997 | /* get all descendant nodes (the original context nodes are removed) */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5998 | rc = moveto_node_alldesc(set_all_desc, NULL, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5999 | if (rc != LY_SUCCESS) { |
| 6000 | lyxp_set_free(set_all_desc); |
| 6001 | return rc; |
| 6002 | } |
| 6003 | /* prepend the original context nodes */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6004 | rc = moveto_union(set, set_all_desc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6005 | if (rc != LY_SUCCESS) { |
| 6006 | lyxp_set_free(set_all_desc); |
| 6007 | return rc; |
| 6008 | } |
| 6009 | lyxp_set_free(set_all_desc); |
| 6010 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6011 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6012 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6013 | |
| 6014 | /* only attributes of an elem can be in the result, skip all the rest, |
| 6015 | * we have all attributes qualified in lyd tree */ |
| 6016 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6017 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6018 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6019 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6020 | continue; |
| 6021 | } |
| 6022 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6023 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6024 | /* match */ |
| 6025 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6026 | set->val.meta[i].meta = sub; |
| 6027 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6028 | /* pos does not change */ |
| 6029 | replaced = 1; |
| 6030 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6031 | 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] | 6032 | } |
| 6033 | ++i; |
| 6034 | } |
| 6035 | } |
| 6036 | } |
| 6037 | |
| 6038 | if (!replaced) { |
| 6039 | /* no match */ |
| 6040 | set_remove_node(set, i); |
| 6041 | } |
| 6042 | } |
| 6043 | |
| 6044 | return LY_SUCCESS; |
| 6045 | } |
| 6046 | |
| 6047 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6048 | * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET. |
| 6049 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6050 | * |
| 6051 | * @param[in] parent Current parent. |
| 6052 | * @param[in] parent_pos Position of @p parent. |
| 6053 | * @param[in] parent_type Node type of @p parent. |
| 6054 | * @param[in,out] to_set Set to use. |
| 6055 | * @param[in] dup_check_set Set for checking duplicities. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6056 | * @param[in] options XPath options. |
| 6057 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6058 | */ |
| 6059 | static LY_ERR |
| 6060 | 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] | 6061 | 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] | 6062 | { |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6063 | const struct lyd_node *iter, *first; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6064 | LY_ERR rc; |
| 6065 | |
| 6066 | switch (parent_type) { |
| 6067 | case LYXP_NODE_ROOT: |
| 6068 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6069 | assert(!parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6070 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6071 | /* add all top-level nodes as elements */ |
| 6072 | first = to_set->tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6073 | break; |
| 6074 | case LYXP_NODE_ELEM: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6075 | /* add just the text node of this term element node */ |
| 6076 | if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6077 | if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) { |
| 6078 | set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used); |
| 6079 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6080 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6081 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6082 | |
| 6083 | /* add all the children of this node */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 6084 | first = lyd_child(parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6085 | break; |
| 6086 | default: |
| 6087 | LOGINT_RET(parent->schema->module->ctx); |
| 6088 | } |
| 6089 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6090 | /* add all top-level nodes as elements */ |
| 6091 | LY_LIST_FOR(first, iter) { |
| 6092 | /* context check */ |
| 6093 | if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) { |
| 6094 | continue; |
| 6095 | } |
| 6096 | |
| 6097 | /* when check */ |
| 6098 | if (moveto_when_check(iter)) { |
| 6099 | return LY_EINCOMPLETE; |
| 6100 | } |
| 6101 | |
| 6102 | if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) { |
| 6103 | set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used); |
| 6104 | |
| 6105 | /* also add all the children of this node, recursively */ |
| 6106 | rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options); |
| 6107 | LY_CHECK_RET(rc); |
| 6108 | } |
| 6109 | } |
| 6110 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6111 | return LY_SUCCESS; |
| 6112 | } |
| 6113 | |
| 6114 | /** |
| 6115 | * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET |
| 6116 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6117 | * |
| 6118 | * @param[in,out] set Set to use. |
| 6119 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6120 | * @param[in] options XPath options. |
| 6121 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6122 | */ |
| 6123 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6124 | 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] | 6125 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6126 | struct lyxp_set ret_set; |
| 6127 | LY_ERR rc; |
| 6128 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6129 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6130 | return LY_SUCCESS; |
| 6131 | } |
| 6132 | |
| 6133 | if (set->type != LYXP_SET_NODE_SET) { |
| 6134 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 6135 | return LY_EVALID; |
| 6136 | } |
| 6137 | |
| 6138 | /* nothing to do */ |
| 6139 | if (!all_desc) { |
| 6140 | return LY_SUCCESS; |
| 6141 | } |
| 6142 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6143 | /* add all the children, they get added recursively */ |
| 6144 | set_init(&ret_set, set); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6145 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6146 | /* copy the current node to tmp */ |
| 6147 | set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used); |
| 6148 | |
| 6149 | /* do not touch attributes and text nodes */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6150 | 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] | 6151 | continue; |
| 6152 | } |
| 6153 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6154 | /* add all the children */ |
| 6155 | 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] | 6156 | set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6157 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6158 | lyxp_set_free_content(&ret_set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6159 | return rc; |
| 6160 | } |
| 6161 | } |
| 6162 | |
| 6163 | /* use the temporary set as the current one */ |
| 6164 | ret_set.ctx_pos = set->ctx_pos; |
| 6165 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6166 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6167 | memcpy(set, &ret_set, sizeof *set); |
| 6168 | |
| 6169 | return LY_SUCCESS; |
| 6170 | } |
| 6171 | |
| 6172 | /** |
| 6173 | * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET |
| 6174 | * (or LYXP_SET_EMPTY). |
| 6175 | * |
| 6176 | * @param[in,out] set Set to use. |
| 6177 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6178 | * @param[in] options XPath options. |
| 6179 | * @return LY_ERR |
| 6180 | */ |
| 6181 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6182 | 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] | 6183 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6184 | uint32_t getnext_opts; |
| 6185 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6186 | const struct lysc_node *iter, *start_parent; |
| 6187 | const struct lys_module *mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6188 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6189 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6190 | return LY_SUCCESS; |
| 6191 | } |
| 6192 | |
| 6193 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6194 | 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] | 6195 | return LY_EVALID; |
| 6196 | } |
| 6197 | |
| 6198 | /* nothing to do */ |
| 6199 | if (!all_desc) { |
| 6200 | return LY_SUCCESS; |
| 6201 | } |
| 6202 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6203 | /* getnext opts */ |
| 6204 | getnext_opts = LYS_GETNEXT_NOSTATECHECK; |
| 6205 | if (options & LYXP_SCNODE_OUTPUT) { |
| 6206 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 6207 | } |
| 6208 | |
| 6209 | /* 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] | 6210 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6211 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6212 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6213 | continue; |
| 6214 | } |
| 6215 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6216 | /* remember context node */ |
| 6217 | set->val.scnodes[i].in_ctx = -1; |
| 6218 | } else { |
| 6219 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6220 | } |
| 6221 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6222 | start_parent = set->val.scnodes[i].scnode; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6223 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6224 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
| 6225 | /* it can actually be in any module, it's all <running> */ |
| 6226 | mod_idx = 0; |
| 6227 | while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
| 6228 | iter = NULL; |
| 6229 | /* module may not be implemented */ |
| 6230 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
| 6231 | /* context check */ |
| 6232 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) { |
| 6233 | continue; |
| 6234 | } |
| 6235 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6236 | 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] | 6237 | /* throw away the insert index, we want to consider that node again, recursively */ |
| 6238 | } |
| 6239 | } |
| 6240 | |
| 6241 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 6242 | iter = NULL; |
| 6243 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6244 | /* context check */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6245 | 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] | 6246 | continue; |
| 6247 | } |
| 6248 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6249 | 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] | 6250 | } |
| 6251 | } |
| 6252 | } |
| 6253 | |
| 6254 | return LY_SUCCESS; |
| 6255 | } |
| 6256 | |
| 6257 | /** |
| 6258 | * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET |
| 6259 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6260 | * |
| 6261 | * @param[in] set Set to use. |
| 6262 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6263 | * @param[in] options XPath options. |
| 6264 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6265 | */ |
| 6266 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6267 | 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] | 6268 | { |
| 6269 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6270 | struct lyd_node *node, *new_node; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6271 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6272 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6273 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6274 | return LY_SUCCESS; |
| 6275 | } |
| 6276 | |
| 6277 | if (set->type != LYXP_SET_NODE_SET) { |
| 6278 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 6279 | return LY_EVALID; |
| 6280 | } |
| 6281 | |
| 6282 | if (all_desc) { |
| 6283 | /* <path>//.. == <path>//./.. */ |
| 6284 | rc = moveto_self(set, 1, options); |
| 6285 | LY_CHECK_RET(rc); |
| 6286 | } |
| 6287 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6288 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6289 | node = set->val.nodes[i].node; |
| 6290 | |
| 6291 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 6292 | new_node = (struct lyd_node *)node->parent; |
| 6293 | } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) { |
| 6294 | new_node = node; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6295 | } else if (set->val.nodes[i].type == LYXP_NODE_META) { |
| 6296 | new_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6297 | if (!new_node) { |
| 6298 | LOGINT_RET(set->ctx); |
| 6299 | } |
| 6300 | } else { |
| 6301 | /* root does not have a parent */ |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6302 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6303 | continue; |
| 6304 | } |
| 6305 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6306 | /* when check */ |
| 6307 | if (moveto_when_check(new_node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6308 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6309 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6310 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6311 | if (!new_node) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6312 | /* node already there can also be the root */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6313 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6314 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6315 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6316 | /* 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] | 6317 | new_type = LYXP_NODE_ELEM; |
| 6318 | } |
| 6319 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6320 | if (set_dup_node_check(set, new_node, new_type, -1)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6321 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6322 | } else { |
| 6323 | set_replace_node(set, new_node, 0, new_type, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6324 | } |
| 6325 | } |
| 6326 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6327 | set_remove_nodes_none(set); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6328 | assert(!set_sort(set) && !set_sorted_dup_node_clean(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6329 | |
| 6330 | return LY_SUCCESS; |
| 6331 | } |
| 6332 | |
| 6333 | /** |
| 6334 | * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET |
| 6335 | * (or LYXP_SET_EMPTY). |
| 6336 | * |
| 6337 | * @param[in] set Set to use. |
| 6338 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6339 | * @param[in] options XPath options. |
| 6340 | * @return LY_ERR |
| 6341 | */ |
| 6342 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6343 | 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] | 6344 | { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6345 | uint32_t i, orig_used, idx; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6346 | ly_bool temp_ctx = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6347 | const struct lysc_node *node, *new_node; |
| 6348 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6349 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6350 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6351 | return LY_SUCCESS; |
| 6352 | } |
| 6353 | |
| 6354 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6355 | 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] | 6356 | return LY_EVALID; |
| 6357 | } |
| 6358 | |
| 6359 | if (all_desc) { |
| 6360 | /* <path>//.. == <path>//./.. */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6361 | LY_CHECK_RET(moveto_scnode_self(set, 1, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6362 | } |
| 6363 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6364 | orig_used = set->used; |
| 6365 | for (i = 0; i < orig_used; ++i) { |
| 6366 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6367 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6368 | continue; |
| 6369 | } |
| 6370 | |
| 6371 | /* remember context node */ |
| 6372 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 6373 | } else { |
| 6374 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6375 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6376 | |
| 6377 | node = set->val.scnodes[i].scnode; |
| 6378 | |
| 6379 | if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6380 | new_node = lysc_data_parent(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6381 | } else { |
| 6382 | /* root does not have a parent */ |
| 6383 | continue; |
| 6384 | } |
| 6385 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6386 | if (!new_node) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6387 | /* node has no parent */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6388 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6389 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6390 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6391 | /* 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] | 6392 | new_type = LYXP_NODE_ELEM; |
| 6393 | } |
| 6394 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6395 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx)); |
| 6396 | if ((idx < orig_used) && (idx > i)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6397 | set->val.scnodes[idx].in_ctx = 2; |
| 6398 | temp_ctx = 1; |
| 6399 | } |
| 6400 | } |
| 6401 | |
| 6402 | if (temp_ctx) { |
| 6403 | for (i = 0; i < orig_used; ++i) { |
| 6404 | if (set->val.scnodes[i].in_ctx == 2) { |
| 6405 | set->val.scnodes[i].in_ctx = 1; |
| 6406 | } |
| 6407 | } |
| 6408 | } |
| 6409 | |
| 6410 | return LY_SUCCESS; |
| 6411 | } |
| 6412 | |
| 6413 | /** |
| 6414 | * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'. |
| 6415 | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
| 6416 | * |
| 6417 | * @param[in,out] set1 Set to use for the result. |
| 6418 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6419 | * @param[in] op Comparison operator to process. |
| 6420 | * @param[in] options XPath options. |
| 6421 | * @return LY_ERR |
| 6422 | */ |
| 6423 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6424 | 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] | 6425 | { |
| 6426 | /* |
| 6427 | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
| 6428 | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
| 6429 | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6430 | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6431 | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
| 6432 | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6433 | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6434 | * |
| 6435 | * '=' or '!=' |
| 6436 | * BOOLEAN + BOOLEAN |
| 6437 | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6438 | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6439 | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6440 | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6441 | * NUMBER + NUMBER |
| 6442 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6443 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6444 | * STRING + STRING |
| 6445 | * |
| 6446 | * '<=', '<', '>=', '>' |
| 6447 | * NUMBER + NUMBER |
| 6448 | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6449 | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6450 | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6451 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6452 | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6453 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6454 | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6455 | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6456 | */ |
| 6457 | struct lyxp_set iter1, iter2; |
| 6458 | int result; |
| 6459 | int64_t i; |
| 6460 | LY_ERR rc; |
| 6461 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6462 | memset(&iter1, 0, sizeof iter1); |
| 6463 | memset(&iter2, 0, sizeof iter2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6464 | |
| 6465 | /* iterative evaluation with node-sets */ |
| 6466 | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
| 6467 | if (set1->type == LYXP_SET_NODE_SET) { |
| 6468 | for (i = 0; i < set1->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6469 | /* cast set1 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6470 | switch (set2->type) { |
| 6471 | case LYXP_SET_NUMBER: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6472 | rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6473 | break; |
| 6474 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6475 | rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6476 | break; |
| 6477 | default: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6478 | rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6479 | break; |
| 6480 | } |
| 6481 | LY_CHECK_RET(rc); |
| 6482 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6483 | /* canonize set2 */ |
| 6484 | LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc); |
| 6485 | |
| 6486 | /* compare recursively */ |
| 6487 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
| 6488 | lyxp_set_free_content(&iter2); |
| 6489 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6490 | |
| 6491 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6492 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6493 | set_fill_boolean(set1, 1); |
| 6494 | return LY_SUCCESS; |
| 6495 | } |
| 6496 | } |
| 6497 | } else { |
| 6498 | for (i = 0; i < set2->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6499 | /* set set2 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6500 | switch (set1->type) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6501 | case LYXP_SET_NUMBER: |
| 6502 | rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i); |
| 6503 | break; |
| 6504 | case LYXP_SET_BOOLEAN: |
| 6505 | rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i); |
| 6506 | break; |
| 6507 | default: |
| 6508 | rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i); |
| 6509 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6510 | } |
| 6511 | LY_CHECK_RET(rc); |
| 6512 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6513 | /* canonize set1 */ |
| 6514 | 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] | 6515 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6516 | /* compare recursively */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6517 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6518 | lyxp_set_free_content(&iter2); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6519 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6520 | |
| 6521 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6522 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6523 | set_fill_boolean(set1, 1); |
| 6524 | return LY_SUCCESS; |
| 6525 | } |
| 6526 | } |
| 6527 | } |
| 6528 | |
| 6529 | /* false for all nodes */ |
| 6530 | set_fill_boolean(set1, 0); |
| 6531 | return LY_SUCCESS; |
| 6532 | } |
| 6533 | |
| 6534 | /* first convert properly */ |
| 6535 | if ((op[0] == '=') || (op[0] == '!')) { |
| 6536 | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6537 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
| 6538 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6539 | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6540 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6541 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6542 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6543 | LY_CHECK_RET(rc); |
| 6544 | } /* else we have 2 strings */ |
| 6545 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6546 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6547 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6548 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6549 | LY_CHECK_RET(rc); |
| 6550 | } |
| 6551 | |
| 6552 | assert(set1->type == set2->type); |
| 6553 | |
| 6554 | /* compute result */ |
| 6555 | if (op[0] == '=') { |
| 6556 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6557 | result = (set1->val.bln == set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6558 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6559 | result = (set1->val.num == set2->val.num); |
| 6560 | } else { |
| 6561 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6562 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6563 | } |
| 6564 | } else if (op[0] == '!') { |
| 6565 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6566 | result = (set1->val.bln != set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6567 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6568 | result = (set1->val.num != set2->val.num); |
| 6569 | } else { |
| 6570 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6571 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6572 | } |
| 6573 | } else { |
| 6574 | assert(set1->type == LYXP_SET_NUMBER); |
| 6575 | if (op[0] == '<') { |
| 6576 | if (op[1] == '=') { |
| 6577 | result = (set1->val.num <= set2->val.num); |
| 6578 | } else { |
| 6579 | result = (set1->val.num < set2->val.num); |
| 6580 | } |
| 6581 | } else { |
| 6582 | if (op[1] == '=') { |
| 6583 | result = (set1->val.num >= set2->val.num); |
| 6584 | } else { |
| 6585 | result = (set1->val.num > set2->val.num); |
| 6586 | } |
| 6587 | } |
| 6588 | } |
| 6589 | |
| 6590 | /* assign result */ |
| 6591 | if (result) { |
| 6592 | set_fill_boolean(set1, 1); |
| 6593 | } else { |
| 6594 | set_fill_boolean(set1, 0); |
| 6595 | } |
| 6596 | |
| 6597 | return LY_SUCCESS; |
| 6598 | } |
| 6599 | |
| 6600 | /** |
| 6601 | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
| 6602 | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
| 6603 | * |
| 6604 | * @param[in,out] set1 Set to use for the result. |
| 6605 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6606 | * @param[in] op Operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6607 | * @return LY_ERR |
| 6608 | */ |
| 6609 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6610 | 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] | 6611 | { |
| 6612 | LY_ERR rc; |
| 6613 | |
| 6614 | /* unary '-' */ |
| 6615 | if (!set2 && (op[0] == '-')) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6616 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6617 | LY_CHECK_RET(rc); |
| 6618 | set1->val.num *= -1; |
| 6619 | lyxp_set_free(set2); |
| 6620 | return LY_SUCCESS; |
| 6621 | } |
| 6622 | |
| 6623 | assert(set1 && set2); |
| 6624 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6625 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6626 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6627 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6628 | LY_CHECK_RET(rc); |
| 6629 | |
| 6630 | switch (op[0]) { |
| 6631 | /* '+' */ |
| 6632 | case '+': |
| 6633 | set1->val.num += set2->val.num; |
| 6634 | break; |
| 6635 | |
| 6636 | /* '-' */ |
| 6637 | case '-': |
| 6638 | set1->val.num -= set2->val.num; |
| 6639 | break; |
| 6640 | |
| 6641 | /* '*' */ |
| 6642 | case '*': |
| 6643 | set1->val.num *= set2->val.num; |
| 6644 | break; |
| 6645 | |
| 6646 | /* 'div' */ |
| 6647 | case 'd': |
| 6648 | set1->val.num /= set2->val.num; |
| 6649 | break; |
| 6650 | |
| 6651 | /* 'mod' */ |
| 6652 | case 'm': |
| 6653 | set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num); |
| 6654 | break; |
| 6655 | |
| 6656 | default: |
| 6657 | LOGINT_RET(set1->ctx); |
| 6658 | } |
| 6659 | |
| 6660 | return LY_SUCCESS; |
| 6661 | } |
| 6662 | |
| 6663 | /* |
| 6664 | * eval functions |
| 6665 | * |
| 6666 | * They execute a parsed XPath expression on some data subtree. |
| 6667 | */ |
| 6668 | |
| 6669 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6670 | * @brief Evaluate Predicate. Logs directly on error. |
| 6671 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6672 | * [9] Predicate ::= '[' Expr ']' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6673 | * |
| 6674 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6675 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6676 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6677 | * @param[in] options XPath options. |
| 6678 | * @param[in] parent_pos_pred Whether parent predicate was a positional one. |
| 6679 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6680 | */ |
| 6681 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6682 | 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] | 6683 | { |
| 6684 | LY_ERR rc; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 6685 | uint16_t i, orig_exp; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6686 | uint32_t orig_pos, orig_size; |
| 6687 | int32_t pred_in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6688 | struct lyxp_set set2; |
| 6689 | struct lyd_node *orig_parent; |
| 6690 | |
| 6691 | /* '[' */ |
| 6692 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6693 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6694 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6695 | |
| 6696 | if (!set) { |
| 6697 | only_parse: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6698 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6699 | LY_CHECK_RET(rc); |
| 6700 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 6701 | /* 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] | 6702 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6703 | |
| 6704 | /* empty set, nothing to evaluate */ |
| 6705 | if (!set->used) { |
| 6706 | goto only_parse; |
| 6707 | } |
| 6708 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6709 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6710 | orig_pos = 0; |
| 6711 | orig_size = set->used; |
| 6712 | orig_parent = NULL; |
Michal Vasko | 39dbf35 | 2020-05-21 10:08:59 +0200 | [diff] [blame] | 6713 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6714 | set_init(&set2, set); |
| 6715 | set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0); |
| 6716 | /* remember the node context position for position() and context size for last(), |
| 6717 | * predicates should always be evaluated with respect to the child axis (since we do |
| 6718 | * not support explicit axes) so we assign positions based on their parents */ |
| 6719 | if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) { |
| 6720 | orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent; |
| 6721 | orig_pos = 1; |
| 6722 | } else { |
| 6723 | ++orig_pos; |
| 6724 | } |
| 6725 | |
| 6726 | set2.ctx_pos = orig_pos; |
| 6727 | set2.ctx_size = orig_size; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6728 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6729 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6730 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6731 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6732 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6733 | return rc; |
| 6734 | } |
| 6735 | |
| 6736 | /* number is a position */ |
| 6737 | if (set2.type == LYXP_SET_NUMBER) { |
| 6738 | if ((long long)set2.val.num == orig_pos) { |
| 6739 | set2.val.num = 1; |
| 6740 | } else { |
| 6741 | set2.val.num = 0; |
| 6742 | } |
| 6743 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6744 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6745 | |
| 6746 | /* predicate satisfied or not? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6747 | if (!set2.val.bln) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6748 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6749 | } |
| 6750 | } |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6751 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6752 | |
| 6753 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 6754 | for (i = 0; i < set->used; ++i) { |
| 6755 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6756 | /* there is a currently-valid node */ |
| 6757 | break; |
| 6758 | } |
| 6759 | } |
| 6760 | /* empty set, nothing to evaluate */ |
| 6761 | if (i == set->used) { |
| 6762 | goto only_parse; |
| 6763 | } |
| 6764 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6765 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6766 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6767 | /* set special in_ctx to all the valid snodes */ |
| 6768 | pred_in_ctx = set_scnode_new_in_ctx(set); |
| 6769 | |
| 6770 | /* use the valid snodes one-by-one */ |
| 6771 | for (i = 0; i < set->used; ++i) { |
| 6772 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
| 6773 | continue; |
| 6774 | } |
| 6775 | set->val.scnodes[i].in_ctx = 1; |
| 6776 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6777 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6778 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6779 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6780 | LY_CHECK_RET(rc); |
| 6781 | |
| 6782 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
| 6783 | } |
| 6784 | |
| 6785 | /* restore the state as it was before the predicate */ |
| 6786 | for (i = 0; i < set->used; ++i) { |
| 6787 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6788 | set->val.scnodes[i].in_ctx = 0; |
| 6789 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
| 6790 | set->val.scnodes[i].in_ctx = 1; |
| 6791 | } |
| 6792 | } |
| 6793 | |
| 6794 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6795 | set2.type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6796 | set_fill_set(&set2, set); |
| 6797 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6798 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6799 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6800 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6801 | return rc; |
| 6802 | } |
| 6803 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6804 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6805 | if (!set2.val.bln) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6806 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6807 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6808 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6809 | } |
| 6810 | |
| 6811 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6812 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6813 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6814 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6815 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6816 | |
| 6817 | return LY_SUCCESS; |
| 6818 | } |
| 6819 | |
| 6820 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6821 | * @brief Evaluate Literal. Logs directly on error. |
| 6822 | * |
| 6823 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6824 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6825 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6826 | */ |
| 6827 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6828 | 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] | 6829 | { |
| 6830 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6831 | if (exp->tok_len[*tok_idx] == 2) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6832 | set_fill_string(set, "", 0); |
| 6833 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6834 | 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] | 6835 | } |
| 6836 | } |
| 6837 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6838 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6839 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6840 | } |
| 6841 | |
| 6842 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6843 | * @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] | 6844 | * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6845 | * @param[in] exp Full parsed XPath expression. |
| 6846 | * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success. |
| 6847 | * @param[in] scnode Found schema node as context for the predicate. |
| 6848 | * @param[in] format Format of any prefixes in key names/values. |
| 6849 | * @param[out] predicates Parsed predicates. |
| 6850 | * @param[out] pred_type Type of @p predicates. |
| 6851 | * @return LY_SUCCESS on success, |
| 6852 | * @return LY_ERR on any error. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6853 | */ |
| 6854 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6855 | 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] | 6856 | LY_PREFIX_FORMAT format, struct ly_path_predicate **predicates, |
| 6857 | enum ly_path_pred_type *pred_type) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6858 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6859 | LY_ERR ret = LY_SUCCESS; |
| 6860 | uint16_t key_count, e_idx, pred_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6861 | const struct lysc_node *key; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6862 | size_t pred_len; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6863 | uint32_t prev_lo; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6864 | struct lyxp_expr *exp2 = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6865 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6866 | assert(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6867 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6868 | if (scnode->nodetype == LYS_LIST) { |
| 6869 | /* get key count */ |
| 6870 | if (scnode->flags & LYS_KEYLESS) { |
| 6871 | return LY_EINVAL; |
| 6872 | } |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 6873 | 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] | 6874 | assert(key_count); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6875 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6876 | /* learn where the predicates end */ |
| 6877 | e_idx = *tok_idx; |
| 6878 | while (key_count) { |
| 6879 | /* '[' */ |
| 6880 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6881 | return LY_EINVAL; |
| 6882 | } |
| 6883 | ++e_idx; |
| 6884 | |
| 6885 | /* ']' */ |
| 6886 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6887 | ++e_idx; |
| 6888 | } |
| 6889 | ++e_idx; |
| 6890 | |
| 6891 | /* another presumably key predicate parsed */ |
| 6892 | --key_count; |
| 6893 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6894 | } else { |
| 6895 | /* learn just where this single predicate ends */ |
| 6896 | e_idx = *tok_idx; |
| 6897 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6898 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6899 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6900 | return LY_EINVAL; |
| 6901 | } |
| 6902 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6903 | |
| 6904 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6905 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6906 | ++e_idx; |
| 6907 | } |
| 6908 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6909 | } |
| 6910 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6911 | /* get the joined length of all the keys predicates/of the single leaf-list predicate */ |
| 6912 | pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx]; |
| 6913 | |
| 6914 | /* turn logging off */ |
| 6915 | prev_lo = ly_log_options(0); |
| 6916 | |
| 6917 | /* parse the predicate(s) */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6918 | 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] | 6919 | LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup); |
| 6920 | |
| 6921 | /* compile */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6922 | ret = ly_path_compile_predicate(scnode->module->ctx, scnode, scnode->module, scnode, exp2, &pred_idx, |
| 6923 | format, scnode->module, predicates, pred_type); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6924 | LY_CHECK_GOTO(ret, cleanup); |
| 6925 | |
| 6926 | /* success, the predicate must include all the needed information for hash-based search */ |
| 6927 | *tok_idx = e_idx; |
| 6928 | |
| 6929 | cleanup: |
| 6930 | ly_log_options(prev_lo); |
| 6931 | lyxp_expr_free(scnode->module->ctx, exp2); |
| 6932 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6933 | } |
| 6934 | |
| 6935 | /** |
| 6936 | * @brief Evaluate NameTest and any following Predicates. Logs directly on error. |
| 6937 | * |
| 6938 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 6939 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 6940 | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
| 6941 | * |
| 6942 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6943 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6944 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 6945 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 6946 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6947 | * @param[in] options XPath options. |
| 6948 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6949 | */ |
| 6950 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6951 | 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] | 6952 | uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6953 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6954 | char *path; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6955 | const char *ncname, *ncname_dict = NULL; |
| 6956 | uint16_t ncname_len; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6957 | const struct lys_module *moveto_mod; |
| 6958 | const struct lysc_node *scnode = NULL, *tmp; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6959 | struct ly_path_predicate *predicates = NULL; |
| 6960 | enum ly_path_pred_type pred_type = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6961 | LY_ERR rc = LY_SUCCESS; |
| 6962 | |
| 6963 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6964 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6965 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6966 | |
| 6967 | if (!set) { |
| 6968 | goto moveto; |
| 6969 | } |
| 6970 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6971 | ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]]; |
| 6972 | ncname_len = exp->tok_len[*tok_idx - 1]; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6973 | |
| 6974 | /* parse (and skip) module name */ |
| 6975 | rc = moveto_resolve_model(&ncname, &ncname_len, set, &moveto_mod); |
| 6976 | LY_CHECK_GOTO(rc, cleanup); |
| 6977 | |
| 6978 | if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) { |
| 6979 | /* find the matching schema node in some parent in the context */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6980 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6981 | if (set->val.nodes[i].type == set->root_type) { |
| 6982 | tmp = lys_find_child(NULL, moveto_mod, ncname, ncname_len, 0, 0); |
| 6983 | } else if ((set->val.nodes[i].type == LYXP_NODE_ELEM) |
| 6984 | && (!scnode || (lysc_data_parent(scnode) != set->val.nodes[i].node->schema))) { |
| 6985 | /* do not repeat the same search */ |
| 6986 | 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] | 6987 | } else { |
| 6988 | tmp = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6989 | } |
| 6990 | |
| 6991 | /* additional context check */ |
| 6992 | if (tmp && (set->root_type == LYXP_NODE_ROOT_CONFIG) && (tmp->flags & LYS_CONFIG_R)) { |
| 6993 | tmp = NULL; |
| 6994 | } |
| 6995 | |
| 6996 | if (tmp) { |
| 6997 | if (scnode) { |
| 6998 | /* we found a schema node with the same name but at different level, give up, too complicated */ |
| 6999 | scnode = NULL; |
| 7000 | break; |
| 7001 | } else { |
| 7002 | /* remember the found schema node and continue to make sure it can be used */ |
| 7003 | scnode = tmp; |
| 7004 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7005 | } |
| 7006 | } |
| 7007 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7008 | if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 7009 | /* try to create the predicates */ |
| 7010 | if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->format, &predicates, &pred_type)) { |
| 7011 | /* hashes cannot be used */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7012 | scnode = NULL; |
| 7013 | } |
| 7014 | } |
| 7015 | } |
| 7016 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7017 | if (!scnode && moveto_mod) { |
| 7018 | /* we are not able to match based on a schema node and not all the modules match, |
| 7019 | * use dictionary for efficient comparison */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 7020 | 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] | 7021 | } |
| 7022 | |
| 7023 | moveto: |
| 7024 | /* move to the attribute(s), data node(s), or schema node(s) */ |
| 7025 | if (attr_axis) { |
| 7026 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7027 | set_scnode_clear_ctx(set); |
| 7028 | } else { |
| 7029 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7030 | rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7031 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7032 | rc = moveto_attr(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7033 | } |
| 7034 | LY_CHECK_GOTO(rc, cleanup); |
| 7035 | } |
| 7036 | } else { |
| 7037 | if (set && (options & LYXP_SCNODE_ALL)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7038 | int64_t i; |
| 7039 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7040 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7041 | rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7042 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7043 | rc = moveto_scnode(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7044 | } |
| 7045 | LY_CHECK_GOTO(rc, cleanup); |
| 7046 | |
| 7047 | for (i = set->used - 1; i > -1; --i) { |
| 7048 | if (set->val.scnodes[i].in_ctx > 0) { |
| 7049 | break; |
| 7050 | } |
| 7051 | } |
| 7052 | if (i == -1) { |
| 7053 | path = lysc_path(set->ctx_scnode, LYSC_PATH_LOG, NULL, 0); |
| 7054 | 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] | 7055 | ncname_len, ncname, ncname - exp->expr, exp->expr, path); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7056 | free(path); |
| 7057 | } |
| 7058 | } else { |
| 7059 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7060 | rc = moveto_node_alldesc(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7061 | } else { |
| 7062 | if (scnode) { |
| 7063 | /* we can find the nodes using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7064 | rc = moveto_node_hash(set, scnode, predicates); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7065 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7066 | rc = moveto_node(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7067 | } |
| 7068 | } |
| 7069 | LY_CHECK_GOTO(rc, cleanup); |
| 7070 | } |
| 7071 | } |
| 7072 | |
| 7073 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7074 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7075 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7076 | LY_CHECK_RET(rc); |
| 7077 | } |
| 7078 | |
| 7079 | cleanup: |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7080 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7081 | lydict_remove(set->ctx, ncname_dict); |
| 7082 | ly_path_predicates_free(set->ctx, pred_type, scnode, predicates); |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7083 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7084 | return rc; |
| 7085 | } |
| 7086 | |
| 7087 | /** |
| 7088 | * @brief Evaluate NodeType and any following Predicates. Logs directly on error. |
| 7089 | * |
| 7090 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7091 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7092 | * [8] NodeType ::= 'text' | 'node' |
| 7093 | * |
| 7094 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7095 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7096 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 7097 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7098 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7099 | * @param[in] options XPath options. |
| 7100 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7101 | */ |
| 7102 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7103 | 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] | 7104 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7105 | { |
| 7106 | LY_ERR rc; |
| 7107 | |
| 7108 | /* TODO */ |
| 7109 | (void)attr_axis; |
| 7110 | (void)all_desc; |
| 7111 | |
| 7112 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7113 | assert(exp->tok_len[*tok_idx] == 4); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7114 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 7115 | set_scnode_clear_ctx(set); |
| 7116 | /* just for the debug message below */ |
| 7117 | set = NULL; |
| 7118 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7119 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7120 | rc = xpath_node(NULL, 0, set, options); |
| 7121 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7122 | assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7123 | rc = xpath_text(NULL, 0, set, options); |
| 7124 | } |
| 7125 | LY_CHECK_RET(rc); |
| 7126 | } |
| 7127 | } |
| 7128 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7129 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7130 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7131 | |
| 7132 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7133 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7134 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7135 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7136 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7137 | |
| 7138 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7139 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7140 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7141 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7142 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7143 | |
| 7144 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7145 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7146 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7147 | LY_CHECK_RET(rc); |
| 7148 | } |
| 7149 | |
| 7150 | return LY_SUCCESS; |
| 7151 | } |
| 7152 | |
| 7153 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7154 | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
| 7155 | * |
| 7156 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 7157 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7158 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7159 | * |
| 7160 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7161 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7162 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7163 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7164 | * @param[in] options XPath options. |
| 7165 | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
| 7166 | */ |
| 7167 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7168 | 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] | 7169 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7170 | ly_bool attr_axis; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7171 | LY_ERR rc; |
| 7172 | |
| 7173 | goto step; |
| 7174 | do { |
| 7175 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7176 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7177 | all_desc = 0; |
| 7178 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7179 | assert(exp->tok_len[*tok_idx] == 2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7180 | all_desc = 1; |
| 7181 | } |
| 7182 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7183 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7184 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7185 | |
| 7186 | step: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7187 | /* evaluate abbreviated axis '@'? if any */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7188 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7189 | attr_axis = 1; |
| 7190 | |
| 7191 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7192 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7193 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7194 | } else { |
| 7195 | attr_axis = 0; |
| 7196 | } |
| 7197 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7198 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7199 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7200 | case LYXP_TOKEN_DOT: |
| 7201 | /* evaluate '.' */ |
| 7202 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7203 | rc = moveto_scnode_self(set, all_desc, options); |
| 7204 | } else { |
| 7205 | rc = moveto_self(set, all_desc, options); |
| 7206 | } |
| 7207 | LY_CHECK_RET(rc); |
| 7208 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7209 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7210 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7211 | break; |
| 7212 | |
| 7213 | case LYXP_TOKEN_DDOT: |
| 7214 | /* evaluate '..' */ |
| 7215 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7216 | rc = moveto_scnode_parent(set, all_desc, options); |
| 7217 | } else { |
| 7218 | rc = moveto_parent(set, all_desc, options); |
| 7219 | } |
| 7220 | LY_CHECK_RET(rc); |
| 7221 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7222 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7223 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7224 | break; |
| 7225 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7226 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7227 | /* evaluate NameTest Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7228 | 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] | 7229 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7230 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7231 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7232 | case LYXP_TOKEN_NODETYPE: |
| 7233 | /* evaluate NodeType Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7234 | 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] | 7235 | LY_CHECK_RET(rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7236 | break; |
| 7237 | |
| 7238 | default: |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 7239 | LOGINT_RET(set ? set->ctx : NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7240 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7241 | } 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] | 7242 | |
| 7243 | return LY_SUCCESS; |
| 7244 | } |
| 7245 | |
| 7246 | /** |
| 7247 | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
| 7248 | * |
| 7249 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 7250 | * |
| 7251 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7252 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7253 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7254 | * @param[in] options XPath options. |
| 7255 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7256 | */ |
| 7257 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7258 | 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] | 7259 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7260 | ly_bool all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7261 | |
| 7262 | if (set) { |
| 7263 | /* no matter what tokens follow, we need to be at the root */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7264 | LY_CHECK_RET(moveto_root(set, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7265 | } |
| 7266 | |
| 7267 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7268 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7269 | /* evaluate '/' - deferred */ |
| 7270 | all_desc = 0; |
| 7271 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7272 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7273 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7274 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7275 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7276 | return LY_SUCCESS; |
| 7277 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7278 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7279 | case LYXP_TOKEN_DOT: |
| 7280 | case LYXP_TOKEN_DDOT: |
| 7281 | case LYXP_TOKEN_AT: |
| 7282 | case LYXP_TOKEN_NAMETEST: |
| 7283 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7284 | 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] | 7285 | break; |
| 7286 | default: |
| 7287 | break; |
| 7288 | } |
| 7289 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7290 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 7291 | /* '//' RelativeLocationPath */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7292 | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
| 7293 | all_desc = 1; |
| 7294 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7295 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7296 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7297 | |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7298 | 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] | 7299 | } |
| 7300 | |
| 7301 | return LY_SUCCESS; |
| 7302 | } |
| 7303 | |
| 7304 | /** |
| 7305 | * @brief Evaluate FunctionCall. Logs directly on error. |
| 7306 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7307 | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7308 | * |
| 7309 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7310 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7311 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7312 | * @param[in] options XPath options. |
| 7313 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7314 | */ |
| 7315 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7316 | 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] | 7317 | { |
| 7318 | LY_ERR rc; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7319 | 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] | 7320 | uint16_t arg_count = 0, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7321 | struct lyxp_set **args = NULL, **args_aux; |
| 7322 | |
| 7323 | if (set) { |
| 7324 | /* FunctionName */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7325 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7326 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7327 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7328 | xpath_func = &xpath_not; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7329 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7330 | xpath_func = &xpath_sum; |
| 7331 | } |
| 7332 | break; |
| 7333 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7334 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7335 | xpath_func = &xpath_lang; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7336 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7337 | xpath_func = &xpath_last; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7338 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7339 | xpath_func = &xpath_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7340 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7341 | xpath_func = &xpath_true; |
| 7342 | } |
| 7343 | break; |
| 7344 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7345 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7346 | xpath_func = &xpath_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7347 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7348 | xpath_func = &xpath_false; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7349 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7350 | xpath_func = &xpath_floor; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7351 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7352 | xpath_func = &xpath_round; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7353 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7354 | xpath_func = &xpath_deref; |
| 7355 | } |
| 7356 | break; |
| 7357 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7358 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7359 | xpath_func = &xpath_concat; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7360 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7361 | xpath_func = &xpath_number; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7362 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7363 | xpath_func = &xpath_string; |
| 7364 | } |
| 7365 | break; |
| 7366 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7367 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7368 | xpath_func = &xpath_boolean; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7369 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7370 | xpath_func = &xpath_ceiling; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7371 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7372 | xpath_func = &xpath_current; |
| 7373 | } |
| 7374 | break; |
| 7375 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7376 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7377 | xpath_func = &xpath_contains; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7378 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7379 | xpath_func = &xpath_position; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7380 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7381 | xpath_func = &xpath_re_match; |
| 7382 | } |
| 7383 | break; |
| 7384 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7385 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7386 | xpath_func = &xpath_substring; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7387 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7388 | xpath_func = &xpath_translate; |
| 7389 | } |
| 7390 | break; |
| 7391 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7392 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7393 | xpath_func = &xpath_local_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7394 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7395 | xpath_func = &xpath_enum_value; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7396 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7397 | xpath_func = &xpath_bit_is_set; |
| 7398 | } |
| 7399 | break; |
| 7400 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7401 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7402 | xpath_func = &xpath_starts_with; |
| 7403 | } |
| 7404 | break; |
| 7405 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7406 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7407 | xpath_func = &xpath_derived_from; |
| 7408 | } |
| 7409 | break; |
| 7410 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7411 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7412 | xpath_func = &xpath_namespace_uri; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7413 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7414 | xpath_func = &xpath_string_length; |
| 7415 | } |
| 7416 | break; |
| 7417 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7418 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7419 | xpath_func = &xpath_normalize_space; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7420 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7421 | xpath_func = &xpath_substring_after; |
| 7422 | } |
| 7423 | break; |
| 7424 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7425 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7426 | xpath_func = &xpath_substring_before; |
| 7427 | } |
| 7428 | break; |
| 7429 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7430 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7431 | xpath_func = &xpath_derived_from_or_self; |
| 7432 | } |
| 7433 | break; |
| 7434 | } |
| 7435 | |
| 7436 | if (!xpath_func) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7437 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7438 | return LY_EVALID; |
| 7439 | } |
| 7440 | } |
| 7441 | |
| 7442 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7443 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7444 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7445 | |
| 7446 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7447 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7448 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7449 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7450 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7451 | |
| 7452 | /* ( Expr ( ',' Expr )* )? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7453 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7454 | if (set) { |
| 7455 | args = malloc(sizeof *args); |
| 7456 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7457 | arg_count = 1; |
| 7458 | args[0] = set_copy(set); |
| 7459 | if (!args[0]) { |
| 7460 | rc = LY_EMEM; |
| 7461 | goto cleanup; |
| 7462 | } |
| 7463 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7464 | rc = eval_expr_select(exp, tok_idx, 0, args[0], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7465 | LY_CHECK_GOTO(rc, cleanup); |
| 7466 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7467 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7468 | LY_CHECK_GOTO(rc, cleanup); |
| 7469 | } |
| 7470 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7471 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7472 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7473 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7474 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7475 | |
| 7476 | if (set) { |
| 7477 | ++arg_count; |
| 7478 | args_aux = realloc(args, arg_count * sizeof *args); |
| 7479 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7480 | args = args_aux; |
| 7481 | args[arg_count - 1] = set_copy(set); |
| 7482 | if (!args[arg_count - 1]) { |
| 7483 | rc = LY_EMEM; |
| 7484 | goto cleanup; |
| 7485 | } |
| 7486 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7487 | rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7488 | LY_CHECK_GOTO(rc, cleanup); |
| 7489 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7490 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7491 | LY_CHECK_GOTO(rc, cleanup); |
| 7492 | } |
| 7493 | } |
| 7494 | |
| 7495 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7496 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7497 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7498 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7499 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7500 | |
| 7501 | if (set) { |
| 7502 | /* evaluate function */ |
| 7503 | rc = xpath_func(args, arg_count, set, options); |
| 7504 | |
| 7505 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7506 | /* merge all nodes from arg evaluations */ |
| 7507 | for (i = 0; i < arg_count; ++i) { |
| 7508 | set_scnode_clear_ctx(args[i]); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7509 | lyxp_set_scnode_merge(set, args[i]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7510 | } |
| 7511 | } |
| 7512 | } else { |
| 7513 | rc = LY_SUCCESS; |
| 7514 | } |
| 7515 | |
| 7516 | cleanup: |
| 7517 | for (i = 0; i < arg_count; ++i) { |
| 7518 | lyxp_set_free(args[i]); |
| 7519 | } |
| 7520 | free(args); |
| 7521 | |
| 7522 | return rc; |
| 7523 | } |
| 7524 | |
| 7525 | /** |
| 7526 | * @brief Evaluate Number. Logs directly on error. |
| 7527 | * |
| 7528 | * @param[in] ctx Context for errors. |
| 7529 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7530 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7531 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7532 | * @return LY_ERR |
| 7533 | */ |
| 7534 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7535 | eval_number(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7536 | { |
| 7537 | long double num; |
| 7538 | char *endptr; |
| 7539 | |
| 7540 | if (set) { |
| 7541 | errno = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7542 | num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7543 | if (errno) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7544 | LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7545 | LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).", |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7546 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7547 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7548 | } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) { |
| 7549 | LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7550 | LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.", |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7551 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7552 | return LY_EVALID; |
| 7553 | } |
| 7554 | |
| 7555 | set_fill_number(set, num); |
| 7556 | } |
| 7557 | |
| 7558 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7559 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7560 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7561 | return LY_SUCCESS; |
| 7562 | } |
| 7563 | |
| 7564 | /** |
| 7565 | * @brief Evaluate PathExpr. Logs directly on error. |
| 7566 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7567 | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7568 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 7569 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 7570 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7571 | * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7572 | * |
| 7573 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7574 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7575 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7576 | * @param[in] options XPath options. |
| 7577 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7578 | */ |
| 7579 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7580 | 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] | 7581 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7582 | ly_bool all_desc, parent_pos_pred; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7583 | LY_ERR rc; |
| 7584 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7585 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7586 | case LYXP_TOKEN_PAR1: |
| 7587 | /* '(' Expr ')' */ |
| 7588 | |
| 7589 | /* '(' */ |
| 7590 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7591 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7592 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7593 | |
| 7594 | /* Expr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7595 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7596 | LY_CHECK_RET(rc); |
| 7597 | |
| 7598 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7599 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7600 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7601 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7602 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7603 | |
| 7604 | parent_pos_pred = 0; |
| 7605 | goto predicate; |
| 7606 | |
| 7607 | case LYXP_TOKEN_DOT: |
| 7608 | case LYXP_TOKEN_DDOT: |
| 7609 | case LYXP_TOKEN_AT: |
| 7610 | case LYXP_TOKEN_NAMETEST: |
| 7611 | case LYXP_TOKEN_NODETYPE: |
| 7612 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7613 | rc = eval_relative_location_path(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7614 | LY_CHECK_RET(rc); |
| 7615 | break; |
| 7616 | |
| 7617 | case LYXP_TOKEN_FUNCNAME: |
| 7618 | /* FunctionCall */ |
| 7619 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7620 | rc = eval_function_call(exp, tok_idx, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7621 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7622 | rc = eval_function_call(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7623 | } |
| 7624 | LY_CHECK_RET(rc); |
| 7625 | |
| 7626 | parent_pos_pred = 1; |
| 7627 | goto predicate; |
| 7628 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 7629 | case LYXP_TOKEN_OPER_PATH: |
| 7630 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7631 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7632 | rc = eval_absolute_location_path(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7633 | LY_CHECK_RET(rc); |
| 7634 | break; |
| 7635 | |
| 7636 | case LYXP_TOKEN_LITERAL: |
| 7637 | /* Literal */ |
| 7638 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7639 | if (set) { |
| 7640 | set_scnode_clear_ctx(set); |
| 7641 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7642 | eval_literal(exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7643 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7644 | eval_literal(exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7645 | } |
| 7646 | |
| 7647 | parent_pos_pred = 1; |
| 7648 | goto predicate; |
| 7649 | |
| 7650 | case LYXP_TOKEN_NUMBER: |
| 7651 | /* Number */ |
| 7652 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7653 | if (set) { |
| 7654 | set_scnode_clear_ctx(set); |
| 7655 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7656 | rc = eval_number(NULL, exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7657 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7658 | rc = eval_number(set->ctx, exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7659 | } |
| 7660 | LY_CHECK_RET(rc); |
| 7661 | |
| 7662 | parent_pos_pred = 1; |
| 7663 | goto predicate; |
| 7664 | |
| 7665 | default: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7666 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), |
| 7667 | &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7668 | return LY_EVALID; |
| 7669 | } |
| 7670 | |
| 7671 | return LY_SUCCESS; |
| 7672 | |
| 7673 | predicate: |
| 7674 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7675 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7676 | rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7677 | LY_CHECK_RET(rc); |
| 7678 | } |
| 7679 | |
| 7680 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7681 | if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7682 | |
| 7683 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7684 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7685 | all_desc = 0; |
| 7686 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7687 | all_desc = 1; |
| 7688 | } |
| 7689 | |
| 7690 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7691 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7692 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7693 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7694 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7695 | LY_CHECK_RET(rc); |
| 7696 | } |
| 7697 | |
| 7698 | return LY_SUCCESS; |
| 7699 | } |
| 7700 | |
| 7701 | /** |
| 7702 | * @brief Evaluate UnionExpr. Logs directly on error. |
| 7703 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7704 | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7705 | * |
| 7706 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7707 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7708 | * @param[in] repeat How many times this expression is repeated. |
| 7709 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7710 | * @param[in] options XPath options. |
| 7711 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7712 | */ |
| 7713 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7714 | 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] | 7715 | { |
| 7716 | LY_ERR rc = LY_SUCCESS; |
| 7717 | struct lyxp_set orig_set, set2; |
| 7718 | uint16_t i; |
| 7719 | |
| 7720 | assert(repeat); |
| 7721 | |
| 7722 | set_init(&orig_set, set); |
| 7723 | set_init(&set2, set); |
| 7724 | |
| 7725 | set_fill_set(&orig_set, set); |
| 7726 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7727 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7728 | LY_CHECK_GOTO(rc, cleanup); |
| 7729 | |
| 7730 | /* ('|' PathExpr)* */ |
| 7731 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7732 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7733 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7734 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7735 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7736 | |
| 7737 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7738 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7739 | LY_CHECK_GOTO(rc, cleanup); |
| 7740 | continue; |
| 7741 | } |
| 7742 | |
| 7743 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7744 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7745 | LY_CHECK_GOTO(rc, cleanup); |
| 7746 | |
| 7747 | /* eval */ |
| 7748 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7749 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7750 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7751 | rc = moveto_union(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7752 | LY_CHECK_GOTO(rc, cleanup); |
| 7753 | } |
| 7754 | } |
| 7755 | |
| 7756 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7757 | lyxp_set_free_content(&orig_set); |
| 7758 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7759 | return rc; |
| 7760 | } |
| 7761 | |
| 7762 | /** |
| 7763 | * @brief Evaluate UnaryExpr. Logs directly on error. |
| 7764 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7765 | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7766 | * |
| 7767 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7768 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7769 | * @param[in] repeat How many times this expression is repeated. |
| 7770 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7771 | * @param[in] options XPath options. |
| 7772 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7773 | */ |
| 7774 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7775 | 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] | 7776 | { |
| 7777 | LY_ERR rc; |
| 7778 | uint16_t this_op, i; |
| 7779 | |
| 7780 | assert(repeat); |
| 7781 | |
| 7782 | /* ('-')+ */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7783 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7784 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7785 | assert(!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && (exp->expr[exp->tok_pos[*tok_idx]] == '-')); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7786 | |
| 7787 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7788 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7789 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7790 | } |
| 7791 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7792 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7793 | LY_CHECK_RET(rc); |
| 7794 | |
| 7795 | if (set && (repeat % 2)) { |
| 7796 | if (options & LYXP_SCNODE_ALL) { |
| 7797 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
| 7798 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7799 | rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7800 | LY_CHECK_RET(rc); |
| 7801 | } |
| 7802 | } |
| 7803 | |
| 7804 | return LY_SUCCESS; |
| 7805 | } |
| 7806 | |
| 7807 | /** |
| 7808 | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
| 7809 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7810 | * [18] MultiplicativeExpr ::= UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7811 | * | MultiplicativeExpr '*' UnaryExpr |
| 7812 | * | MultiplicativeExpr 'div' UnaryExpr |
| 7813 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 7814 | * |
| 7815 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7816 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7817 | * @param[in] repeat How many times this expression is repeated. |
| 7818 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7819 | * @param[in] options XPath options. |
| 7820 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7821 | */ |
| 7822 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7823 | 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] | 7824 | { |
| 7825 | LY_ERR rc; |
| 7826 | uint16_t this_op; |
| 7827 | struct lyxp_set orig_set, set2; |
| 7828 | uint16_t i; |
| 7829 | |
| 7830 | assert(repeat); |
| 7831 | |
| 7832 | set_init(&orig_set, set); |
| 7833 | set_init(&set2, set); |
| 7834 | |
| 7835 | set_fill_set(&orig_set, set); |
| 7836 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7837 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7838 | LY_CHECK_GOTO(rc, cleanup); |
| 7839 | |
| 7840 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 7841 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7842 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7843 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7844 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7845 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7846 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7847 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7848 | |
| 7849 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7850 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7851 | LY_CHECK_GOTO(rc, cleanup); |
| 7852 | continue; |
| 7853 | } |
| 7854 | |
| 7855 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7856 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7857 | LY_CHECK_GOTO(rc, cleanup); |
| 7858 | |
| 7859 | /* eval */ |
| 7860 | if (options & LYXP_SCNODE_ALL) { |
| 7861 | warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7862 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7863 | set_scnode_clear_ctx(set); |
| 7864 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7865 | rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7866 | LY_CHECK_GOTO(rc, cleanup); |
| 7867 | } |
| 7868 | } |
| 7869 | |
| 7870 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7871 | lyxp_set_free_content(&orig_set); |
| 7872 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7873 | return rc; |
| 7874 | } |
| 7875 | |
| 7876 | /** |
| 7877 | * @brief Evaluate AdditiveExpr. Logs directly on error. |
| 7878 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7879 | * [17] AdditiveExpr ::= MultiplicativeExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7880 | * | AdditiveExpr '+' MultiplicativeExpr |
| 7881 | * | AdditiveExpr '-' MultiplicativeExpr |
| 7882 | * |
| 7883 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7884 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7885 | * @param[in] repeat How many times this expression is repeated. |
| 7886 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7887 | * @param[in] options XPath options. |
| 7888 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7889 | */ |
| 7890 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7891 | 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] | 7892 | { |
| 7893 | LY_ERR rc; |
| 7894 | uint16_t this_op; |
| 7895 | struct lyxp_set orig_set, set2; |
| 7896 | uint16_t i; |
| 7897 | |
| 7898 | assert(repeat); |
| 7899 | |
| 7900 | set_init(&orig_set, set); |
| 7901 | set_init(&set2, set); |
| 7902 | |
| 7903 | set_fill_set(&orig_set, set); |
| 7904 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7905 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7906 | LY_CHECK_GOTO(rc, cleanup); |
| 7907 | |
| 7908 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 7909 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7910 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7911 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7912 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7913 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7914 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7915 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7916 | |
| 7917 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7918 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7919 | LY_CHECK_GOTO(rc, cleanup); |
| 7920 | continue; |
| 7921 | } |
| 7922 | |
| 7923 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7924 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7925 | LY_CHECK_GOTO(rc, cleanup); |
| 7926 | |
| 7927 | /* eval */ |
| 7928 | if (options & LYXP_SCNODE_ALL) { |
| 7929 | warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7930 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7931 | set_scnode_clear_ctx(set); |
| 7932 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7933 | rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7934 | LY_CHECK_GOTO(rc, cleanup); |
| 7935 | } |
| 7936 | } |
| 7937 | |
| 7938 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7939 | lyxp_set_free_content(&orig_set); |
| 7940 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7941 | return rc; |
| 7942 | } |
| 7943 | |
| 7944 | /** |
| 7945 | * @brief Evaluate RelationalExpr. Logs directly on error. |
| 7946 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7947 | * [16] RelationalExpr ::= AdditiveExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7948 | * | RelationalExpr '<' AdditiveExpr |
| 7949 | * | RelationalExpr '>' AdditiveExpr |
| 7950 | * | RelationalExpr '<=' AdditiveExpr |
| 7951 | * | RelationalExpr '>=' AdditiveExpr |
| 7952 | * |
| 7953 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7954 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7955 | * @param[in] repeat How many times this expression is repeated. |
| 7956 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7957 | * @param[in] options XPath options. |
| 7958 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7959 | */ |
| 7960 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7961 | 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] | 7962 | { |
| 7963 | LY_ERR rc; |
| 7964 | uint16_t this_op; |
| 7965 | struct lyxp_set orig_set, set2; |
| 7966 | uint16_t i; |
| 7967 | |
| 7968 | assert(repeat); |
| 7969 | |
| 7970 | set_init(&orig_set, set); |
| 7971 | set_init(&set2, set); |
| 7972 | |
| 7973 | set_fill_set(&orig_set, set); |
| 7974 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7975 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7976 | LY_CHECK_GOTO(rc, cleanup); |
| 7977 | |
| 7978 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 7979 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7980 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7981 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7982 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7983 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7984 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7985 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7986 | |
| 7987 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7988 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7989 | LY_CHECK_GOTO(rc, cleanup); |
| 7990 | continue; |
| 7991 | } |
| 7992 | |
| 7993 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7994 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7995 | LY_CHECK_GOTO(rc, cleanup); |
| 7996 | |
| 7997 | /* eval */ |
| 7998 | if (options & LYXP_SCNODE_ALL) { |
| 7999 | warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8000 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8001 | set_scnode_clear_ctx(set); |
| 8002 | } else { |
| 8003 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8004 | LY_CHECK_GOTO(rc, cleanup); |
| 8005 | } |
| 8006 | } |
| 8007 | |
| 8008 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8009 | lyxp_set_free_content(&orig_set); |
| 8010 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8011 | return rc; |
| 8012 | } |
| 8013 | |
| 8014 | /** |
| 8015 | * @brief Evaluate EqualityExpr. Logs directly on error. |
| 8016 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8017 | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8018 | * | EqualityExpr '!=' RelationalExpr |
| 8019 | * |
| 8020 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8021 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8022 | * @param[in] repeat How many times this expression is repeated. |
| 8023 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8024 | * @param[in] options XPath options. |
| 8025 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8026 | */ |
| 8027 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8028 | 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] | 8029 | { |
| 8030 | LY_ERR rc; |
| 8031 | uint16_t this_op; |
| 8032 | struct lyxp_set orig_set, set2; |
| 8033 | uint16_t i; |
| 8034 | |
| 8035 | assert(repeat); |
| 8036 | |
| 8037 | set_init(&orig_set, set); |
| 8038 | set_init(&set2, set); |
| 8039 | |
| 8040 | set_fill_set(&orig_set, set); |
| 8041 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8042 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8043 | LY_CHECK_GOTO(rc, cleanup); |
| 8044 | |
| 8045 | /* ('=' / '!=' RelationalExpr)* */ |
| 8046 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8047 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8048 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8049 | assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8050 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8051 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8052 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8053 | |
| 8054 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8055 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8056 | LY_CHECK_GOTO(rc, cleanup); |
| 8057 | continue; |
| 8058 | } |
| 8059 | |
| 8060 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8061 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8062 | LY_CHECK_GOTO(rc, cleanup); |
| 8063 | |
| 8064 | /* eval */ |
| 8065 | if (options & LYXP_SCNODE_ALL) { |
| 8066 | warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8067 | warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1); |
| 8068 | warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8069 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8070 | set_scnode_clear_ctx(set); |
| 8071 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8072 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8073 | LY_CHECK_GOTO(rc, cleanup); |
| 8074 | } |
| 8075 | } |
| 8076 | |
| 8077 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8078 | lyxp_set_free_content(&orig_set); |
| 8079 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8080 | return rc; |
| 8081 | } |
| 8082 | |
| 8083 | /** |
| 8084 | * @brief Evaluate AndExpr. Logs directly on error. |
| 8085 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8086 | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8087 | * |
| 8088 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8089 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8090 | * @param[in] repeat How many times this expression is repeated. |
| 8091 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8092 | * @param[in] options XPath options. |
| 8093 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8094 | */ |
| 8095 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8096 | 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] | 8097 | { |
| 8098 | LY_ERR rc; |
| 8099 | struct lyxp_set orig_set, set2; |
| 8100 | uint16_t i; |
| 8101 | |
| 8102 | assert(repeat); |
| 8103 | |
| 8104 | set_init(&orig_set, set); |
| 8105 | set_init(&set2, set); |
| 8106 | |
| 8107 | set_fill_set(&orig_set, set); |
| 8108 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8109 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8110 | LY_CHECK_GOTO(rc, cleanup); |
| 8111 | |
| 8112 | /* cast to boolean, we know that will be the final result */ |
| 8113 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8114 | set_scnode_clear_ctx(set); |
| 8115 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8116 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8117 | } |
| 8118 | |
| 8119 | /* ('and' EqualityExpr)* */ |
| 8120 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8121 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8122 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"), |
| 8123 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8124 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8125 | |
| 8126 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8127 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) { |
| 8128 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8129 | LY_CHECK_GOTO(rc, cleanup); |
| 8130 | continue; |
| 8131 | } |
| 8132 | |
| 8133 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8134 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8135 | LY_CHECK_GOTO(rc, cleanup); |
| 8136 | |
| 8137 | /* eval - just get boolean value actually */ |
| 8138 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8139 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8140 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8141 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8142 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8143 | set_fill_set(set, &set2); |
| 8144 | } |
| 8145 | } |
| 8146 | |
| 8147 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8148 | lyxp_set_free_content(&orig_set); |
| 8149 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8150 | return rc; |
| 8151 | } |
| 8152 | |
| 8153 | /** |
| 8154 | * @brief Evaluate OrExpr. Logs directly on error. |
| 8155 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8156 | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8157 | * |
| 8158 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8159 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8160 | * @param[in] repeat How many times this expression is repeated. |
| 8161 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8162 | * @param[in] options XPath options. |
| 8163 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8164 | */ |
| 8165 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8166 | 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] | 8167 | { |
| 8168 | LY_ERR rc; |
| 8169 | struct lyxp_set orig_set, set2; |
| 8170 | uint16_t i; |
| 8171 | |
| 8172 | assert(repeat); |
| 8173 | |
| 8174 | set_init(&orig_set, set); |
| 8175 | set_init(&set2, set); |
| 8176 | |
| 8177 | set_fill_set(&orig_set, set); |
| 8178 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8179 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8180 | LY_CHECK_GOTO(rc, cleanup); |
| 8181 | |
| 8182 | /* cast to boolean, we know that will be the final result */ |
| 8183 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8184 | set_scnode_clear_ctx(set); |
| 8185 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8186 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8187 | } |
| 8188 | |
| 8189 | /* ('or' AndExpr)* */ |
| 8190 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8191 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8192 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"), |
| 8193 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8194 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8195 | |
| 8196 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8197 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) { |
| 8198 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8199 | LY_CHECK_GOTO(rc, cleanup); |
| 8200 | continue; |
| 8201 | } |
| 8202 | |
| 8203 | set_fill_set(&set2, &orig_set); |
| 8204 | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
| 8205 | * but it does not matter */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8206 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8207 | LY_CHECK_GOTO(rc, cleanup); |
| 8208 | |
| 8209 | /* eval - just get boolean value actually */ |
| 8210 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8211 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8212 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8213 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8214 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8215 | set_fill_set(set, &set2); |
| 8216 | } |
| 8217 | } |
| 8218 | |
| 8219 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8220 | lyxp_set_free_content(&orig_set); |
| 8221 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8222 | return rc; |
| 8223 | } |
| 8224 | |
| 8225 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8226 | * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8227 | * |
| 8228 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8229 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8230 | * @param[in] etype Expression type to evaluate. |
| 8231 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8232 | * @param[in] options XPath options. |
| 8233 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8234 | */ |
| 8235 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8236 | 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] | 8237 | { |
| 8238 | uint16_t i, count; |
| 8239 | enum lyxp_expr_type next_etype; |
| 8240 | LY_ERR rc; |
| 8241 | |
| 8242 | /* process operator repeats */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8243 | if (!exp->repeat[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8244 | next_etype = LYXP_EXPR_NONE; |
| 8245 | } else { |
| 8246 | /* find etype repeat */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8247 | for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8248 | |
| 8249 | /* select one-priority lower because etype expression called us */ |
| 8250 | if (i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8251 | next_etype = exp->repeat[*tok_idx][i - 1]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8252 | /* count repeats for that expression */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8253 | for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8254 | } else { |
| 8255 | next_etype = LYXP_EXPR_NONE; |
| 8256 | } |
| 8257 | } |
| 8258 | |
| 8259 | /* decide what expression are we parsing based on the repeat */ |
| 8260 | switch (next_etype) { |
| 8261 | case LYXP_EXPR_OR: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8262 | rc = eval_or_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8263 | break; |
| 8264 | case LYXP_EXPR_AND: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8265 | rc = eval_and_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8266 | break; |
| 8267 | case LYXP_EXPR_EQUALITY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8268 | rc = eval_equality_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8269 | break; |
| 8270 | case LYXP_EXPR_RELATIONAL: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8271 | rc = eval_relational_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8272 | break; |
| 8273 | case LYXP_EXPR_ADDITIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8274 | rc = eval_additive_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8275 | break; |
| 8276 | case LYXP_EXPR_MULTIPLICATIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8277 | rc = eval_multiplicative_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8278 | break; |
| 8279 | case LYXP_EXPR_UNARY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8280 | rc = eval_unary_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8281 | break; |
| 8282 | case LYXP_EXPR_UNION: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8283 | rc = eval_union_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8284 | break; |
| 8285 | case LYXP_EXPR_NONE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8286 | rc = eval_path_expr(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8287 | break; |
| 8288 | default: |
| 8289 | LOGINT_RET(set->ctx); |
| 8290 | } |
| 8291 | |
| 8292 | return rc; |
| 8293 | } |
| 8294 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8295 | /** |
| 8296 | * @brief Get root type. |
| 8297 | * |
| 8298 | * @param[in] ctx_node Context node. |
| 8299 | * @param[in] ctx_scnode Schema context node. |
| 8300 | * @param[in] options XPath options. |
| 8301 | * @return Root type. |
| 8302 | */ |
| 8303 | static enum lyxp_node_type |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8304 | 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] | 8305 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8306 | const struct lysc_node *op; |
| 8307 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8308 | if (options & LYXP_SCNODE_ALL) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8309 | 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] | 8310 | |
| 8311 | if (op || (options & LYXP_SCNODE)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8312 | /* general root that can access everything */ |
| 8313 | return LYXP_NODE_ROOT; |
| 8314 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
| 8315 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8316 | return LYXP_NODE_ROOT_CONFIG; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8317 | } |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8318 | return LYXP_NODE_ROOT; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8319 | } |
| 8320 | |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8321 | op = ctx_node ? ctx_node->schema : NULL; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 8322 | 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] | 8323 | |
| 8324 | if (!ctx_node || (!op && (ctx_node->schema->flags & LYS_CONFIG_W))) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8325 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8326 | return LYXP_NODE_ROOT_CONFIG; |
| 8327 | } |
| 8328 | |
| 8329 | return LYXP_NODE_ROOT; |
| 8330 | } |
| 8331 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8332 | LY_ERR |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 8333 | 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] | 8334 | 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] | 8335 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8336 | uint16_t tok_idx = 0; |
| 8337 | const struct lyd_node *real_ctx_node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8338 | LY_ERR rc; |
| 8339 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8340 | LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_node, set, LY_EINVAL); |
| 8341 | |
| 8342 | if ((ctx_node_type == LYXP_NODE_ROOT) || (ctx_node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 8343 | /* we always need some context node because it is used for resolving unqualified names */ |
| 8344 | real_ctx_node = NULL; |
| 8345 | } else { |
| 8346 | real_ctx_node = ctx_node; |
| 8347 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8348 | |
| 8349 | /* prepare set for evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8350 | tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8351 | memset(set, 0, sizeof *set); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8352 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8353 | 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] | 8354 | set->ctx = local_mod->ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8355 | set->ctx_node = ctx_node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8356 | set->root_type = lyxp_get_root_type(real_ctx_node, NULL, options); |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8357 | for (set->context_op = ctx_node->schema; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 8358 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8359 | set->context_op = set->context_op->parent) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8360 | set->local_mod = local_mod; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 8361 | set->tree = tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8362 | set->format = format; |
| 8363 | |
| 8364 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8365 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8366 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8367 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8368 | } |
| 8369 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8370 | return rc; |
| 8371 | } |
| 8372 | |
| 8373 | #if 0 |
| 8374 | |
| 8375 | /* full xml printing of set elements, not used currently */ |
| 8376 | |
| 8377 | void |
| 8378 | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
| 8379 | { |
| 8380 | uint32_t i; |
| 8381 | char *str_num; |
| 8382 | struct lyout out; |
| 8383 | |
| 8384 | memset(&out, 0, sizeof out); |
| 8385 | |
| 8386 | out.type = LYOUT_STREAM; |
| 8387 | out.method.f = f; |
| 8388 | |
| 8389 | switch (set->type) { |
| 8390 | case LYXP_SET_EMPTY: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8391 | ly_print_(&out, "Empty XPath set\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8392 | break; |
| 8393 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8394 | ly_print_(&out, "Boolean XPath set:\n"); |
| 8395 | ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8396 | break; |
| 8397 | case LYXP_SET_STRING: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8398 | ly_print_(&out, "String XPath set:\n"); |
| 8399 | ly_print_(&out, "\"%s\"\n\n", set->value.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8400 | break; |
| 8401 | case LYXP_SET_NUMBER: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8402 | ly_print_(&out, "Number XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8403 | |
| 8404 | if (isnan(set->value.num)) { |
| 8405 | str_num = strdup("NaN"); |
| 8406 | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
| 8407 | str_num = strdup("0"); |
| 8408 | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
| 8409 | str_num = strdup("Infinity"); |
| 8410 | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
| 8411 | str_num = strdup("-Infinity"); |
| 8412 | } else if ((long long)set->value.num == set->value.num) { |
| 8413 | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
| 8414 | str_num = NULL; |
| 8415 | } |
| 8416 | } else { |
| 8417 | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
| 8418 | str_num = NULL; |
| 8419 | } |
| 8420 | } |
| 8421 | if (!str_num) { |
| 8422 | LOGMEM; |
| 8423 | return; |
| 8424 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8425 | ly_print_(&out, "%s\n\n", str_num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8426 | free(str_num); |
| 8427 | break; |
| 8428 | case LYXP_SET_NODE_SET: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8429 | ly_print_(&out, "Node XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8430 | |
| 8431 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8432 | ly_print_(&out, "%d. ", i + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8433 | switch (set->node_type[i]) { |
| 8434 | case LYXP_NODE_ROOT_ALL: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8435 | ly_print_(&out, "ROOT all\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8436 | break; |
| 8437 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8438 | ly_print_(&out, "ROOT config\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8439 | break; |
| 8440 | case LYXP_NODE_ROOT_STATE: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8441 | ly_print_(&out, "ROOT state\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8442 | break; |
| 8443 | case LYXP_NODE_ROOT_NOTIF: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8444 | 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] | 8445 | break; |
| 8446 | case LYXP_NODE_ROOT_RPC: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8447 | 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] | 8448 | break; |
| 8449 | case LYXP_NODE_ROOT_OUTPUT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8450 | 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] | 8451 | break; |
| 8452 | case LYXP_NODE_ELEM: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8453 | ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8454 | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8455 | ly_print_(&out, "\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8456 | break; |
| 8457 | case LYXP_NODE_TEXT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8458 | 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] | 8459 | break; |
| 8460 | case LYXP_NODE_ATTR: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8461 | 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] | 8462 | break; |
| 8463 | } |
| 8464 | } |
| 8465 | break; |
| 8466 | } |
| 8467 | } |
| 8468 | |
| 8469 | #endif |
| 8470 | |
| 8471 | LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8472 | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8473 | { |
| 8474 | long double num; |
| 8475 | char *str; |
| 8476 | LY_ERR rc; |
| 8477 | |
| 8478 | if (!set || (set->type == target)) { |
| 8479 | return LY_SUCCESS; |
| 8480 | } |
| 8481 | |
| 8482 | /* it's not possible to convert anything into a node set */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8483 | assert(target != LYXP_SET_NODE_SET); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8484 | |
| 8485 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8486 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8487 | return LY_EINVAL; |
| 8488 | } |
| 8489 | |
| 8490 | /* to STRING */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8491 | 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] | 8492 | switch (set->type) { |
| 8493 | case LYXP_SET_NUMBER: |
| 8494 | if (isnan(set->val.num)) { |
| 8495 | set->val.str = strdup("NaN"); |
| 8496 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8497 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 8498 | set->val.str = strdup("0"); |
| 8499 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8500 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 8501 | set->val.str = strdup("Infinity"); |
| 8502 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8503 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 8504 | set->val.str = strdup("-Infinity"); |
| 8505 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8506 | } else if ((long long)set->val.num == set->val.num) { |
| 8507 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 8508 | LOGMEM_RET(set->ctx); |
| 8509 | } |
| 8510 | set->val.str = str; |
| 8511 | } else { |
| 8512 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 8513 | LOGMEM_RET(set->ctx); |
| 8514 | } |
| 8515 | set->val.str = str; |
| 8516 | } |
| 8517 | break; |
| 8518 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8519 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8520 | set->val.str = strdup("true"); |
| 8521 | } else { |
| 8522 | set->val.str = strdup("false"); |
| 8523 | } |
| 8524 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 8525 | break; |
| 8526 | case LYXP_SET_NODE_SET: |
| 8527 | assert(set->used); |
| 8528 | |
| 8529 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8530 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8531 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8532 | rc = cast_node_set_to_string(set, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8533 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8534 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8535 | set->val.str = str; |
| 8536 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8537 | default: |
| 8538 | LOGINT_RET(set->ctx); |
| 8539 | } |
| 8540 | set->type = LYXP_SET_STRING; |
| 8541 | } |
| 8542 | |
| 8543 | /* to NUMBER */ |
| 8544 | if (target == LYXP_SET_NUMBER) { |
| 8545 | switch (set->type) { |
| 8546 | case LYXP_SET_STRING: |
| 8547 | num = cast_string_to_number(set->val.str); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8548 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8549 | set->val.num = num; |
| 8550 | break; |
| 8551 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8552 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8553 | set->val.num = 1; |
| 8554 | } else { |
| 8555 | set->val.num = 0; |
| 8556 | } |
| 8557 | break; |
| 8558 | default: |
| 8559 | LOGINT_RET(set->ctx); |
| 8560 | } |
| 8561 | set->type = LYXP_SET_NUMBER; |
| 8562 | } |
| 8563 | |
| 8564 | /* to BOOLEAN */ |
| 8565 | if (target == LYXP_SET_BOOLEAN) { |
| 8566 | switch (set->type) { |
| 8567 | case LYXP_SET_NUMBER: |
| 8568 | 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] | 8569 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8570 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8571 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8572 | } |
| 8573 | break; |
| 8574 | case LYXP_SET_STRING: |
| 8575 | if (set->val.str[0]) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8576 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8577 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8578 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8579 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8580 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8581 | } |
| 8582 | break; |
| 8583 | case LYXP_SET_NODE_SET: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8584 | if (set->used) { |
| 8585 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8586 | set->val.bln = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8587 | } else { |
| 8588 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8589 | set->val.bln = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8590 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8591 | break; |
| 8592 | default: |
| 8593 | LOGINT_RET(set->ctx); |
| 8594 | } |
| 8595 | set->type = LYXP_SET_BOOLEAN; |
| 8596 | } |
| 8597 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8598 | return LY_SUCCESS; |
| 8599 | } |
| 8600 | |
| 8601 | LY_ERR |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 8602 | 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] | 8603 | 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] | 8604 | { |
| 8605 | struct ly_ctx *ctx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8606 | const struct lysc_node *real_ctx_scnode; |
| 8607 | uint16_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8608 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8609 | 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] | 8610 | |
| 8611 | ctx = local_mod->ctx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8612 | if ((ctx_scnode_type == LYXP_NODE_ROOT) || (ctx_scnode_type == LYXP_NODE_ROOT_CONFIG)) { |
| 8613 | /* we always need some context node because it is used for resolving unqualified names */ |
| 8614 | real_ctx_scnode = NULL; |
| 8615 | } else { |
| 8616 | real_ctx_scnode = ctx_scnode; |
| 8617 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8618 | |
| 8619 | /* prepare set for evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8620 | tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8621 | memset(set, 0, sizeof *set); |
| 8622 | set->type = LYXP_SET_SCNODE_SET; |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 8623 | 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] | 8624 | set->val.scnodes[0].in_ctx = -2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8625 | set->ctx = ctx; |
| 8626 | set->ctx_scnode = ctx_scnode; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8627 | set->root_type = lyxp_get_root_type(NULL, real_ctx_scnode, options); |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8628 | for (set->context_op = ctx_scnode; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 8629 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8630 | set->context_op = set->context_op->parent) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8631 | set->local_mod = local_mod; |
| 8632 | set->format = format; |
| 8633 | |
| 8634 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8635 | return eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8636 | } |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 8637 | |
| 8638 | API const char * |
| 8639 | lyxp_get_expr(const struct lyxp_expr *path) |
| 8640 | { |
| 8641 | if (!path) { |
| 8642 | return NULL; |
| 8643 | } |
| 8644 | |
| 8645 | return path->expr; |
| 8646 | } |