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" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 39 | #include "out.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 40 | #include "printer_data.h" |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 41 | #include "schema_compile_node.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 42 | #include "tree.h" |
| 43 | #include "tree_data_internal.h" |
| 44 | #include "tree_schema_internal.h" |
| 45 | #include "xml.h" |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 46 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 47 | 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] | 48 | 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] | 49 | |
| 50 | /** |
| 51 | * @brief Print the type of an XPath \p set. |
| 52 | * |
| 53 | * @param[in] set Set to use. |
| 54 | * @return Set type string. |
| 55 | */ |
| 56 | static const char * |
| 57 | print_set_type(struct lyxp_set *set) |
| 58 | { |
| 59 | switch (set->type) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 60 | case LYXP_SET_NODE_SET: |
| 61 | return "node set"; |
| 62 | case LYXP_SET_SCNODE_SET: |
| 63 | return "schema node set"; |
| 64 | case LYXP_SET_BOOLEAN: |
| 65 | return "boolean"; |
| 66 | case LYXP_SET_NUMBER: |
| 67 | return "number"; |
| 68 | case LYXP_SET_STRING: |
| 69 | return "string"; |
| 70 | } |
| 71 | |
| 72 | return NULL; |
| 73 | } |
| 74 | |
Michal Vasko | 24cddf8 | 2020-06-01 08:17:01 +0200 | [diff] [blame] | 75 | const char * |
| 76 | lyxp_print_token(enum lyxp_token tok) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 77 | { |
| 78 | switch (tok) { |
| 79 | case LYXP_TOKEN_PAR1: |
| 80 | return "("; |
| 81 | case LYXP_TOKEN_PAR2: |
| 82 | return ")"; |
| 83 | case LYXP_TOKEN_BRACK1: |
| 84 | return "["; |
| 85 | case LYXP_TOKEN_BRACK2: |
| 86 | return "]"; |
| 87 | case LYXP_TOKEN_DOT: |
| 88 | return "."; |
| 89 | case LYXP_TOKEN_DDOT: |
| 90 | return ".."; |
| 91 | case LYXP_TOKEN_AT: |
| 92 | return "@"; |
| 93 | case LYXP_TOKEN_COMMA: |
| 94 | return ","; |
| 95 | case LYXP_TOKEN_NAMETEST: |
| 96 | return "NameTest"; |
| 97 | case LYXP_TOKEN_NODETYPE: |
| 98 | return "NodeType"; |
| 99 | case LYXP_TOKEN_FUNCNAME: |
| 100 | return "FunctionName"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 101 | case LYXP_TOKEN_OPER_LOG: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 102 | return "Operator(Logic)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 103 | case LYXP_TOKEN_OPER_EQUAL: |
| 104 | return "Operator(Equal)"; |
| 105 | case LYXP_TOKEN_OPER_NEQUAL: |
| 106 | return "Operator(Non-equal)"; |
| 107 | case LYXP_TOKEN_OPER_COMP: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 108 | return "Operator(Comparison)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 109 | case LYXP_TOKEN_OPER_MATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 110 | return "Operator(Math)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 111 | case LYXP_TOKEN_OPER_UNI: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 112 | return "Operator(Union)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 113 | case LYXP_TOKEN_OPER_PATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 114 | return "Operator(Path)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 115 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 116 | return "Operator(Recursive Path)"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 117 | case LYXP_TOKEN_LITERAL: |
| 118 | return "Literal"; |
| 119 | case LYXP_TOKEN_NUMBER: |
| 120 | return "Number"; |
| 121 | default: |
| 122 | LOGINT(NULL); |
| 123 | return ""; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @brief Print the whole expression \p exp to debug output. |
| 129 | * |
| 130 | * @param[in] exp Expression to use. |
| 131 | */ |
| 132 | static void |
| 133 | print_expr_struct_debug(struct lyxp_expr *exp) |
| 134 | { |
| 135 | uint16_t i, j; |
| 136 | char tmp[128]; |
| 137 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 138 | if (!exp || (ly_ll < LY_LLDBG)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 139 | return; |
| 140 | } |
| 141 | |
| 142 | LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr); |
| 143 | for (i = 0; i < exp->used; ++i) { |
Michal Vasko | 24cddf8 | 2020-06-01 08:17:01 +0200 | [diff] [blame] | 144 | sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i], |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 145 | &exp->expr[exp->tok_pos[i]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 146 | if (exp->repeat[i]) { |
| 147 | sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]); |
| 148 | for (j = 1; exp->repeat[i][j]; ++j) { |
| 149 | sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]); |
| 150 | } |
| 151 | strcat(tmp, ")"); |
| 152 | } |
| 153 | LOGDBG(LY_LDGXPATH, tmp); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | #ifndef NDEBUG |
| 158 | |
| 159 | /** |
| 160 | * @brief Print XPath set content to debug output. |
| 161 | * |
| 162 | * @param[in] set Set to print. |
| 163 | */ |
| 164 | static void |
| 165 | print_set_debug(struct lyxp_set *set) |
| 166 | { |
| 167 | uint32_t i; |
| 168 | char *str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 169 | struct lyxp_set_node *item; |
| 170 | struct lyxp_set_scnode *sitem; |
| 171 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 172 | if (ly_ll < LY_LLDBG) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 173 | return; |
| 174 | } |
| 175 | |
| 176 | switch (set->type) { |
| 177 | case LYXP_SET_NODE_SET: |
| 178 | LOGDBG(LY_LDGXPATH, "set NODE SET:"); |
| 179 | for (i = 0; i < set->used; ++i) { |
| 180 | item = &set->val.nodes[i]; |
| 181 | |
| 182 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 183 | case LYXP_NODE_NONE: |
| 184 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos); |
| 185 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 186 | case LYXP_NODE_ROOT: |
| 187 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos); |
| 188 | break; |
| 189 | case LYXP_NODE_ROOT_CONFIG: |
| 190 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos); |
| 191 | break; |
| 192 | case LYXP_NODE_ELEM: |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 193 | if ((item->node->schema->nodetype == LYS_LIST) && |
| 194 | (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 195 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 196 | item->node->schema->name, LYD_CANON_VALUE(lyd_child(item->node))); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 197 | } else if (((struct lyd_node_inner *)item->node)->schema->nodetype == LYS_LEAFLIST) { |
| 198 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 199 | item->node->schema->name, LYD_CANON_VALUE(item->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 200 | } else { |
| 201 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name); |
| 202 | } |
| 203 | break; |
| 204 | case LYXP_NODE_TEXT: |
| 205 | if (item->node->schema->nodetype & LYS_ANYDATA) { |
| 206 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 207 | item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 208 | } else { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 209 | 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] | 210 | } |
| 211 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 212 | case LYXP_NODE_META: |
| 213 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 214 | set->val.meta[i].meta->value); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 215 | break; |
| 216 | } |
| 217 | } |
| 218 | break; |
| 219 | |
| 220 | case LYXP_SET_SCNODE_SET: |
| 221 | LOGDBG(LY_LDGXPATH, "set SCNODE SET:"); |
| 222 | for (i = 0; i < set->used; ++i) { |
| 223 | sitem = &set->val.scnodes[i]; |
| 224 | |
| 225 | switch (sitem->type) { |
| 226 | case LYXP_NODE_ROOT: |
| 227 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx); |
| 228 | break; |
| 229 | case LYXP_NODE_ROOT_CONFIG: |
| 230 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx); |
| 231 | break; |
| 232 | case LYXP_NODE_ELEM: |
| 233 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name); |
| 234 | break; |
| 235 | default: |
| 236 | LOGINT(NULL); |
| 237 | break; |
| 238 | } |
| 239 | } |
| 240 | break; |
| 241 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 242 | case LYXP_SET_BOOLEAN: |
| 243 | LOGDBG(LY_LDGXPATH, "set BOOLEAN"); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 244 | LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false")); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 245 | break; |
| 246 | |
| 247 | case LYXP_SET_STRING: |
| 248 | LOGDBG(LY_LDGXPATH, "set STRING"); |
| 249 | LOGDBG(LY_LDGXPATH, "\t%s", set->val.str); |
| 250 | break; |
| 251 | |
| 252 | case LYXP_SET_NUMBER: |
| 253 | LOGDBG(LY_LDGXPATH, "set NUMBER"); |
| 254 | |
| 255 | if (isnan(set->val.num)) { |
| 256 | str = strdup("NaN"); |
| 257 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 258 | str = strdup("0"); |
| 259 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 260 | str = strdup("Infinity"); |
| 261 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 262 | str = strdup("-Infinity"); |
| 263 | } else if ((long long)set->val.num == set->val.num) { |
| 264 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 265 | str = NULL; |
| 266 | } |
| 267 | } else { |
| 268 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 269 | str = NULL; |
| 270 | } |
| 271 | } |
| 272 | LY_CHECK_ERR_RET(!str, LOGMEM(NULL), ); |
| 273 | |
| 274 | LOGDBG(LY_LDGXPATH, "\t%s", str); |
| 275 | free(str); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | #endif |
| 280 | |
| 281 | /** |
| 282 | * @brief Realloc the string \p str. |
| 283 | * |
| 284 | * @param[in] ctx libyang context for logging. |
| 285 | * @param[in] needed How much free space is required. |
| 286 | * @param[in,out] str Pointer to the string to use. |
| 287 | * @param[in,out] used Used bytes in \p str. |
| 288 | * @param[in,out] size Allocated bytes in \p str. |
| 289 | * @return LY_ERR |
| 290 | */ |
| 291 | static LY_ERR |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 292 | 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] | 293 | { |
| 294 | if (*size - *used < needed) { |
| 295 | do { |
| 296 | if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) { |
| 297 | LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX); |
| 298 | return LY_EINVAL; |
| 299 | } |
| 300 | *size += LYXP_STRING_CAST_SIZE_STEP; |
| 301 | } while (*size - *used < needed); |
| 302 | *str = ly_realloc(*str, *size * sizeof(char)); |
| 303 | LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM); |
| 304 | } |
| 305 | |
| 306 | return LY_SUCCESS; |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * @brief Cast nodes recursively to one string @p str. |
| 311 | * |
| 312 | * @param[in] node Node to cast. |
| 313 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 314 | * @param[in] root_type Type of the XPath root. |
| 315 | * @param[in] indent Current indent. |
| 316 | * @param[in,out] str Resulting string. |
| 317 | * @param[in,out] used Used bytes in @p str. |
| 318 | * @param[in,out] size Allocated bytes in @p str. |
| 319 | * @return LY_ERR |
| 320 | */ |
| 321 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 322 | 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] | 323 | uint16_t *used, uint16_t *size) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 324 | { |
Radek Krejci | 7f769d7 | 2020-07-11 23:13:56 +0200 | [diff] [blame] | 325 | char *buf, *line, *ptr = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 326 | const char *value_str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 327 | const struct lyd_node *child; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 328 | struct lyd_node *tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 329 | struct lyd_node_any *any; |
| 330 | LY_ERR rc; |
| 331 | |
| 332 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) { |
| 333 | return LY_SUCCESS; |
| 334 | } |
| 335 | |
| 336 | if (fake_cont) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 337 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 338 | LY_CHECK_RET(rc); |
| 339 | strcpy(*str + (*used - 1), "\n"); |
| 340 | ++(*used); |
| 341 | |
| 342 | ++indent; |
| 343 | } |
| 344 | |
| 345 | switch (node->schema->nodetype) { |
| 346 | case LYS_CONTAINER: |
| 347 | case LYS_LIST: |
| 348 | case LYS_RPC: |
| 349 | case LYS_NOTIF: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 350 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 351 | LY_CHECK_RET(rc); |
| 352 | strcpy(*str + (*used - 1), "\n"); |
| 353 | ++(*used); |
| 354 | |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 355 | for (child = lyd_child(node); child; child = child->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 356 | rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size); |
| 357 | LY_CHECK_RET(rc); |
| 358 | } |
| 359 | |
| 360 | break; |
| 361 | |
| 362 | case LYS_LEAF: |
| 363 | case LYS_LEAFLIST: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 364 | value_str = LYD_CANON_VALUE(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 365 | |
| 366 | /* print indent */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 367 | 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] | 368 | memset(*str + (*used - 1), ' ', indent * 2); |
| 369 | *used += indent * 2; |
| 370 | |
| 371 | /* print value */ |
| 372 | if (*used == 1) { |
| 373 | sprintf(*str + (*used - 1), "%s", value_str); |
| 374 | *used += strlen(value_str); |
| 375 | } else { |
| 376 | sprintf(*str + (*used - 1), "%s\n", value_str); |
| 377 | *used += strlen(value_str) + 1; |
| 378 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 379 | |
| 380 | break; |
| 381 | |
| 382 | case LYS_ANYXML: |
| 383 | case LYS_ANYDATA: |
| 384 | any = (struct lyd_node_any *)node; |
| 385 | if (!(void *)any->value.tree) { |
| 386 | /* no content */ |
| 387 | buf = strdup(""); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 388 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 389 | } else { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 390 | struct ly_out *out; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 391 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 392 | if (any->value_type == LYD_ANYDATA_LYB) { |
| 393 | /* try to parse it into a data tree */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 394 | 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] | 395 | /* successfully parsed */ |
| 396 | free(any->value.mem); |
| 397 | any->value.tree = tree; |
| 398 | any->value_type = LYD_ANYDATA_DATATREE; |
| 399 | } |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 400 | /* 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] | 401 | } |
| 402 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 403 | switch (any->value_type) { |
| 404 | case LYD_ANYDATA_STRING: |
| 405 | case LYD_ANYDATA_XML: |
| 406 | case LYD_ANYDATA_JSON: |
| 407 | buf = strdup(any->value.json); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 408 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 409 | break; |
| 410 | case LYD_ANYDATA_DATATREE: |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 411 | LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 412 | rc = lyd_print_all(out, any->value.tree, LYD_XML, 0); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 413 | ly_out_free(out, NULL, 0); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 414 | LY_CHECK_RET(rc < 0, -rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 415 | break; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 416 | case LYD_ANYDATA_LYB: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 417 | LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string."); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 418 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
| 422 | line = strtok_r(buf, "\n", &ptr); |
| 423 | do { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 424 | 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] | 425 | if (rc != LY_SUCCESS) { |
| 426 | free(buf); |
| 427 | return rc; |
| 428 | } |
| 429 | memset(*str + (*used - 1), ' ', indent * 2); |
| 430 | *used += indent * 2; |
| 431 | |
| 432 | strcpy(*str + (*used - 1), line); |
| 433 | *used += strlen(line); |
| 434 | |
| 435 | strcpy(*str + (*used - 1), "\n"); |
| 436 | *used += 1; |
| 437 | } while ((line = strtok_r(NULL, "\n", &ptr))); |
| 438 | |
| 439 | free(buf); |
| 440 | break; |
| 441 | |
| 442 | default: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 443 | LOGINT_RET(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | if (fake_cont) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 447 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 448 | LY_CHECK_RET(rc); |
| 449 | strcpy(*str + (*used - 1), "\n"); |
| 450 | ++(*used); |
| 451 | |
| 452 | --indent; |
| 453 | } |
| 454 | |
| 455 | return LY_SUCCESS; |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * @brief Cast an element into a string. |
| 460 | * |
| 461 | * @param[in] node Node to cast. |
| 462 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 463 | * @param[in] root_type Type of the XPath root. |
| 464 | * @param[out] str Element cast to dynamically-allocated string. |
| 465 | * @return LY_ERR |
| 466 | */ |
| 467 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 468 | 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] | 469 | { |
| 470 | uint16_t used, size; |
| 471 | LY_ERR rc; |
| 472 | |
| 473 | *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char)); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 474 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 475 | (*str)[0] = '\0'; |
| 476 | used = 1; |
| 477 | size = LYXP_STRING_CAST_SIZE_START; |
| 478 | |
| 479 | rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size); |
| 480 | if (rc != LY_SUCCESS) { |
| 481 | free(*str); |
| 482 | return rc; |
| 483 | } |
| 484 | |
| 485 | if (size > used) { |
| 486 | *str = ly_realloc(*str, used * sizeof(char)); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 487 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 488 | } |
| 489 | return LY_SUCCESS; |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * @brief Cast a LYXP_SET_NODE_SET set into a string. |
| 494 | * Context position aware. |
| 495 | * |
| 496 | * @param[in] set Set to cast. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 497 | * @param[out] str Cast dynamically-allocated string. |
| 498 | * @return LY_ERR |
| 499 | */ |
| 500 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 501 | cast_node_set_to_string(struct lyxp_set *set, char **str) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 502 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 503 | switch (set->val.nodes[0].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 504 | case LYXP_NODE_NONE: |
| 505 | /* invalid */ |
| 506 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 507 | case LYXP_NODE_ROOT: |
| 508 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 509 | 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] | 510 | case LYXP_NODE_ELEM: |
| 511 | case LYXP_NODE_TEXT: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 512 | 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] | 513 | case LYXP_NODE_META: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 514 | *str = strdup(set->val.meta[0].meta->value.canonical); |
| 515 | if (!*str) { |
| 516 | LOGMEM_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 517 | } |
| 518 | return LY_SUCCESS; |
| 519 | } |
| 520 | |
| 521 | LOGINT_RET(set->ctx); |
| 522 | } |
| 523 | |
| 524 | /** |
| 525 | * @brief Cast a string into an XPath number. |
| 526 | * |
| 527 | * @param[in] str String to use. |
| 528 | * @return Cast number. |
| 529 | */ |
| 530 | static long double |
| 531 | cast_string_to_number(const char *str) |
| 532 | { |
| 533 | long double num; |
| 534 | char *ptr; |
| 535 | |
| 536 | errno = 0; |
| 537 | num = strtold(str, &ptr); |
| 538 | if (errno || *ptr) { |
| 539 | num = NAN; |
| 540 | } |
| 541 | return num; |
| 542 | } |
| 543 | |
| 544 | /** |
| 545 | * @brief Callback for checking value equality. |
| 546 | * |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 547 | * Implementation of ::values_equal_cb. |
| 548 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 549 | * @param[in] val1_p First value. |
| 550 | * @param[in] val2_p Second value. |
| 551 | * @param[in] mod Whether hash table is being modified. |
| 552 | * @param[in] cb_data Callback data. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 553 | * @return Boolean value whether values are equal or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 554 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 555 | static ly_bool |
| 556 | 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] | 557 | { |
| 558 | struct lyxp_set_hash_node *val1, *val2; |
| 559 | |
| 560 | val1 = (struct lyxp_set_hash_node *)val1_p; |
| 561 | val2 = (struct lyxp_set_hash_node *)val2_p; |
| 562 | |
| 563 | if ((val1->node == val2->node) && (val1->type == val2->type)) { |
| 564 | return 1; |
| 565 | } |
| 566 | |
| 567 | return 0; |
| 568 | } |
| 569 | |
| 570 | /** |
| 571 | * @brief Insert node and its hash into set. |
| 572 | * |
| 573 | * @param[in] set et to insert to. |
| 574 | * @param[in] node Node with hash. |
| 575 | * @param[in] type Node type. |
| 576 | */ |
| 577 | static void |
| 578 | set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 579 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 580 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 581 | uint32_t i, hash; |
| 582 | struct lyxp_set_hash_node hnode; |
| 583 | |
| 584 | if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) { |
| 585 | /* create hash table and add all the nodes */ |
| 586 | set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1); |
| 587 | for (i = 0; i < set->used; ++i) { |
| 588 | hnode.node = set->val.nodes[i].node; |
| 589 | hnode.type = set->val.nodes[i].type; |
| 590 | |
| 591 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 592 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 593 | hash = dict_hash_multi(hash, NULL, 0); |
| 594 | |
| 595 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 596 | assert(!r); |
| 597 | (void)r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 598 | |
Michal Vasko | 9d6befd | 2019-12-11 14:56:56 +0100 | [diff] [blame] | 599 | if (hnode.node == node) { |
| 600 | /* it was just added, do not add it twice */ |
| 601 | node = NULL; |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 607 | /* add the new node into hash table */ |
| 608 | hnode.node = node; |
| 609 | hnode.type = type; |
| 610 | |
| 611 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 612 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 613 | hash = dict_hash_multi(hash, NULL, 0); |
| 614 | |
| 615 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 616 | assert(!r); |
| 617 | (void)r; |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | /** |
| 622 | * @brief Remove node and its hash from set. |
| 623 | * |
| 624 | * @param[in] set Set to remove from. |
| 625 | * @param[in] node Node to remove. |
| 626 | * @param[in] type Node type. |
| 627 | */ |
| 628 | static void |
| 629 | set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 630 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 631 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 632 | struct lyxp_set_hash_node hnode; |
| 633 | uint32_t hash; |
| 634 | |
| 635 | if (set->ht) { |
| 636 | hnode.node = node; |
| 637 | hnode.type = type; |
| 638 | |
| 639 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 640 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 641 | hash = dict_hash_multi(hash, NULL, 0); |
| 642 | |
| 643 | r = lyht_remove(set->ht, &hnode, hash); |
| 644 | assert(!r); |
| 645 | (void)r; |
| 646 | |
| 647 | if (!set->ht->used) { |
| 648 | lyht_free(set->ht); |
| 649 | set->ht = NULL; |
| 650 | } |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | /** |
| 655 | * @brief Check whether node is in set based on its hash. |
| 656 | * |
| 657 | * @param[in] set Set to search in. |
| 658 | * @param[in] node Node to search for. |
| 659 | * @param[in] type Node type. |
| 660 | * @param[in] skip_idx Index in @p set to skip. |
| 661 | * @return LY_ERR |
| 662 | */ |
| 663 | static LY_ERR |
| 664 | set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx) |
| 665 | { |
| 666 | struct lyxp_set_hash_node hnode, *match_p; |
| 667 | uint32_t hash; |
| 668 | |
| 669 | hnode.node = node; |
| 670 | hnode.type = type; |
| 671 | |
| 672 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 673 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 674 | hash = dict_hash_multi(hash, NULL, 0); |
| 675 | |
| 676 | if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) { |
| 677 | if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) { |
| 678 | /* we found it on the index that should be skipped, find another */ |
| 679 | hnode = *match_p; |
| 680 | if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) { |
| 681 | /* none other found */ |
| 682 | return LY_SUCCESS; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | return LY_EEXIST; |
| 687 | } |
| 688 | |
| 689 | /* not found */ |
| 690 | return LY_SUCCESS; |
| 691 | } |
| 692 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 693 | void |
| 694 | lyxp_set_free_content(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 695 | { |
| 696 | if (!set) { |
| 697 | return; |
| 698 | } |
| 699 | |
| 700 | if (set->type == LYXP_SET_NODE_SET) { |
| 701 | free(set->val.nodes); |
| 702 | lyht_free(set->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 703 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 704 | free(set->val.scnodes); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 705 | lyht_free(set->ht); |
| 706 | } else { |
| 707 | if (set->type == LYXP_SET_STRING) { |
| 708 | free(set->val.str); |
| 709 | } |
| 710 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 711 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 712 | |
| 713 | set->val.nodes = NULL; |
| 714 | set->used = 0; |
| 715 | set->size = 0; |
| 716 | set->ht = NULL; |
| 717 | set->ctx_pos = 0; |
| 718 | set->ctx_pos = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 719 | } |
| 720 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 721 | /** |
| 722 | * @brief Free dynamically-allocated set. |
| 723 | * |
| 724 | * @param[in] set Set to free. |
| 725 | */ |
| 726 | static void |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 727 | lyxp_set_free(struct lyxp_set *set) |
| 728 | { |
| 729 | if (!set) { |
| 730 | return; |
| 731 | } |
| 732 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 733 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 734 | free(set); |
| 735 | } |
| 736 | |
| 737 | /** |
| 738 | * @brief Initialize set context. |
| 739 | * |
| 740 | * @param[in] new Set to initialize. |
| 741 | * @param[in] set Arbitrary initialized set. |
| 742 | */ |
| 743 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 744 | set_init(struct lyxp_set *new, const struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 745 | { |
| 746 | memset(new, 0, sizeof *new); |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 747 | if (set) { |
| 748 | new->ctx = set->ctx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 749 | new->cur_node = set->cur_node; |
Michal Vasko | 588112f | 2019-12-10 14:51:53 +0100 | [diff] [blame] | 750 | new->root_type = set->root_type; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 751 | new->context_op = set->context_op; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 752 | new->tree = set->tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 753 | new->cur_mod = set->cur_mod; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 754 | new->format = set->format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 755 | new->prefix_data = set->prefix_data; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 756 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | /** |
| 760 | * @brief Create a deep copy of a set. |
| 761 | * |
| 762 | * @param[in] set Set to copy. |
| 763 | * @return Copy of @p set. |
| 764 | */ |
| 765 | static struct lyxp_set * |
| 766 | set_copy(struct lyxp_set *set) |
| 767 | { |
| 768 | struct lyxp_set *ret; |
| 769 | uint16_t i; |
| 770 | |
| 771 | if (!set) { |
| 772 | return NULL; |
| 773 | } |
| 774 | |
| 775 | ret = malloc(sizeof *ret); |
| 776 | LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL); |
| 777 | set_init(ret, set); |
| 778 | |
| 779 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 780 | ret->type = set->type; |
| 781 | |
| 782 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 783 | 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] | 784 | uint32_t idx; |
| 785 | LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx), |
| 786 | lyxp_set_free(ret), NULL); |
Michal Vasko | 3f27c52 | 2020-01-06 08:37:49 +0100 | [diff] [blame] | 787 | /* coverity seems to think scnodes can be NULL */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 788 | if (!ret->val.scnodes) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 789 | lyxp_set_free(ret); |
| 790 | return NULL; |
| 791 | } |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 792 | ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 793 | } |
| 794 | } |
| 795 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 796 | ret->type = set->type; |
| 797 | ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes); |
| 798 | LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL); |
| 799 | memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes); |
| 800 | |
| 801 | ret->used = ret->size = set->used; |
| 802 | ret->ctx_pos = set->ctx_pos; |
| 803 | ret->ctx_size = set->ctx_size; |
| 804 | ret->ht = lyht_dup(set->ht); |
| 805 | } else { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 806 | memcpy(ret, set, sizeof *ret); |
| 807 | if (set->type == LYXP_SET_STRING) { |
| 808 | ret->val.str = strdup(set->val.str); |
| 809 | LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL); |
| 810 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | return ret; |
| 814 | } |
| 815 | |
| 816 | /** |
| 817 | * @brief Fill XPath set with a string. Any current data are disposed of. |
| 818 | * |
| 819 | * @param[in] set Set to fill. |
| 820 | * @param[in] string String to fill into \p set. |
| 821 | * @param[in] str_len Length of \p string. 0 is a valid value! |
| 822 | */ |
| 823 | static void |
| 824 | set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len) |
| 825 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 826 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 827 | |
| 828 | set->type = LYXP_SET_STRING; |
| 829 | if ((str_len == 0) && (string[0] != '\0')) { |
| 830 | string = ""; |
| 831 | } |
| 832 | set->val.str = strndup(string, str_len); |
| 833 | } |
| 834 | |
| 835 | /** |
| 836 | * @brief Fill XPath set with a number. Any current data are disposed of. |
| 837 | * |
| 838 | * @param[in] set Set to fill. |
| 839 | * @param[in] number Number to fill into \p set. |
| 840 | */ |
| 841 | static void |
| 842 | set_fill_number(struct lyxp_set *set, long double number) |
| 843 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 844 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 845 | |
| 846 | set->type = LYXP_SET_NUMBER; |
| 847 | set->val.num = number; |
| 848 | } |
| 849 | |
| 850 | /** |
| 851 | * @brief Fill XPath set with a boolean. Any current data are disposed of. |
| 852 | * |
| 853 | * @param[in] set Set to fill. |
| 854 | * @param[in] boolean Boolean to fill into \p set. |
| 855 | */ |
| 856 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 857 | set_fill_boolean(struct lyxp_set *set, ly_bool boolean) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 858 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 859 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 860 | |
| 861 | set->type = LYXP_SET_BOOLEAN; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 862 | set->val.bln = boolean; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | /** |
| 866 | * @brief Fill XPath set with the value from another set (deep assign). |
| 867 | * Any current data are disposed of. |
| 868 | * |
| 869 | * @param[in] trg Set to fill. |
| 870 | * @param[in] src Source set to copy into \p trg. |
| 871 | */ |
| 872 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 873 | set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 874 | { |
| 875 | if (!trg || !src) { |
| 876 | return; |
| 877 | } |
| 878 | |
| 879 | if (trg->type == LYXP_SET_NODE_SET) { |
| 880 | free(trg->val.nodes); |
| 881 | } else if (trg->type == LYXP_SET_STRING) { |
| 882 | free(trg->val.str); |
| 883 | } |
| 884 | set_init(trg, src); |
| 885 | |
| 886 | if (src->type == LYXP_SET_SCNODE_SET) { |
| 887 | trg->type = LYXP_SET_SCNODE_SET; |
| 888 | trg->used = src->used; |
| 889 | trg->size = src->used; |
| 890 | |
| 891 | trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes); |
| 892 | LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 893 | memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes); |
| 894 | } else if (src->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 895 | set_fill_boolean(trg, src->val.bln); |
Michal Vasko | 44f3d2c | 2020-08-24 09:49:38 +0200 | [diff] [blame] | 896 | } else if (src->type == LYXP_SET_NUMBER) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 897 | set_fill_number(trg, src->val.num); |
| 898 | } else if (src->type == LYXP_SET_STRING) { |
| 899 | set_fill_string(trg, src->val.str, strlen(src->val.str)); |
| 900 | } else { |
| 901 | if (trg->type == LYXP_SET_NODE_SET) { |
| 902 | free(trg->val.nodes); |
| 903 | } else if (trg->type == LYXP_SET_STRING) { |
| 904 | free(trg->val.str); |
| 905 | } |
| 906 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 907 | assert(src->type == LYXP_SET_NODE_SET); |
| 908 | |
| 909 | trg->type = LYXP_SET_NODE_SET; |
| 910 | trg->used = src->used; |
| 911 | trg->size = src->used; |
| 912 | trg->ctx_pos = src->ctx_pos; |
| 913 | trg->ctx_size = src->ctx_size; |
| 914 | |
| 915 | trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes); |
| 916 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 917 | memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes); |
| 918 | if (src->ht) { |
| 919 | trg->ht = lyht_dup(src->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 920 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 921 | trg->ht = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 922 | } |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | /** |
| 927 | * @brief Clear context of all schema nodes. |
| 928 | * |
| 929 | * @param[in] set Set to clear. |
| 930 | */ |
| 931 | static void |
| 932 | set_scnode_clear_ctx(struct lyxp_set *set) |
| 933 | { |
| 934 | uint32_t i; |
| 935 | |
| 936 | for (i = 0; i < set->used; ++i) { |
| 937 | if (set->val.scnodes[i].in_ctx == 1) { |
| 938 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 939 | } else if (set->val.scnodes[i].in_ctx == -2) { |
| 940 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 941 | } |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | /** |
| 946 | * @brief Remove a node from a set. Removing last node changes |
| 947 | * set into LYXP_SET_EMPTY. Context position aware. |
| 948 | * |
| 949 | * @param[in] set Set to use. |
| 950 | * @param[in] idx Index from @p set of the node to be removed. |
| 951 | */ |
| 952 | static void |
| 953 | set_remove_node(struct lyxp_set *set, uint32_t idx) |
| 954 | { |
| 955 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 956 | assert(idx < set->used); |
| 957 | |
| 958 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 959 | |
| 960 | --set->used; |
| 961 | if (set->used) { |
| 962 | memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], |
| 963 | (set->used - idx) * sizeof *set->val.nodes); |
| 964 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 965 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 966 | } |
| 967 | } |
| 968 | |
| 969 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 970 | * @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] | 971 | * |
| 972 | * @param[in] set Set to use. |
| 973 | * @param[in] idx Index from @p set of the node to be removed. |
| 974 | */ |
| 975 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 976 | set_remove_node_none(struct lyxp_set *set, uint32_t idx) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 977 | { |
| 978 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 979 | assert(idx < set->used); |
| 980 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 981 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 982 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 983 | } |
| 984 | set->val.nodes[idx].type = LYXP_NODE_NONE; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 988 | * @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] | 989 | * set into LYXP_SET_EMPTY. Context position aware. |
| 990 | * |
| 991 | * @param[in] set Set to consolidate. |
| 992 | */ |
| 993 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 994 | set_remove_nodes_none(struct lyxp_set *set) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 995 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 996 | uint16_t i, orig_used, end = 0; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 997 | int32_t start; |
| 998 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 999 | assert(set); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1000 | |
| 1001 | orig_used = set->used; |
| 1002 | set->used = 0; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1003 | for (i = 0; i < orig_used; ) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1004 | start = -1; |
| 1005 | do { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1006 | if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1007 | start = i; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1008 | } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1009 | end = i; |
| 1010 | ++i; |
| 1011 | break; |
| 1012 | } |
| 1013 | |
| 1014 | ++i; |
| 1015 | if (i == orig_used) { |
| 1016 | end = i; |
| 1017 | } |
| 1018 | } while (i < orig_used); |
| 1019 | |
| 1020 | if (start > -1) { |
| 1021 | /* move the whole chunk of valid nodes together */ |
| 1022 | if (set->used != (unsigned)start) { |
| 1023 | memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes); |
| 1024 | } |
| 1025 | set->used += end - start; |
| 1026 | } |
| 1027 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1031 | * @brief Check for duplicates in a node set. |
| 1032 | * |
| 1033 | * @param[in] set Set to check. |
| 1034 | * @param[in] node Node to look for in @p set. |
| 1035 | * @param[in] node_type Type of @p node. |
| 1036 | * @param[in] skip_idx Index from @p set to skip. |
| 1037 | * @return LY_ERR |
| 1038 | */ |
| 1039 | static LY_ERR |
| 1040 | set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx) |
| 1041 | { |
| 1042 | uint32_t i; |
| 1043 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1044 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1045 | return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx); |
| 1046 | } |
| 1047 | |
| 1048 | for (i = 0; i < set->used; ++i) { |
| 1049 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1050 | continue; |
| 1051 | } |
| 1052 | |
| 1053 | if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) { |
| 1054 | return LY_EEXIST; |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | return LY_SUCCESS; |
| 1059 | } |
| 1060 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1061 | ly_bool |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1062 | lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx, |
| 1063 | uint32_t *index_p) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1064 | { |
| 1065 | uint32_t i; |
| 1066 | |
| 1067 | for (i = 0; i < set->used; ++i) { |
| 1068 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1069 | continue; |
| 1070 | } |
| 1071 | |
| 1072 | 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] | 1073 | if (index_p) { |
| 1074 | *index_p = i; |
| 1075 | } |
| 1076 | return 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1077 | } |
| 1078 | } |
| 1079 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1080 | return 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1081 | } |
| 1082 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1083 | void |
| 1084 | lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1085 | { |
| 1086 | uint32_t orig_used, i, j; |
| 1087 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1088 | 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] | 1089 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1090 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1091 | return; |
| 1092 | } |
| 1093 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1094 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1095 | memcpy(set1, set2, sizeof *set1); |
| 1096 | return; |
| 1097 | } |
| 1098 | |
| 1099 | if (set1->used + set2->used > set1->size) { |
| 1100 | set1->size = set1->used + set2->used; |
| 1101 | set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes); |
| 1102 | LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), ); |
| 1103 | } |
| 1104 | |
| 1105 | orig_used = set1->used; |
| 1106 | |
| 1107 | for (i = 0; i < set2->used; ++i) { |
| 1108 | for (j = 0; j < orig_used; ++j) { |
| 1109 | /* detect duplicities */ |
| 1110 | if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) { |
| 1111 | break; |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | if (j == orig_used) { |
| 1116 | memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes); |
| 1117 | ++set1->used; |
| 1118 | } |
| 1119 | } |
| 1120 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1121 | lyxp_set_free_content(set2); |
| 1122 | set2->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | /** |
| 1126 | * @brief Insert a node into a set. Context position aware. |
| 1127 | * |
| 1128 | * @param[in] set Set to use. |
| 1129 | * @param[in] node Node to insert to @p set. |
| 1130 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1131 | * @param[in] node_type Node type of @p node. |
| 1132 | * @param[in] idx Index in @p set to insert into. |
| 1133 | */ |
| 1134 | static void |
| 1135 | set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1136 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1137 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1138 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1139 | if (!set->size) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1140 | /* first item */ |
| 1141 | if (idx) { |
| 1142 | /* no real harm done, but it is a bug */ |
| 1143 | LOGINT(set->ctx); |
| 1144 | idx = 0; |
| 1145 | } |
| 1146 | set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes); |
| 1147 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1148 | set->type = LYXP_SET_NODE_SET; |
| 1149 | set->used = 0; |
| 1150 | set->size = LYXP_SET_SIZE_START; |
| 1151 | set->ctx_pos = 1; |
| 1152 | set->ctx_size = 1; |
| 1153 | set->ht = NULL; |
| 1154 | } else { |
| 1155 | /* not an empty set */ |
| 1156 | if (set->used == set->size) { |
| 1157 | |
| 1158 | /* set is full */ |
| 1159 | set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes); |
| 1160 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1161 | set->size += LYXP_SET_SIZE_STEP; |
| 1162 | } |
| 1163 | |
| 1164 | if (idx > set->used) { |
| 1165 | LOGINT(set->ctx); |
| 1166 | idx = set->used; |
| 1167 | } |
| 1168 | |
| 1169 | /* make space for the new node */ |
| 1170 | if (idx < set->used) { |
| 1171 | memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes); |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | /* finally assign the value */ |
| 1176 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1177 | set->val.nodes[idx].type = node_type; |
| 1178 | set->val.nodes[idx].pos = pos; |
| 1179 | ++set->used; |
| 1180 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1181 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1182 | set_insert_node_hash(set, (struct lyd_node *)node, node_type); |
| 1183 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1184 | } |
| 1185 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1186 | LY_ERR |
| 1187 | 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] | 1188 | { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1189 | uint32_t index; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1190 | |
| 1191 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1192 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1193 | if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) { |
| 1194 | set->val.scnodes[index].in_ctx = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1195 | } else { |
| 1196 | if (set->used == set->size) { |
| 1197 | set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes); |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1198 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1199 | set->size += LYXP_SET_SIZE_STEP; |
| 1200 | } |
| 1201 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1202 | index = set->used; |
| 1203 | set->val.scnodes[index].scnode = (struct lysc_node *)node; |
| 1204 | set->val.scnodes[index].type = node_type; |
| 1205 | set->val.scnodes[index].in_ctx = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1206 | ++set->used; |
| 1207 | } |
| 1208 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1209 | if (index_p) { |
| 1210 | *index_p = index; |
| 1211 | } |
| 1212 | |
| 1213 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | /** |
| 1217 | * @brief Replace a node in a set with another. Context position aware. |
| 1218 | * |
| 1219 | * @param[in] set Set to use. |
| 1220 | * @param[in] node Node to insert to @p set. |
| 1221 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1222 | * @param[in] node_type Node type of @p node. |
| 1223 | * @param[in] idx Index in @p set of the node to replace. |
| 1224 | */ |
| 1225 | static void |
| 1226 | set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1227 | { |
| 1228 | assert(set && (idx < set->used)); |
| 1229 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1230 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1231 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1232 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1233 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1234 | set->val.nodes[idx].type = node_type; |
| 1235 | set->val.nodes[idx].pos = pos; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1236 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1237 | set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1238 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | /** |
| 1242 | * @brief Set all nodes with ctx 1 to a new unique context value. |
| 1243 | * |
| 1244 | * @param[in] set Set to modify. |
| 1245 | * @return New context value. |
| 1246 | */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1247 | static int32_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1248 | set_scnode_new_in_ctx(struct lyxp_set *set) |
| 1249 | { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1250 | uint32_t i; |
| 1251 | int32_t ret_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1252 | |
| 1253 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1254 | |
| 1255 | ret_ctx = 3; |
| 1256 | retry: |
| 1257 | for (i = 0; i < set->used; ++i) { |
| 1258 | if (set->val.scnodes[i].in_ctx >= ret_ctx) { |
| 1259 | ret_ctx = set->val.scnodes[i].in_ctx + 1; |
| 1260 | goto retry; |
| 1261 | } |
| 1262 | } |
| 1263 | for (i = 0; i < set->used; ++i) { |
| 1264 | if (set->val.scnodes[i].in_ctx == 1) { |
| 1265 | set->val.scnodes[i].in_ctx = ret_ctx; |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | return ret_ctx; |
| 1270 | } |
| 1271 | |
| 1272 | /** |
| 1273 | * @brief Get unique @p node position in the data. |
| 1274 | * |
| 1275 | * @param[in] node Node to find. |
| 1276 | * @param[in] node_type Node type of @p node. |
| 1277 | * @param[in] root Root node. |
| 1278 | * @param[in] root_type Type of the XPath @p root node. |
| 1279 | * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally |
| 1280 | * be used to increase efficiency and start the DFS from this node. |
| 1281 | * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set. |
| 1282 | * @return Node position. |
| 1283 | */ |
| 1284 | static uint32_t |
| 1285 | 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] | 1286 | 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] | 1287 | { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1288 | const struct lyd_node *elem = NULL, *top_sibling; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1289 | uint32_t pos = 1; |
| 1290 | |
| 1291 | assert(prev && prev_pos && !root->prev->next); |
| 1292 | |
| 1293 | if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 1294 | return 0; |
| 1295 | } |
| 1296 | |
| 1297 | if (*prev) { |
| 1298 | /* start from the previous element instead from the root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1299 | pos = *prev_pos; |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1300 | 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] | 1301 | goto dfs_search; |
| 1302 | } |
| 1303 | |
| 1304 | for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1305 | LYD_TREE_DFS_BEGIN(top_sibling, elem) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1306 | dfs_search: |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1307 | if (*prev && !elem) { |
| 1308 | /* resume previous DFS */ |
| 1309 | elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev; |
| 1310 | LYD_TREE_DFS_continue = 0; |
| 1311 | } |
| 1312 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1313 | 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] | 1314 | /* skip */ |
| 1315 | LYD_TREE_DFS_continue = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1316 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1317 | if (elem == node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1318 | break; |
| 1319 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1320 | ++pos; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1321 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1322 | |
| 1323 | LYD_TREE_DFS_END(top_sibling, elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | /* node found */ |
| 1327 | if (elem) { |
| 1328 | break; |
| 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | if (!elem) { |
| 1333 | if (!(*prev)) { |
| 1334 | /* we went from root and failed to find it, cannot be */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1335 | LOGINT(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1336 | return 0; |
| 1337 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1338 | /* start the search again from the beginning */ |
| 1339 | *prev = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1340 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1341 | top_sibling = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1342 | pos = 1; |
| 1343 | goto dfs_search; |
| 1344 | } |
| 1345 | } |
| 1346 | |
| 1347 | /* remember the last found node for next time */ |
| 1348 | *prev = node; |
| 1349 | *prev_pos = pos; |
| 1350 | |
| 1351 | return pos; |
| 1352 | } |
| 1353 | |
| 1354 | /** |
| 1355 | * @brief Assign (fill) missing node positions. |
| 1356 | * |
| 1357 | * @param[in] set Set to fill positions in. |
| 1358 | * @param[in] root Context root node. |
| 1359 | * @param[in] root_type Context root type. |
| 1360 | * @return LY_ERR |
| 1361 | */ |
| 1362 | static LY_ERR |
| 1363 | set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type) |
| 1364 | { |
| 1365 | const struct lyd_node *prev = NULL, *tmp_node; |
| 1366 | uint32_t i, tmp_pos = 0; |
| 1367 | |
| 1368 | for (i = 0; i < set->used; ++i) { |
| 1369 | if (!set->val.nodes[i].pos) { |
| 1370 | tmp_node = NULL; |
| 1371 | switch (set->val.nodes[i].type) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1372 | case LYXP_NODE_META: |
| 1373 | tmp_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1374 | if (!tmp_node) { |
| 1375 | LOGINT_RET(root->schema->module->ctx); |
| 1376 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1377 | /* fallthrough */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1378 | case LYXP_NODE_ELEM: |
| 1379 | case LYXP_NODE_TEXT: |
| 1380 | if (!tmp_node) { |
| 1381 | tmp_node = set->val.nodes[i].node; |
| 1382 | } |
| 1383 | set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos); |
| 1384 | break; |
| 1385 | default: |
| 1386 | /* all roots have position 0 */ |
| 1387 | break; |
| 1388 | } |
| 1389 | } |
| 1390 | } |
| 1391 | |
| 1392 | return LY_SUCCESS; |
| 1393 | } |
| 1394 | |
| 1395 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1396 | * @brief Get unique @p meta position in the parent metadata. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1397 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1398 | * @param[in] meta Metadata to use. |
| 1399 | * @return Metadata position. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1400 | */ |
| 1401 | static uint16_t |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1402 | get_meta_pos(struct lyd_meta *meta) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1403 | { |
| 1404 | uint16_t pos = 0; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1405 | struct lyd_meta *meta2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1406 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1407 | for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1408 | ++pos; |
| 1409 | } |
| 1410 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1411 | assert(meta2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1412 | return pos; |
| 1413 | } |
| 1414 | |
| 1415 | /** |
| 1416 | * @brief Compare 2 nodes in respect to XPath document order. |
| 1417 | * |
| 1418 | * @param[in] item1 1st node. |
| 1419 | * @param[in] item2 2nd node. |
| 1420 | * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1. |
| 1421 | */ |
| 1422 | static int |
| 1423 | set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2) |
| 1424 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1425 | uint32_t meta_pos1 = 0, meta_pos2 = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1426 | |
| 1427 | if (item1->pos < item2->pos) { |
| 1428 | return -1; |
| 1429 | } |
| 1430 | |
| 1431 | if (item1->pos > item2->pos) { |
| 1432 | return 1; |
| 1433 | } |
| 1434 | |
| 1435 | /* node positions are equal, the fun case */ |
| 1436 | |
| 1437 | /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */ |
| 1438 | /* special case since text nodes are actually saved as their parents */ |
| 1439 | if ((item1->node == item2->node) && (item1->type != item2->type)) { |
| 1440 | if (item1->type == LYXP_NODE_ELEM) { |
| 1441 | assert(item2->type == LYXP_NODE_TEXT); |
| 1442 | return -1; |
| 1443 | } else { |
| 1444 | assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM)); |
| 1445 | return 1; |
| 1446 | } |
| 1447 | } |
| 1448 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1449 | /* we need meta positions now */ |
| 1450 | if (item1->type == LYXP_NODE_META) { |
| 1451 | meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1452 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1453 | if (item2->type == LYXP_NODE_META) { |
| 1454 | meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1455 | } |
| 1456 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1457 | /* 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] | 1458 | /* check for duplicates */ |
| 1459 | if (item1->node == item2->node) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1460 | 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] | 1461 | return 0; |
| 1462 | } |
| 1463 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1464 | /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1465 | /* elem is always first, 2nd node is after it */ |
| 1466 | if (item1->type == LYXP_NODE_ELEM) { |
| 1467 | assert(item2->type != LYXP_NODE_ELEM); |
| 1468 | return -1; |
| 1469 | } |
| 1470 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1471 | /* 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] | 1472 | /* 2nd is before 1st */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1473 | if (((item1->type == LYXP_NODE_TEXT) && |
| 1474 | ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) || |
| 1475 | ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) || |
| 1476 | (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) && |
| 1477 | (meta_pos1 > meta_pos2))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1478 | return 1; |
| 1479 | } |
| 1480 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1481 | /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1482 | /* 2nd is after 1st */ |
| 1483 | return -1; |
| 1484 | } |
| 1485 | |
| 1486 | /** |
| 1487 | * @brief Set cast for comparisons. |
| 1488 | * |
| 1489 | * @param[in] trg Target set to cast source into. |
| 1490 | * @param[in] src Source set. |
| 1491 | * @param[in] type Target set type. |
| 1492 | * @param[in] src_idx Source set node index. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1493 | * @return LY_ERR |
| 1494 | */ |
| 1495 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1496 | 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] | 1497 | { |
| 1498 | assert(src->type == LYXP_SET_NODE_SET); |
| 1499 | |
| 1500 | set_init(trg, src); |
| 1501 | |
| 1502 | /* insert node into target set */ |
| 1503 | set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0); |
| 1504 | |
| 1505 | /* cast target set appropriately */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1506 | return lyxp_set_cast(trg, type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1507 | } |
| 1508 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1509 | /** |
| 1510 | * @brief Set content canonization for comparisons. |
| 1511 | * |
| 1512 | * @param[in] trg Target set to put the canononized source into. |
| 1513 | * @param[in] src Source set. |
| 1514 | * @param[in] xp_node Source XPath node/meta to use for canonization. |
| 1515 | * @return LY_ERR |
| 1516 | */ |
| 1517 | static LY_ERR |
| 1518 | set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node) |
| 1519 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1520 | const struct lysc_type *type = NULL; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1521 | struct lyd_value val; |
| 1522 | struct ly_err_item *err = NULL; |
| 1523 | char *str, *ptr; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1524 | ly_bool dynamic; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1525 | LY_ERR rc; |
| 1526 | |
| 1527 | /* is there anything to canonize even? */ |
| 1528 | if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) { |
| 1529 | /* do we have a type to use for canonization? */ |
| 1530 | if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) { |
| 1531 | type = ((struct lyd_node_term *)xp_node->node)->value.realtype; |
| 1532 | } else if (xp_node->type == LYXP_NODE_META) { |
| 1533 | type = ((struct lyd_meta *)xp_node->node)->value.realtype; |
| 1534 | } |
| 1535 | } |
| 1536 | if (!type) { |
| 1537 | goto fill; |
| 1538 | } |
| 1539 | |
| 1540 | if (src->type == LYXP_SET_NUMBER) { |
| 1541 | /* canonize number */ |
| 1542 | if (asprintf(&str, "%Lf", src->val.num) == -1) { |
| 1543 | LOGMEM(src->ctx); |
| 1544 | return LY_EMEM; |
| 1545 | } |
| 1546 | } else { |
| 1547 | /* canonize string */ |
| 1548 | str = strdup(src->val.str); |
| 1549 | } |
| 1550 | |
| 1551 | /* ignore errors, the value may not satisfy schema constraints */ |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1552 | rc = type->plugin->store(src->ctx, type, str, strlen(str), LY_TYPE_OPTS_DYNAMIC, LY_PREF_JSON, NULL, LYD_HINT_DATA, |
| 1553 | xp_node->node->schema, &val, &err); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1554 | ly_err_free(err); |
| 1555 | if (rc) { |
| 1556 | /* invalid value */ |
| 1557 | free(str); |
| 1558 | goto fill; |
| 1559 | } |
| 1560 | |
| 1561 | /* storing successful, now print the canonical value */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 1562 | str = (char *)type->plugin->print(&val, LY_PREF_JSON, NULL, &dynamic); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1563 | |
| 1564 | /* use the canonized value */ |
| 1565 | set_init(trg, src); |
| 1566 | trg->type = src->type; |
| 1567 | if (src->type == LYXP_SET_NUMBER) { |
| 1568 | trg->val.num = strtold(str, &ptr); |
| 1569 | if (dynamic) { |
| 1570 | free(str); |
| 1571 | } |
| 1572 | LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT); |
| 1573 | } else { |
| 1574 | trg->val.str = (dynamic ? str : strdup(str)); |
| 1575 | } |
| 1576 | type->plugin->free(src->ctx, &val); |
| 1577 | return LY_SUCCESS; |
| 1578 | |
| 1579 | fill: |
| 1580 | /* no canonization needed/possible */ |
| 1581 | set_fill_set(trg, src); |
| 1582 | return LY_SUCCESS; |
| 1583 | } |
| 1584 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1585 | #ifndef NDEBUG |
| 1586 | |
| 1587 | /** |
| 1588 | * @brief Bubble sort @p set into XPath document order. |
| 1589 | * Context position aware. Unused in the 'Release' build target. |
| 1590 | * |
| 1591 | * @param[in] set Set to sort. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1592 | * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0). |
| 1593 | */ |
| 1594 | static int |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1595 | set_sort(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1596 | { |
| 1597 | uint32_t i, j; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1598 | int ret = 0, cmp; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1599 | ly_bool inverted, change; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1600 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1601 | struct lyxp_set_node item; |
| 1602 | struct lyxp_set_hash_node hnode; |
| 1603 | uint64_t hash; |
| 1604 | |
Michal Vasko | 3cf8fbf | 2020-05-27 15:21:21 +0200 | [diff] [blame] | 1605 | if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1606 | return 0; |
| 1607 | } |
| 1608 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1609 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1610 | for (root = set->cur_node; root->parent; root = (const struct lyd_node *)root->parent) {} |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1611 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1612 | |
| 1613 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1614 | if (set_assign_pos(set, root, set->root_type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1615 | return -1; |
| 1616 | } |
| 1617 | |
| 1618 | LOGDBG(LY_LDGXPATH, "SORT BEGIN"); |
| 1619 | print_set_debug(set); |
| 1620 | |
| 1621 | for (i = 0; i < set->used; ++i) { |
| 1622 | inverted = 0; |
| 1623 | change = 0; |
| 1624 | |
| 1625 | for (j = 1; j < set->used - i; ++j) { |
| 1626 | /* compare node positions */ |
| 1627 | if (inverted) { |
| 1628 | cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]); |
| 1629 | } else { |
| 1630 | cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]); |
| 1631 | } |
| 1632 | |
| 1633 | /* swap if needed */ |
| 1634 | if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) { |
| 1635 | change = 1; |
| 1636 | |
| 1637 | item = set->val.nodes[j - 1]; |
| 1638 | set->val.nodes[j - 1] = set->val.nodes[j]; |
| 1639 | set->val.nodes[j] = item; |
| 1640 | } else { |
| 1641 | /* whether node_pos1 should be smaller than node_pos2 or the other way around */ |
| 1642 | inverted = !inverted; |
| 1643 | } |
| 1644 | } |
| 1645 | |
| 1646 | ++ret; |
| 1647 | |
| 1648 | if (!change) { |
| 1649 | break; |
| 1650 | } |
| 1651 | } |
| 1652 | |
| 1653 | LOGDBG(LY_LDGXPATH, "SORT END %d", ret); |
| 1654 | print_set_debug(set); |
| 1655 | |
| 1656 | /* check node hashes */ |
| 1657 | if (set->used >= LYD_HT_MIN_ITEMS) { |
| 1658 | assert(set->ht); |
| 1659 | for (i = 0; i < set->used; ++i) { |
| 1660 | hnode.node = set->val.nodes[i].node; |
| 1661 | hnode.type = set->val.nodes[i].type; |
| 1662 | |
| 1663 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 1664 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 1665 | hash = dict_hash_multi(hash, NULL, 0); |
| 1666 | |
| 1667 | assert(!lyht_find(set->ht, &hnode, hash, NULL)); |
| 1668 | } |
| 1669 | } |
| 1670 | |
| 1671 | return ret - 1; |
| 1672 | } |
| 1673 | |
| 1674 | /** |
| 1675 | * @brief Remove duplicate entries in a sorted node set. |
| 1676 | * |
| 1677 | * @param[in] set Sorted set to check. |
| 1678 | * @return LY_ERR (LY_EEXIST if some duplicates are found) |
| 1679 | */ |
| 1680 | static LY_ERR |
| 1681 | set_sorted_dup_node_clean(struct lyxp_set *set) |
| 1682 | { |
| 1683 | uint32_t i = 0; |
| 1684 | LY_ERR ret = LY_SUCCESS; |
| 1685 | |
| 1686 | if (set->used > 1) { |
| 1687 | while (i < set->used - 1) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1688 | if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) && |
| 1689 | (set->val.nodes[i].type == set->val.nodes[i + 1].type)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1690 | set_remove_node_none(set, i + 1); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1691 | ret = LY_EEXIST; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1692 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1693 | ++i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1694 | } |
| 1695 | } |
| 1696 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1697 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1698 | return ret; |
| 1699 | } |
| 1700 | |
| 1701 | #endif |
| 1702 | |
| 1703 | /** |
| 1704 | * @brief Merge 2 sorted sets into one. |
| 1705 | * |
| 1706 | * @param[in,out] trg Set to merge into. Duplicates are removed. |
| 1707 | * @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] | 1708 | * @return LY_ERR |
| 1709 | */ |
| 1710 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1711 | set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1712 | { |
| 1713 | uint32_t i, j, k, count, dup_count; |
| 1714 | int cmp; |
| 1715 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1716 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1717 | 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] | 1718 | return LY_EINVAL; |
| 1719 | } |
| 1720 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1721 | if (!src->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1722 | return LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1723 | } else if (!trg->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1724 | set_fill_set(trg, src); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1725 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1726 | return LY_SUCCESS; |
| 1727 | } |
| 1728 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1729 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1730 | for (root = trg->cur_node; root->parent; root = (const struct lyd_node *)root->parent) {} |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1731 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1732 | |
| 1733 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1734 | 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] | 1735 | return LY_EINT; |
| 1736 | } |
| 1737 | |
| 1738 | #ifndef NDEBUG |
| 1739 | LOGDBG(LY_LDGXPATH, "MERGE target"); |
| 1740 | print_set_debug(trg); |
| 1741 | LOGDBG(LY_LDGXPATH, "MERGE source"); |
| 1742 | print_set_debug(src); |
| 1743 | #endif |
| 1744 | |
| 1745 | /* make memory for the merge (duplicates are not detected yet, so space |
| 1746 | * will likely be wasted on them, too bad) */ |
| 1747 | if (trg->size - trg->used < src->used) { |
| 1748 | trg->size = trg->used + src->used; |
| 1749 | |
| 1750 | trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes); |
| 1751 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM); |
| 1752 | } |
| 1753 | |
| 1754 | i = 0; |
| 1755 | j = 0; |
| 1756 | count = 0; |
| 1757 | dup_count = 0; |
| 1758 | do { |
| 1759 | cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]); |
| 1760 | if (!cmp) { |
| 1761 | if (!count) { |
| 1762 | /* duplicate, just skip it */ |
| 1763 | ++i; |
| 1764 | ++j; |
| 1765 | } else { |
| 1766 | /* we are copying something already, so let's copy the duplicate too, |
| 1767 | * we are hoping that afterwards there are some more nodes to |
| 1768 | * copy and this way we can copy them all together */ |
| 1769 | ++count; |
| 1770 | ++dup_count; |
| 1771 | ++i; |
| 1772 | ++j; |
| 1773 | } |
| 1774 | } else if (cmp < 0) { |
| 1775 | /* inserting src node into trg, just remember it for now */ |
| 1776 | ++count; |
| 1777 | ++i; |
| 1778 | |
| 1779 | /* insert the hash now */ |
| 1780 | set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type); |
| 1781 | } else if (count) { |
| 1782 | copy_nodes: |
| 1783 | /* time to actually copy the nodes, we have found the largest block of nodes */ |
| 1784 | memmove(&trg->val.nodes[j + (count - dup_count)], |
| 1785 | &trg->val.nodes[j], |
| 1786 | (trg->used - j) * sizeof *trg->val.nodes); |
| 1787 | memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes); |
| 1788 | |
| 1789 | trg->used += count - dup_count; |
| 1790 | /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */ |
| 1791 | j += count - dup_count; |
| 1792 | |
| 1793 | count = 0; |
| 1794 | dup_count = 0; |
| 1795 | } else { |
| 1796 | ++j; |
| 1797 | } |
| 1798 | } while ((i < src->used) && (j < trg->used)); |
| 1799 | |
| 1800 | if ((i < src->used) || count) { |
| 1801 | /* insert all the hashes first */ |
| 1802 | for (k = i; k < src->used; ++k) { |
| 1803 | set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type); |
| 1804 | } |
| 1805 | |
| 1806 | /* loop ended, but we need to copy something at trg end */ |
| 1807 | count += src->used - i; |
| 1808 | i = src->used; |
| 1809 | goto copy_nodes; |
| 1810 | } |
| 1811 | |
| 1812 | /* we are inserting hashes before the actual node insert, which causes |
| 1813 | * situations when there were initially not enough items for a hash table, |
| 1814 | * but even after some were inserted, hash table was not created (during |
| 1815 | * insertion the number of items is not updated yet) */ |
| 1816 | if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) { |
| 1817 | set_insert_node_hash(trg, NULL, 0); |
| 1818 | } |
| 1819 | |
| 1820 | #ifndef NDEBUG |
| 1821 | LOGDBG(LY_LDGXPATH, "MERGE result"); |
| 1822 | print_set_debug(trg); |
| 1823 | #endif |
| 1824 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1825 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1826 | return LY_SUCCESS; |
| 1827 | } |
| 1828 | |
| 1829 | /* |
| 1830 | * (re)parse functions |
| 1831 | * |
| 1832 | * Parse functions parse the expression into |
| 1833 | * tokens (syntactic analysis). |
| 1834 | * |
| 1835 | * Reparse functions perform semantic analysis |
| 1836 | * (do not save the result, just a check) of |
| 1837 | * the expression and fill repeat indices. |
| 1838 | */ |
| 1839 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1840 | LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1841 | 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] | 1842 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1843 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1844 | if (ctx) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1845 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1846 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1847 | return LY_EINCOMPLETE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1848 | } |
| 1849 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1850 | if (want_tok && (exp->tokens[tok_idx] != want_tok)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1851 | if (ctx) { |
Michal Vasko | 0b468e6 | 2020-10-19 09:33:04 +0200 | [diff] [blame] | 1852 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]), |
| 1853 | &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1854 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1855 | return LY_ENOT; |
| 1856 | } |
| 1857 | |
| 1858 | return LY_SUCCESS; |
| 1859 | } |
| 1860 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1861 | LY_ERR |
| 1862 | lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok) |
| 1863 | { |
| 1864 | LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok)); |
| 1865 | |
| 1866 | /* skip the token */ |
| 1867 | ++(*tok_idx); |
| 1868 | |
| 1869 | return LY_SUCCESS; |
| 1870 | } |
| 1871 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1872 | /* just like lyxp_check_token() but tests for 2 tokens */ |
| 1873 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1874 | 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] | 1875 | enum lyxp_token want_tok2) |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1876 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1877 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1878 | if (ctx) { |
| 1879 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1880 | } |
| 1881 | return LY_EINCOMPLETE; |
| 1882 | } |
| 1883 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1884 | 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] | 1885 | if (ctx) { |
Michal Vasko | 0b468e6 | 2020-10-19 09:33:04 +0200 | [diff] [blame] | 1886 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]), |
| 1887 | &exp->expr[exp->tok_pos[tok_idx]]); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1888 | } |
| 1889 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | return LY_SUCCESS; |
| 1893 | } |
| 1894 | |
| 1895 | /** |
| 1896 | * @brief Stack operation push on the repeat array. |
| 1897 | * |
| 1898 | * @param[in] exp Expression to use. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1899 | * @param[in] tok_idx Position in the expresion \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1900 | * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed. |
| 1901 | */ |
| 1902 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1903 | 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] | 1904 | { |
| 1905 | uint16_t i; |
| 1906 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1907 | if (exp->repeat[tok_idx]) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1908 | for (i = 0; exp->repeat[tok_idx][i]; ++i) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1909 | exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]); |
| 1910 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1911 | exp->repeat[tok_idx][i] = repeat_op_idx; |
| 1912 | exp->repeat[tok_idx][i + 1] = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1913 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1914 | exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]); |
| 1915 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1916 | exp->repeat[tok_idx][0] = repeat_op_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1917 | } |
| 1918 | } |
| 1919 | |
| 1920 | /** |
| 1921 | * @brief Reparse Predicate. Logs directly on error. |
| 1922 | * |
| 1923 | * [7] Predicate ::= '[' Expr ']' |
| 1924 | * |
| 1925 | * @param[in] ctx Context for logging. |
| 1926 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1927 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1928 | * @return LY_ERR |
| 1929 | */ |
| 1930 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1931 | 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] | 1932 | { |
| 1933 | LY_ERR rc; |
| 1934 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1935 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1936 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1937 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1938 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1939 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1940 | LY_CHECK_RET(rc); |
| 1941 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1942 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1943 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1944 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1945 | |
| 1946 | return LY_SUCCESS; |
| 1947 | } |
| 1948 | |
| 1949 | /** |
| 1950 | * @brief Reparse RelativeLocationPath. Logs directly on error. |
| 1951 | * |
| 1952 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 1953 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 1954 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 1955 | * |
| 1956 | * @param[in] ctx Context for logging. |
| 1957 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1958 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1959 | * @return LY_ERR (LY_EINCOMPLETE on forward reference) |
| 1960 | */ |
| 1961 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1962 | 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] | 1963 | { |
| 1964 | LY_ERR rc; |
| 1965 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1966 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1967 | LY_CHECK_RET(rc); |
| 1968 | |
| 1969 | goto step; |
| 1970 | do { |
| 1971 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1972 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1973 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1974 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1975 | LY_CHECK_RET(rc); |
| 1976 | step: |
| 1977 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1978 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1979 | case LYXP_TOKEN_DOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1980 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1981 | break; |
| 1982 | |
| 1983 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1984 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1985 | break; |
| 1986 | |
| 1987 | case LYXP_TOKEN_AT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1988 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1989 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1990 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1991 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1992 | 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] | 1993 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1994 | 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] | 1995 | return LY_EVALID; |
| 1996 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1997 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1998 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1999 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2000 | goto reparse_predicate; |
| 2001 | break; |
| 2002 | |
| 2003 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2004 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2005 | |
| 2006 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2007 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2008 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2009 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2010 | |
| 2011 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2012 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2013 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2014 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2015 | |
| 2016 | reparse_predicate: |
| 2017 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2018 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 2019 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2020 | LY_CHECK_RET(rc); |
| 2021 | } |
| 2022 | break; |
| 2023 | default: |
| 2024 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2025 | 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] | 2026 | return LY_EVALID; |
| 2027 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2028 | } 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] | 2029 | |
| 2030 | return LY_SUCCESS; |
| 2031 | } |
| 2032 | |
| 2033 | /** |
| 2034 | * @brief Reparse AbsoluteLocationPath. Logs directly on error. |
| 2035 | * |
| 2036 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 2037 | * |
| 2038 | * @param[in] ctx Context for logging. |
| 2039 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2040 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2041 | * @return LY_ERR |
| 2042 | */ |
| 2043 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2044 | 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] | 2045 | { |
| 2046 | LY_ERR rc; |
| 2047 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2048 | 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] | 2049 | |
| 2050 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2051 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
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 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2054 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2055 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2056 | return LY_SUCCESS; |
| 2057 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2058 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2059 | case LYXP_TOKEN_DOT: |
| 2060 | case LYXP_TOKEN_DDOT: |
| 2061 | case LYXP_TOKEN_AT: |
| 2062 | case LYXP_TOKEN_NAMETEST: |
| 2063 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2064 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2065 | LY_CHECK_RET(rc); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2066 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2067 | default: |
| 2068 | break; |
| 2069 | } |
| 2070 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2071 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 2072 | /* '//' RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2073 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2074 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2075 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2076 | LY_CHECK_RET(rc); |
| 2077 | } |
| 2078 | |
| 2079 | return LY_SUCCESS; |
| 2080 | } |
| 2081 | |
| 2082 | /** |
| 2083 | * @brief Reparse FunctionCall. Logs directly on error. |
| 2084 | * |
| 2085 | * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
| 2086 | * |
| 2087 | * @param[in] ctx Context for logging. |
| 2088 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2089 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2090 | * @return LY_ERR |
| 2091 | */ |
| 2092 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2093 | 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] | 2094 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2095 | int8_t min_arg_count = -1; |
| 2096 | uint32_t arg_count, max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2097 | uint16_t func_tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2098 | LY_ERR rc; |
| 2099 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2100 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2101 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2102 | func_tok_idx = *tok_idx; |
| 2103 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2104 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2105 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2106 | min_arg_count = 1; |
| 2107 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2108 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2109 | min_arg_count = 1; |
| 2110 | max_arg_count = 1; |
| 2111 | } |
| 2112 | break; |
| 2113 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2114 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2115 | min_arg_count = 1; |
| 2116 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2117 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2118 | min_arg_count = 0; |
| 2119 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2120 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2121 | min_arg_count = 0; |
| 2122 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2123 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2124 | min_arg_count = 0; |
| 2125 | max_arg_count = 0; |
| 2126 | } |
| 2127 | break; |
| 2128 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2129 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2130 | min_arg_count = 1; |
| 2131 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2132 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2133 | min_arg_count = 0; |
| 2134 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2135 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2136 | min_arg_count = 1; |
| 2137 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2138 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2139 | min_arg_count = 1; |
| 2140 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2141 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2142 | min_arg_count = 1; |
| 2143 | max_arg_count = 1; |
| 2144 | } |
| 2145 | break; |
| 2146 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2147 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2148 | min_arg_count = 2; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2149 | max_arg_count = UINT32_MAX; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2150 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2151 | min_arg_count = 0; |
| 2152 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2153 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2154 | min_arg_count = 0; |
| 2155 | max_arg_count = 1; |
| 2156 | } |
| 2157 | break; |
| 2158 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2159 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2160 | min_arg_count = 1; |
| 2161 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2162 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2163 | min_arg_count = 1; |
| 2164 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2165 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2166 | min_arg_count = 0; |
| 2167 | max_arg_count = 0; |
| 2168 | } |
| 2169 | break; |
| 2170 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2171 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2172 | min_arg_count = 2; |
| 2173 | max_arg_count = 2; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2174 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2175 | min_arg_count = 0; |
| 2176 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2177 | } 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] | 2178 | min_arg_count = 2; |
| 2179 | max_arg_count = 2; |
| 2180 | } |
| 2181 | break; |
| 2182 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2183 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2184 | min_arg_count = 2; |
| 2185 | max_arg_count = 3; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2186 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2187 | min_arg_count = 3; |
| 2188 | max_arg_count = 3; |
| 2189 | } |
| 2190 | break; |
| 2191 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2192 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2193 | min_arg_count = 0; |
| 2194 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2195 | } 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] | 2196 | min_arg_count = 1; |
| 2197 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2198 | } 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] | 2199 | min_arg_count = 2; |
| 2200 | max_arg_count = 2; |
| 2201 | } |
| 2202 | break; |
| 2203 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2204 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2205 | min_arg_count = 2; |
| 2206 | max_arg_count = 2; |
| 2207 | } |
| 2208 | break; |
| 2209 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2210 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2211 | min_arg_count = 2; |
| 2212 | max_arg_count = 2; |
| 2213 | } |
| 2214 | break; |
| 2215 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2216 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2217 | min_arg_count = 0; |
| 2218 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2219 | } 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] | 2220 | min_arg_count = 0; |
| 2221 | max_arg_count = 1; |
| 2222 | } |
| 2223 | break; |
| 2224 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2225 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2226 | min_arg_count = 0; |
| 2227 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2228 | } 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] | 2229 | min_arg_count = 2; |
| 2230 | max_arg_count = 2; |
| 2231 | } |
| 2232 | break; |
| 2233 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2234 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2235 | min_arg_count = 2; |
| 2236 | max_arg_count = 2; |
| 2237 | } |
| 2238 | break; |
| 2239 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2240 | 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] | 2241 | min_arg_count = 2; |
| 2242 | max_arg_count = 2; |
| 2243 | } |
| 2244 | break; |
| 2245 | } |
| 2246 | if (min_arg_count == -1) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2247 | 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] | 2248 | return LY_EINVAL; |
| 2249 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2250 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2251 | |
| 2252 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2253 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2254 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2255 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2256 | |
| 2257 | /* ( Expr ( ',' Expr )* )? */ |
| 2258 | arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2259 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2260 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2261 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2262 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2263 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2264 | LY_CHECK_RET(rc); |
| 2265 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2266 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
| 2267 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2268 | |
| 2269 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2270 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2271 | LY_CHECK_RET(rc); |
| 2272 | } |
| 2273 | |
| 2274 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2275 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2276 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2277 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2278 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2279 | 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] | 2280 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx], |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2281 | &exp->expr[exp->tok_pos[func_tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2282 | return LY_EVALID; |
| 2283 | } |
| 2284 | |
| 2285 | return LY_SUCCESS; |
| 2286 | } |
| 2287 | |
| 2288 | /** |
| 2289 | * @brief Reparse PathExpr. Logs directly on error. |
| 2290 | * |
| 2291 | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
| 2292 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 2293 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 2294 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
| 2295 | * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
| 2296 | * |
| 2297 | * @param[in] ctx Context for logging. |
| 2298 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2299 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2300 | * @return LY_ERR |
| 2301 | */ |
| 2302 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2303 | 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] | 2304 | { |
| 2305 | LY_ERR rc; |
| 2306 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2307 | if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2308 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2309 | } |
| 2310 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2311 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2312 | case LYXP_TOKEN_PAR1: |
| 2313 | /* '(' Expr ')' Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2314 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2315 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2316 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2317 | LY_CHECK_RET(rc); |
| 2318 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2319 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2320 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2321 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2322 | goto predicate; |
| 2323 | break; |
| 2324 | case LYXP_TOKEN_DOT: |
| 2325 | case LYXP_TOKEN_DDOT: |
| 2326 | case LYXP_TOKEN_AT: |
| 2327 | case LYXP_TOKEN_NAMETEST: |
| 2328 | case LYXP_TOKEN_NODETYPE: |
| 2329 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2330 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2331 | LY_CHECK_RET(rc); |
| 2332 | break; |
| 2333 | case LYXP_TOKEN_FUNCNAME: |
| 2334 | /* FunctionCall */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2335 | rc = reparse_function_call(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2336 | LY_CHECK_RET(rc); |
| 2337 | goto predicate; |
| 2338 | break; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2339 | case LYXP_TOKEN_OPER_PATH: |
| 2340 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2341 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2342 | rc = reparse_absolute_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2343 | LY_CHECK_RET(rc); |
| 2344 | break; |
| 2345 | case LYXP_TOKEN_LITERAL: |
| 2346 | /* Literal */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2347 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2348 | goto predicate; |
| 2349 | break; |
| 2350 | case LYXP_TOKEN_NUMBER: |
| 2351 | /* Number */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2352 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2353 | goto predicate; |
| 2354 | break; |
| 2355 | default: |
| 2356 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2357 | 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] | 2358 | return LY_EVALID; |
| 2359 | } |
| 2360 | |
| 2361 | return LY_SUCCESS; |
| 2362 | |
| 2363 | predicate: |
| 2364 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2365 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 2366 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2367 | LY_CHECK_RET(rc); |
| 2368 | } |
| 2369 | |
| 2370 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2371 | 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] | 2372 | |
| 2373 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2374 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2375 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2376 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2377 | LY_CHECK_RET(rc); |
| 2378 | } |
| 2379 | |
| 2380 | return LY_SUCCESS; |
| 2381 | } |
| 2382 | |
| 2383 | /** |
| 2384 | * @brief Reparse UnaryExpr. Logs directly on error. |
| 2385 | * |
| 2386 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 2387 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 2388 | * |
| 2389 | * @param[in] ctx Context for logging. |
| 2390 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2391 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2392 | * @return LY_ERR |
| 2393 | */ |
| 2394 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2395 | 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] | 2396 | { |
| 2397 | uint16_t prev_exp; |
| 2398 | LY_ERR rc; |
| 2399 | |
| 2400 | /* ('-')* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2401 | prev_exp = *tok_idx; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2402 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2403 | (exp->expr[exp->tok_pos[*tok_idx]] == '-')) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2404 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2405 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2406 | } |
| 2407 | |
| 2408 | /* PathExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2409 | prev_exp = *tok_idx; |
| 2410 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2411 | LY_CHECK_RET(rc); |
| 2412 | |
| 2413 | /* ('|' PathExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2414 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2415 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2416 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2417 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2418 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2419 | LY_CHECK_RET(rc); |
| 2420 | } |
| 2421 | |
| 2422 | return LY_SUCCESS; |
| 2423 | } |
| 2424 | |
| 2425 | /** |
| 2426 | * @brief Reparse AdditiveExpr. Logs directly on error. |
| 2427 | * |
| 2428 | * [15] AdditiveExpr ::= MultiplicativeExpr |
| 2429 | * | AdditiveExpr '+' MultiplicativeExpr |
| 2430 | * | AdditiveExpr '-' MultiplicativeExpr |
| 2431 | * [16] MultiplicativeExpr ::= UnaryExpr |
| 2432 | * | MultiplicativeExpr '*' UnaryExpr |
| 2433 | * | MultiplicativeExpr 'div' UnaryExpr |
| 2434 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 2435 | * |
| 2436 | * @param[in] ctx Context for logging. |
| 2437 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2438 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2439 | * @return LY_ERR |
| 2440 | */ |
| 2441 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2442 | 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] | 2443 | { |
| 2444 | uint16_t prev_add_exp, prev_mul_exp; |
| 2445 | LY_ERR rc; |
| 2446 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2447 | prev_add_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2448 | goto reparse_multiplicative_expr; |
| 2449 | |
| 2450 | /* ('+' / '-' MultiplicativeExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2451 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2452 | ((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] | 2453 | exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2454 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2455 | |
| 2456 | reparse_multiplicative_expr: |
| 2457 | /* UnaryExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2458 | prev_mul_exp = *tok_idx; |
| 2459 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2460 | LY_CHECK_RET(rc); |
| 2461 | |
| 2462 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2463 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2464 | ((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] | 2465 | exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2466 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2467 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2468 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2469 | LY_CHECK_RET(rc); |
| 2470 | } |
| 2471 | } |
| 2472 | |
| 2473 | return LY_SUCCESS; |
| 2474 | } |
| 2475 | |
| 2476 | /** |
| 2477 | * @brief Reparse EqualityExpr. Logs directly on error. |
| 2478 | * |
| 2479 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
| 2480 | * | EqualityExpr '!=' RelationalExpr |
| 2481 | * [14] RelationalExpr ::= AdditiveExpr |
| 2482 | * | RelationalExpr '<' AdditiveExpr |
| 2483 | * | RelationalExpr '>' AdditiveExpr |
| 2484 | * | RelationalExpr '<=' AdditiveExpr |
| 2485 | * | RelationalExpr '>=' AdditiveExpr |
| 2486 | * |
| 2487 | * @param[in] ctx Context for logging. |
| 2488 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2489 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2490 | * @return LY_ERR |
| 2491 | */ |
| 2492 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2493 | 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] | 2494 | { |
| 2495 | uint16_t prev_eq_exp, prev_rel_exp; |
| 2496 | LY_ERR rc; |
| 2497 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2498 | prev_eq_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2499 | goto reparse_additive_expr; |
| 2500 | |
| 2501 | /* ('=' / '!=' RelationalExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2502 | 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] | 2503 | exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2504 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2505 | |
| 2506 | reparse_additive_expr: |
| 2507 | /* AdditiveExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2508 | prev_rel_exp = *tok_idx; |
| 2509 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2510 | LY_CHECK_RET(rc); |
| 2511 | |
| 2512 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2513 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2514 | exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2515 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2516 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2517 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2518 | LY_CHECK_RET(rc); |
| 2519 | } |
| 2520 | } |
| 2521 | |
| 2522 | return LY_SUCCESS; |
| 2523 | } |
| 2524 | |
| 2525 | /** |
| 2526 | * @brief Reparse OrExpr. Logs directly on error. |
| 2527 | * |
| 2528 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 2529 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 2530 | * |
| 2531 | * @param[in] ctx Context for logging. |
| 2532 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2533 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2534 | * @return LY_ERR |
| 2535 | */ |
| 2536 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2537 | 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] | 2538 | { |
| 2539 | uint16_t prev_or_exp, prev_and_exp; |
| 2540 | LY_ERR rc; |
| 2541 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2542 | prev_or_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2543 | goto reparse_equality_expr; |
| 2544 | |
| 2545 | /* ('or' AndExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2546 | 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] | 2547 | exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2548 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2549 | |
| 2550 | reparse_equality_expr: |
| 2551 | /* EqualityExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2552 | prev_and_exp = *tok_idx; |
| 2553 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2554 | LY_CHECK_RET(rc); |
| 2555 | |
| 2556 | /* ('and' EqualityExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2557 | 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] | 2558 | exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2559 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2560 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2561 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2562 | LY_CHECK_RET(rc); |
| 2563 | } |
| 2564 | } |
| 2565 | |
| 2566 | return LY_SUCCESS; |
| 2567 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2568 | |
| 2569 | /** |
| 2570 | * @brief Parse NCName. |
| 2571 | * |
| 2572 | * @param[in] ncname Name to parse. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2573 | * @return Length of @p ncname valid bytes. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2574 | */ |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2575 | static long int |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2576 | parse_ncname(const char *ncname) |
| 2577 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2578 | uint32_t uc; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2579 | size_t size; |
| 2580 | long int len = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2581 | |
| 2582 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0); |
| 2583 | if (!is_xmlqnamestartchar(uc) || (uc == ':')) { |
| 2584 | return len; |
| 2585 | } |
| 2586 | |
| 2587 | do { |
| 2588 | len += size; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2589 | if (!*ncname) { |
| 2590 | break; |
| 2591 | } |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2592 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2593 | } while (is_xmlqnamechar(uc) && (uc != ':')); |
| 2594 | |
| 2595 | return len; |
| 2596 | } |
| 2597 | |
| 2598 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2599 | * @brief Add @p token into the expression @p exp. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2600 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2601 | * @param[in] ctx Context for logging. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2602 | * @param[in] exp Expression to use. |
| 2603 | * @param[in] token Token to add. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2604 | * @param[in] tok_pos Token position in the XPath expression. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2605 | * @param[in] tok_len Token length in the XPath expression. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2606 | * @return LY_ERR |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2607 | */ |
| 2608 | static LY_ERR |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2609 | 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] | 2610 | { |
| 2611 | uint32_t prev; |
| 2612 | |
| 2613 | if (exp->used == exp->size) { |
| 2614 | prev = exp->size; |
| 2615 | exp->size += LYXP_EXPR_SIZE_STEP; |
| 2616 | if (prev > exp->size) { |
| 2617 | LOGINT(ctx); |
| 2618 | return LY_EINT; |
| 2619 | } |
| 2620 | |
| 2621 | exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens); |
| 2622 | LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM); |
| 2623 | exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos); |
| 2624 | LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM); |
| 2625 | exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len); |
| 2626 | LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM); |
| 2627 | } |
| 2628 | |
| 2629 | exp->tokens[exp->used] = token; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2630 | exp->tok_pos[exp->used] = tok_pos; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2631 | exp->tok_len[exp->used] = tok_len; |
| 2632 | ++exp->used; |
| 2633 | return LY_SUCCESS; |
| 2634 | } |
| 2635 | |
| 2636 | void |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2637 | lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2638 | { |
| 2639 | uint16_t i; |
| 2640 | |
| 2641 | if (!expr) { |
| 2642 | return; |
| 2643 | } |
| 2644 | |
| 2645 | lydict_remove(ctx, expr->expr); |
| 2646 | free(expr->tokens); |
| 2647 | free(expr->tok_pos); |
| 2648 | free(expr->tok_len); |
| 2649 | if (expr->repeat) { |
| 2650 | for (i = 0; i < expr->used; ++i) { |
| 2651 | free(expr->repeat[i]); |
| 2652 | } |
| 2653 | } |
| 2654 | free(expr->repeat); |
| 2655 | free(expr); |
| 2656 | } |
| 2657 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2658 | LY_ERR |
| 2659 | 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] | 2660 | { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2661 | LY_ERR ret = LY_SUCCESS; |
| 2662 | struct lyxp_expr *expr; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2663 | size_t parsed = 0, tok_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2664 | enum lyxp_token tok_type; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2665 | ly_bool prev_function_check = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2666 | uint16_t tok_idx = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2667 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2668 | assert(expr_p); |
| 2669 | |
| 2670 | if (!expr_str[0]) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2671 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2672 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2673 | } |
| 2674 | |
| 2675 | if (!expr_len) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2676 | expr_len = strlen(expr_str); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2677 | } |
| 2678 | if (expr_len > UINT16_MAX) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2679 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "XPath expression cannot be longer than %ud characters.", UINT16_MAX); |
| 2680 | return LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2681 | } |
| 2682 | |
| 2683 | /* init lyxp_expr structure */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2684 | expr = calloc(1, sizeof *expr); |
| 2685 | LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error); |
| 2686 | LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error); |
| 2687 | expr->used = 0; |
| 2688 | expr->size = LYXP_EXPR_SIZE_START; |
| 2689 | expr->tokens = malloc(expr->size * sizeof *expr->tokens); |
| 2690 | LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2691 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2692 | expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos); |
| 2693 | 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] | 2694 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2695 | expr->tok_len = malloc(expr->size * sizeof *expr->tok_len); |
| 2696 | 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] | 2697 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2698 | /* make expr 0-terminated */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2699 | expr_str = expr->expr; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2700 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2701 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2702 | ++parsed; |
| 2703 | } |
| 2704 | |
| 2705 | do { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2706 | if (expr_str[parsed] == '(') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2707 | |
| 2708 | /* '(' */ |
| 2709 | tok_len = 1; |
| 2710 | tok_type = LYXP_TOKEN_PAR1; |
| 2711 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2712 | 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] | 2713 | /* it is a NodeType/FunctionName after all */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2714 | if (((expr->tok_len[expr->used - 1] == 4) && |
| 2715 | (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) || |
| 2716 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) || |
| 2717 | ((expr->tok_len[expr->used - 1] == 7) && |
| 2718 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2719 | expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2720 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2721 | expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2722 | } |
| 2723 | prev_function_check = 0; |
| 2724 | } |
| 2725 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2726 | } else if (expr_str[parsed] == ')') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2727 | |
| 2728 | /* ')' */ |
| 2729 | tok_len = 1; |
| 2730 | tok_type = LYXP_TOKEN_PAR2; |
| 2731 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2732 | } else if (expr_str[parsed] == '[') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2733 | |
| 2734 | /* '[' */ |
| 2735 | tok_len = 1; |
| 2736 | tok_type = LYXP_TOKEN_BRACK1; |
| 2737 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2738 | } else if (expr_str[parsed] == ']') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2739 | |
| 2740 | /* ']' */ |
| 2741 | tok_len = 1; |
| 2742 | tok_type = LYXP_TOKEN_BRACK2; |
| 2743 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2744 | } else if (!strncmp(&expr_str[parsed], "..", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2745 | |
| 2746 | /* '..' */ |
| 2747 | tok_len = 2; |
| 2748 | tok_type = LYXP_TOKEN_DDOT; |
| 2749 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2750 | } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2751 | |
| 2752 | /* '.' */ |
| 2753 | tok_len = 1; |
| 2754 | tok_type = LYXP_TOKEN_DOT; |
| 2755 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2756 | } else if (expr_str[parsed] == '@') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2757 | |
| 2758 | /* '@' */ |
| 2759 | tok_len = 1; |
| 2760 | tok_type = LYXP_TOKEN_AT; |
| 2761 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2762 | } else if (expr_str[parsed] == ',') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2763 | |
| 2764 | /* ',' */ |
| 2765 | tok_len = 1; |
| 2766 | tok_type = LYXP_TOKEN_COMMA; |
| 2767 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2768 | } else if (expr_str[parsed] == '\'') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2769 | |
| 2770 | /* Literal with ' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2771 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {} |
| 2772 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2773 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID, |
| 2774 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2775 | ++tok_len; |
| 2776 | tok_type = LYXP_TOKEN_LITERAL; |
| 2777 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2778 | } else if (expr_str[parsed] == '\"') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2779 | |
| 2780 | /* Literal with " */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2781 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {} |
| 2782 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2783 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID, |
| 2784 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2785 | ++tok_len; |
| 2786 | tok_type = LYXP_TOKEN_LITERAL; |
| 2787 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2788 | } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2789 | |
| 2790 | /* Number */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2791 | for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
| 2792 | if (expr_str[parsed + tok_len] == '.') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2793 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2794 | for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2795 | } |
| 2796 | tok_type = LYXP_TOKEN_NUMBER; |
| 2797 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2798 | } else if (expr_str[parsed] == '/') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2799 | |
| 2800 | /* Operator '/', '//' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2801 | if (!strncmp(&expr_str[parsed], "//", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2802 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2803 | tok_type = LYXP_TOKEN_OPER_RPATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2804 | } else { |
| 2805 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2806 | tok_type = LYXP_TOKEN_OPER_PATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2807 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2808 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2809 | } else if (!strncmp(&expr_str[parsed], "!=", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2810 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2811 | /* Operator '!=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2812 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2813 | tok_type = LYXP_TOKEN_OPER_NEQUAL; |
| 2814 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2815 | } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2816 | |
| 2817 | /* Operator '<=', '>=' */ |
| 2818 | tok_len = 2; |
| 2819 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2820 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2821 | } else if (expr_str[parsed] == '|') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2822 | |
| 2823 | /* Operator '|' */ |
| 2824 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2825 | tok_type = LYXP_TOKEN_OPER_UNI; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2826 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2827 | } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2828 | |
| 2829 | /* Operator '+', '-' */ |
| 2830 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2831 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2832 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2833 | } else if (expr_str[parsed] == '=') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2834 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2835 | /* Operator '=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2836 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2837 | tok_type = LYXP_TOKEN_OPER_EQUAL; |
| 2838 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2839 | } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2840 | |
| 2841 | /* Operator '<', '>' */ |
| 2842 | tok_len = 1; |
| 2843 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2844 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2845 | } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) && |
| 2846 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) && |
| 2847 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) && |
| 2848 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) && |
| 2849 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) && |
| 2850 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) && |
| 2851 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) && |
| 2852 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) && |
| 2853 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) && |
| 2854 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) && |
| 2855 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) && |
| 2856 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2857 | |
| 2858 | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2859 | if (expr_str[parsed] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2860 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2861 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2862 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2863 | } else if (!strncmp(&expr_str[parsed], "or", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2864 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2865 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2866 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2867 | } else if (!strncmp(&expr_str[parsed], "and", 3)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2868 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2869 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2870 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2871 | } 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] | 2872 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2873 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2874 | |
| 2875 | } else if (prev_function_check) { |
Michal Vasko | 5307857 | 2019-05-24 08:50:15 +0200 | [diff] [blame] | 2876 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2877 | expr_str[parsed], expr_str[parsed], expr->tok_len[expr->used - 1], &expr->expr[expr->tok_pos[expr->used - 1]]); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2878 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2879 | goto error; |
| 2880 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2881 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed + 1, expr_str); |
| 2882 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2883 | goto error; |
| 2884 | } |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2885 | } else if (expr_str[parsed] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2886 | |
| 2887 | /* NameTest '*' */ |
| 2888 | tok_len = 1; |
| 2889 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2890 | |
| 2891 | } else { |
| 2892 | |
| 2893 | /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2894 | long int ncname_len = parse_ncname(&expr_str[parsed]); |
| 2895 | LY_CHECK_ERR_GOTO(ncname_len < 0, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2896 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID, |
| 2897 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2898 | tok_len = ncname_len; |
| 2899 | |
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; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2902 | if (expr_str[parsed + tok_len] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2903 | ++tok_len; |
| 2904 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2905 | ncname_len = parse_ncname(&expr_str[parsed + tok_len]); |
| 2906 | LY_CHECK_ERR_GOTO(ncname_len < 0, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2907 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID, |
| 2908 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2909 | tok_len += ncname_len; |
| 2910 | } |
| 2911 | /* remove old flag to prevent ambiguities */ |
| 2912 | prev_function_check = 0; |
| 2913 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2914 | } else { |
| 2915 | /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */ |
| 2916 | prev_function_check = 1; |
| 2917 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2918 | } |
| 2919 | } |
| 2920 | |
| 2921 | /* store the token, move on to the next one */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2922 | 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] | 2923 | parsed += tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2924 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2925 | ++parsed; |
| 2926 | } |
| 2927 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2928 | } while (expr_str[parsed]); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2929 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2930 | if (reparse) { |
| 2931 | /* prealloc repeat */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2932 | expr->repeat = calloc(expr->size, sizeof *expr->repeat); |
| 2933 | LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2934 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2935 | /* fill repeat */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2936 | LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx), ret = LY_EVALID, error); |
| 2937 | if (expr->used > tok_idx) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2938 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2939 | &expr->expr[expr->tok_pos[tok_idx]]); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2940 | ret = LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2941 | goto error; |
| 2942 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2943 | } |
| 2944 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2945 | print_expr_struct_debug(expr); |
| 2946 | *expr_p = expr; |
| 2947 | return LY_SUCCESS; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2948 | |
| 2949 | error: |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2950 | lyxp_expr_free(ctx, expr); |
| 2951 | return ret; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2952 | } |
| 2953 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2954 | LY_ERR |
| 2955 | 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] | 2956 | { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2957 | LY_ERR ret = LY_SUCCESS; |
| 2958 | struct lyxp_expr *dup = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2959 | uint32_t i, j; |
| 2960 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2961 | if (!exp) { |
| 2962 | goto cleanup; |
| 2963 | } |
| 2964 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2965 | dup = calloc(1, sizeof *dup); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2966 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2967 | |
| 2968 | dup->tokens = malloc(exp->used * sizeof *dup->tokens); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2969 | LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2970 | memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens); |
| 2971 | |
| 2972 | dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2973 | 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] | 2974 | memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos); |
| 2975 | |
| 2976 | dup->tok_len = malloc(exp->used * sizeof *dup->tok_len); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2977 | 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] | 2978 | memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len); |
| 2979 | |
| 2980 | dup->repeat = malloc(exp->used * sizeof *dup->repeat); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2981 | LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2982 | for (i = 0; i < exp->used; ++i) { |
| 2983 | if (!exp->repeat[i]) { |
| 2984 | dup->repeat[i] = NULL; |
| 2985 | } else { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2986 | for (j = 0; exp->repeat[i][j]; ++j) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2987 | /* the ending 0 as well */ |
| 2988 | ++j; |
| 2989 | |
Michal Vasko | 99c7164 | 2020-07-03 13:33:36 +0200 | [diff] [blame] | 2990 | dup->repeat[i] = malloc(j * sizeof **dup->repeat); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2991 | 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] | 2992 | memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat); |
| 2993 | dup->repeat[i][j - 1] = 0; |
| 2994 | } |
| 2995 | } |
| 2996 | |
| 2997 | dup->used = exp->used; |
| 2998 | dup->size = exp->used; |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2999 | 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] | 3000 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3001 | cleanup: |
| 3002 | if (ret) { |
| 3003 | lyxp_expr_free(ctx, dup); |
| 3004 | } else { |
| 3005 | *dup_p = dup; |
| 3006 | } |
| 3007 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3008 | } |
| 3009 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3010 | /* |
| 3011 | * warn functions |
| 3012 | * |
| 3013 | * Warn functions check specific reasonable conditions for schema XPath |
| 3014 | * and print a warning if they are not satisfied. |
| 3015 | */ |
| 3016 | |
| 3017 | /** |
| 3018 | * @brief Get the last-added schema node that is currently in the context. |
| 3019 | * |
| 3020 | * @param[in] set Set to search in. |
| 3021 | * @return Last-added schema context node, NULL if no node is in context. |
| 3022 | */ |
| 3023 | static struct lysc_node * |
| 3024 | warn_get_scnode_in_ctx(struct lyxp_set *set) |
| 3025 | { |
| 3026 | uint32_t i; |
| 3027 | |
| 3028 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
| 3029 | return NULL; |
| 3030 | } |
| 3031 | |
| 3032 | i = set->used; |
| 3033 | do { |
| 3034 | --i; |
| 3035 | if (set->val.scnodes[i].in_ctx == 1) { |
| 3036 | /* if there are more, simply return the first found (last added) */ |
| 3037 | return set->val.scnodes[i].scnode; |
| 3038 | } |
| 3039 | } while (i); |
| 3040 | |
| 3041 | return NULL; |
| 3042 | } |
| 3043 | |
| 3044 | /** |
| 3045 | * @brief Test whether a type is numeric - integer type or decimal64. |
| 3046 | * |
| 3047 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3048 | * @return Boolean value whether @p type is numeric type or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3049 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3050 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3051 | warn_is_numeric_type(struct lysc_type *type) |
| 3052 | { |
| 3053 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3054 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3055 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3056 | |
| 3057 | switch (type->basetype) { |
| 3058 | case LY_TYPE_DEC64: |
| 3059 | case LY_TYPE_INT8: |
| 3060 | case LY_TYPE_UINT8: |
| 3061 | case LY_TYPE_INT16: |
| 3062 | case LY_TYPE_UINT16: |
| 3063 | case LY_TYPE_INT32: |
| 3064 | case LY_TYPE_UINT32: |
| 3065 | case LY_TYPE_INT64: |
| 3066 | case LY_TYPE_UINT64: |
| 3067 | return 1; |
| 3068 | case LY_TYPE_UNION: |
| 3069 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3070 | LY_ARRAY_FOR(uni->types, u) { |
| 3071 | ret = warn_is_numeric_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3072 | if (ret) { |
| 3073 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3074 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3075 | } |
| 3076 | } |
| 3077 | /* did not find any suitable type */ |
| 3078 | return 0; |
| 3079 | case LY_TYPE_LEAFREF: |
| 3080 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
| 3081 | default: |
| 3082 | return 0; |
| 3083 | } |
| 3084 | } |
| 3085 | |
| 3086 | /** |
| 3087 | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
| 3088 | * |
| 3089 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3090 | * @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] | 3091 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3092 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3093 | warn_is_string_type(struct lysc_type *type) |
| 3094 | { |
| 3095 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3096 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3097 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3098 | |
| 3099 | switch (type->basetype) { |
| 3100 | case LY_TYPE_BITS: |
| 3101 | case LY_TYPE_ENUM: |
| 3102 | case LY_TYPE_IDENT: |
| 3103 | case LY_TYPE_INST: |
| 3104 | case LY_TYPE_STRING: |
| 3105 | return 1; |
| 3106 | case LY_TYPE_UNION: |
| 3107 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3108 | LY_ARRAY_FOR(uni->types, u) { |
| 3109 | ret = warn_is_string_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3110 | if (ret) { |
| 3111 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3112 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3113 | } |
| 3114 | } |
| 3115 | /* did not find any suitable type */ |
| 3116 | return 0; |
| 3117 | case LY_TYPE_LEAFREF: |
| 3118 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
| 3119 | default: |
| 3120 | return 0; |
| 3121 | } |
| 3122 | } |
| 3123 | |
| 3124 | /** |
| 3125 | * @brief Test whether a type is one specific type. |
| 3126 | * |
| 3127 | * @param[in] type Type to test. |
| 3128 | * @param[in] base Expected type. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3129 | * @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] | 3130 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3131 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3132 | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
| 3133 | { |
| 3134 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3135 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3136 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3137 | |
| 3138 | if (type->basetype == base) { |
| 3139 | return 1; |
| 3140 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3141 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3142 | LY_ARRAY_FOR(uni->types, u) { |
| 3143 | ret = warn_is_specific_type(uni->types[u], base); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3144 | if (ret) { |
| 3145 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3146 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3147 | } |
| 3148 | } |
| 3149 | /* did not find any suitable type */ |
| 3150 | return 0; |
| 3151 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
| 3152 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
| 3153 | } |
| 3154 | |
| 3155 | return 0; |
| 3156 | } |
| 3157 | |
| 3158 | /** |
| 3159 | * @brief Get next type of a (union) type. |
| 3160 | * |
| 3161 | * @param[in] type Base type. |
| 3162 | * @param[in] prev_type Previously returned type. |
| 3163 | * @return Next type or NULL. |
| 3164 | */ |
| 3165 | static struct lysc_type * |
| 3166 | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type) |
| 3167 | { |
| 3168 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3169 | ly_bool found = 0; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3170 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3171 | |
| 3172 | switch (type->basetype) { |
| 3173 | case LY_TYPE_UNION: |
| 3174 | uni = (struct lysc_type_union *)type; |
| 3175 | if (!prev_type) { |
| 3176 | return uni->types[0]; |
| 3177 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3178 | LY_ARRAY_FOR(uni->types, u) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3179 | if (found) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3180 | return uni->types[u]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3181 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3182 | if (prev_type == uni->types[u]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3183 | found = 1; |
| 3184 | } |
| 3185 | } |
| 3186 | return NULL; |
| 3187 | default: |
| 3188 | if (prev_type) { |
| 3189 | assert(type == prev_type); |
| 3190 | return NULL; |
| 3191 | } else { |
| 3192 | return type; |
| 3193 | } |
| 3194 | } |
| 3195 | } |
| 3196 | |
| 3197 | /** |
| 3198 | * @brief Test whether 2 types have a common type. |
| 3199 | * |
| 3200 | * @param[in] type1 First type. |
| 3201 | * @param[in] type2 Second type. |
| 3202 | * @return 1 if they do, 0 otherwise. |
| 3203 | */ |
| 3204 | static int |
| 3205 | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
| 3206 | { |
| 3207 | struct lysc_type *t1, *rt1, *t2, *rt2; |
| 3208 | |
| 3209 | t1 = NULL; |
| 3210 | while ((t1 = warn_is_equal_type_next_type(type1, t1))) { |
| 3211 | if (t1->basetype == LY_TYPE_LEAFREF) { |
| 3212 | rt1 = ((struct lysc_type_leafref *)t1)->realtype; |
| 3213 | } else { |
| 3214 | rt1 = t1; |
| 3215 | } |
| 3216 | |
| 3217 | t2 = NULL; |
| 3218 | while ((t2 = warn_is_equal_type_next_type(type2, t2))) { |
| 3219 | if (t2->basetype == LY_TYPE_LEAFREF) { |
| 3220 | rt2 = ((struct lysc_type_leafref *)t2)->realtype; |
| 3221 | } else { |
| 3222 | rt2 = t2; |
| 3223 | } |
| 3224 | |
| 3225 | if (rt2->basetype == rt1->basetype) { |
| 3226 | /* match found */ |
| 3227 | return 1; |
| 3228 | } |
| 3229 | } |
| 3230 | } |
| 3231 | |
| 3232 | return 0; |
| 3233 | } |
| 3234 | |
| 3235 | /** |
| 3236 | * @brief Check both operands of comparison operators. |
| 3237 | * |
| 3238 | * @param[in] ctx Context for errors. |
| 3239 | * @param[in] set1 First operand set. |
| 3240 | * @param[in] set2 Second operand set. |
| 3241 | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
| 3242 | * @param[in] expr Start of the expression to print with the warning. |
| 3243 | * @param[in] tok_pos Token position. |
| 3244 | */ |
| 3245 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3246 | 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] | 3247 | { |
| 3248 | struct lysc_node_leaf *node1, *node2; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3249 | ly_bool leaves = 1, warning = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3250 | |
| 3251 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
| 3252 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
| 3253 | |
| 3254 | if (!node1 && !node2) { |
| 3255 | /* no node-sets involved, nothing to do */ |
| 3256 | return; |
| 3257 | } |
| 3258 | |
| 3259 | if (node1) { |
| 3260 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3261 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
| 3262 | warning = 1; |
| 3263 | leaves = 0; |
| 3264 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
| 3265 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
| 3266 | warning = 1; |
| 3267 | } |
| 3268 | } |
| 3269 | |
| 3270 | if (node2) { |
| 3271 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3272 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
| 3273 | warning = 1; |
| 3274 | leaves = 0; |
| 3275 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
| 3276 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
| 3277 | warning = 1; |
| 3278 | } |
| 3279 | } |
| 3280 | |
| 3281 | if (node1 && node2 && leaves && !numbers_only) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3282 | if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) || |
| 3283 | (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) || |
| 3284 | (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) && |
| 3285 | !warn_is_equal_type(node1->type, node2->type))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3286 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
| 3287 | warning = 1; |
| 3288 | } |
| 3289 | } |
| 3290 | |
| 3291 | if (warning) { |
| 3292 | LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos); |
| 3293 | } |
| 3294 | } |
| 3295 | |
| 3296 | /** |
| 3297 | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
| 3298 | * |
| 3299 | * @param[in] exp Parsed XPath expression. |
| 3300 | * @param[in] set Set with the leaf/leaf-list. |
| 3301 | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
| 3302 | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
| 3303 | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
| 3304 | */ |
| 3305 | static void |
| 3306 | warn_equality_value(struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, uint16_t last_equal_exp) |
| 3307 | { |
| 3308 | struct lysc_node *scnode; |
| 3309 | struct lysc_type *type; |
| 3310 | char *value; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3311 | struct lyd_value storage; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3312 | LY_ERR rc; |
| 3313 | struct ly_err_item *err = NULL; |
| 3314 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3315 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && |
| 3316 | ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3317 | /* check that the node can have the specified value */ |
| 3318 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
| 3319 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
| 3320 | } else { |
| 3321 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
| 3322 | } |
| 3323 | if (!value) { |
| 3324 | LOGMEM(set->ctx); |
| 3325 | return; |
| 3326 | } |
| 3327 | |
| 3328 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
| 3329 | LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding" |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3330 | " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3331 | 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] | 3332 | (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp], |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3333 | exp->expr + exp->tok_pos[equal_exp]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3334 | } |
| 3335 | |
| 3336 | type = ((struct lysc_node_leaf *)scnode)->type; |
| 3337 | if (type->basetype != LY_TYPE_IDENT) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3338 | rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data, |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 3339 | LYD_HINT_DATA, scnode, &storage, &err); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3340 | |
| 3341 | if (err) { |
| 3342 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
| 3343 | ly_err_free(err); |
| 3344 | } else if (rc != LY_SUCCESS) { |
| 3345 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
| 3346 | } |
| 3347 | if (rc != LY_SUCCESS) { |
| 3348 | 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] | 3349 | (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp], |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3350 | exp->expr + exp->tok_pos[equal_exp]); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3351 | } else { |
| 3352 | type->plugin->free(set->ctx, &storage); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3353 | } |
| 3354 | } |
| 3355 | free(value); |
| 3356 | } |
| 3357 | } |
| 3358 | |
| 3359 | /* |
| 3360 | * XPath functions |
| 3361 | */ |
| 3362 | |
| 3363 | /** |
| 3364 | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
| 3365 | * depending on whether the first node bit value from the second argument is set. |
| 3366 | * |
| 3367 | * @param[in] args Array of arguments. |
| 3368 | * @param[in] arg_count Count of elements in @p args. |
| 3369 | * @param[in,out] set Context and result set at the same time. |
| 3370 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3371 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3372 | */ |
| 3373 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3374 | 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] | 3375 | { |
| 3376 | struct lyd_node_term *leaf; |
| 3377 | struct lysc_node_leaf *sleaf; |
| 3378 | struct lysc_type_bits *bits; |
| 3379 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3380 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3381 | |
| 3382 | if (options & LYXP_SCNODE_ALL) { |
| 3383 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3384 | 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] | 3385 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3386 | 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] | 3387 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
| 3388 | 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] | 3389 | } |
| 3390 | |
| 3391 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3392 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3393 | 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] | 3394 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3395 | 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] | 3396 | } |
| 3397 | } |
| 3398 | set_scnode_clear_ctx(set); |
| 3399 | return rc; |
| 3400 | } |
| 3401 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3402 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3403 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 3404 | "bit-is-set(node-set, string)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3405 | return LY_EVALID; |
| 3406 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3407 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3408 | LY_CHECK_RET(rc); |
| 3409 | |
| 3410 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3411 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3412 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3413 | if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && |
| 3414 | (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3415 | bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3416 | LY_ARRAY_FOR(bits->bits, u) { |
| 3417 | if (!strcmp(bits->bits[u].name, args[1]->val.str)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3418 | set_fill_boolean(set, 1); |
| 3419 | break; |
| 3420 | } |
| 3421 | } |
| 3422 | } |
| 3423 | } |
| 3424 | |
| 3425 | return LY_SUCCESS; |
| 3426 | } |
| 3427 | |
| 3428 | /** |
| 3429 | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
| 3430 | * with the argument converted to boolean. |
| 3431 | * |
| 3432 | * @param[in] args Array of arguments. |
| 3433 | * @param[in] arg_count Count of elements in @p args. |
| 3434 | * @param[in,out] set Context and result set at the same time. |
| 3435 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3436 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3437 | */ |
| 3438 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3439 | 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] | 3440 | { |
| 3441 | LY_ERR rc; |
| 3442 | |
| 3443 | if (options & LYXP_SCNODE_ALL) { |
| 3444 | set_scnode_clear_ctx(set); |
| 3445 | return LY_SUCCESS; |
| 3446 | } |
| 3447 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3448 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3449 | LY_CHECK_RET(rc); |
| 3450 | set_fill_set(set, args[0]); |
| 3451 | |
| 3452 | return LY_SUCCESS; |
| 3453 | } |
| 3454 | |
| 3455 | /** |
| 3456 | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
| 3457 | * with the first argument rounded up to the nearest integer. |
| 3458 | * |
| 3459 | * @param[in] args Array of arguments. |
| 3460 | * @param[in] arg_count Count of elements in @p args. |
| 3461 | * @param[in,out] set Context and result set at the same time. |
| 3462 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3463 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3464 | */ |
| 3465 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3466 | 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] | 3467 | { |
| 3468 | struct lysc_node_leaf *sleaf; |
| 3469 | LY_ERR rc = LY_SUCCESS; |
| 3470 | |
| 3471 | if (options & LYXP_SCNODE_ALL) { |
| 3472 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3473 | 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] | 3474 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3475 | 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] | 3476 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 3477 | 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] | 3478 | } |
| 3479 | set_scnode_clear_ctx(set); |
| 3480 | return rc; |
| 3481 | } |
| 3482 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3483 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3484 | LY_CHECK_RET(rc); |
| 3485 | if ((long long)args[0]->val.num != args[0]->val.num) { |
| 3486 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
| 3487 | } else { |
| 3488 | set_fill_number(set, args[0]->val.num); |
| 3489 | } |
| 3490 | |
| 3491 | return LY_SUCCESS; |
| 3492 | } |
| 3493 | |
| 3494 | /** |
| 3495 | * @brief Execute the XPath concat(string, string, string*) function. |
| 3496 | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
| 3497 | * |
| 3498 | * @param[in] args Array of arguments. |
| 3499 | * @param[in] arg_count Count of elements in @p args. |
| 3500 | * @param[in,out] set Context and result set at the same time. |
| 3501 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3502 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3503 | */ |
| 3504 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3505 | 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] | 3506 | { |
| 3507 | uint16_t i; |
| 3508 | char *str = NULL; |
| 3509 | size_t used = 1; |
| 3510 | LY_ERR rc = LY_SUCCESS; |
| 3511 | struct lysc_node_leaf *sleaf; |
| 3512 | |
| 3513 | if (options & LYXP_SCNODE_ALL) { |
| 3514 | for (i = 0; i < arg_count; ++i) { |
| 3515 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
| 3516 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3517 | LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3518 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3519 | } else if (!warn_is_string_type(sleaf->type)) { |
Radek Krejci | 70124c8 | 2020-08-14 22:17:03 +0200 | [diff] [blame] | 3520 | 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] | 3521 | } |
| 3522 | } |
| 3523 | } |
| 3524 | set_scnode_clear_ctx(set); |
| 3525 | return rc; |
| 3526 | } |
| 3527 | |
| 3528 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3529 | rc = lyxp_set_cast(args[i], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3530 | if (rc != LY_SUCCESS) { |
| 3531 | free(str); |
| 3532 | return rc; |
| 3533 | } |
| 3534 | |
| 3535 | str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char)); |
| 3536 | LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM); |
| 3537 | strcpy(str + used - 1, args[i]->val.str); |
| 3538 | used += strlen(args[i]->val.str); |
| 3539 | } |
| 3540 | |
| 3541 | /* free, kind of */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3542 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3543 | set->type = LYXP_SET_STRING; |
| 3544 | set->val.str = str; |
| 3545 | |
| 3546 | return LY_SUCCESS; |
| 3547 | } |
| 3548 | |
| 3549 | /** |
| 3550 | * @brief Execute the XPath contains(string, string) function. |
| 3551 | * Returns LYXP_SET_BOOLEAN whether the second argument can |
| 3552 | * be found in the first or not. |
| 3553 | * |
| 3554 | * @param[in] args Array of arguments. |
| 3555 | * @param[in] arg_count Count of elements in @p args. |
| 3556 | * @param[in,out] set Context and result set at the same time. |
| 3557 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3558 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3559 | */ |
| 3560 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3561 | 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] | 3562 | { |
| 3563 | struct lysc_node_leaf *sleaf; |
| 3564 | LY_ERR rc = LY_SUCCESS; |
| 3565 | |
| 3566 | if (options & LYXP_SCNODE_ALL) { |
| 3567 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3568 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3569 | 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] | 3570 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3571 | 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] | 3572 | } |
| 3573 | } |
| 3574 | |
| 3575 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3576 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3577 | 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] | 3578 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3579 | 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] | 3580 | } |
| 3581 | } |
| 3582 | set_scnode_clear_ctx(set); |
| 3583 | return rc; |
| 3584 | } |
| 3585 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3586 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3587 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3588 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3589 | LY_CHECK_RET(rc); |
| 3590 | |
| 3591 | if (strstr(args[0]->val.str, args[1]->val.str)) { |
| 3592 | set_fill_boolean(set, 1); |
| 3593 | } else { |
| 3594 | set_fill_boolean(set, 0); |
| 3595 | } |
| 3596 | |
| 3597 | return LY_SUCCESS; |
| 3598 | } |
| 3599 | |
| 3600 | /** |
| 3601 | * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER |
| 3602 | * with the size of the node-set from the argument. |
| 3603 | * |
| 3604 | * @param[in] args Array of arguments. |
| 3605 | * @param[in] arg_count Count of elements in @p args. |
| 3606 | * @param[in,out] set Context and result set at the same time. |
| 3607 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3608 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3609 | */ |
| 3610 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3611 | 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] | 3612 | { |
| 3613 | struct lysc_node *scnode = NULL; |
| 3614 | LY_ERR rc = LY_SUCCESS; |
| 3615 | |
| 3616 | if (options & LYXP_SCNODE_ALL) { |
| 3617 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) { |
| 3618 | 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] | 3619 | } |
| 3620 | set_scnode_clear_ctx(set); |
| 3621 | return rc; |
| 3622 | } |
| 3623 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3624 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3625 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3626 | return LY_EVALID; |
| 3627 | } |
| 3628 | |
| 3629 | set_fill_number(set, args[0]->used); |
| 3630 | return LY_SUCCESS; |
| 3631 | } |
| 3632 | |
| 3633 | /** |
| 3634 | * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET |
| 3635 | * with the context with the intial node. |
| 3636 | * |
| 3637 | * @param[in] args Array of arguments. |
| 3638 | * @param[in] arg_count Count of elements in @p args. |
| 3639 | * @param[in,out] set Context and result set at the same time. |
| 3640 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3641 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3642 | */ |
| 3643 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3644 | 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] | 3645 | { |
| 3646 | if (arg_count || args) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3647 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3648 | return LY_EVALID; |
| 3649 | } |
| 3650 | |
| 3651 | if (options & LYXP_SCNODE_ALL) { |
| 3652 | set_scnode_clear_ctx(set); |
| 3653 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3654 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3655 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3656 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3657 | |
| 3658 | /* position is filled later */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3659 | set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3660 | } |
| 3661 | |
| 3662 | return LY_SUCCESS; |
| 3663 | } |
| 3664 | |
| 3665 | /** |
| 3666 | * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either |
| 3667 | * leafref or instance-identifier target node(s). |
| 3668 | * |
| 3669 | * @param[in] args Array of arguments. |
| 3670 | * @param[in] arg_count Count of elements in @p args. |
| 3671 | * @param[in,out] set Context and result set at the same time. |
| 3672 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3673 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3674 | */ |
| 3675 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3676 | 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] | 3677 | { |
| 3678 | struct lyd_node_term *leaf; |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3679 | struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3680 | struct lysc_type_leafref *lref; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3681 | const struct lysc_node *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3682 | struct ly_path *p; |
| 3683 | struct lyd_node *node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3684 | char *errmsg = NULL; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3685 | uint8_t oper; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3686 | LY_ERR rc = LY_SUCCESS; |
| 3687 | |
| 3688 | if (options & LYXP_SCNODE_ALL) { |
| 3689 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3690 | 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] | 3691 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3692 | 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] | 3693 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
| 3694 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3695 | __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3696 | } |
| 3697 | set_scnode_clear_ctx(set); |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3698 | if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3699 | lref = (struct lysc_type_leafref *)sleaf->type; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3700 | 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] | 3701 | |
| 3702 | /* it was already evaluated on schema, it must succeed */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3703 | rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, |
| 3704 | LY_PATH_LREF_TRUE, oper, LY_PATH_TARGET_MANY, LY_PREF_SCHEMA_RESOLVED, lref->prefixes, &p); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3705 | assert(!rc); |
| 3706 | |
| 3707 | /* get the target node */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3708 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3709 | ly_path_free(set->ctx, p); |
| 3710 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 3711 | 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] | 3712 | } |
| 3713 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3714 | return rc; |
| 3715 | } |
| 3716 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3717 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3718 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3719 | return LY_EVALID; |
| 3720 | } |
| 3721 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3722 | lyxp_set_free_content(set); |
| 3723 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3724 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3725 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3726 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 3727 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3728 | /* find leafref target */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3729 | if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3730 | &leaf->value, set->tree, &node, &errmsg)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3731 | LOGERR(set->ctx, LY_EVALID, errmsg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3732 | free(errmsg); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3733 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3734 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3735 | } else { |
| 3736 | assert(sleaf->type->basetype == LY_TYPE_INST); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3737 | if (ly_path_eval(leaf->value.target, set->tree, &node)) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3738 | LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3739 | LYD_CANON_VALUE(leaf)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3740 | return LY_EVALID; |
| 3741 | } |
| 3742 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3743 | |
| 3744 | /* insert it */ |
| 3745 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3746 | } |
| 3747 | } |
| 3748 | |
| 3749 | return LY_SUCCESS; |
| 3750 | } |
| 3751 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3752 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3753 | 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] | 3754 | { |
| 3755 | uint16_t i; |
| 3756 | LY_ARRAY_COUNT_TYPE u; |
| 3757 | struct lyd_node_term *leaf; |
| 3758 | struct lysc_node_leaf *sleaf; |
| 3759 | struct lyd_meta *meta; |
| 3760 | struct lyd_value data = {0}, *val; |
| 3761 | struct ly_err_item *err = NULL; |
| 3762 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3763 | ly_bool found; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3764 | |
| 3765 | if (options & LYXP_SCNODE_ALL) { |
| 3766 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3767 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func); |
| 3768 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3769 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3770 | sleaf->name); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3771 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 3772 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name); |
| 3773 | } |
| 3774 | |
| 3775 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3776 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3777 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3778 | sleaf->name); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3779 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3780 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name); |
| 3781 | } |
| 3782 | } |
| 3783 | set_scnode_clear_ctx(set); |
| 3784 | return rc; |
| 3785 | } |
| 3786 | |
| 3787 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3788 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3789 | "derived-from(-or-self)(node-set, string)"); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3790 | return LY_EVALID; |
| 3791 | } |
| 3792 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
| 3793 | LY_CHECK_RET(rc); |
| 3794 | |
| 3795 | set_fill_boolean(set, 0); |
| 3796 | found = 0; |
| 3797 | for (i = 0; i < args[0]->used; ++i) { |
| 3798 | if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) { |
| 3799 | continue; |
| 3800 | } |
| 3801 | |
| 3802 | if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 3803 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 3804 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3805 | val = &leaf->value; |
| 3806 | if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) { |
| 3807 | /* uninteresting */ |
| 3808 | continue; |
| 3809 | } |
| 3810 | |
| 3811 | /* store args[1] as ident */ |
| 3812 | rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str), |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3813 | 0, set->format, set->prefix_data, LYD_HINT_DATA, leaf->schema, &data, &err); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3814 | } else { |
| 3815 | meta = args[0]->val.meta[i].meta; |
| 3816 | val = &meta->value; |
| 3817 | if (val->realtype->basetype != LY_TYPE_IDENT) { |
| 3818 | /* uninteresting */ |
| 3819 | continue; |
| 3820 | } |
| 3821 | |
| 3822 | /* store args[1] as ident */ |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 3823 | rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str), 0, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3824 | set->format, set->prefix_data, LYD_HINT_DATA, meta->parent->schema, &data, &err); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3825 | } |
| 3826 | |
| 3827 | if (err) { |
| 3828 | ly_err_print(err); |
| 3829 | ly_err_free(err); |
| 3830 | } |
| 3831 | LY_CHECK_RET(rc); |
| 3832 | |
| 3833 | /* finally check the identity itself */ |
| 3834 | if (self_match && (data.ident == val->ident)) { |
| 3835 | set_fill_boolean(set, 1); |
| 3836 | found = 1; |
| 3837 | } |
| 3838 | if (!found) { |
| 3839 | LY_ARRAY_FOR(data.ident->derived, u) { |
| 3840 | if (data.ident->derived[u] == val->ident) { |
| 3841 | set_fill_boolean(set, 1); |
| 3842 | found = 1; |
| 3843 | break; |
| 3844 | } |
| 3845 | } |
| 3846 | } |
| 3847 | |
| 3848 | /* free temporary value */ |
| 3849 | val->realtype->plugin->free(set->ctx, &data); |
| 3850 | if (found) { |
| 3851 | break; |
| 3852 | } |
| 3853 | } |
| 3854 | |
| 3855 | return LY_SUCCESS; |
| 3856 | } |
| 3857 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3858 | /** |
| 3859 | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3860 | * on whether the first argument nodes contain a node of an identity derived from the second |
| 3861 | * argument identity. |
| 3862 | * |
| 3863 | * @param[in] args Array of arguments. |
| 3864 | * @param[in] arg_count Count of elements in @p args. |
| 3865 | * @param[in,out] set Context and result set at the same time. |
| 3866 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3867 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3868 | */ |
| 3869 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3870 | 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] | 3871 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3872 | return xpath_derived_(args, set, options, 0, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3873 | } |
| 3874 | |
| 3875 | /** |
| 3876 | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3877 | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
| 3878 | * the second argument identity. |
| 3879 | * |
| 3880 | * @param[in] args Array of arguments. |
| 3881 | * @param[in] arg_count Count of elements in @p args. |
| 3882 | * @param[in,out] set Context and result set at the same time. |
| 3883 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3884 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3885 | */ |
| 3886 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3887 | 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] | 3888 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3889 | return xpath_derived_(args, set, options, 1, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3890 | } |
| 3891 | |
| 3892 | /** |
| 3893 | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
| 3894 | * with the integer value of the first node's enum value, otherwise NaN. |
| 3895 | * |
| 3896 | * @param[in] args Array of arguments. |
| 3897 | * @param[in] arg_count Count of elements in @p args. |
| 3898 | * @param[in,out] set Context and result set at the same time. |
| 3899 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3900 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3901 | */ |
| 3902 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3903 | 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] | 3904 | { |
| 3905 | struct lyd_node_term *leaf; |
| 3906 | struct lysc_node_leaf *sleaf; |
| 3907 | LY_ERR rc = LY_SUCCESS; |
| 3908 | |
| 3909 | if (options & LYXP_SCNODE_ALL) { |
| 3910 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3911 | 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] | 3912 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3913 | 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] | 3914 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
| 3915 | 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] | 3916 | } |
| 3917 | set_scnode_clear_ctx(set); |
| 3918 | return rc; |
| 3919 | } |
| 3920 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3921 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3922 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3923 | return LY_EVALID; |
| 3924 | } |
| 3925 | |
| 3926 | set_fill_number(set, NAN); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3927 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3928 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3929 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3930 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
| 3931 | set_fill_number(set, leaf->value.enum_item->value); |
| 3932 | } |
| 3933 | } |
| 3934 | |
| 3935 | return LY_SUCCESS; |
| 3936 | } |
| 3937 | |
| 3938 | /** |
| 3939 | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
| 3940 | * with false value. |
| 3941 | * |
| 3942 | * @param[in] args Array of arguments. |
| 3943 | * @param[in] arg_count Count of elements in @p args. |
| 3944 | * @param[in,out] set Context and result set at the same time. |
| 3945 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3946 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3947 | */ |
| 3948 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3949 | 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] | 3950 | { |
| 3951 | if (options & LYXP_SCNODE_ALL) { |
| 3952 | set_scnode_clear_ctx(set); |
| 3953 | return LY_SUCCESS; |
| 3954 | } |
| 3955 | |
| 3956 | set_fill_boolean(set, 0); |
| 3957 | return LY_SUCCESS; |
| 3958 | } |
| 3959 | |
| 3960 | /** |
| 3961 | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
| 3962 | * with the first argument floored (truncated). |
| 3963 | * |
| 3964 | * @param[in] args Array of arguments. |
| 3965 | * @param[in] arg_count Count of elements in @p args. |
| 3966 | * @param[in,out] set Context and result set at the same time. |
| 3967 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3968 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3969 | */ |
| 3970 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3971 | 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] | 3972 | { |
| 3973 | LY_ERR rc; |
| 3974 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3975 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3976 | LY_CHECK_RET(rc); |
| 3977 | if (isfinite(args[0]->val.num)) { |
| 3978 | set_fill_number(set, (long long)args[0]->val.num); |
| 3979 | } |
| 3980 | |
| 3981 | return LY_SUCCESS; |
| 3982 | } |
| 3983 | |
| 3984 | /** |
| 3985 | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
| 3986 | * whether the language of the text matches the one from the argument. |
| 3987 | * |
| 3988 | * @param[in] args Array of arguments. |
| 3989 | * @param[in] arg_count Count of elements in @p args. |
| 3990 | * @param[in,out] set Context and result set at the same time. |
| 3991 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3992 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3993 | */ |
| 3994 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3995 | 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] | 3996 | { |
| 3997 | const struct lyd_node *node; |
| 3998 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 3999 | struct lyd_meta *meta = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4000 | const char *val; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4001 | LY_ERR rc = LY_SUCCESS; |
| 4002 | |
| 4003 | if (options & LYXP_SCNODE_ALL) { |
| 4004 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4005 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4006 | 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] | 4007 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4008 | 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] | 4009 | } |
| 4010 | } |
| 4011 | set_scnode_clear_ctx(set); |
| 4012 | return rc; |
| 4013 | } |
| 4014 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4015 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4016 | LY_CHECK_RET(rc); |
| 4017 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4018 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4019 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4020 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4021 | } else if (!set->used) { |
| 4022 | set_fill_boolean(set, 0); |
| 4023 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4024 | } |
| 4025 | |
| 4026 | switch (set->val.nodes[0].type) { |
| 4027 | case LYXP_NODE_ELEM: |
| 4028 | case LYXP_NODE_TEXT: |
| 4029 | node = set->val.nodes[0].node; |
| 4030 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4031 | case LYXP_NODE_META: |
| 4032 | node = set->val.meta[0].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4033 | break; |
| 4034 | default: |
| 4035 | /* nothing to do with roots */ |
| 4036 | set_fill_boolean(set, 0); |
| 4037 | return LY_SUCCESS; |
| 4038 | } |
| 4039 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4040 | /* find lang metadata */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 4041 | for ( ; node; node = (struct lyd_node *)node->parent) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4042 | for (meta = node->meta; meta; meta = meta->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4043 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4044 | 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] | 4045 | break; |
| 4046 | } |
| 4047 | } |
| 4048 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4049 | if (meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4050 | break; |
| 4051 | } |
| 4052 | } |
| 4053 | |
| 4054 | /* compare languages */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4055 | if (!meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4056 | set_fill_boolean(set, 0); |
| 4057 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4058 | uint64_t i; |
| 4059 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4060 | val = meta->value.canonical; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4061 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 4062 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
| 4063 | set_fill_boolean(set, 0); |
| 4064 | break; |
| 4065 | } |
| 4066 | } |
| 4067 | if (!args[0]->val.str[i]) { |
| 4068 | if (!val[i] || (val[i] == '-')) { |
| 4069 | set_fill_boolean(set, 1); |
| 4070 | } else { |
| 4071 | set_fill_boolean(set, 0); |
| 4072 | } |
| 4073 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4074 | } |
| 4075 | |
| 4076 | return LY_SUCCESS; |
| 4077 | } |
| 4078 | |
| 4079 | /** |
| 4080 | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
| 4081 | * with the context size. |
| 4082 | * |
| 4083 | * @param[in] args Array of arguments. |
| 4084 | * @param[in] arg_count Count of elements in @p args. |
| 4085 | * @param[in,out] set Context and result set at the same time. |
| 4086 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4087 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4088 | */ |
| 4089 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4090 | 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] | 4091 | { |
| 4092 | if (options & LYXP_SCNODE_ALL) { |
| 4093 | set_scnode_clear_ctx(set); |
| 4094 | return LY_SUCCESS; |
| 4095 | } |
| 4096 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4097 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4098 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4099 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4100 | } else if (!set->used) { |
| 4101 | set_fill_number(set, 0); |
| 4102 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4103 | } |
| 4104 | |
| 4105 | set_fill_number(set, set->ctx_size); |
| 4106 | return LY_SUCCESS; |
| 4107 | } |
| 4108 | |
| 4109 | /** |
| 4110 | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
| 4111 | * with the node name without namespace from the argument or the context. |
| 4112 | * |
| 4113 | * @param[in] args Array of arguments. |
| 4114 | * @param[in] arg_count Count of elements in @p args. |
| 4115 | * @param[in,out] set Context and result set at the same time. |
| 4116 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4117 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4118 | */ |
| 4119 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4120 | 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] | 4121 | { |
| 4122 | struct lyxp_set_node *item; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4123 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4124 | /* suppress unused variable warning */ |
| 4125 | (void)options; |
| 4126 | |
| 4127 | if (options & LYXP_SCNODE_ALL) { |
| 4128 | set_scnode_clear_ctx(set); |
| 4129 | return LY_SUCCESS; |
| 4130 | } |
| 4131 | |
| 4132 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4133 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4134 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 4135 | "local-name(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4136 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4137 | } else if (!args[0]->used) { |
| 4138 | set_fill_string(set, "", 0); |
| 4139 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4140 | } |
| 4141 | |
| 4142 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4143 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4144 | |
| 4145 | item = &args[0]->val.nodes[0]; |
| 4146 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4147 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4148 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4149 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4150 | } else if (!set->used) { |
| 4151 | set_fill_string(set, "", 0); |
| 4152 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4153 | } |
| 4154 | |
| 4155 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4156 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4157 | |
| 4158 | item = &set->val.nodes[0]; |
| 4159 | } |
| 4160 | |
| 4161 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4162 | case LYXP_NODE_NONE: |
| 4163 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4164 | case LYXP_NODE_ROOT: |
| 4165 | case LYXP_NODE_ROOT_CONFIG: |
| 4166 | case LYXP_NODE_TEXT: |
| 4167 | set_fill_string(set, "", 0); |
| 4168 | break; |
| 4169 | case LYXP_NODE_ELEM: |
| 4170 | set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name)); |
| 4171 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4172 | case LYXP_NODE_META: |
| 4173 | 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] | 4174 | break; |
| 4175 | } |
| 4176 | |
| 4177 | return LY_SUCCESS; |
| 4178 | } |
| 4179 | |
| 4180 | /** |
| 4181 | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
| 4182 | * with the node name fully qualified (with namespace) from the argument or the context. |
| 4183 | * |
| 4184 | * @param[in] args Array of arguments. |
| 4185 | * @param[in] arg_count Count of elements in @p args. |
| 4186 | * @param[in,out] set Context and result set at the same time. |
| 4187 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4188 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4189 | */ |
| 4190 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4191 | 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] | 4192 | { |
| 4193 | struct lyxp_set_node *item; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4194 | struct lys_module *mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4195 | char *str; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4196 | const char *name = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4197 | |
| 4198 | if (options & LYXP_SCNODE_ALL) { |
| 4199 | set_scnode_clear_ctx(set); |
| 4200 | return LY_SUCCESS; |
| 4201 | } |
| 4202 | |
| 4203 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4204 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4205 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4206 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4207 | } else if (!args[0]->used) { |
| 4208 | set_fill_string(set, "", 0); |
| 4209 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4210 | } |
| 4211 | |
| 4212 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4213 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4214 | |
| 4215 | item = &args[0]->val.nodes[0]; |
| 4216 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4217 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4218 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4219 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4220 | } else if (!set->used) { |
| 4221 | set_fill_string(set, "", 0); |
| 4222 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4223 | } |
| 4224 | |
| 4225 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4226 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4227 | |
| 4228 | item = &set->val.nodes[0]; |
| 4229 | } |
| 4230 | |
| 4231 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4232 | case LYXP_NODE_NONE: |
| 4233 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4234 | case LYXP_NODE_ROOT: |
| 4235 | case LYXP_NODE_ROOT_CONFIG: |
| 4236 | case LYXP_NODE_TEXT: |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4237 | /* keep NULL */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4238 | break; |
| 4239 | case LYXP_NODE_ELEM: |
| 4240 | mod = item->node->schema->module; |
| 4241 | name = item->node->schema->name; |
| 4242 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4243 | case LYXP_NODE_META: |
| 4244 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
| 4245 | name = ((struct lyd_meta *)item->node)->name; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4246 | break; |
| 4247 | } |
| 4248 | |
| 4249 | if (mod && name) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4250 | int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4251 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
| 4252 | set_fill_string(set, str, strlen(str)); |
| 4253 | free(str); |
| 4254 | } else { |
| 4255 | set_fill_string(set, "", 0); |
| 4256 | } |
| 4257 | |
| 4258 | return LY_SUCCESS; |
| 4259 | } |
| 4260 | |
| 4261 | /** |
| 4262 | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
| 4263 | * with the namespace of the node from the argument or the context. |
| 4264 | * |
| 4265 | * @param[in] args Array of arguments. |
| 4266 | * @param[in] arg_count Count of elements in @p args. |
| 4267 | * @param[in,out] set Context and result set at the same time. |
| 4268 | * @param[in] options XPath options. |
| 4269 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4270 | */ |
| 4271 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4272 | 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] | 4273 | { |
| 4274 | struct lyxp_set_node *item; |
| 4275 | struct lys_module *mod; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4276 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4277 | /* suppress unused variable warning */ |
| 4278 | (void)options; |
| 4279 | |
| 4280 | if (options & LYXP_SCNODE_ALL) { |
| 4281 | set_scnode_clear_ctx(set); |
| 4282 | return LY_SUCCESS; |
| 4283 | } |
| 4284 | |
| 4285 | if (arg_count) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4286 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4287 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4288 | "namespace-uri(node-set?)"); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4289 | return LY_EVALID; |
| 4290 | } else if (!args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4291 | set_fill_string(set, "", 0); |
| 4292 | return LY_SUCCESS; |
| 4293 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4294 | |
| 4295 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4296 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4297 | |
| 4298 | item = &args[0]->val.nodes[0]; |
| 4299 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4300 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4301 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4302 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4303 | } else if (!set->used) { |
| 4304 | set_fill_string(set, "", 0); |
| 4305 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4306 | } |
| 4307 | |
| 4308 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4309 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4310 | |
| 4311 | item = &set->val.nodes[0]; |
| 4312 | } |
| 4313 | |
| 4314 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4315 | case LYXP_NODE_NONE: |
| 4316 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4317 | case LYXP_NODE_ROOT: |
| 4318 | case LYXP_NODE_ROOT_CONFIG: |
| 4319 | case LYXP_NODE_TEXT: |
| 4320 | set_fill_string(set, "", 0); |
| 4321 | break; |
| 4322 | case LYXP_NODE_ELEM: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4323 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4324 | if (item->type == LYXP_NODE_ELEM) { |
| 4325 | mod = item->node->schema->module; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4326 | } else { /* LYXP_NODE_META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4327 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4328 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4329 | } |
| 4330 | |
| 4331 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
| 4332 | break; |
| 4333 | } |
| 4334 | |
| 4335 | return LY_SUCCESS; |
| 4336 | } |
| 4337 | |
| 4338 | /** |
| 4339 | * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET |
| 4340 | * with only nodes from the context. In practice it either leaves the context |
| 4341 | * as it is or returns an empty node set. |
| 4342 | * |
| 4343 | * @param[in] args Array of arguments. |
| 4344 | * @param[in] arg_count Count of elements in @p args. |
| 4345 | * @param[in,out] set Context and result set at the same time. |
| 4346 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4347 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4348 | */ |
| 4349 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4350 | 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] | 4351 | { |
| 4352 | if (options & LYXP_SCNODE_ALL) { |
| 4353 | set_scnode_clear_ctx(set); |
| 4354 | return LY_SUCCESS; |
| 4355 | } |
| 4356 | |
| 4357 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4358 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4359 | } |
| 4360 | return LY_SUCCESS; |
| 4361 | } |
| 4362 | |
| 4363 | /** |
| 4364 | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
| 4365 | * with normalized value (no leading, trailing, double white spaces) of the node |
| 4366 | * from the argument or the context. |
| 4367 | * |
| 4368 | * @param[in] args Array of arguments. |
| 4369 | * @param[in] arg_count Count of elements in @p args. |
| 4370 | * @param[in,out] set Context and result set at the same time. |
| 4371 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4372 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4373 | */ |
| 4374 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4375 | 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] | 4376 | { |
| 4377 | uint16_t i, new_used; |
| 4378 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4379 | ly_bool have_spaces = 0, space_before = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4380 | struct lysc_node_leaf *sleaf; |
| 4381 | LY_ERR rc = LY_SUCCESS; |
| 4382 | |
| 4383 | if (options & LYXP_SCNODE_ALL) { |
| 4384 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4385 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4386 | 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] | 4387 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4388 | 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] | 4389 | } |
| 4390 | } |
| 4391 | set_scnode_clear_ctx(set); |
| 4392 | return rc; |
| 4393 | } |
| 4394 | |
| 4395 | if (arg_count) { |
| 4396 | set_fill_set(set, args[0]); |
| 4397 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4398 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4399 | LY_CHECK_RET(rc); |
| 4400 | |
| 4401 | /* is there any normalization necessary? */ |
| 4402 | for (i = 0; set->val.str[i]; ++i) { |
| 4403 | if (is_xmlws(set->val.str[i])) { |
| 4404 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
| 4405 | have_spaces = 1; |
| 4406 | break; |
| 4407 | } |
| 4408 | space_before = 1; |
| 4409 | } else { |
| 4410 | space_before = 0; |
| 4411 | } |
| 4412 | } |
| 4413 | |
| 4414 | /* yep, there is */ |
| 4415 | if (have_spaces) { |
| 4416 | /* it's enough, at least one character will go, makes space for ending '\0' */ |
| 4417 | new = malloc(strlen(set->val.str) * sizeof(char)); |
| 4418 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4419 | new_used = 0; |
| 4420 | |
| 4421 | space_before = 0; |
| 4422 | for (i = 0; set->val.str[i]; ++i) { |
| 4423 | if (is_xmlws(set->val.str[i])) { |
| 4424 | if ((i == 0) || space_before) { |
| 4425 | space_before = 1; |
| 4426 | continue; |
| 4427 | } else { |
| 4428 | space_before = 1; |
| 4429 | } |
| 4430 | } else { |
| 4431 | space_before = 0; |
| 4432 | } |
| 4433 | |
| 4434 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
| 4435 | ++new_used; |
| 4436 | } |
| 4437 | |
| 4438 | /* at worst there is one trailing space now */ |
| 4439 | if (new_used && is_xmlws(new[new_used - 1])) { |
| 4440 | --new_used; |
| 4441 | } |
| 4442 | |
| 4443 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 4444 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4445 | new[new_used] = '\0'; |
| 4446 | |
| 4447 | free(set->val.str); |
| 4448 | set->val.str = new; |
| 4449 | } |
| 4450 | |
| 4451 | return LY_SUCCESS; |
| 4452 | } |
| 4453 | |
| 4454 | /** |
| 4455 | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
| 4456 | * with the argument converted to boolean and logically inverted. |
| 4457 | * |
| 4458 | * @param[in] args Array of arguments. |
| 4459 | * @param[in] arg_count Count of elements in @p args. |
| 4460 | * @param[in,out] set Context and result set at the same time. |
| 4461 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4462 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4463 | */ |
| 4464 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4465 | 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] | 4466 | { |
| 4467 | if (options & LYXP_SCNODE_ALL) { |
| 4468 | set_scnode_clear_ctx(set); |
| 4469 | return LY_SUCCESS; |
| 4470 | } |
| 4471 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4472 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4473 | if (args[0]->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4474 | set_fill_boolean(set, 0); |
| 4475 | } else { |
| 4476 | set_fill_boolean(set, 1); |
| 4477 | } |
| 4478 | |
| 4479 | return LY_SUCCESS; |
| 4480 | } |
| 4481 | |
| 4482 | /** |
| 4483 | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
| 4484 | * with the number representation of either the argument or the context. |
| 4485 | * |
| 4486 | * @param[in] args Array of arguments. |
| 4487 | * @param[in] arg_count Count of elements in @p args. |
| 4488 | * @param[in,out] set Context and result set at the same time. |
| 4489 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4490 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4491 | */ |
| 4492 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4493 | 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] | 4494 | { |
| 4495 | LY_ERR rc; |
| 4496 | |
| 4497 | if (options & LYXP_SCNODE_ALL) { |
| 4498 | set_scnode_clear_ctx(set); |
| 4499 | return LY_SUCCESS; |
| 4500 | } |
| 4501 | |
| 4502 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4503 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4504 | LY_CHECK_RET(rc); |
| 4505 | set_fill_set(set, args[0]); |
| 4506 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4507 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4508 | LY_CHECK_RET(rc); |
| 4509 | } |
| 4510 | |
| 4511 | return LY_SUCCESS; |
| 4512 | } |
| 4513 | |
| 4514 | /** |
| 4515 | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
| 4516 | * with the context position. |
| 4517 | * |
| 4518 | * @param[in] args Array of arguments. |
| 4519 | * @param[in] arg_count Count of elements in @p args. |
| 4520 | * @param[in,out] set Context and result set at the same time. |
| 4521 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4522 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4523 | */ |
| 4524 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4525 | 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] | 4526 | { |
| 4527 | if (options & LYXP_SCNODE_ALL) { |
| 4528 | set_scnode_clear_ctx(set); |
| 4529 | return LY_SUCCESS; |
| 4530 | } |
| 4531 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4532 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4533 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4534 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4535 | } else if (!set->used) { |
| 4536 | set_fill_number(set, 0); |
| 4537 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4538 | } |
| 4539 | |
| 4540 | set_fill_number(set, set->ctx_pos); |
| 4541 | |
| 4542 | /* UNUSED in 'Release' build type */ |
| 4543 | (void)options; |
| 4544 | return LY_SUCCESS; |
| 4545 | } |
| 4546 | |
| 4547 | /** |
| 4548 | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
| 4549 | * depending on whether the second argument regex matches the first argument string. For details refer to |
| 4550 | * YANG 1.1 RFC section 10.2.1. |
| 4551 | * |
| 4552 | * @param[in] args Array of arguments. |
| 4553 | * @param[in] arg_count Count of elements in @p args. |
| 4554 | * @param[in,out] set Context and result set at the same time. |
| 4555 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4556 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4557 | */ |
| 4558 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4559 | 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] | 4560 | { |
| 4561 | struct lysc_pattern **patterns = NULL, **pattern; |
| 4562 | struct lysc_node_leaf *sleaf; |
| 4563 | char *path; |
| 4564 | LY_ERR rc = LY_SUCCESS; |
| 4565 | struct ly_err_item *err; |
| 4566 | |
| 4567 | if (options & LYXP_SCNODE_ALL) { |
| 4568 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4569 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4570 | 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] | 4571 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4572 | 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] | 4573 | } |
| 4574 | } |
| 4575 | |
| 4576 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4577 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4578 | 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] | 4579 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4580 | 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] | 4581 | } |
| 4582 | } |
| 4583 | set_scnode_clear_ctx(set); |
| 4584 | return rc; |
| 4585 | } |
| 4586 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4587 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4588 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4589 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4590 | LY_CHECK_RET(rc); |
| 4591 | |
| 4592 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
| 4593 | *pattern = malloc(sizeof **pattern); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4594 | path = lyd_path(set->cur_node, LYD_PATH_LOG, NULL, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4595 | rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code); |
| 4596 | free(path); |
| 4597 | if (rc != LY_SUCCESS) { |
| 4598 | LY_ARRAY_FREE(patterns); |
| 4599 | return rc; |
| 4600 | } |
| 4601 | |
| 4602 | rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err); |
| 4603 | pcre2_code_free((*pattern)->code); |
| 4604 | free(*pattern); |
| 4605 | LY_ARRAY_FREE(patterns); |
| 4606 | if (rc && (rc != LY_EVALID)) { |
| 4607 | ly_err_print(err); |
| 4608 | ly_err_free(err); |
| 4609 | return rc; |
| 4610 | } |
| 4611 | |
| 4612 | if (rc == LY_EVALID) { |
| 4613 | ly_err_free(err); |
| 4614 | set_fill_boolean(set, 0); |
| 4615 | } else { |
| 4616 | set_fill_boolean(set, 1); |
| 4617 | } |
| 4618 | |
| 4619 | return LY_SUCCESS; |
| 4620 | } |
| 4621 | |
| 4622 | /** |
| 4623 | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
| 4624 | * with the rounded first argument. For details refer to |
| 4625 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
| 4626 | * |
| 4627 | * @param[in] args Array of arguments. |
| 4628 | * @param[in] arg_count Count of elements in @p args. |
| 4629 | * @param[in,out] set Context and result set at the same time. |
| 4630 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4631 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4632 | */ |
| 4633 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4634 | 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] | 4635 | { |
| 4636 | struct lysc_node_leaf *sleaf; |
| 4637 | LY_ERR rc = LY_SUCCESS; |
| 4638 | |
| 4639 | if (options & LYXP_SCNODE_ALL) { |
| 4640 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4641 | 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] | 4642 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4643 | 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] | 4644 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4645 | 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] | 4646 | } |
| 4647 | set_scnode_clear_ctx(set); |
| 4648 | return rc; |
| 4649 | } |
| 4650 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4651 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4652 | LY_CHECK_RET(rc); |
| 4653 | |
| 4654 | /* cover only the cases where floor can't be used */ |
| 4655 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
| 4656 | set_fill_number(set, -0.0f); |
| 4657 | } else { |
| 4658 | args[0]->val.num += 0.5; |
| 4659 | rc = xpath_floor(args, 1, args[0], options); |
| 4660 | LY_CHECK_RET(rc); |
| 4661 | set_fill_number(set, args[0]->val.num); |
| 4662 | } |
| 4663 | |
| 4664 | return LY_SUCCESS; |
| 4665 | } |
| 4666 | |
| 4667 | /** |
| 4668 | * @brief Execute the XPath starts-with(string, string) function. |
| 4669 | * Returns LYXP_SET_BOOLEAN whether the second argument is |
| 4670 | * the prefix of the first or not. |
| 4671 | * |
| 4672 | * @param[in] args Array of arguments. |
| 4673 | * @param[in] arg_count Count of elements in @p args. |
| 4674 | * @param[in,out] set Context and result set at the same time. |
| 4675 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4676 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4677 | */ |
| 4678 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4679 | 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] | 4680 | { |
| 4681 | struct lysc_node_leaf *sleaf; |
| 4682 | LY_ERR rc = LY_SUCCESS; |
| 4683 | |
| 4684 | if (options & LYXP_SCNODE_ALL) { |
| 4685 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4686 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4687 | 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] | 4688 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4689 | 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] | 4690 | } |
| 4691 | } |
| 4692 | |
| 4693 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4694 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4695 | 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] | 4696 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4697 | 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] | 4698 | } |
| 4699 | } |
| 4700 | set_scnode_clear_ctx(set); |
| 4701 | return rc; |
| 4702 | } |
| 4703 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4704 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4705 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4706 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4707 | LY_CHECK_RET(rc); |
| 4708 | |
| 4709 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
| 4710 | set_fill_boolean(set, 0); |
| 4711 | } else { |
| 4712 | set_fill_boolean(set, 1); |
| 4713 | } |
| 4714 | |
| 4715 | return LY_SUCCESS; |
| 4716 | } |
| 4717 | |
| 4718 | /** |
| 4719 | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
| 4720 | * with the string representation of either the argument or the context. |
| 4721 | * |
| 4722 | * @param[in] args Array of arguments. |
| 4723 | * @param[in] arg_count Count of elements in @p args. |
| 4724 | * @param[in,out] set Context and result set at the same time. |
| 4725 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4726 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4727 | */ |
| 4728 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4729 | 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] | 4730 | { |
| 4731 | LY_ERR rc; |
| 4732 | |
| 4733 | if (options & LYXP_SCNODE_ALL) { |
| 4734 | set_scnode_clear_ctx(set); |
| 4735 | return LY_SUCCESS; |
| 4736 | } |
| 4737 | |
| 4738 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4739 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4740 | LY_CHECK_RET(rc); |
| 4741 | set_fill_set(set, args[0]); |
| 4742 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4743 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4744 | LY_CHECK_RET(rc); |
| 4745 | } |
| 4746 | |
| 4747 | return LY_SUCCESS; |
| 4748 | } |
| 4749 | |
| 4750 | /** |
| 4751 | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
| 4752 | * with the length of the string in either the argument or the context. |
| 4753 | * |
| 4754 | * @param[in] args Array of arguments. |
| 4755 | * @param[in] arg_count Count of elements in @p args. |
| 4756 | * @param[in,out] set Context and result set at the same time. |
| 4757 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4758 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4759 | */ |
| 4760 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4761 | 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] | 4762 | { |
| 4763 | struct lysc_node_leaf *sleaf; |
| 4764 | LY_ERR rc = LY_SUCCESS; |
| 4765 | |
| 4766 | if (options & LYXP_SCNODE_ALL) { |
| 4767 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4768 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4769 | 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] | 4770 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4771 | 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] | 4772 | } |
| 4773 | } |
| 4774 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
| 4775 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4776 | 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] | 4777 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4778 | 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] | 4779 | } |
| 4780 | } |
| 4781 | set_scnode_clear_ctx(set); |
| 4782 | return rc; |
| 4783 | } |
| 4784 | |
| 4785 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4786 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4787 | LY_CHECK_RET(rc); |
| 4788 | set_fill_number(set, strlen(args[0]->val.str)); |
| 4789 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4790 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4791 | LY_CHECK_RET(rc); |
| 4792 | set_fill_number(set, strlen(set->val.str)); |
| 4793 | } |
| 4794 | |
| 4795 | return LY_SUCCESS; |
| 4796 | } |
| 4797 | |
| 4798 | /** |
| 4799 | * @brief Execute the XPath substring(string, number, number?) function. |
| 4800 | * Returns LYXP_SET_STRING substring of the first argument starting |
| 4801 | * on the second argument index ending on the third argument index, |
| 4802 | * indexed from 1. For exact definition refer to |
| 4803 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
| 4804 | * |
| 4805 | * @param[in] args Array of arguments. |
| 4806 | * @param[in] arg_count Count of elements in @p args. |
| 4807 | * @param[in,out] set Context and result set at the same time. |
| 4808 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4809 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4810 | */ |
| 4811 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4812 | 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] | 4813 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4814 | int32_t start, len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4815 | uint16_t str_start, str_len, pos; |
| 4816 | struct lysc_node_leaf *sleaf; |
| 4817 | LY_ERR rc = LY_SUCCESS; |
| 4818 | |
| 4819 | if (options & LYXP_SCNODE_ALL) { |
| 4820 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4821 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4822 | 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] | 4823 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4824 | 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] | 4825 | } |
| 4826 | } |
| 4827 | |
| 4828 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4829 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4830 | 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] | 4831 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4832 | 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] | 4833 | } |
| 4834 | } |
| 4835 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4836 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) && |
| 4837 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4838 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4839 | 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] | 4840 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4841 | 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] | 4842 | } |
| 4843 | } |
| 4844 | set_scnode_clear_ctx(set); |
| 4845 | return rc; |
| 4846 | } |
| 4847 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4848 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4849 | LY_CHECK_RET(rc); |
| 4850 | |
| 4851 | /* start */ |
| 4852 | if (xpath_round(&args[1], 1, args[1], options)) { |
| 4853 | return -1; |
| 4854 | } |
| 4855 | if (isfinite(args[1]->val.num)) { |
| 4856 | start = args[1]->val.num - 1; |
| 4857 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4858 | start = INT32_MIN; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4859 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4860 | start = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4861 | } |
| 4862 | |
| 4863 | /* len */ |
| 4864 | if (arg_count == 3) { |
| 4865 | rc = xpath_round(&args[2], 1, args[2], options); |
| 4866 | LY_CHECK_RET(rc); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4867 | if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4868 | len = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4869 | } else if (isfinite(args[2]->val.num)) { |
| 4870 | len = args[2]->val.num; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4871 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4872 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4873 | } |
| 4874 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4875 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4876 | } |
| 4877 | |
| 4878 | /* find matching character positions */ |
| 4879 | str_start = 0; |
| 4880 | str_len = 0; |
| 4881 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
| 4882 | if (pos < start) { |
| 4883 | ++str_start; |
| 4884 | } else if (pos < start + len) { |
| 4885 | ++str_len; |
| 4886 | } else { |
| 4887 | break; |
| 4888 | } |
| 4889 | } |
| 4890 | |
| 4891 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
| 4892 | return LY_SUCCESS; |
| 4893 | } |
| 4894 | |
| 4895 | /** |
| 4896 | * @brief Execute the XPath substring-after(string, string) function. |
| 4897 | * Returns LYXP_SET_STRING with the string succeeding the occurance |
| 4898 | * of the second argument in the first or an empty string. |
| 4899 | * |
| 4900 | * @param[in] args Array of arguments. |
| 4901 | * @param[in] arg_count Count of elements in @p args. |
| 4902 | * @param[in,out] set Context and result set at the same time. |
| 4903 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4904 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4905 | */ |
| 4906 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4907 | 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] | 4908 | { |
| 4909 | char *ptr; |
| 4910 | struct lysc_node_leaf *sleaf; |
| 4911 | LY_ERR rc = LY_SUCCESS; |
| 4912 | |
| 4913 | if (options & LYXP_SCNODE_ALL) { |
| 4914 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4915 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4916 | 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] | 4917 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4918 | 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] | 4919 | } |
| 4920 | } |
| 4921 | |
| 4922 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4923 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4924 | 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] | 4925 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4926 | 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] | 4927 | } |
| 4928 | } |
| 4929 | set_scnode_clear_ctx(set); |
| 4930 | return rc; |
| 4931 | } |
| 4932 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4933 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4934 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4935 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4936 | LY_CHECK_RET(rc); |
| 4937 | |
| 4938 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4939 | if (ptr) { |
| 4940 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
| 4941 | } else { |
| 4942 | set_fill_string(set, "", 0); |
| 4943 | } |
| 4944 | |
| 4945 | return LY_SUCCESS; |
| 4946 | } |
| 4947 | |
| 4948 | /** |
| 4949 | * @brief Execute the XPath substring-before(string, string) function. |
| 4950 | * Returns LYXP_SET_STRING with the string preceding the occurance |
| 4951 | * of the second argument in the first or an empty string. |
| 4952 | * |
| 4953 | * @param[in] args Array of arguments. |
| 4954 | * @param[in] arg_count Count of elements in @p args. |
| 4955 | * @param[in,out] set Context and result set at the same time. |
| 4956 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4957 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4958 | */ |
| 4959 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4960 | 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] | 4961 | { |
| 4962 | char *ptr; |
| 4963 | struct lysc_node_leaf *sleaf; |
| 4964 | LY_ERR rc = LY_SUCCESS; |
| 4965 | |
| 4966 | if (options & LYXP_SCNODE_ALL) { |
| 4967 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4968 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4969 | 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] | 4970 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4971 | 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] | 4972 | } |
| 4973 | } |
| 4974 | |
| 4975 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4976 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4977 | 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] | 4978 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4979 | 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] | 4980 | } |
| 4981 | } |
| 4982 | set_scnode_clear_ctx(set); |
| 4983 | return rc; |
| 4984 | } |
| 4985 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4986 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4987 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4988 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4989 | LY_CHECK_RET(rc); |
| 4990 | |
| 4991 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4992 | if (ptr) { |
| 4993 | set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str); |
| 4994 | } else { |
| 4995 | set_fill_string(set, "", 0); |
| 4996 | } |
| 4997 | |
| 4998 | return LY_SUCCESS; |
| 4999 | } |
| 5000 | |
| 5001 | /** |
| 5002 | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
| 5003 | * with the sum of all the nodes in the context. |
| 5004 | * |
| 5005 | * @param[in] args Array of arguments. |
| 5006 | * @param[in] arg_count Count of elements in @p args. |
| 5007 | * @param[in,out] set Context and result set at the same time. |
| 5008 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5009 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5010 | */ |
| 5011 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5012 | 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] | 5013 | { |
| 5014 | long double num; |
| 5015 | char *str; |
| 5016 | uint16_t i; |
| 5017 | struct lyxp_set set_item; |
| 5018 | struct lysc_node_leaf *sleaf; |
| 5019 | LY_ERR rc = LY_SUCCESS; |
| 5020 | |
| 5021 | if (options & LYXP_SCNODE_ALL) { |
| 5022 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
| 5023 | for (i = 0; i < args[0]->used; ++i) { |
| 5024 | if (args[0]->val.scnodes[i].in_ctx == 1) { |
| 5025 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
| 5026 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5027 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 5028 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5029 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5030 | 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] | 5031 | } |
| 5032 | } |
| 5033 | } |
| 5034 | } |
| 5035 | set_scnode_clear_ctx(set); |
| 5036 | return rc; |
| 5037 | } |
| 5038 | |
| 5039 | set_fill_number(set, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5040 | |
| 5041 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5042 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5043 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5044 | } else if (!args[0]->used) { |
| 5045 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5046 | } |
| 5047 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5048 | set_init(&set_item, set); |
| 5049 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5050 | set_item.type = LYXP_SET_NODE_SET; |
| 5051 | set_item.val.nodes = malloc(sizeof *set_item.val.nodes); |
| 5052 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
| 5053 | |
| 5054 | set_item.used = 1; |
| 5055 | set_item.size = 1; |
| 5056 | |
| 5057 | for (i = 0; i < args[0]->used; ++i) { |
| 5058 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
| 5059 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5060 | rc = cast_node_set_to_string(&set_item, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5061 | LY_CHECK_RET(rc); |
| 5062 | num = cast_string_to_number(str); |
| 5063 | free(str); |
| 5064 | set->val.num += num; |
| 5065 | } |
| 5066 | |
| 5067 | free(set_item.val.nodes); |
| 5068 | |
| 5069 | return LY_SUCCESS; |
| 5070 | } |
| 5071 | |
| 5072 | /** |
| 5073 | * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET |
| 5074 | * with the text content of the nodes in the context. |
| 5075 | * |
| 5076 | * @param[in] args Array of arguments. |
| 5077 | * @param[in] arg_count Count of elements in @p args. |
| 5078 | * @param[in,out] set Context and result set at the same time. |
| 5079 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5080 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5081 | */ |
| 5082 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5083 | 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] | 5084 | { |
| 5085 | uint32_t i; |
| 5086 | |
| 5087 | if (options & LYXP_SCNODE_ALL) { |
| 5088 | set_scnode_clear_ctx(set); |
| 5089 | return LY_SUCCESS; |
| 5090 | } |
| 5091 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5092 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5093 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5094 | return LY_EVALID; |
| 5095 | } |
| 5096 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 5097 | for (i = 0; i < set->used; ) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5098 | switch (set->val.nodes[i].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 5099 | case LYXP_NODE_NONE: |
| 5100 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5101 | case LYXP_NODE_ELEM: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5102 | if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 5103 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
| 5104 | ++i; |
| 5105 | break; |
| 5106 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5107 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5108 | case LYXP_NODE_ROOT: |
| 5109 | case LYXP_NODE_ROOT_CONFIG: |
| 5110 | case LYXP_NODE_TEXT: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5111 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5112 | set_remove_node(set, i); |
| 5113 | break; |
| 5114 | } |
| 5115 | } |
| 5116 | |
| 5117 | return LY_SUCCESS; |
| 5118 | } |
| 5119 | |
| 5120 | /** |
| 5121 | * @brief Execute the XPath translate(string, string, string) function. |
| 5122 | * Returns LYXP_SET_STRING with the first argument with the characters |
| 5123 | * from the second argument replaced by those on the corresponding |
| 5124 | * positions in the third argument. |
| 5125 | * |
| 5126 | * @param[in] args Array of arguments. |
| 5127 | * @param[in] arg_count Count of elements in @p args. |
| 5128 | * @param[in,out] set Context and result set at the same time. |
| 5129 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5130 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5131 | */ |
| 5132 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5133 | 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] | 5134 | { |
| 5135 | uint16_t i, j, new_used; |
| 5136 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5137 | ly_bool have_removed; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5138 | struct lysc_node_leaf *sleaf; |
| 5139 | LY_ERR rc = LY_SUCCESS; |
| 5140 | |
| 5141 | if (options & LYXP_SCNODE_ALL) { |
| 5142 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5143 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5144 | 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] | 5145 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5146 | 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] | 5147 | } |
| 5148 | } |
| 5149 | |
| 5150 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5151 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5152 | 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] | 5153 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5154 | 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] | 5155 | } |
| 5156 | } |
| 5157 | |
| 5158 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 5159 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5160 | 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] | 5161 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5162 | 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] | 5163 | } |
| 5164 | } |
| 5165 | set_scnode_clear_ctx(set); |
| 5166 | return rc; |
| 5167 | } |
| 5168 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5169 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5170 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5171 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5172 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5173 | rc = lyxp_set_cast(args[2], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5174 | LY_CHECK_RET(rc); |
| 5175 | |
| 5176 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
| 5177 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5178 | new_used = 0; |
| 5179 | |
| 5180 | have_removed = 0; |
| 5181 | for (i = 0; args[0]->val.str[i]; ++i) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5182 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5183 | |
| 5184 | for (j = 0; args[1]->val.str[j]; ++j) { |
| 5185 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
| 5186 | /* removing this char */ |
| 5187 | if (j >= strlen(args[2]->val.str)) { |
| 5188 | have_removed = 1; |
| 5189 | found = 1; |
| 5190 | break; |
| 5191 | } |
| 5192 | /* replacing this char */ |
| 5193 | new[new_used] = args[2]->val.str[j]; |
| 5194 | ++new_used; |
| 5195 | found = 1; |
| 5196 | break; |
| 5197 | } |
| 5198 | } |
| 5199 | |
| 5200 | /* copying this char */ |
| 5201 | if (!found) { |
| 5202 | new[new_used] = args[0]->val.str[i]; |
| 5203 | ++new_used; |
| 5204 | } |
| 5205 | } |
| 5206 | |
| 5207 | if (have_removed) { |
| 5208 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 5209 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5210 | } |
| 5211 | new[new_used] = '\0'; |
| 5212 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5213 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5214 | set->type = LYXP_SET_STRING; |
| 5215 | set->val.str = new; |
| 5216 | |
| 5217 | return LY_SUCCESS; |
| 5218 | } |
| 5219 | |
| 5220 | /** |
| 5221 | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
| 5222 | * with true value. |
| 5223 | * |
| 5224 | * @param[in] args Array of arguments. |
| 5225 | * @param[in] arg_count Count of elements in @p args. |
| 5226 | * @param[in,out] set Context and result set at the same time. |
| 5227 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5228 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5229 | */ |
| 5230 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5231 | 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] | 5232 | { |
| 5233 | if (options & LYXP_SCNODE_ALL) { |
| 5234 | set_scnode_clear_ctx(set); |
| 5235 | return LY_SUCCESS; |
| 5236 | } |
| 5237 | |
| 5238 | set_fill_boolean(set, 1); |
| 5239 | return LY_SUCCESS; |
| 5240 | } |
| 5241 | |
| 5242 | /* |
| 5243 | * moveto functions |
| 5244 | * |
| 5245 | * They and only they actually change the context (set). |
| 5246 | */ |
| 5247 | |
| 5248 | /** |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5249 | * @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] | 5250 | * |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5251 | * XPath @p set is expected to be a (sc)node set! |
| 5252 | * |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5253 | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
| 5254 | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5255 | * @param[in] set Set with general XPath context. |
| 5256 | * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5257 | * @param[out] moveto_mod Expected module of a matching node. |
| 5258 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5259 | */ |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5260 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5261 | moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set, |
| 5262 | const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5263 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 5264 | const struct lys_module *mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5265 | const char *ptr; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5266 | size_t pref_len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5267 | |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5268 | assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET)); |
| 5269 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5270 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
| 5271 | /* specific module */ |
| 5272 | pref_len = ptr - *qname; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5273 | mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5274 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5275 | /* check for errors and non-implemented modules, as they are not valid */ |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5276 | if (!mod || !mod->implemented) { |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5277 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5278 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->cur_scnode, LY_VCODE_XP_INMOD, pref_len, *qname); |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5279 | } else { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5280 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INMOD, pref_len, *qname); |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5281 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5282 | return LY_EVALID; |
| 5283 | } |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5284 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5285 | *qname += pref_len + 1; |
| 5286 | *qname_len -= pref_len + 1; |
| 5287 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
| 5288 | /* all modules - special case */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5289 | mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5290 | } else { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5291 | switch (set->format) { |
| 5292 | case LY_PREF_SCHEMA: |
| 5293 | case LY_PREF_SCHEMA_RESOLVED: |
| 5294 | /* current module */ |
| 5295 | mod = set->cur_mod; |
| 5296 | break; |
| 5297 | case LY_PREF_JSON: |
| 5298 | /* inherit parent (context node) module */ |
| 5299 | if (ctx_scnode) { |
| 5300 | mod = ctx_scnode->module; |
| 5301 | } else { |
| 5302 | mod = NULL; |
| 5303 | } |
| 5304 | break; |
| 5305 | case LY_PREF_XML: |
| 5306 | /* not defined */ |
| 5307 | LOGINT_RET(set->ctx); |
| 5308 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5309 | } |
| 5310 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5311 | *moveto_mod = mod; |
| 5312 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5313 | } |
| 5314 | |
| 5315 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5316 | * @brief Move context @p set to the root. Handles absolute path. |
| 5317 | * Result is LYXP_SET_NODE_SET. |
| 5318 | * |
| 5319 | * @param[in,out] set Set to use. |
| 5320 | * @param[in] options Xpath options. |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5321 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5322 | */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5323 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5324 | moveto_root(struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5325 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5326 | if (!set) { |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5327 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5328 | } |
| 5329 | |
| 5330 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5331 | set_scnode_clear_ctx(set); |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5332 | 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] | 5333 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5334 | set->type = LYXP_SET_NODE_SET; |
| 5335 | set->used = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5336 | set_insert_node(set, NULL, 0, set->root_type, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5337 | } |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5338 | |
| 5339 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5340 | } |
| 5341 | |
| 5342 | /** |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5343 | * @brief Check whether a node has some unresolved "when". |
| 5344 | * |
| 5345 | * @param[in] node Node to check. |
| 5346 | * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when") |
| 5347 | */ |
| 5348 | static LY_ERR |
| 5349 | moveto_when_check(const struct lyd_node *node) |
| 5350 | { |
| 5351 | const struct lysc_node *schema; |
| 5352 | |
| 5353 | if (!node) { |
| 5354 | return LY_SUCCESS; |
| 5355 | } |
| 5356 | |
| 5357 | schema = node->schema; |
| 5358 | do { |
| 5359 | if (schema->when && !(node->flags & LYD_WHEN_TRUE)) { |
| 5360 | return LY_EINCOMPLETE; |
| 5361 | } |
| 5362 | schema = schema->parent; |
| 5363 | } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 5364 | |
| 5365 | return LY_SUCCESS; |
| 5366 | } |
| 5367 | |
| 5368 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5369 | * @brief Check @p node as a part of NameTest processing. |
| 5370 | * |
| 5371 | * @param[in] node Node to check. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5372 | * @param[in] ctx_node Context node. |
| 5373 | * @param[in] set Set to read general context from. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5374 | * @param[in] node_name Node name in the dictionary to move to, NULL for any node. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5375 | * @param[in] moveto_mod Expected module of the node, NULL for no prefix. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5376 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when, |
| 5377 | * LY_EINVAL if netither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5378 | */ |
| 5379 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5380 | moveto_node_check(const struct lyd_node *node, const struct lyd_node *ctx_node, const struct lyxp_set *set, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5381 | const char *node_name, const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5382 | { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5383 | if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) { |
| 5384 | switch (set->format) { |
| 5385 | case LY_PREF_SCHEMA: |
| 5386 | case LY_PREF_SCHEMA_RESOLVED: |
| 5387 | /* use current module */ |
| 5388 | moveto_mod = set->cur_mod; |
| 5389 | break; |
| 5390 | case LY_PREF_JSON: |
| 5391 | /* inherit module of the context node, if any */ |
| 5392 | if (ctx_node) { |
| 5393 | moveto_mod = ctx_node->schema->module; |
| 5394 | } |
| 5395 | break; |
| 5396 | case LY_PREF_XML: |
| 5397 | /* not defined */ |
| 5398 | LOGINT(set->ctx); |
| 5399 | return LY_EINVAL; |
| 5400 | } |
| 5401 | } |
| 5402 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5403 | /* module check */ |
| 5404 | if (moveto_mod && (node->schema->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5405 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5406 | } |
| 5407 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5408 | /* context check */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5409 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5410 | return LY_EINVAL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5411 | } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 5412 | (node->schema != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5413 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5414 | } |
| 5415 | |
| 5416 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5417 | if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5418 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5419 | } |
| 5420 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5421 | /* when check */ |
| 5422 | if (moveto_when_check(node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5423 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5424 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5425 | |
| 5426 | /* match */ |
| 5427 | return LY_SUCCESS; |
| 5428 | } |
| 5429 | |
| 5430 | /** |
| 5431 | * @brief Check @p node as a part of schema NameTest processing. |
| 5432 | * |
| 5433 | * @param[in] node Schema node to check. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5434 | * @param[in] ctx_scnode Context node. |
| 5435 | * @param[in] set Set to read general context from. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5436 | * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5437 | * @param[in] moveto_mod Expected module of the node, NULL for no prefix. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5438 | * @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] | 5439 | */ |
| 5440 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5441 | moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5442 | const char *node_name, const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5443 | { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5444 | if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) { |
| 5445 | switch (set->format) { |
| 5446 | case LY_PREF_SCHEMA: |
| 5447 | case LY_PREF_SCHEMA_RESOLVED: |
| 5448 | /* use current module */ |
| 5449 | moveto_mod = set->cur_mod; |
| 5450 | break; |
| 5451 | case LY_PREF_JSON: |
| 5452 | /* inherit module of the context node, if any */ |
| 5453 | if (ctx_scnode) { |
| 5454 | moveto_mod = ctx_scnode->module; |
| 5455 | } |
| 5456 | break; |
| 5457 | case LY_PREF_XML: |
| 5458 | /* not defined */ |
| 5459 | LOGINT(set->ctx); |
| 5460 | return LY_EINVAL; |
| 5461 | } |
| 5462 | } |
| 5463 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5464 | /* module check */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5465 | if (moveto_mod && (node->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5466 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5467 | } |
| 5468 | |
| 5469 | /* context check */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5470 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5471 | return LY_EINVAL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5472 | } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5473 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5474 | } |
| 5475 | |
| 5476 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5477 | if (node_name && strcmp(node_name, "*") && (node->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5478 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5479 | } |
| 5480 | |
| 5481 | /* match */ |
| 5482 | return LY_SUCCESS; |
| 5483 | } |
| 5484 | |
| 5485 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5486 | * @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] | 5487 | * |
| 5488 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5489 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5490 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5491 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5492 | */ |
| 5493 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5494 | moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5495 | { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 5496 | uint32_t i; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5497 | const struct lyd_node *siblings, *sub, *ctx_node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5498 | LY_ERR rc; |
| 5499 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5500 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5501 | return LY_SUCCESS; |
| 5502 | } |
| 5503 | |
| 5504 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5505 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5506 | return LY_EVALID; |
| 5507 | } |
| 5508 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5509 | for (i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5510 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5511 | |
| 5512 | 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] | 5513 | assert(!set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5514 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5515 | /* search in all the trees */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5516 | ctx_node = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5517 | siblings = set->tree; |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5518 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5519 | /* search in children */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5520 | ctx_node = set->val.nodes[i].node; |
| 5521 | siblings = lyd_child(ctx_node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5522 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5523 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5524 | for (sub = siblings; sub; sub = sub->next) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5525 | rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5526 | if (rc == LY_SUCCESS) { |
| 5527 | if (!replaced) { |
| 5528 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
| 5529 | replaced = 1; |
| 5530 | } else { |
| 5531 | set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5532 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5533 | ++i; |
| 5534 | } else if (rc == LY_EINCOMPLETE) { |
| 5535 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5536 | } |
| 5537 | } |
| 5538 | |
| 5539 | if (!replaced) { |
| 5540 | /* no match */ |
| 5541 | set_remove_node(set, i); |
| 5542 | } |
| 5543 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5544 | |
| 5545 | return LY_SUCCESS; |
| 5546 | } |
| 5547 | |
| 5548 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5549 | * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). |
| 5550 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5551 | * |
| 5552 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5553 | * @param[in] scnode Matching node schema. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5554 | * @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] | 5555 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5556 | */ |
| 5557 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5558 | 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] | 5559 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5560 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5561 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5562 | const struct lyd_node *siblings; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5563 | struct lyd_node *sub, *inst = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5564 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5565 | assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5566 | |
| 5567 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5568 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5569 | } |
| 5570 | |
| 5571 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5572 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5573 | ret = LY_EVALID; |
| 5574 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5575 | } |
| 5576 | |
| 5577 | /* context check for all the nodes since we have the schema node */ |
| 5578 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 5579 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5580 | goto cleanup; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 5581 | } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 5582 | (scnode != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5583 | lyxp_set_free_content(set); |
| 5584 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5585 | } |
| 5586 | |
| 5587 | /* create specific data instance if needed */ |
| 5588 | if (scnode->nodetype == LYS_LIST) { |
| 5589 | LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup); |
| 5590 | } else if (scnode->nodetype == LYS_LEAFLIST) { |
| 5591 | 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] | 5592 | } |
| 5593 | |
| 5594 | for (i = 0; i < set->used; ) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5595 | siblings = NULL; |
| 5596 | |
| 5597 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
| 5598 | assert(!set->val.nodes[i].node); |
| 5599 | |
| 5600 | /* search in all the trees */ |
| 5601 | siblings = set->tree; |
| 5602 | } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 5603 | /* search in children */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 5604 | siblings = lyd_child(set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5605 | } |
| 5606 | |
| 5607 | /* find the node using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5608 | if (inst) { |
| 5609 | lyd_find_sibling_first(siblings, inst, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5610 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5611 | lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5612 | } |
| 5613 | |
| 5614 | /* when check */ |
| 5615 | if (sub && moveto_when_check(sub)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5616 | ret = LY_EINCOMPLETE; |
| 5617 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5618 | } |
| 5619 | |
| 5620 | if (sub) { |
| 5621 | /* pos filled later */ |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5622 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5623 | ++i; |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5624 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5625 | /* no match */ |
| 5626 | set_remove_node(set, i); |
| 5627 | } |
| 5628 | } |
| 5629 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5630 | cleanup: |
| 5631 | lyd_free_tree(inst); |
| 5632 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5633 | } |
| 5634 | |
| 5635 | /** |
| 5636 | * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
| 5637 | * |
| 5638 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5639 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5640 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5641 | * @param[in] options XPath options. |
| 5642 | * @return LY_ERR |
| 5643 | */ |
| 5644 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5645 | moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5646 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5647 | ly_bool temp_ctx = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5648 | uint32_t getnext_opts; |
| 5649 | uint32_t orig_used, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5650 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5651 | const struct lysc_node *iter, *start_parent; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5652 | const struct lys_module *mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5653 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5654 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5655 | return LY_SUCCESS; |
| 5656 | } |
| 5657 | |
| 5658 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5659 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->cur_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5660 | return LY_EVALID; |
| 5661 | } |
| 5662 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5663 | /* getnext opts */ |
| 5664 | getnext_opts = LYS_GETNEXT_NOSTATECHECK; |
| 5665 | if (options & LYXP_SCNODE_OUTPUT) { |
| 5666 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 5667 | } |
| 5668 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5669 | orig_used = set->used; |
| 5670 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5671 | uint32_t idx; |
| 5672 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5673 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5674 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5675 | continue; |
| 5676 | } |
| 5677 | |
| 5678 | /* remember context node */ |
| 5679 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5680 | } else { |
| 5681 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5682 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5683 | |
| 5684 | start_parent = set->val.scnodes[i].scnode; |
| 5685 | |
| 5686 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5687 | /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set), |
Michal Vasko | 9e9f26d | 2020-10-12 16:31:33 +0200 | [diff] [blame] | 5688 | * it can be in a top-level augment (the root node itself is useless in this case) */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5689 | mod_idx = 0; |
Michal Vasko | 9e9f26d | 2020-10-12 16:31:33 +0200 | [diff] [blame] | 5690 | while ((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] | 5691 | iter = NULL; |
Michal Vasko | 509de4d | 2019-12-10 14:51:30 +0100 | [diff] [blame] | 5692 | /* module may not be implemented */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5693 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5694 | if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5695 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx)); |
| 5696 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5697 | /* we need to prevent these nodes from being considered in this moveto */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5698 | if ((idx < orig_used) && (idx > i)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5699 | set->val.scnodes[idx].in_ctx = 2; |
| 5700 | temp_ctx = 1; |
| 5701 | } |
| 5702 | } |
| 5703 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5704 | } |
| 5705 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5706 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 5707 | iter = NULL; |
| 5708 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5709 | if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5710 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx)); |
| 5711 | |
| 5712 | if ((idx < orig_used) && (idx > i)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5713 | set->val.scnodes[idx].in_ctx = 2; |
| 5714 | temp_ctx = 1; |
| 5715 | } |
| 5716 | } |
| 5717 | } |
| 5718 | } |
| 5719 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5720 | |
| 5721 | /* correct temporary in_ctx values */ |
| 5722 | if (temp_ctx) { |
| 5723 | for (i = 0; i < orig_used; ++i) { |
| 5724 | if (set->val.scnodes[i].in_ctx == 2) { |
| 5725 | set->val.scnodes[i].in_ctx = 1; |
| 5726 | } |
| 5727 | } |
| 5728 | } |
| 5729 | |
| 5730 | return LY_SUCCESS; |
| 5731 | } |
| 5732 | |
| 5733 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5734 | * @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] | 5735 | * Context position aware. |
| 5736 | * |
| 5737 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5738 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5739 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5740 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5741 | */ |
| 5742 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5743 | moveto_node_alldesc(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5744 | { |
| 5745 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5746 | const struct lyd_node *next, *elem, *start; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5747 | struct lyxp_set ret_set; |
| 5748 | LY_ERR rc; |
| 5749 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5750 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5751 | return LY_SUCCESS; |
| 5752 | } |
| 5753 | |
| 5754 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5755 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5756 | return LY_EVALID; |
| 5757 | } |
| 5758 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5759 | /* 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] | 5760 | rc = moveto_node(set, NULL, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5761 | LY_CHECK_RET(rc); |
| 5762 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5763 | /* 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] | 5764 | set_init(&ret_set, set); |
| 5765 | for (i = 0; i < set->used; ++i) { |
| 5766 | |
| 5767 | /* TREE DFS */ |
| 5768 | start = set->val.nodes[i].node; |
| 5769 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5770 | rc = moveto_node_check(elem, start, set, ncname, moveto_mod); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5771 | if (!rc) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5772 | /* add matching node into result set */ |
| 5773 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
| 5774 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
| 5775 | /* the node is a duplicate, we'll process it later in the set */ |
| 5776 | goto skip_children; |
| 5777 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5778 | } else if (rc == LY_EINCOMPLETE) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5779 | return rc; |
| 5780 | } else if (rc == LY_EINVAL) { |
| 5781 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5782 | } |
| 5783 | |
| 5784 | /* TREE DFS NEXT ELEM */ |
| 5785 | /* select element for the next run - children first */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 5786 | next = lyd_child(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5787 | if (!next) { |
| 5788 | skip_children: |
| 5789 | /* no children, so try siblings, but only if it's not the start, |
| 5790 | * that is considered to be the root and it's siblings are not traversed */ |
| 5791 | if (elem != start) { |
| 5792 | next = elem->next; |
| 5793 | } else { |
| 5794 | break; |
| 5795 | } |
| 5796 | } |
| 5797 | while (!next) { |
| 5798 | /* no siblings, go back through the parents */ |
| 5799 | if ((struct lyd_node *)elem->parent == start) { |
| 5800 | /* we are done, no next element to process */ |
| 5801 | break; |
| 5802 | } |
| 5803 | /* parent is already processed, go to its sibling */ |
| 5804 | elem = (struct lyd_node *)elem->parent; |
| 5805 | next = elem->next; |
| 5806 | } |
| 5807 | } |
| 5808 | } |
| 5809 | |
| 5810 | /* make the temporary set the current one */ |
| 5811 | ret_set.ctx_pos = set->ctx_pos; |
| 5812 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5813 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5814 | memcpy(set, &ret_set, sizeof *set); |
| 5815 | |
| 5816 | return LY_SUCCESS; |
| 5817 | } |
| 5818 | |
| 5819 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5820 | * @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] | 5821 | * |
| 5822 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5823 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5824 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5825 | * @param[in] options XPath options. |
| 5826 | * @return LY_ERR |
| 5827 | */ |
| 5828 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5829 | moveto_scnode_alldesc(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5830 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5831 | uint32_t i, orig_used; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5832 | const struct lysc_node *next, *elem, *start; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5833 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5834 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5835 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5836 | return LY_SUCCESS; |
| 5837 | } |
| 5838 | |
| 5839 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5840 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->cur_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5841 | return LY_EVALID; |
| 5842 | } |
| 5843 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5844 | orig_used = set->used; |
| 5845 | for (i = 0; i < orig_used; ++i) { |
| 5846 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5847 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5848 | continue; |
| 5849 | } |
| 5850 | |
| 5851 | /* remember context node */ |
| 5852 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5853 | } else { |
| 5854 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5855 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5856 | |
| 5857 | /* TREE DFS */ |
| 5858 | start = set->val.scnodes[i].scnode; |
| 5859 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5860 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 5861 | /* schema-only nodes, skip root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5862 | goto next_iter; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5863 | } |
| 5864 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5865 | rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5866 | if (!rc) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5867 | uint32_t idx; |
| 5868 | |
| 5869 | if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5870 | set->val.scnodes[idx].in_ctx = 1; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5871 | if ((uint32_t)idx > i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5872 | /* we will process it later in the set */ |
| 5873 | goto skip_children; |
| 5874 | } |
| 5875 | } else { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5876 | 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] | 5877 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5878 | } else if (rc == LY_EINVAL) { |
| 5879 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5880 | } |
| 5881 | |
| 5882 | next_iter: |
| 5883 | /* TREE DFS NEXT ELEM */ |
| 5884 | /* select element for the next run - children first */ |
| 5885 | 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] | 5886 | if (!next) { |
| 5887 | skip_children: |
| 5888 | /* no children, so try siblings, but only if it's not the start, |
| 5889 | * that is considered to be the root and it's siblings are not traversed */ |
| 5890 | if (elem != start) { |
| 5891 | next = elem->next; |
| 5892 | } else { |
| 5893 | break; |
| 5894 | } |
| 5895 | } |
| 5896 | while (!next) { |
| 5897 | /* no siblings, go back through the parents */ |
| 5898 | if (elem->parent == start) { |
| 5899 | /* we are done, no next element to process */ |
| 5900 | break; |
| 5901 | } |
| 5902 | /* parent is already processed, go to its sibling */ |
| 5903 | elem = elem->parent; |
| 5904 | next = elem->next; |
| 5905 | } |
| 5906 | } |
| 5907 | } |
| 5908 | |
| 5909 | return LY_SUCCESS; |
| 5910 | } |
| 5911 | |
| 5912 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5913 | * @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] | 5914 | * Indirectly context position aware. |
| 5915 | * |
| 5916 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5917 | * @param[in] mod Matching metadata module, NULL for any. |
| 5918 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5919 | * @return LY_ERR |
| 5920 | */ |
| 5921 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5922 | 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] | 5923 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5924 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5925 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5926 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5927 | return LY_SUCCESS; |
| 5928 | } |
| 5929 | |
| 5930 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5931 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5932 | return LY_EVALID; |
| 5933 | } |
| 5934 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5935 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5936 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5937 | |
| 5938 | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
| 5939 | * our attributes are always qualified */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5940 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5941 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5942 | |
| 5943 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5944 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5945 | continue; |
| 5946 | } |
| 5947 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5948 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5949 | /* match */ |
| 5950 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5951 | set->val.meta[i].meta = sub; |
| 5952 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5953 | /* pos does not change */ |
| 5954 | replaced = 1; |
| 5955 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5956 | 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] | 5957 | } |
| 5958 | ++i; |
| 5959 | } |
| 5960 | } |
| 5961 | } |
| 5962 | |
| 5963 | if (!replaced) { |
| 5964 | /* no match */ |
| 5965 | set_remove_node(set, i); |
| 5966 | } |
| 5967 | } |
| 5968 | |
| 5969 | return LY_SUCCESS; |
| 5970 | } |
| 5971 | |
| 5972 | /** |
| 5973 | * @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] | 5974 | * Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5975 | * |
| 5976 | * @param[in,out] set1 Set to use for the result. |
| 5977 | * @param[in] set2 Set that is copied to @p set1. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5978 | * @return LY_ERR |
| 5979 | */ |
| 5980 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5981 | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5982 | { |
| 5983 | LY_ERR rc; |
| 5984 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5985 | if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5986 | LOGVAL(set1->ctx, LY_VLOG_LYD, set1->cur_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5987 | return LY_EVALID; |
| 5988 | } |
| 5989 | |
| 5990 | /* set2 is empty or both set1 and set2 */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5991 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5992 | return LY_SUCCESS; |
| 5993 | } |
| 5994 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5995 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5996 | memcpy(set1, set2, sizeof *set1); |
| 5997 | /* dynamic memory belongs to set1 now, do not free */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5998 | memset(set2, 0, sizeof *set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5999 | return LY_SUCCESS; |
| 6000 | } |
| 6001 | |
| 6002 | /* we assume sets are sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6003 | assert(!set_sort(set1) && !set_sort(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6004 | |
| 6005 | /* sort, remove duplicates */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6006 | rc = set_sorted_merge(set1, set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6007 | LY_CHECK_RET(rc); |
| 6008 | |
| 6009 | /* final set must be sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6010 | assert(!set_sort(set1)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6011 | |
| 6012 | return LY_SUCCESS; |
| 6013 | } |
| 6014 | |
| 6015 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6016 | * @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] | 6017 | * Context position aware. |
| 6018 | * |
| 6019 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6020 | * @param[in] mod Matching metadata module, NULL for any. |
| 6021 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6022 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6023 | */ |
| 6024 | static int |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6025 | 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] | 6026 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6027 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6028 | struct lyxp_set *set_all_desc = NULL; |
| 6029 | LY_ERR rc; |
| 6030 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6031 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6032 | return LY_SUCCESS; |
| 6033 | } |
| 6034 | |
| 6035 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6036 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6037 | return LY_EVALID; |
| 6038 | } |
| 6039 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6040 | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
| 6041 | * but it likely won't be used much, so it's a waste of time */ |
| 6042 | /* copy the context */ |
| 6043 | set_all_desc = set_copy(set); |
| 6044 | /* get all descendant nodes (the original context nodes are removed) */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6045 | rc = moveto_node_alldesc(set_all_desc, NULL, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6046 | if (rc != LY_SUCCESS) { |
| 6047 | lyxp_set_free(set_all_desc); |
| 6048 | return rc; |
| 6049 | } |
| 6050 | /* prepend the original context nodes */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6051 | rc = moveto_union(set, set_all_desc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6052 | if (rc != LY_SUCCESS) { |
| 6053 | lyxp_set_free(set_all_desc); |
| 6054 | return rc; |
| 6055 | } |
| 6056 | lyxp_set_free(set_all_desc); |
| 6057 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6058 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6059 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6060 | |
| 6061 | /* only attributes of an elem can be in the result, skip all the rest, |
| 6062 | * we have all attributes qualified in lyd tree */ |
| 6063 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6064 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6065 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6066 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6067 | continue; |
| 6068 | } |
| 6069 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6070 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6071 | /* match */ |
| 6072 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6073 | set->val.meta[i].meta = sub; |
| 6074 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6075 | /* pos does not change */ |
| 6076 | replaced = 1; |
| 6077 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6078 | 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] | 6079 | } |
| 6080 | ++i; |
| 6081 | } |
| 6082 | } |
| 6083 | } |
| 6084 | |
| 6085 | if (!replaced) { |
| 6086 | /* no match */ |
| 6087 | set_remove_node(set, i); |
| 6088 | } |
| 6089 | } |
| 6090 | |
| 6091 | return LY_SUCCESS; |
| 6092 | } |
| 6093 | |
| 6094 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6095 | * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET. |
| 6096 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6097 | * |
| 6098 | * @param[in] parent Current parent. |
| 6099 | * @param[in] parent_pos Position of @p parent. |
| 6100 | * @param[in] parent_type Node type of @p parent. |
| 6101 | * @param[in,out] to_set Set to use. |
| 6102 | * @param[in] dup_check_set Set for checking duplicities. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6103 | * @param[in] options XPath options. |
| 6104 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6105 | */ |
| 6106 | static LY_ERR |
| 6107 | 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] | 6108 | 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] | 6109 | { |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6110 | const struct lyd_node *iter, *first; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6111 | LY_ERR rc; |
| 6112 | |
| 6113 | switch (parent_type) { |
| 6114 | case LYXP_NODE_ROOT: |
| 6115 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6116 | assert(!parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6117 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6118 | /* add all top-level nodes as elements */ |
| 6119 | first = to_set->tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6120 | break; |
| 6121 | case LYXP_NODE_ELEM: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6122 | /* add just the text node of this term element node */ |
| 6123 | if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6124 | if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) { |
| 6125 | set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used); |
| 6126 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6127 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6128 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6129 | |
| 6130 | /* add all the children of this node */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 6131 | first = lyd_child(parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6132 | break; |
| 6133 | default: |
| 6134 | LOGINT_RET(parent->schema->module->ctx); |
| 6135 | } |
| 6136 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6137 | /* add all top-level nodes as elements */ |
| 6138 | LY_LIST_FOR(first, iter) { |
| 6139 | /* context check */ |
| 6140 | if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) { |
| 6141 | continue; |
| 6142 | } |
| 6143 | |
| 6144 | /* when check */ |
| 6145 | if (moveto_when_check(iter)) { |
| 6146 | return LY_EINCOMPLETE; |
| 6147 | } |
| 6148 | |
| 6149 | if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) { |
| 6150 | set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used); |
| 6151 | |
| 6152 | /* also add all the children of this node, recursively */ |
| 6153 | rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options); |
| 6154 | LY_CHECK_RET(rc); |
| 6155 | } |
| 6156 | } |
| 6157 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6158 | return LY_SUCCESS; |
| 6159 | } |
| 6160 | |
| 6161 | /** |
| 6162 | * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET |
| 6163 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6164 | * |
| 6165 | * @param[in,out] set Set to use. |
| 6166 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6167 | * @param[in] options XPath options. |
| 6168 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6169 | */ |
| 6170 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6171 | 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] | 6172 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6173 | struct lyxp_set ret_set; |
| 6174 | LY_ERR rc; |
| 6175 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6176 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6177 | return LY_SUCCESS; |
| 6178 | } |
| 6179 | |
| 6180 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6181 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6182 | return LY_EVALID; |
| 6183 | } |
| 6184 | |
| 6185 | /* nothing to do */ |
| 6186 | if (!all_desc) { |
| 6187 | return LY_SUCCESS; |
| 6188 | } |
| 6189 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6190 | /* add all the children, they get added recursively */ |
| 6191 | set_init(&ret_set, set); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6192 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6193 | /* copy the current node to tmp */ |
| 6194 | set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used); |
| 6195 | |
| 6196 | /* do not touch attributes and text nodes */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6197 | 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] | 6198 | continue; |
| 6199 | } |
| 6200 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6201 | /* add all the children */ |
| 6202 | 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 | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6203 | set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6204 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6205 | lyxp_set_free_content(&ret_set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6206 | return rc; |
| 6207 | } |
| 6208 | } |
| 6209 | |
| 6210 | /* use the temporary set as the current one */ |
| 6211 | ret_set.ctx_pos = set->ctx_pos; |
| 6212 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6213 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6214 | memcpy(set, &ret_set, sizeof *set); |
| 6215 | |
| 6216 | return LY_SUCCESS; |
| 6217 | } |
| 6218 | |
| 6219 | /** |
| 6220 | * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET |
| 6221 | * (or LYXP_SET_EMPTY). |
| 6222 | * |
| 6223 | * @param[in,out] set Set to use. |
| 6224 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6225 | * @param[in] options XPath options. |
| 6226 | * @return LY_ERR |
| 6227 | */ |
| 6228 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6229 | 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] | 6230 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6231 | uint32_t getnext_opts; |
| 6232 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6233 | const struct lysc_node *iter, *start_parent; |
| 6234 | const struct lys_module *mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6235 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6236 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6237 | return LY_SUCCESS; |
| 6238 | } |
| 6239 | |
| 6240 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6241 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->cur_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6242 | return LY_EVALID; |
| 6243 | } |
| 6244 | |
| 6245 | /* nothing to do */ |
| 6246 | if (!all_desc) { |
| 6247 | return LY_SUCCESS; |
| 6248 | } |
| 6249 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6250 | /* getnext opts */ |
| 6251 | getnext_opts = LYS_GETNEXT_NOSTATECHECK; |
| 6252 | if (options & LYXP_SCNODE_OUTPUT) { |
| 6253 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 6254 | } |
| 6255 | |
| 6256 | /* 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] | 6257 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6258 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6259 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6260 | continue; |
| 6261 | } |
| 6262 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6263 | /* remember context node */ |
| 6264 | set->val.scnodes[i].in_ctx = -1; |
| 6265 | } else { |
| 6266 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6267 | } |
| 6268 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6269 | start_parent = set->val.scnodes[i].scnode; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6270 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6271 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
| 6272 | /* it can actually be in any module, it's all <running> */ |
| 6273 | mod_idx = 0; |
| 6274 | while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
| 6275 | iter = NULL; |
| 6276 | /* module may not be implemented */ |
| 6277 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
| 6278 | /* context check */ |
| 6279 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) { |
| 6280 | continue; |
| 6281 | } |
| 6282 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6283 | 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] | 6284 | /* throw away the insert index, we want to consider that node again, recursively */ |
| 6285 | } |
| 6286 | } |
| 6287 | |
| 6288 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 6289 | iter = NULL; |
| 6290 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6291 | /* context check */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6292 | 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] | 6293 | continue; |
| 6294 | } |
| 6295 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6296 | 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] | 6297 | } |
| 6298 | } |
| 6299 | } |
| 6300 | |
| 6301 | return LY_SUCCESS; |
| 6302 | } |
| 6303 | |
| 6304 | /** |
| 6305 | * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET |
| 6306 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6307 | * |
| 6308 | * @param[in] set Set to use. |
| 6309 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6310 | * @param[in] options XPath options. |
| 6311 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6312 | */ |
| 6313 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6314 | 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] | 6315 | { |
| 6316 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6317 | struct lyd_node *node, *new_node; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6318 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6319 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6320 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6321 | return LY_SUCCESS; |
| 6322 | } |
| 6323 | |
| 6324 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6325 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6326 | return LY_EVALID; |
| 6327 | } |
| 6328 | |
| 6329 | if (all_desc) { |
| 6330 | /* <path>//.. == <path>//./.. */ |
| 6331 | rc = moveto_self(set, 1, options); |
| 6332 | LY_CHECK_RET(rc); |
| 6333 | } |
| 6334 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6335 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6336 | node = set->val.nodes[i].node; |
| 6337 | |
| 6338 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 6339 | new_node = (struct lyd_node *)node->parent; |
| 6340 | } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) { |
| 6341 | new_node = node; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6342 | } else if (set->val.nodes[i].type == LYXP_NODE_META) { |
| 6343 | new_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6344 | if (!new_node) { |
| 6345 | LOGINT_RET(set->ctx); |
| 6346 | } |
| 6347 | } else { |
| 6348 | /* root does not have a parent */ |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6349 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6350 | continue; |
| 6351 | } |
| 6352 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6353 | /* when check */ |
| 6354 | if (moveto_when_check(new_node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6355 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6356 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6357 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6358 | if (!new_node) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6359 | /* node already there can also be the root */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6360 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6361 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6362 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6363 | /* 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] | 6364 | new_type = LYXP_NODE_ELEM; |
| 6365 | } |
| 6366 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6367 | if (set_dup_node_check(set, new_node, new_type, -1)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6368 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6369 | } else { |
| 6370 | set_replace_node(set, new_node, 0, new_type, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6371 | } |
| 6372 | } |
| 6373 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6374 | set_remove_nodes_none(set); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6375 | assert(!set_sort(set) && !set_sorted_dup_node_clean(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6376 | |
| 6377 | return LY_SUCCESS; |
| 6378 | } |
| 6379 | |
| 6380 | /** |
| 6381 | * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET |
| 6382 | * (or LYXP_SET_EMPTY). |
| 6383 | * |
| 6384 | * @param[in] set Set to use. |
| 6385 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6386 | * @param[in] options XPath options. |
| 6387 | * @return LY_ERR |
| 6388 | */ |
| 6389 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6390 | 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] | 6391 | { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6392 | uint32_t i, orig_used, idx; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6393 | ly_bool temp_ctx = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6394 | const struct lysc_node *node, *new_node; |
| 6395 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6396 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6397 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6398 | return LY_SUCCESS; |
| 6399 | } |
| 6400 | |
| 6401 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6402 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->cur_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6403 | return LY_EVALID; |
| 6404 | } |
| 6405 | |
| 6406 | if (all_desc) { |
| 6407 | /* <path>//.. == <path>//./.. */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6408 | LY_CHECK_RET(moveto_scnode_self(set, 1, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6409 | } |
| 6410 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6411 | orig_used = set->used; |
| 6412 | for (i = 0; i < orig_used; ++i) { |
| 6413 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6414 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6415 | continue; |
| 6416 | } |
| 6417 | |
| 6418 | /* remember context node */ |
| 6419 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 6420 | } else { |
| 6421 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6422 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6423 | |
| 6424 | node = set->val.scnodes[i].scnode; |
| 6425 | |
| 6426 | if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6427 | new_node = lysc_data_parent(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6428 | } else { |
| 6429 | /* root does not have a parent */ |
| 6430 | continue; |
| 6431 | } |
| 6432 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6433 | if (!new_node) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6434 | /* node has no parent */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6435 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6436 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6437 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6438 | /* 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] | 6439 | new_type = LYXP_NODE_ELEM; |
| 6440 | } |
| 6441 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6442 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx)); |
| 6443 | if ((idx < orig_used) && (idx > i)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6444 | set->val.scnodes[idx].in_ctx = 2; |
| 6445 | temp_ctx = 1; |
| 6446 | } |
| 6447 | } |
| 6448 | |
| 6449 | if (temp_ctx) { |
| 6450 | for (i = 0; i < orig_used; ++i) { |
| 6451 | if (set->val.scnodes[i].in_ctx == 2) { |
| 6452 | set->val.scnodes[i].in_ctx = 1; |
| 6453 | } |
| 6454 | } |
| 6455 | } |
| 6456 | |
| 6457 | return LY_SUCCESS; |
| 6458 | } |
| 6459 | |
| 6460 | /** |
| 6461 | * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'. |
| 6462 | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
| 6463 | * |
| 6464 | * @param[in,out] set1 Set to use for the result. |
| 6465 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6466 | * @param[in] op Comparison operator to process. |
| 6467 | * @param[in] options XPath options. |
| 6468 | * @return LY_ERR |
| 6469 | */ |
| 6470 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6471 | 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] | 6472 | { |
| 6473 | /* |
| 6474 | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
| 6475 | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
| 6476 | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6477 | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6478 | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
| 6479 | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6480 | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6481 | * |
| 6482 | * '=' or '!=' |
| 6483 | * BOOLEAN + BOOLEAN |
| 6484 | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6485 | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6486 | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6487 | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6488 | * NUMBER + NUMBER |
| 6489 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6490 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6491 | * STRING + STRING |
| 6492 | * |
| 6493 | * '<=', '<', '>=', '>' |
| 6494 | * NUMBER + NUMBER |
| 6495 | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6496 | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6497 | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6498 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6499 | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6500 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6501 | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6502 | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6503 | */ |
| 6504 | struct lyxp_set iter1, iter2; |
| 6505 | int result; |
| 6506 | int64_t i; |
| 6507 | LY_ERR rc; |
| 6508 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6509 | memset(&iter1, 0, sizeof iter1); |
| 6510 | memset(&iter2, 0, sizeof iter2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6511 | |
| 6512 | /* iterative evaluation with node-sets */ |
| 6513 | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
| 6514 | if (set1->type == LYXP_SET_NODE_SET) { |
| 6515 | for (i = 0; i < set1->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6516 | /* cast set1 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6517 | switch (set2->type) { |
| 6518 | case LYXP_SET_NUMBER: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6519 | rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6520 | break; |
| 6521 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6522 | rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6523 | break; |
| 6524 | default: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6525 | rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6526 | break; |
| 6527 | } |
| 6528 | LY_CHECK_RET(rc); |
| 6529 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6530 | /* canonize set2 */ |
| 6531 | LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc); |
| 6532 | |
| 6533 | /* compare recursively */ |
| 6534 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
| 6535 | lyxp_set_free_content(&iter2); |
| 6536 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6537 | |
| 6538 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6539 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6540 | set_fill_boolean(set1, 1); |
| 6541 | return LY_SUCCESS; |
| 6542 | } |
| 6543 | } |
| 6544 | } else { |
| 6545 | for (i = 0; i < set2->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6546 | /* set set2 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6547 | switch (set1->type) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6548 | case LYXP_SET_NUMBER: |
| 6549 | rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i); |
| 6550 | break; |
| 6551 | case LYXP_SET_BOOLEAN: |
| 6552 | rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i); |
| 6553 | break; |
| 6554 | default: |
| 6555 | rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i); |
| 6556 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6557 | } |
| 6558 | LY_CHECK_RET(rc); |
| 6559 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6560 | /* canonize set1 */ |
| 6561 | 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] | 6562 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6563 | /* compare recursively */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6564 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6565 | lyxp_set_free_content(&iter2); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6566 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6567 | |
| 6568 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6569 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6570 | set_fill_boolean(set1, 1); |
| 6571 | return LY_SUCCESS; |
| 6572 | } |
| 6573 | } |
| 6574 | } |
| 6575 | |
| 6576 | /* false for all nodes */ |
| 6577 | set_fill_boolean(set1, 0); |
| 6578 | return LY_SUCCESS; |
| 6579 | } |
| 6580 | |
| 6581 | /* first convert properly */ |
| 6582 | if ((op[0] == '=') || (op[0] == '!')) { |
| 6583 | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6584 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
| 6585 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6586 | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6587 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6588 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6589 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6590 | LY_CHECK_RET(rc); |
| 6591 | } /* else we have 2 strings */ |
| 6592 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6593 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6594 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6595 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6596 | LY_CHECK_RET(rc); |
| 6597 | } |
| 6598 | |
| 6599 | assert(set1->type == set2->type); |
| 6600 | |
| 6601 | /* compute result */ |
| 6602 | if (op[0] == '=') { |
| 6603 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6604 | result = (set1->val.bln == set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6605 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6606 | result = (set1->val.num == set2->val.num); |
| 6607 | } else { |
| 6608 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6609 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6610 | } |
| 6611 | } else if (op[0] == '!') { |
| 6612 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6613 | result = (set1->val.bln != set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6614 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6615 | result = (set1->val.num != set2->val.num); |
| 6616 | } else { |
| 6617 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6618 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6619 | } |
| 6620 | } else { |
| 6621 | assert(set1->type == LYXP_SET_NUMBER); |
| 6622 | if (op[0] == '<') { |
| 6623 | if (op[1] == '=') { |
| 6624 | result = (set1->val.num <= set2->val.num); |
| 6625 | } else { |
| 6626 | result = (set1->val.num < set2->val.num); |
| 6627 | } |
| 6628 | } else { |
| 6629 | if (op[1] == '=') { |
| 6630 | result = (set1->val.num >= set2->val.num); |
| 6631 | } else { |
| 6632 | result = (set1->val.num > set2->val.num); |
| 6633 | } |
| 6634 | } |
| 6635 | } |
| 6636 | |
| 6637 | /* assign result */ |
| 6638 | if (result) { |
| 6639 | set_fill_boolean(set1, 1); |
| 6640 | } else { |
| 6641 | set_fill_boolean(set1, 0); |
| 6642 | } |
| 6643 | |
| 6644 | return LY_SUCCESS; |
| 6645 | } |
| 6646 | |
| 6647 | /** |
| 6648 | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
| 6649 | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
| 6650 | * |
| 6651 | * @param[in,out] set1 Set to use for the result. |
| 6652 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6653 | * @param[in] op Operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6654 | * @return LY_ERR |
| 6655 | */ |
| 6656 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6657 | 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] | 6658 | { |
| 6659 | LY_ERR rc; |
| 6660 | |
| 6661 | /* unary '-' */ |
| 6662 | if (!set2 && (op[0] == '-')) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6663 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6664 | LY_CHECK_RET(rc); |
| 6665 | set1->val.num *= -1; |
| 6666 | lyxp_set_free(set2); |
| 6667 | return LY_SUCCESS; |
| 6668 | } |
| 6669 | |
| 6670 | assert(set1 && set2); |
| 6671 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6672 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6673 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6674 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6675 | LY_CHECK_RET(rc); |
| 6676 | |
| 6677 | switch (op[0]) { |
| 6678 | /* '+' */ |
| 6679 | case '+': |
| 6680 | set1->val.num += set2->val.num; |
| 6681 | break; |
| 6682 | |
| 6683 | /* '-' */ |
| 6684 | case '-': |
| 6685 | set1->val.num -= set2->val.num; |
| 6686 | break; |
| 6687 | |
| 6688 | /* '*' */ |
| 6689 | case '*': |
| 6690 | set1->val.num *= set2->val.num; |
| 6691 | break; |
| 6692 | |
| 6693 | /* 'div' */ |
| 6694 | case 'd': |
| 6695 | set1->val.num /= set2->val.num; |
| 6696 | break; |
| 6697 | |
| 6698 | /* 'mod' */ |
| 6699 | case 'm': |
| 6700 | set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num); |
| 6701 | break; |
| 6702 | |
| 6703 | default: |
| 6704 | LOGINT_RET(set1->ctx); |
| 6705 | } |
| 6706 | |
| 6707 | return LY_SUCCESS; |
| 6708 | } |
| 6709 | |
| 6710 | /* |
| 6711 | * eval functions |
| 6712 | * |
| 6713 | * They execute a parsed XPath expression on some data subtree. |
| 6714 | */ |
| 6715 | |
| 6716 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6717 | * @brief Evaluate Predicate. Logs directly on error. |
| 6718 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6719 | * [9] Predicate ::= '[' Expr ']' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6720 | * |
| 6721 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6722 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6723 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6724 | * @param[in] options XPath options. |
| 6725 | * @param[in] parent_pos_pred Whether parent predicate was a positional one. |
| 6726 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6727 | */ |
| 6728 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6729 | 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] | 6730 | { |
| 6731 | LY_ERR rc; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 6732 | uint16_t i, orig_exp; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6733 | uint32_t orig_pos, orig_size; |
| 6734 | int32_t pred_in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6735 | struct lyxp_set set2; |
| 6736 | struct lyd_node *orig_parent; |
| 6737 | |
| 6738 | /* '[' */ |
| 6739 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6740 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6741 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6742 | |
| 6743 | if (!set) { |
| 6744 | only_parse: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6745 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6746 | LY_CHECK_RET(rc); |
| 6747 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 6748 | /* 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] | 6749 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6750 | |
| 6751 | /* empty set, nothing to evaluate */ |
| 6752 | if (!set->used) { |
| 6753 | goto only_parse; |
| 6754 | } |
| 6755 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6756 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6757 | orig_pos = 0; |
| 6758 | orig_size = set->used; |
| 6759 | orig_parent = NULL; |
Michal Vasko | 39dbf35 | 2020-05-21 10:08:59 +0200 | [diff] [blame] | 6760 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6761 | set_init(&set2, set); |
| 6762 | set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0); |
| 6763 | /* remember the node context position for position() and context size for last(), |
| 6764 | * predicates should always be evaluated with respect to the child axis (since we do |
| 6765 | * not support explicit axes) so we assign positions based on their parents */ |
| 6766 | if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) { |
| 6767 | orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent; |
| 6768 | orig_pos = 1; |
| 6769 | } else { |
| 6770 | ++orig_pos; |
| 6771 | } |
| 6772 | |
| 6773 | set2.ctx_pos = orig_pos; |
| 6774 | set2.ctx_size = orig_size; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6775 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6776 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6777 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6778 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6779 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6780 | return rc; |
| 6781 | } |
| 6782 | |
| 6783 | /* number is a position */ |
| 6784 | if (set2.type == LYXP_SET_NUMBER) { |
| 6785 | if ((long long)set2.val.num == orig_pos) { |
| 6786 | set2.val.num = 1; |
| 6787 | } else { |
| 6788 | set2.val.num = 0; |
| 6789 | } |
| 6790 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6791 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6792 | |
| 6793 | /* predicate satisfied or not? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6794 | if (!set2.val.bln) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6795 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6796 | } |
| 6797 | } |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6798 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6799 | |
| 6800 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 6801 | for (i = 0; i < set->used; ++i) { |
| 6802 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6803 | /* there is a currently-valid node */ |
| 6804 | break; |
| 6805 | } |
| 6806 | } |
| 6807 | /* empty set, nothing to evaluate */ |
| 6808 | if (i == set->used) { |
| 6809 | goto only_parse; |
| 6810 | } |
| 6811 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6812 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6813 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6814 | /* set special in_ctx to all the valid snodes */ |
| 6815 | pred_in_ctx = set_scnode_new_in_ctx(set); |
| 6816 | |
| 6817 | /* use the valid snodes one-by-one */ |
| 6818 | for (i = 0; i < set->used; ++i) { |
| 6819 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
| 6820 | continue; |
| 6821 | } |
| 6822 | set->val.scnodes[i].in_ctx = 1; |
| 6823 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6824 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6825 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6826 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6827 | LY_CHECK_RET(rc); |
| 6828 | |
| 6829 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
| 6830 | } |
| 6831 | |
| 6832 | /* restore the state as it was before the predicate */ |
| 6833 | for (i = 0; i < set->used; ++i) { |
| 6834 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6835 | set->val.scnodes[i].in_ctx = 0; |
| 6836 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
| 6837 | set->val.scnodes[i].in_ctx = 1; |
| 6838 | } |
| 6839 | } |
| 6840 | |
| 6841 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6842 | set2.type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6843 | set_fill_set(&set2, set); |
| 6844 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6845 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6846 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6847 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6848 | return rc; |
| 6849 | } |
| 6850 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6851 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6852 | if (!set2.val.bln) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6853 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6854 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6855 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6856 | } |
| 6857 | |
| 6858 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6859 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6860 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6861 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6862 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6863 | |
| 6864 | return LY_SUCCESS; |
| 6865 | } |
| 6866 | |
| 6867 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6868 | * @brief Evaluate Literal. Logs directly on error. |
| 6869 | * |
| 6870 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6871 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6872 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6873 | */ |
| 6874 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6875 | 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] | 6876 | { |
| 6877 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6878 | if (exp->tok_len[*tok_idx] == 2) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6879 | set_fill_string(set, "", 0); |
| 6880 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6881 | 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] | 6882 | } |
| 6883 | } |
| 6884 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6885 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6886 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6887 | } |
| 6888 | |
| 6889 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6890 | * @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] | 6891 | * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6892 | * @param[in] exp Full parsed XPath expression. |
| 6893 | * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6894 | * @param[in] ctx_node Found schema node as the context for the predicate. |
| 6895 | * @param[in] cur_mod Current module for the expression. |
| 6896 | * @param[in] cur_node Current (original context) node. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6897 | * @param[in] format Format of any prefixes in key names/values. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6898 | * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix). |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6899 | * @param[out] predicates Parsed predicates. |
| 6900 | * @param[out] pred_type Type of @p predicates. |
| 6901 | * @return LY_SUCCESS on success, |
| 6902 | * @return LY_ERR on any error. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6903 | */ |
| 6904 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6905 | eval_name_test_try_compile_predicates(struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *ctx_node, |
| 6906 | const struct lys_module *cur_mod, const struct lysc_node *cur_node, LY_PREFIX_FORMAT format, void *prefix_data, |
| 6907 | struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6908 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6909 | LY_ERR ret = LY_SUCCESS; |
| 6910 | uint16_t key_count, e_idx, pred_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6911 | const struct lysc_node *key; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6912 | size_t pred_len; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6913 | uint32_t prev_lo; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6914 | struct lyxp_expr *exp2 = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6915 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6916 | assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6917 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6918 | if (ctx_node->nodetype == LYS_LIST) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6919 | /* get key count */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6920 | if (ctx_node->flags & LYS_KEYLESS) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6921 | return LY_EINVAL; |
| 6922 | } |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6923 | for (key_count = 0, key = lysc_node_children(ctx_node, 0); key && (key->flags & LYS_KEY); key = key->next, ++key_count) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6924 | assert(key_count); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6925 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6926 | /* learn where the predicates end */ |
| 6927 | e_idx = *tok_idx; |
| 6928 | while (key_count) { |
| 6929 | /* '[' */ |
| 6930 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6931 | return LY_EINVAL; |
| 6932 | } |
| 6933 | ++e_idx; |
| 6934 | |
| 6935 | /* ']' */ |
| 6936 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6937 | ++e_idx; |
| 6938 | } |
| 6939 | ++e_idx; |
| 6940 | |
| 6941 | /* another presumably key predicate parsed */ |
| 6942 | --key_count; |
| 6943 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6944 | } else { |
| 6945 | /* learn just where this single predicate ends */ |
| 6946 | e_idx = *tok_idx; |
| 6947 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6948 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6949 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6950 | return LY_EINVAL; |
| 6951 | } |
| 6952 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6953 | |
| 6954 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6955 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6956 | ++e_idx; |
| 6957 | } |
| 6958 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6959 | } |
| 6960 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6961 | /* get the joined length of all the keys predicates/of the single leaf-list predicate */ |
| 6962 | pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx]; |
| 6963 | |
| 6964 | /* turn logging off */ |
| 6965 | prev_lo = ly_log_options(0); |
| 6966 | |
| 6967 | /* parse the predicate(s) */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6968 | LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx], |
| 6969 | pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6970 | |
| 6971 | /* compile */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6972 | ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format, |
| 6973 | prefix_data, predicates, pred_type); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6974 | LY_CHECK_GOTO(ret, cleanup); |
| 6975 | |
| 6976 | /* success, the predicate must include all the needed information for hash-based search */ |
| 6977 | *tok_idx = e_idx; |
| 6978 | |
| 6979 | cleanup: |
| 6980 | ly_log_options(prev_lo); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6981 | lyxp_expr_free(ctx_node->module->ctx, exp2); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6982 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6983 | } |
| 6984 | |
| 6985 | /** |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6986 | * @brief Search for/check the next schema node that could be the only matching schema node meaning the |
| 6987 | * data node(s) could be found using a single hash-based search. |
| 6988 | * |
| 6989 | * @param[in] node Next context node to check. |
| 6990 | * @param[in] name Expected node name. |
| 6991 | * @param[in] name_len Length of @p name. |
| 6992 | * @param[in] moveto_mod Expected node module. |
| 6993 | * @param[in] root_type XPath root type. |
| 6994 | * @param[in] no_prefix Whether the node name was unprefixed. |
| 6995 | * @param[in] format Prefix format. |
| 6996 | * @param[in,out] found Previously found node, is updated. |
| 6997 | * @return LY_SUCCESS on success, |
| 6998 | * @return LY_ENOT if the whole check failed and hashes cannot be used. |
| 6999 | */ |
| 7000 | static LY_ERR |
| 7001 | eval_name_test_with_predicate_get_scnode(const struct lyd_node *node, const char *name, uint16_t name_len, |
| 7002 | const struct lys_module *moveto_mod, enum lyxp_node_type root_type, ly_bool no_prefix, LY_PREFIX_FORMAT format, |
| 7003 | const struct lysc_node **found) |
| 7004 | { |
| 7005 | const struct lysc_node *scnode; |
| 7006 | const struct lys_module *mod; |
| 7007 | uint32_t idx = 0; |
| 7008 | |
| 7009 | continue_search: |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7010 | scnode = NULL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7011 | if (!node) { |
| 7012 | if (no_prefix && (format == LY_PREF_JSON)) { |
| 7013 | /* search all modules for a single match */ |
| 7014 | while ((mod = ly_ctx_get_module_iter(moveto_mod->ctx, &idx))) { |
| 7015 | scnode = lys_find_child(NULL, mod, name, name_len, 0, 0); |
| 7016 | if (scnode) { |
| 7017 | /* we have found a match */ |
| 7018 | break; |
| 7019 | } |
| 7020 | } |
| 7021 | |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7022 | if (!scnode) { |
| 7023 | /* all modules searched */ |
| 7024 | idx = 0; |
| 7025 | } |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7026 | } else { |
| 7027 | /* search in top-level */ |
| 7028 | scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0); |
| 7029 | } |
| 7030 | } else if (!*found || (lysc_data_parent(*found) != node->schema)) { |
| 7031 | if (no_prefix && (format == LY_PREF_JSON)) { |
| 7032 | /* we must adjust the module to inherit the one from the context node */ |
| 7033 | moveto_mod = node->schema->module; |
| 7034 | } |
| 7035 | |
| 7036 | /* search in children, do not repeat the same search */ |
| 7037 | scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0); |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7038 | } /* else skip redundant search */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7039 | |
| 7040 | /* additional context check */ |
| 7041 | if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 7042 | scnode = NULL; |
| 7043 | } |
| 7044 | |
| 7045 | if (scnode) { |
| 7046 | if (*found) { |
| 7047 | /* we found a schema node with the same name but at different level, give up, too complicated |
| 7048 | * (more hash-based searches would be required, not supported) */ |
| 7049 | return LY_ENOT; |
| 7050 | } else { |
| 7051 | /* remember the found schema node and continue to make sure it can be used */ |
| 7052 | *found = scnode; |
| 7053 | } |
| 7054 | } |
| 7055 | |
| 7056 | if (idx) { |
| 7057 | /* continue searching all the following models */ |
| 7058 | goto continue_search; |
| 7059 | } |
| 7060 | |
| 7061 | return LY_SUCCESS; |
| 7062 | } |
| 7063 | |
| 7064 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7065 | * @brief Evaluate NameTest and any following Predicates. Logs directly on error. |
| 7066 | * |
| 7067 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7068 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7069 | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
| 7070 | * |
| 7071 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7072 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7073 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 7074 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7075 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7076 | * @param[in] options XPath options. |
| 7077 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7078 | */ |
| 7079 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7080 | eval_name_test_with_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool attr_axis, ly_bool all_desc, |
| 7081 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7082 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7083 | char *path; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7084 | const char *ncname, *ncname_dict = NULL; |
| 7085 | uint16_t ncname_len; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7086 | const struct lys_module *moveto_mod = NULL; |
| 7087 | const struct lysc_node *scnode = NULL, *ctx_scnode; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7088 | struct ly_path_predicate *predicates = NULL; |
| 7089 | enum ly_path_pred_type pred_type = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7090 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7091 | ly_bool no_prefix; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7092 | |
| 7093 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7094 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7095 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7096 | |
| 7097 | if (!set) { |
| 7098 | goto moveto; |
| 7099 | } |
| 7100 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7101 | ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]]; |
| 7102 | ncname_len = exp->tok_len[*tok_idx - 1]; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7103 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7104 | /* get the first schema context node */ |
| 7105 | if (set->used) { |
| 7106 | if (set->type == LYXP_SET_NODE_SET) { |
| 7107 | ctx_scnode = set->val.nodes[0].node ? set->val.nodes[0].node->schema : NULL; |
| 7108 | } else { |
| 7109 | ctx_scnode = set->val.scnodes[0].scnode; |
| 7110 | } |
| 7111 | } else { |
| 7112 | ctx_scnode = NULL; |
| 7113 | } |
| 7114 | |
| 7115 | /* parse (and skip) module name, use any context node (if available) just so we get some moveto_mod if there |
| 7116 | * is no prefix for ::LY_PREF_JSON */ |
| 7117 | rc = moveto_resolve_model(&ncname, &ncname_len, set, ctx_scnode, &moveto_mod); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7118 | LY_CHECK_GOTO(rc, cleanup); |
| 7119 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7120 | if (ncname_len == exp->tok_len[*tok_idx - 1]) { |
| 7121 | /* remember that there is no prefix */ |
| 7122 | no_prefix = 1; |
| 7123 | } else { |
| 7124 | no_prefix = 0; |
| 7125 | } |
| 7126 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7127 | if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) { |
| 7128 | /* find the matching schema node in some parent in the context */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7129 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7130 | if (eval_name_test_with_predicate_get_scnode(set->val.nodes[i].node, ncname, ncname_len, moveto_mod, |
| 7131 | set->root_type, no_prefix, set->format, &scnode)) { |
| 7132 | /* check failed */ |
| 7133 | scnode = NULL; |
| 7134 | break; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7135 | } |
| 7136 | } |
| 7137 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7138 | if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 7139 | /* try to create the predicates */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7140 | if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ? |
| 7141 | set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7142 | /* hashes cannot be used */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7143 | scnode = NULL; |
| 7144 | } |
| 7145 | } |
| 7146 | } |
| 7147 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7148 | if (!scnode && moveto_mod) { |
| 7149 | /* we are not able to match based on a schema node and not all the modules match, |
| 7150 | * use dictionary for efficient comparison */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 7151 | 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] | 7152 | } |
| 7153 | |
| 7154 | moveto: |
| 7155 | /* move to the attribute(s), data node(s), or schema node(s) */ |
| 7156 | if (attr_axis) { |
| 7157 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7158 | set_scnode_clear_ctx(set); |
| 7159 | } else { |
| 7160 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7161 | rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7162 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7163 | rc = moveto_attr(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7164 | } |
| 7165 | LY_CHECK_GOTO(rc, cleanup); |
| 7166 | } |
| 7167 | } else { |
| 7168 | if (set && (options & LYXP_SCNODE_ALL)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7169 | int64_t i; |
| 7170 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7171 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7172 | rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7173 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7174 | rc = moveto_scnode(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7175 | } |
| 7176 | LY_CHECK_GOTO(rc, cleanup); |
| 7177 | |
| 7178 | for (i = set->used - 1; i > -1; --i) { |
| 7179 | if (set->val.scnodes[i].in_ctx > 0) { |
| 7180 | break; |
| 7181 | } |
| 7182 | } |
| 7183 | if (i == -1) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7184 | path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7185 | LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7186 | ncname_len, ncname, ncname - exp->expr, exp->expr, path); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7187 | free(path); |
| 7188 | } |
| 7189 | } else { |
| 7190 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7191 | rc = moveto_node_alldesc(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7192 | } else { |
| 7193 | if (scnode) { |
| 7194 | /* we can find the nodes using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7195 | rc = moveto_node_hash(set, scnode, predicates); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7196 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7197 | rc = moveto_node(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7198 | } |
| 7199 | } |
| 7200 | LY_CHECK_GOTO(rc, cleanup); |
| 7201 | } |
| 7202 | } |
| 7203 | |
| 7204 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7205 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7206 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7207 | LY_CHECK_RET(rc); |
| 7208 | } |
| 7209 | |
| 7210 | cleanup: |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7211 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7212 | lydict_remove(set->ctx, ncname_dict); |
| 7213 | ly_path_predicates_free(set->ctx, pred_type, scnode, predicates); |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7214 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7215 | return rc; |
| 7216 | } |
| 7217 | |
| 7218 | /** |
| 7219 | * @brief Evaluate NodeType and any following Predicates. Logs directly on error. |
| 7220 | * |
| 7221 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7222 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7223 | * [8] NodeType ::= 'text' | 'node' |
| 7224 | * |
| 7225 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7226 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7227 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 7228 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7229 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7230 | * @param[in] options XPath options. |
| 7231 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7232 | */ |
| 7233 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7234 | 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] | 7235 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7236 | { |
| 7237 | LY_ERR rc; |
| 7238 | |
| 7239 | /* TODO */ |
| 7240 | (void)attr_axis; |
| 7241 | (void)all_desc; |
| 7242 | |
| 7243 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7244 | assert(exp->tok_len[*tok_idx] == 4); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7245 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 7246 | set_scnode_clear_ctx(set); |
| 7247 | /* just for the debug message below */ |
| 7248 | set = NULL; |
| 7249 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7250 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7251 | rc = xpath_node(NULL, 0, set, options); |
| 7252 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7253 | assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7254 | rc = xpath_text(NULL, 0, set, options); |
| 7255 | } |
| 7256 | LY_CHECK_RET(rc); |
| 7257 | } |
| 7258 | } |
| 7259 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7260 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7261 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7262 | |
| 7263 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7264 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7265 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7266 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7267 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7268 | |
| 7269 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7270 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7271 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7272 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7273 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7274 | |
| 7275 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7276 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7277 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7278 | LY_CHECK_RET(rc); |
| 7279 | } |
| 7280 | |
| 7281 | return LY_SUCCESS; |
| 7282 | } |
| 7283 | |
| 7284 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7285 | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
| 7286 | * |
| 7287 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 7288 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7289 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7290 | * |
| 7291 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7292 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7293 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7294 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7295 | * @param[in] options XPath options. |
| 7296 | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
| 7297 | */ |
| 7298 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7299 | 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] | 7300 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7301 | ly_bool attr_axis; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7302 | LY_ERR rc; |
| 7303 | |
| 7304 | goto step; |
| 7305 | do { |
| 7306 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7307 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7308 | all_desc = 0; |
| 7309 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7310 | assert(exp->tok_len[*tok_idx] == 2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7311 | all_desc = 1; |
| 7312 | } |
| 7313 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7314 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7315 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7316 | |
| 7317 | step: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7318 | /* evaluate abbreviated axis '@'? if any */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7319 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7320 | attr_axis = 1; |
| 7321 | |
| 7322 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7323 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7324 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7325 | } else { |
| 7326 | attr_axis = 0; |
| 7327 | } |
| 7328 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7329 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7330 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7331 | case LYXP_TOKEN_DOT: |
| 7332 | /* evaluate '.' */ |
| 7333 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7334 | rc = moveto_scnode_self(set, all_desc, options); |
| 7335 | } else { |
| 7336 | rc = moveto_self(set, all_desc, options); |
| 7337 | } |
| 7338 | LY_CHECK_RET(rc); |
| 7339 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7340 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7341 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7342 | break; |
| 7343 | |
| 7344 | case LYXP_TOKEN_DDOT: |
| 7345 | /* evaluate '..' */ |
| 7346 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7347 | rc = moveto_scnode_parent(set, all_desc, options); |
| 7348 | } else { |
| 7349 | rc = moveto_parent(set, all_desc, options); |
| 7350 | } |
| 7351 | LY_CHECK_RET(rc); |
| 7352 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7353 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7354 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7355 | break; |
| 7356 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7357 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7358 | /* evaluate NameTest Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7359 | 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] | 7360 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7361 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7362 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7363 | case LYXP_TOKEN_NODETYPE: |
| 7364 | /* evaluate NodeType Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7365 | 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] | 7366 | LY_CHECK_RET(rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7367 | break; |
| 7368 | |
| 7369 | default: |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 7370 | LOGINT_RET(set ? set->ctx : NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7371 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7372 | } 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] | 7373 | |
| 7374 | return LY_SUCCESS; |
| 7375 | } |
| 7376 | |
| 7377 | /** |
| 7378 | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
| 7379 | * |
| 7380 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 7381 | * |
| 7382 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7383 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7384 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7385 | * @param[in] options XPath options. |
| 7386 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7387 | */ |
| 7388 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7389 | 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] | 7390 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7391 | ly_bool all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7392 | |
| 7393 | if (set) { |
| 7394 | /* no matter what tokens follow, we need to be at the root */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7395 | LY_CHECK_RET(moveto_root(set, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7396 | } |
| 7397 | |
| 7398 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7399 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7400 | /* evaluate '/' - deferred */ |
| 7401 | all_desc = 0; |
| 7402 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7403 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7404 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7405 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7406 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7407 | return LY_SUCCESS; |
| 7408 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7409 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7410 | case LYXP_TOKEN_DOT: |
| 7411 | case LYXP_TOKEN_DDOT: |
| 7412 | case LYXP_TOKEN_AT: |
| 7413 | case LYXP_TOKEN_NAMETEST: |
| 7414 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7415 | 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] | 7416 | break; |
| 7417 | default: |
| 7418 | break; |
| 7419 | } |
| 7420 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7421 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 7422 | /* '//' RelativeLocationPath */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7423 | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
| 7424 | all_desc = 1; |
| 7425 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7426 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7427 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7428 | |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7429 | 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] | 7430 | } |
| 7431 | |
| 7432 | return LY_SUCCESS; |
| 7433 | } |
| 7434 | |
| 7435 | /** |
| 7436 | * @brief Evaluate FunctionCall. Logs directly on error. |
| 7437 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7438 | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7439 | * |
| 7440 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7441 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7442 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7443 | * @param[in] options XPath options. |
| 7444 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7445 | */ |
| 7446 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7447 | 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] | 7448 | { |
| 7449 | LY_ERR rc; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7450 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7451 | 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] | 7452 | uint16_t arg_count = 0, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7453 | struct lyxp_set **args = NULL, **args_aux; |
| 7454 | |
| 7455 | if (set) { |
| 7456 | /* FunctionName */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7457 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7458 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7459 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7460 | xpath_func = &xpath_not; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7461 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7462 | xpath_func = &xpath_sum; |
| 7463 | } |
| 7464 | break; |
| 7465 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7466 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7467 | xpath_func = &xpath_lang; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7468 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7469 | xpath_func = &xpath_last; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7470 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7471 | xpath_func = &xpath_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7472 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7473 | xpath_func = &xpath_true; |
| 7474 | } |
| 7475 | break; |
| 7476 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7477 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7478 | xpath_func = &xpath_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7479 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7480 | xpath_func = &xpath_false; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7481 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7482 | xpath_func = &xpath_floor; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7483 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7484 | xpath_func = &xpath_round; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7485 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7486 | xpath_func = &xpath_deref; |
| 7487 | } |
| 7488 | break; |
| 7489 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7490 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7491 | xpath_func = &xpath_concat; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7492 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7493 | xpath_func = &xpath_number; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7494 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7495 | xpath_func = &xpath_string; |
| 7496 | } |
| 7497 | break; |
| 7498 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7499 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7500 | xpath_func = &xpath_boolean; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7501 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7502 | xpath_func = &xpath_ceiling; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7503 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7504 | xpath_func = &xpath_current; |
| 7505 | } |
| 7506 | break; |
| 7507 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7508 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7509 | xpath_func = &xpath_contains; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7510 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7511 | xpath_func = &xpath_position; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7512 | } 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] | 7513 | xpath_func = &xpath_re_match; |
| 7514 | } |
| 7515 | break; |
| 7516 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7517 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7518 | xpath_func = &xpath_substring; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7519 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7520 | xpath_func = &xpath_translate; |
| 7521 | } |
| 7522 | break; |
| 7523 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7524 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7525 | xpath_func = &xpath_local_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7526 | } 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] | 7527 | xpath_func = &xpath_enum_value; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7528 | } 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] | 7529 | xpath_func = &xpath_bit_is_set; |
| 7530 | } |
| 7531 | break; |
| 7532 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7533 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7534 | xpath_func = &xpath_starts_with; |
| 7535 | } |
| 7536 | break; |
| 7537 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7538 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7539 | xpath_func = &xpath_derived_from; |
| 7540 | } |
| 7541 | break; |
| 7542 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7543 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7544 | xpath_func = &xpath_namespace_uri; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7545 | } 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] | 7546 | xpath_func = &xpath_string_length; |
| 7547 | } |
| 7548 | break; |
| 7549 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7550 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7551 | xpath_func = &xpath_normalize_space; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7552 | } 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] | 7553 | xpath_func = &xpath_substring_after; |
| 7554 | } |
| 7555 | break; |
| 7556 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7557 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7558 | xpath_func = &xpath_substring_before; |
| 7559 | } |
| 7560 | break; |
| 7561 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7562 | 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] | 7563 | xpath_func = &xpath_derived_from_or_self; |
| 7564 | } |
| 7565 | break; |
| 7566 | } |
| 7567 | |
| 7568 | if (!xpath_func) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7569 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_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] | 7570 | return LY_EVALID; |
| 7571 | } |
| 7572 | } |
| 7573 | |
| 7574 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7575 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7576 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7577 | |
| 7578 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7579 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7580 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7581 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7582 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7583 | |
| 7584 | /* ( Expr ( ',' Expr )* )? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7585 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7586 | if (set) { |
| 7587 | args = malloc(sizeof *args); |
| 7588 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7589 | arg_count = 1; |
| 7590 | args[0] = set_copy(set); |
| 7591 | if (!args[0]) { |
| 7592 | rc = LY_EMEM; |
| 7593 | goto cleanup; |
| 7594 | } |
| 7595 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7596 | rc = eval_expr_select(exp, tok_idx, 0, args[0], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7597 | LY_CHECK_GOTO(rc, cleanup); |
| 7598 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7599 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7600 | LY_CHECK_GOTO(rc, cleanup); |
| 7601 | } |
| 7602 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7603 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7604 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7605 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7606 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7607 | |
| 7608 | if (set) { |
| 7609 | ++arg_count; |
| 7610 | args_aux = realloc(args, arg_count * sizeof *args); |
| 7611 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7612 | args = args_aux; |
| 7613 | args[arg_count - 1] = set_copy(set); |
| 7614 | if (!args[arg_count - 1]) { |
| 7615 | rc = LY_EMEM; |
| 7616 | goto cleanup; |
| 7617 | } |
| 7618 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7619 | 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] | 7620 | LY_CHECK_GOTO(rc, cleanup); |
| 7621 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7622 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7623 | LY_CHECK_GOTO(rc, cleanup); |
| 7624 | } |
| 7625 | } |
| 7626 | |
| 7627 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7628 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7629 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7630 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7631 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7632 | |
| 7633 | if (set) { |
| 7634 | /* evaluate function */ |
| 7635 | rc = xpath_func(args, arg_count, set, options); |
| 7636 | |
| 7637 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7638 | /* merge all nodes from arg evaluations */ |
| 7639 | for (i = 0; i < arg_count; ++i) { |
| 7640 | set_scnode_clear_ctx(args[i]); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7641 | lyxp_set_scnode_merge(set, args[i]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7642 | } |
| 7643 | } |
| 7644 | } else { |
| 7645 | rc = LY_SUCCESS; |
| 7646 | } |
| 7647 | |
| 7648 | cleanup: |
| 7649 | for (i = 0; i < arg_count; ++i) { |
| 7650 | lyxp_set_free(args[i]); |
| 7651 | } |
| 7652 | free(args); |
| 7653 | |
| 7654 | return rc; |
| 7655 | } |
| 7656 | |
| 7657 | /** |
| 7658 | * @brief Evaluate Number. Logs directly on error. |
| 7659 | * |
| 7660 | * @param[in] ctx Context for errors. |
| 7661 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7662 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7663 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7664 | * @return LY_ERR |
| 7665 | */ |
| 7666 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7667 | 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] | 7668 | { |
| 7669 | long double num; |
| 7670 | char *endptr; |
| 7671 | |
| 7672 | if (set) { |
| 7673 | errno = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7674 | num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7675 | if (errno) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7676 | LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 7677 | LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7678 | 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] | 7679 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7680 | } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7681 | LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 7682 | LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7683 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7684 | return LY_EVALID; |
| 7685 | } |
| 7686 | |
| 7687 | set_fill_number(set, num); |
| 7688 | } |
| 7689 | |
| 7690 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7691 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7692 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7693 | return LY_SUCCESS; |
| 7694 | } |
| 7695 | |
| 7696 | /** |
| 7697 | * @brief Evaluate PathExpr. Logs directly on error. |
| 7698 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7699 | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7700 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 7701 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 7702 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7703 | * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7704 | * |
| 7705 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7706 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7707 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7708 | * @param[in] options XPath options. |
| 7709 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7710 | */ |
| 7711 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7712 | 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] | 7713 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7714 | ly_bool all_desc, parent_pos_pred; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7715 | LY_ERR rc; |
| 7716 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7717 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7718 | case LYXP_TOKEN_PAR1: |
| 7719 | /* '(' Expr ')' */ |
| 7720 | |
| 7721 | /* '(' */ |
| 7722 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7723 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7724 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7725 | |
| 7726 | /* Expr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7727 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7728 | LY_CHECK_RET(rc); |
| 7729 | |
| 7730 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7731 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7732 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7733 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7734 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7735 | |
| 7736 | parent_pos_pred = 0; |
| 7737 | goto predicate; |
| 7738 | |
| 7739 | case LYXP_TOKEN_DOT: |
| 7740 | case LYXP_TOKEN_DDOT: |
| 7741 | case LYXP_TOKEN_AT: |
| 7742 | case LYXP_TOKEN_NAMETEST: |
| 7743 | case LYXP_TOKEN_NODETYPE: |
| 7744 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7745 | rc = eval_relative_location_path(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7746 | LY_CHECK_RET(rc); |
| 7747 | break; |
| 7748 | |
| 7749 | case LYXP_TOKEN_FUNCNAME: |
| 7750 | /* FunctionCall */ |
| 7751 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7752 | rc = eval_function_call(exp, tok_idx, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7753 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7754 | rc = eval_function_call(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7755 | } |
| 7756 | LY_CHECK_RET(rc); |
| 7757 | |
| 7758 | parent_pos_pred = 1; |
| 7759 | goto predicate; |
| 7760 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 7761 | case LYXP_TOKEN_OPER_PATH: |
| 7762 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7763 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7764 | rc = eval_absolute_location_path(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7765 | LY_CHECK_RET(rc); |
| 7766 | break; |
| 7767 | |
| 7768 | case LYXP_TOKEN_LITERAL: |
| 7769 | /* Literal */ |
| 7770 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7771 | if (set) { |
| 7772 | set_scnode_clear_ctx(set); |
| 7773 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7774 | eval_literal(exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7775 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7776 | eval_literal(exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7777 | } |
| 7778 | |
| 7779 | parent_pos_pred = 1; |
| 7780 | goto predicate; |
| 7781 | |
| 7782 | case LYXP_TOKEN_NUMBER: |
| 7783 | /* Number */ |
| 7784 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7785 | if (set) { |
| 7786 | set_scnode_clear_ctx(set); |
| 7787 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7788 | rc = eval_number(NULL, exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7789 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7790 | rc = eval_number(set->ctx, exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7791 | } |
| 7792 | LY_CHECK_RET(rc); |
| 7793 | |
| 7794 | parent_pos_pred = 1; |
| 7795 | goto predicate; |
| 7796 | |
| 7797 | default: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7798 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7799 | &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7800 | return LY_EVALID; |
| 7801 | } |
| 7802 | |
| 7803 | return LY_SUCCESS; |
| 7804 | |
| 7805 | predicate: |
| 7806 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7807 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7808 | rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7809 | LY_CHECK_RET(rc); |
| 7810 | } |
| 7811 | |
| 7812 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7813 | 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] | 7814 | |
| 7815 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7816 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7817 | all_desc = 0; |
| 7818 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7819 | all_desc = 1; |
| 7820 | } |
| 7821 | |
| 7822 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7823 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7824 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7825 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7826 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7827 | LY_CHECK_RET(rc); |
| 7828 | } |
| 7829 | |
| 7830 | return LY_SUCCESS; |
| 7831 | } |
| 7832 | |
| 7833 | /** |
| 7834 | * @brief Evaluate UnionExpr. Logs directly on error. |
| 7835 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7836 | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7837 | * |
| 7838 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7839 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7840 | * @param[in] repeat How many times this expression is repeated. |
| 7841 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7842 | * @param[in] options XPath options. |
| 7843 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7844 | */ |
| 7845 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7846 | 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] | 7847 | { |
| 7848 | LY_ERR rc = LY_SUCCESS; |
| 7849 | struct lyxp_set orig_set, set2; |
| 7850 | uint16_t i; |
| 7851 | |
| 7852 | assert(repeat); |
| 7853 | |
| 7854 | set_init(&orig_set, set); |
| 7855 | set_init(&set2, set); |
| 7856 | |
| 7857 | set_fill_set(&orig_set, set); |
| 7858 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7859 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7860 | LY_CHECK_GOTO(rc, cleanup); |
| 7861 | |
| 7862 | /* ('|' PathExpr)* */ |
| 7863 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7864 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7865 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7866 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7867 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7868 | |
| 7869 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7870 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7871 | LY_CHECK_GOTO(rc, cleanup); |
| 7872 | continue; |
| 7873 | } |
| 7874 | |
| 7875 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7876 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7877 | LY_CHECK_GOTO(rc, cleanup); |
| 7878 | |
| 7879 | /* eval */ |
| 7880 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7881 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7882 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7883 | rc = moveto_union(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7884 | LY_CHECK_GOTO(rc, cleanup); |
| 7885 | } |
| 7886 | } |
| 7887 | |
| 7888 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7889 | lyxp_set_free_content(&orig_set); |
| 7890 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7891 | return rc; |
| 7892 | } |
| 7893 | |
| 7894 | /** |
| 7895 | * @brief Evaluate UnaryExpr. Logs directly on error. |
| 7896 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7897 | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7898 | * |
| 7899 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7900 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7901 | * @param[in] repeat How many times this expression is repeated. |
| 7902 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7903 | * @param[in] options XPath options. |
| 7904 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7905 | */ |
| 7906 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7907 | 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] | 7908 | { |
| 7909 | LY_ERR rc; |
| 7910 | uint16_t this_op, i; |
| 7911 | |
| 7912 | assert(repeat); |
| 7913 | |
| 7914 | /* ('-')+ */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7915 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7916 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7917 | 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] | 7918 | |
| 7919 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7920 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7921 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7922 | } |
| 7923 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7924 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7925 | LY_CHECK_RET(rc); |
| 7926 | |
| 7927 | if (set && (repeat % 2)) { |
| 7928 | if (options & LYXP_SCNODE_ALL) { |
| 7929 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
| 7930 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7931 | 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] | 7932 | LY_CHECK_RET(rc); |
| 7933 | } |
| 7934 | } |
| 7935 | |
| 7936 | return LY_SUCCESS; |
| 7937 | } |
| 7938 | |
| 7939 | /** |
| 7940 | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
| 7941 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7942 | * [18] MultiplicativeExpr ::= UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7943 | * | MultiplicativeExpr '*' UnaryExpr |
| 7944 | * | MultiplicativeExpr 'div' UnaryExpr |
| 7945 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 7946 | * |
| 7947 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7948 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7949 | * @param[in] repeat How many times this expression is repeated. |
| 7950 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7951 | * @param[in] options XPath options. |
| 7952 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7953 | */ |
| 7954 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7955 | 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] | 7956 | { |
| 7957 | LY_ERR rc; |
| 7958 | uint16_t this_op; |
| 7959 | struct lyxp_set orig_set, set2; |
| 7960 | uint16_t i; |
| 7961 | |
| 7962 | assert(repeat); |
| 7963 | |
| 7964 | set_init(&orig_set, set); |
| 7965 | set_init(&set2, set); |
| 7966 | |
| 7967 | set_fill_set(&orig_set, set); |
| 7968 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7969 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7970 | LY_CHECK_GOTO(rc, cleanup); |
| 7971 | |
| 7972 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 7973 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7974 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7975 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7976 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7977 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7978 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7979 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7980 | |
| 7981 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7982 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7983 | LY_CHECK_GOTO(rc, cleanup); |
| 7984 | continue; |
| 7985 | } |
| 7986 | |
| 7987 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7988 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7989 | LY_CHECK_GOTO(rc, cleanup); |
| 7990 | |
| 7991 | /* eval */ |
| 7992 | if (options & LYXP_SCNODE_ALL) { |
| 7993 | 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] | 7994 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7995 | set_scnode_clear_ctx(set); |
| 7996 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7997 | 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] | 7998 | LY_CHECK_GOTO(rc, cleanup); |
| 7999 | } |
| 8000 | } |
| 8001 | |
| 8002 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8003 | lyxp_set_free_content(&orig_set); |
| 8004 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8005 | return rc; |
| 8006 | } |
| 8007 | |
| 8008 | /** |
| 8009 | * @brief Evaluate AdditiveExpr. Logs directly on error. |
| 8010 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8011 | * [17] AdditiveExpr ::= MultiplicativeExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8012 | * | AdditiveExpr '+' MultiplicativeExpr |
| 8013 | * | AdditiveExpr '-' MultiplicativeExpr |
| 8014 | * |
| 8015 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8016 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8017 | * @param[in] repeat How many times this expression is repeated. |
| 8018 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8019 | * @param[in] options XPath options. |
| 8020 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8021 | */ |
| 8022 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8023 | 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] | 8024 | { |
| 8025 | LY_ERR rc; |
| 8026 | uint16_t this_op; |
| 8027 | struct lyxp_set orig_set, set2; |
| 8028 | uint16_t i; |
| 8029 | |
| 8030 | assert(repeat); |
| 8031 | |
| 8032 | set_init(&orig_set, set); |
| 8033 | set_init(&set2, set); |
| 8034 | |
| 8035 | set_fill_set(&orig_set, set); |
| 8036 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8037 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8038 | LY_CHECK_GOTO(rc, cleanup); |
| 8039 | |
| 8040 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 8041 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8042 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8043 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8044 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8045 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8046 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8047 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8048 | |
| 8049 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8050 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8051 | LY_CHECK_GOTO(rc, cleanup); |
| 8052 | continue; |
| 8053 | } |
| 8054 | |
| 8055 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8056 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8057 | LY_CHECK_GOTO(rc, cleanup); |
| 8058 | |
| 8059 | /* eval */ |
| 8060 | if (options & LYXP_SCNODE_ALL) { |
| 8061 | 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] | 8062 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8063 | set_scnode_clear_ctx(set); |
| 8064 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8065 | 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] | 8066 | LY_CHECK_GOTO(rc, cleanup); |
| 8067 | } |
| 8068 | } |
| 8069 | |
| 8070 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8071 | lyxp_set_free_content(&orig_set); |
| 8072 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8073 | return rc; |
| 8074 | } |
| 8075 | |
| 8076 | /** |
| 8077 | * @brief Evaluate RelationalExpr. Logs directly on error. |
| 8078 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8079 | * [16] RelationalExpr ::= AdditiveExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8080 | * | RelationalExpr '<' AdditiveExpr |
| 8081 | * | RelationalExpr '>' AdditiveExpr |
| 8082 | * | RelationalExpr '<=' AdditiveExpr |
| 8083 | * | RelationalExpr '>=' AdditiveExpr |
| 8084 | * |
| 8085 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8086 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8087 | * @param[in] repeat How many times this expression is repeated. |
| 8088 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8089 | * @param[in] options XPath options. |
| 8090 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8091 | */ |
| 8092 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8093 | 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] | 8094 | { |
| 8095 | LY_ERR rc; |
| 8096 | uint16_t this_op; |
| 8097 | struct lyxp_set orig_set, set2; |
| 8098 | uint16_t i; |
| 8099 | |
| 8100 | assert(repeat); |
| 8101 | |
| 8102 | set_init(&orig_set, set); |
| 8103 | set_init(&set2, set); |
| 8104 | |
| 8105 | set_fill_set(&orig_set, set); |
| 8106 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8107 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8108 | LY_CHECK_GOTO(rc, cleanup); |
| 8109 | |
| 8110 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 8111 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8112 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8113 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8114 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8115 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8116 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8117 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8118 | |
| 8119 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8120 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8121 | LY_CHECK_GOTO(rc, cleanup); |
| 8122 | continue; |
| 8123 | } |
| 8124 | |
| 8125 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8126 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8127 | LY_CHECK_GOTO(rc, cleanup); |
| 8128 | |
| 8129 | /* eval */ |
| 8130 | if (options & LYXP_SCNODE_ALL) { |
| 8131 | 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] | 8132 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8133 | set_scnode_clear_ctx(set); |
| 8134 | } else { |
| 8135 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8136 | LY_CHECK_GOTO(rc, cleanup); |
| 8137 | } |
| 8138 | } |
| 8139 | |
| 8140 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8141 | lyxp_set_free_content(&orig_set); |
| 8142 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8143 | return rc; |
| 8144 | } |
| 8145 | |
| 8146 | /** |
| 8147 | * @brief Evaluate EqualityExpr. Logs directly on error. |
| 8148 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8149 | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8150 | * | EqualityExpr '!=' RelationalExpr |
| 8151 | * |
| 8152 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8153 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8154 | * @param[in] repeat How many times this expression is repeated. |
| 8155 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8156 | * @param[in] options XPath options. |
| 8157 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8158 | */ |
| 8159 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8160 | 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] | 8161 | { |
| 8162 | LY_ERR rc; |
| 8163 | uint16_t this_op; |
| 8164 | struct lyxp_set orig_set, set2; |
| 8165 | uint16_t i; |
| 8166 | |
| 8167 | assert(repeat); |
| 8168 | |
| 8169 | set_init(&orig_set, set); |
| 8170 | set_init(&set2, set); |
| 8171 | |
| 8172 | set_fill_set(&orig_set, set); |
| 8173 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8174 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8175 | LY_CHECK_GOTO(rc, cleanup); |
| 8176 | |
| 8177 | /* ('=' / '!=' RelationalExpr)* */ |
| 8178 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8179 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8180 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8181 | 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] | 8182 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8183 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8184 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8185 | |
| 8186 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8187 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8188 | LY_CHECK_GOTO(rc, cleanup); |
| 8189 | continue; |
| 8190 | } |
| 8191 | |
| 8192 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8193 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8194 | LY_CHECK_GOTO(rc, cleanup); |
| 8195 | |
| 8196 | /* eval */ |
| 8197 | if (options & LYXP_SCNODE_ALL) { |
| 8198 | 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] | 8199 | warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1); |
| 8200 | 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] | 8201 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8202 | set_scnode_clear_ctx(set); |
| 8203 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8204 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8205 | LY_CHECK_GOTO(rc, cleanup); |
| 8206 | } |
| 8207 | } |
| 8208 | |
| 8209 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8210 | lyxp_set_free_content(&orig_set); |
| 8211 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8212 | return rc; |
| 8213 | } |
| 8214 | |
| 8215 | /** |
| 8216 | * @brief Evaluate AndExpr. Logs directly on error. |
| 8217 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8218 | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8219 | * |
| 8220 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8221 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8222 | * @param[in] repeat How many times this expression is repeated. |
| 8223 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8224 | * @param[in] options XPath options. |
| 8225 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8226 | */ |
| 8227 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8228 | 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] | 8229 | { |
| 8230 | LY_ERR rc; |
| 8231 | struct lyxp_set orig_set, set2; |
| 8232 | uint16_t i; |
| 8233 | |
| 8234 | assert(repeat); |
| 8235 | |
| 8236 | set_init(&orig_set, set); |
| 8237 | set_init(&set2, set); |
| 8238 | |
| 8239 | set_fill_set(&orig_set, set); |
| 8240 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8241 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8242 | LY_CHECK_GOTO(rc, cleanup); |
| 8243 | |
| 8244 | /* cast to boolean, we know that will be the final result */ |
| 8245 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8246 | set_scnode_clear_ctx(set); |
| 8247 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8248 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8249 | } |
| 8250 | |
| 8251 | /* ('and' EqualityExpr)* */ |
| 8252 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8253 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8254 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8255 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8256 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8257 | |
| 8258 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8259 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) { |
| 8260 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8261 | LY_CHECK_GOTO(rc, cleanup); |
| 8262 | continue; |
| 8263 | } |
| 8264 | |
| 8265 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8266 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8267 | LY_CHECK_GOTO(rc, cleanup); |
| 8268 | |
| 8269 | /* eval - just get boolean value actually */ |
| 8270 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8271 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8272 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8273 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8274 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8275 | set_fill_set(set, &set2); |
| 8276 | } |
| 8277 | } |
| 8278 | |
| 8279 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8280 | lyxp_set_free_content(&orig_set); |
| 8281 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8282 | return rc; |
| 8283 | } |
| 8284 | |
| 8285 | /** |
| 8286 | * @brief Evaluate OrExpr. Logs directly on error. |
| 8287 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8288 | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8289 | * |
| 8290 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8291 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8292 | * @param[in] repeat How many times this expression is repeated. |
| 8293 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8294 | * @param[in] options XPath options. |
| 8295 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8296 | */ |
| 8297 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8298 | 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] | 8299 | { |
| 8300 | LY_ERR rc; |
| 8301 | struct lyxp_set orig_set, set2; |
| 8302 | uint16_t i; |
| 8303 | |
| 8304 | assert(repeat); |
| 8305 | |
| 8306 | set_init(&orig_set, set); |
| 8307 | set_init(&set2, set); |
| 8308 | |
| 8309 | set_fill_set(&orig_set, set); |
| 8310 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8311 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8312 | LY_CHECK_GOTO(rc, cleanup); |
| 8313 | |
| 8314 | /* cast to boolean, we know that will be the final result */ |
| 8315 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8316 | set_scnode_clear_ctx(set); |
| 8317 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8318 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8319 | } |
| 8320 | |
| 8321 | /* ('or' AndExpr)* */ |
| 8322 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8323 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8324 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8325 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8326 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8327 | |
| 8328 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8329 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) { |
| 8330 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8331 | LY_CHECK_GOTO(rc, cleanup); |
| 8332 | continue; |
| 8333 | } |
| 8334 | |
| 8335 | set_fill_set(&set2, &orig_set); |
| 8336 | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
| 8337 | * but it does not matter */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8338 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8339 | LY_CHECK_GOTO(rc, cleanup); |
| 8340 | |
| 8341 | /* eval - just get boolean value actually */ |
| 8342 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8343 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8344 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8345 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8346 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8347 | set_fill_set(set, &set2); |
| 8348 | } |
| 8349 | } |
| 8350 | |
| 8351 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8352 | lyxp_set_free_content(&orig_set); |
| 8353 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8354 | return rc; |
| 8355 | } |
| 8356 | |
| 8357 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8358 | * @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] | 8359 | * |
| 8360 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8361 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8362 | * @param[in] etype Expression type to evaluate. |
| 8363 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8364 | * @param[in] options XPath options. |
| 8365 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8366 | */ |
| 8367 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8368 | 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] | 8369 | { |
| 8370 | uint16_t i, count; |
| 8371 | enum lyxp_expr_type next_etype; |
| 8372 | LY_ERR rc; |
| 8373 | |
| 8374 | /* process operator repeats */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8375 | if (!exp->repeat[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8376 | next_etype = LYXP_EXPR_NONE; |
| 8377 | } else { |
| 8378 | /* find etype repeat */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8379 | for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8380 | |
| 8381 | /* select one-priority lower because etype expression called us */ |
| 8382 | if (i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8383 | next_etype = exp->repeat[*tok_idx][i - 1]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8384 | /* count repeats for that expression */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8385 | 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] | 8386 | } else { |
| 8387 | next_etype = LYXP_EXPR_NONE; |
| 8388 | } |
| 8389 | } |
| 8390 | |
| 8391 | /* decide what expression are we parsing based on the repeat */ |
| 8392 | switch (next_etype) { |
| 8393 | case LYXP_EXPR_OR: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8394 | rc = eval_or_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8395 | break; |
| 8396 | case LYXP_EXPR_AND: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8397 | rc = eval_and_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8398 | break; |
| 8399 | case LYXP_EXPR_EQUALITY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8400 | rc = eval_equality_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8401 | break; |
| 8402 | case LYXP_EXPR_RELATIONAL: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8403 | rc = eval_relational_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8404 | break; |
| 8405 | case LYXP_EXPR_ADDITIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8406 | rc = eval_additive_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8407 | break; |
| 8408 | case LYXP_EXPR_MULTIPLICATIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8409 | rc = eval_multiplicative_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8410 | break; |
| 8411 | case LYXP_EXPR_UNARY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8412 | rc = eval_unary_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8413 | break; |
| 8414 | case LYXP_EXPR_UNION: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8415 | rc = eval_union_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8416 | break; |
| 8417 | case LYXP_EXPR_NONE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8418 | rc = eval_path_expr(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8419 | break; |
| 8420 | default: |
| 8421 | LOGINT_RET(set->ctx); |
| 8422 | } |
| 8423 | |
| 8424 | return rc; |
| 8425 | } |
| 8426 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8427 | /** |
| 8428 | * @brief Get root type. |
| 8429 | * |
| 8430 | * @param[in] ctx_node Context node. |
| 8431 | * @param[in] ctx_scnode Schema context node. |
| 8432 | * @param[in] options XPath options. |
| 8433 | * @return Root type. |
| 8434 | */ |
| 8435 | static enum lyxp_node_type |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8436 | 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] | 8437 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8438 | const struct lysc_node *op; |
| 8439 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8440 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8441 | /* schema */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8442 | 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] | 8443 | |
| 8444 | if (op || (options & LYXP_SCNODE)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8445 | /* general root that can access everything */ |
| 8446 | return LYXP_NODE_ROOT; |
| 8447 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
| 8448 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8449 | return LYXP_NODE_ROOT_CONFIG; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8450 | } |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8451 | return LYXP_NODE_ROOT; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8452 | } |
| 8453 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8454 | /* data */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8455 | op = ctx_node ? ctx_node->schema : NULL; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 8456 | 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] | 8457 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8458 | if (op || !(options & LYXP_SCHEMA)) { |
| 8459 | /* general root that can access everything */ |
| 8460 | return LYXP_NODE_ROOT; |
| 8461 | } else if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8462 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8463 | return LYXP_NODE_ROOT_CONFIG; |
| 8464 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8465 | return LYXP_NODE_ROOT; |
| 8466 | } |
| 8467 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8468 | LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8469 | lyxp_eval(struct lyxp_expr *exp, const struct lys_module *cur_mod, LY_PREFIX_FORMAT format, void *prefix_data, |
| 8470 | const struct lyd_node *ctx_node, const struct lyd_node *tree, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8471 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8472 | uint16_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8473 | LY_ERR rc; |
| 8474 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8475 | LY_CHECK_ARG_RET(NULL, exp, set, LY_EINVAL); |
| 8476 | if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) { |
| 8477 | LOGARG(NULL, "Current module must be set if schema format is used."); |
| 8478 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8479 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8480 | |
| 8481 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8482 | memset(set, 0, sizeof *set); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8483 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8484 | set->root_type = lyxp_get_root_type(ctx_node, NULL, options); |
| 8485 | set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0); |
| 8486 | |
| 8487 | set->ctx = (struct ly_ctx *)(cur_mod ? cur_mod->ctx : (ctx_node ? LYD_CTX(ctx_node) : (tree ? LYD_CTX(tree) : NULL))); |
| 8488 | set->cur_node = ctx_node; |
| 8489 | for (set->context_op = ctx_node ? ctx_node->schema : NULL; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 8490 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8491 | set->context_op = set->context_op->parent) {} |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 8492 | set->tree = tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8493 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8494 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8495 | set->prefix_data = prefix_data; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8496 | |
| 8497 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8498 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8499 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8500 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8501 | } |
| 8502 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8503 | return rc; |
| 8504 | } |
| 8505 | |
| 8506 | #if 0 |
| 8507 | |
| 8508 | /* full xml printing of set elements, not used currently */ |
| 8509 | |
| 8510 | void |
| 8511 | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
| 8512 | { |
| 8513 | uint32_t i; |
| 8514 | char *str_num; |
| 8515 | struct lyout out; |
| 8516 | |
| 8517 | memset(&out, 0, sizeof out); |
| 8518 | |
| 8519 | out.type = LYOUT_STREAM; |
| 8520 | out.method.f = f; |
| 8521 | |
| 8522 | switch (set->type) { |
| 8523 | case LYXP_SET_EMPTY: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8524 | ly_print_(&out, "Empty XPath set\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8525 | break; |
| 8526 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8527 | ly_print_(&out, "Boolean XPath set:\n"); |
| 8528 | ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8529 | break; |
| 8530 | case LYXP_SET_STRING: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8531 | ly_print_(&out, "String XPath set:\n"); |
| 8532 | ly_print_(&out, "\"%s\"\n\n", set->value.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8533 | break; |
| 8534 | case LYXP_SET_NUMBER: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8535 | ly_print_(&out, "Number XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8536 | |
| 8537 | if (isnan(set->value.num)) { |
| 8538 | str_num = strdup("NaN"); |
| 8539 | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
| 8540 | str_num = strdup("0"); |
| 8541 | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
| 8542 | str_num = strdup("Infinity"); |
| 8543 | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
| 8544 | str_num = strdup("-Infinity"); |
| 8545 | } else if ((long long)set->value.num == set->value.num) { |
| 8546 | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
| 8547 | str_num = NULL; |
| 8548 | } |
| 8549 | } else { |
| 8550 | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
| 8551 | str_num = NULL; |
| 8552 | } |
| 8553 | } |
| 8554 | if (!str_num) { |
| 8555 | LOGMEM; |
| 8556 | return; |
| 8557 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8558 | ly_print_(&out, "%s\n\n", str_num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8559 | free(str_num); |
| 8560 | break; |
| 8561 | case LYXP_SET_NODE_SET: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8562 | ly_print_(&out, "Node XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8563 | |
| 8564 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8565 | ly_print_(&out, "%d. ", i + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8566 | switch (set->node_type[i]) { |
| 8567 | case LYXP_NODE_ROOT_ALL: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8568 | ly_print_(&out, "ROOT all\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8569 | break; |
| 8570 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8571 | ly_print_(&out, "ROOT config\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8572 | break; |
| 8573 | case LYXP_NODE_ROOT_STATE: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8574 | ly_print_(&out, "ROOT state\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8575 | break; |
| 8576 | case LYXP_NODE_ROOT_NOTIF: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8577 | 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] | 8578 | break; |
| 8579 | case LYXP_NODE_ROOT_RPC: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8580 | 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] | 8581 | break; |
| 8582 | case LYXP_NODE_ROOT_OUTPUT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8583 | 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] | 8584 | break; |
| 8585 | case LYXP_NODE_ELEM: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8586 | ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8587 | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8588 | ly_print_(&out, "\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8589 | break; |
| 8590 | case LYXP_NODE_TEXT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8591 | 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] | 8592 | break; |
| 8593 | case LYXP_NODE_ATTR: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8594 | 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] | 8595 | break; |
| 8596 | } |
| 8597 | } |
| 8598 | break; |
| 8599 | } |
| 8600 | } |
| 8601 | |
| 8602 | #endif |
| 8603 | |
| 8604 | LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8605 | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8606 | { |
| 8607 | long double num; |
| 8608 | char *str; |
| 8609 | LY_ERR rc; |
| 8610 | |
| 8611 | if (!set || (set->type == target)) { |
| 8612 | return LY_SUCCESS; |
| 8613 | } |
| 8614 | |
| 8615 | /* it's not possible to convert anything into a node set */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8616 | assert(target != LYXP_SET_NODE_SET); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8617 | |
| 8618 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8619 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8620 | return LY_EINVAL; |
| 8621 | } |
| 8622 | |
| 8623 | /* to STRING */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8624 | 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] | 8625 | switch (set->type) { |
| 8626 | case LYXP_SET_NUMBER: |
| 8627 | if (isnan(set->val.num)) { |
| 8628 | set->val.str = strdup("NaN"); |
| 8629 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8630 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 8631 | set->val.str = strdup("0"); |
| 8632 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8633 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 8634 | set->val.str = strdup("Infinity"); |
| 8635 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8636 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 8637 | set->val.str = strdup("-Infinity"); |
| 8638 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8639 | } else if ((long long)set->val.num == set->val.num) { |
| 8640 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 8641 | LOGMEM_RET(set->ctx); |
| 8642 | } |
| 8643 | set->val.str = str; |
| 8644 | } else { |
| 8645 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 8646 | LOGMEM_RET(set->ctx); |
| 8647 | } |
| 8648 | set->val.str = str; |
| 8649 | } |
| 8650 | break; |
| 8651 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8652 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8653 | set->val.str = strdup("true"); |
| 8654 | } else { |
| 8655 | set->val.str = strdup("false"); |
| 8656 | } |
| 8657 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 8658 | break; |
| 8659 | case LYXP_SET_NODE_SET: |
| 8660 | assert(set->used); |
| 8661 | |
| 8662 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8663 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8664 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8665 | rc = cast_node_set_to_string(set, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8666 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8667 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8668 | set->val.str = str; |
| 8669 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8670 | default: |
| 8671 | LOGINT_RET(set->ctx); |
| 8672 | } |
| 8673 | set->type = LYXP_SET_STRING; |
| 8674 | } |
| 8675 | |
| 8676 | /* to NUMBER */ |
| 8677 | if (target == LYXP_SET_NUMBER) { |
| 8678 | switch (set->type) { |
| 8679 | case LYXP_SET_STRING: |
| 8680 | num = cast_string_to_number(set->val.str); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8681 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8682 | set->val.num = num; |
| 8683 | break; |
| 8684 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8685 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8686 | set->val.num = 1; |
| 8687 | } else { |
| 8688 | set->val.num = 0; |
| 8689 | } |
| 8690 | break; |
| 8691 | default: |
| 8692 | LOGINT_RET(set->ctx); |
| 8693 | } |
| 8694 | set->type = LYXP_SET_NUMBER; |
| 8695 | } |
| 8696 | |
| 8697 | /* to BOOLEAN */ |
| 8698 | if (target == LYXP_SET_BOOLEAN) { |
| 8699 | switch (set->type) { |
| 8700 | case LYXP_SET_NUMBER: |
| 8701 | 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] | 8702 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8703 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8704 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8705 | } |
| 8706 | break; |
| 8707 | case LYXP_SET_STRING: |
| 8708 | if (set->val.str[0]) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8709 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8710 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8711 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8712 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8713 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8714 | } |
| 8715 | break; |
| 8716 | case LYXP_SET_NODE_SET: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8717 | if (set->used) { |
| 8718 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8719 | set->val.bln = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8720 | } else { |
| 8721 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8722 | set->val.bln = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8723 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8724 | break; |
| 8725 | default: |
| 8726 | LOGINT_RET(set->ctx); |
| 8727 | } |
| 8728 | set->type = LYXP_SET_BOOLEAN; |
| 8729 | } |
| 8730 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8731 | return LY_SUCCESS; |
| 8732 | } |
| 8733 | |
| 8734 | LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8735 | lyxp_atomize(struct lyxp_expr *exp, const struct lys_module *cur_mod, LY_PREFIX_FORMAT format, void *prefix_data, |
| 8736 | const struct lysc_node *ctx_scnode, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8737 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8738 | uint16_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8739 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8740 | LY_CHECK_ARG_RET(NULL, exp, set, LY_EINVAL); |
| 8741 | if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) { |
| 8742 | LOGARG(NULL, "Current module must be set if schema format is used."); |
| 8743 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8744 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8745 | |
| 8746 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8747 | memset(set, 0, sizeof *set); |
| 8748 | set->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8749 | set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options); |
| 8750 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, ctx_scnode, ctx_scnode ? LYXP_NODE_ELEM : set->root_type, NULL)); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 8751 | set->val.scnodes[0].in_ctx = -2; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8752 | |
| 8753 | set->ctx = cur_mod ? cur_mod->ctx : (ctx_scnode ? ctx_scnode->module->ctx : NULL); |
| 8754 | set->cur_scnode = ctx_scnode; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8755 | for (set->context_op = ctx_scnode; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 8756 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8757 | set->context_op = set->context_op->parent) {} |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8758 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8759 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8760 | set->prefix_data = prefix_data; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8761 | |
| 8762 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8763 | return eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8764 | } |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 8765 | |
| 8766 | API const char * |
| 8767 | lyxp_get_expr(const struct lyxp_expr *path) |
| 8768 | { |
| 8769 | if (!path) { |
| 8770 | return NULL; |
| 8771 | } |
| 8772 | |
| 8773 | return path->expr; |
| 8774 | } |