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); |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame^] | 48 | static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, |
| 49 | struct lyxp_set *set, uint32_t options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * @brief Print the type of an XPath \p set. |
| 53 | * |
| 54 | * @param[in] set Set to use. |
| 55 | * @return Set type string. |
| 56 | */ |
| 57 | static const char * |
| 58 | print_set_type(struct lyxp_set *set) |
| 59 | { |
| 60 | switch (set->type) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 61 | case LYXP_SET_NODE_SET: |
| 62 | return "node set"; |
| 63 | case LYXP_SET_SCNODE_SET: |
| 64 | return "schema node set"; |
| 65 | case LYXP_SET_BOOLEAN: |
| 66 | return "boolean"; |
| 67 | case LYXP_SET_NUMBER: |
| 68 | return "number"; |
| 69 | case LYXP_SET_STRING: |
| 70 | return "string"; |
| 71 | } |
| 72 | |
| 73 | return NULL; |
| 74 | } |
| 75 | |
Michal Vasko | 24cddf8 | 2020-06-01 08:17:01 +0200 | [diff] [blame] | 76 | const char * |
| 77 | lyxp_print_token(enum lyxp_token tok) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 78 | { |
| 79 | switch (tok) { |
| 80 | case LYXP_TOKEN_PAR1: |
| 81 | return "("; |
| 82 | case LYXP_TOKEN_PAR2: |
| 83 | return ")"; |
| 84 | case LYXP_TOKEN_BRACK1: |
| 85 | return "["; |
| 86 | case LYXP_TOKEN_BRACK2: |
| 87 | return "]"; |
| 88 | case LYXP_TOKEN_DOT: |
| 89 | return "."; |
| 90 | case LYXP_TOKEN_DDOT: |
| 91 | return ".."; |
| 92 | case LYXP_TOKEN_AT: |
| 93 | return "@"; |
| 94 | case LYXP_TOKEN_COMMA: |
| 95 | return ","; |
| 96 | case LYXP_TOKEN_NAMETEST: |
| 97 | return "NameTest"; |
| 98 | case LYXP_TOKEN_NODETYPE: |
| 99 | return "NodeType"; |
| 100 | case LYXP_TOKEN_FUNCNAME: |
| 101 | return "FunctionName"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 102 | case LYXP_TOKEN_OPER_LOG: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 103 | return "Operator(Logic)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 104 | case LYXP_TOKEN_OPER_EQUAL: |
| 105 | return "Operator(Equal)"; |
| 106 | case LYXP_TOKEN_OPER_NEQUAL: |
| 107 | return "Operator(Non-equal)"; |
| 108 | case LYXP_TOKEN_OPER_COMP: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 109 | return "Operator(Comparison)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 110 | case LYXP_TOKEN_OPER_MATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 111 | return "Operator(Math)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 112 | case LYXP_TOKEN_OPER_UNI: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 113 | return "Operator(Union)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 114 | case LYXP_TOKEN_OPER_PATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 115 | return "Operator(Path)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 116 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 117 | return "Operator(Recursive Path)"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 118 | case LYXP_TOKEN_LITERAL: |
| 119 | return "Literal"; |
| 120 | case LYXP_TOKEN_NUMBER: |
| 121 | return "Number"; |
| 122 | default: |
| 123 | LOGINT(NULL); |
| 124 | return ""; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @brief Print the whole expression \p exp to debug output. |
| 130 | * |
| 131 | * @param[in] exp Expression to use. |
| 132 | */ |
| 133 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame^] | 134 | print_expr_struct_debug(const struct lyxp_expr *exp) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 135 | { |
| 136 | uint16_t i, j; |
| 137 | char tmp[128]; |
| 138 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 139 | if (!exp || (ly_ll < LY_LLDBG)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 140 | return; |
| 141 | } |
| 142 | |
| 143 | LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr); |
| 144 | for (i = 0; i < exp->used; ++i) { |
Michal Vasko | 24cddf8 | 2020-06-01 08:17:01 +0200 | [diff] [blame] | 145 | 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] | 146 | &exp->expr[exp->tok_pos[i]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 147 | if (exp->repeat[i]) { |
| 148 | sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]); |
| 149 | for (j = 1; exp->repeat[i][j]; ++j) { |
| 150 | sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]); |
| 151 | } |
| 152 | strcat(tmp, ")"); |
| 153 | } |
| 154 | LOGDBG(LY_LDGXPATH, tmp); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | #ifndef NDEBUG |
| 159 | |
| 160 | /** |
| 161 | * @brief Print XPath set content to debug output. |
| 162 | * |
| 163 | * @param[in] set Set to print. |
| 164 | */ |
| 165 | static void |
| 166 | print_set_debug(struct lyxp_set *set) |
| 167 | { |
| 168 | uint32_t i; |
| 169 | char *str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 170 | struct lyxp_set_node *item; |
| 171 | struct lyxp_set_scnode *sitem; |
| 172 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 173 | if (ly_ll < LY_LLDBG) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 174 | return; |
| 175 | } |
| 176 | |
| 177 | switch (set->type) { |
| 178 | case LYXP_SET_NODE_SET: |
| 179 | LOGDBG(LY_LDGXPATH, "set NODE SET:"); |
| 180 | for (i = 0; i < set->used; ++i) { |
| 181 | item = &set->val.nodes[i]; |
| 182 | |
| 183 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 184 | case LYXP_NODE_NONE: |
| 185 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos); |
| 186 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 187 | case LYXP_NODE_ROOT: |
| 188 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos); |
| 189 | break; |
| 190 | case LYXP_NODE_ROOT_CONFIG: |
| 191 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos); |
| 192 | break; |
| 193 | case LYXP_NODE_ELEM: |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 194 | if ((item->node->schema->nodetype == LYS_LIST) && |
| 195 | (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 196 | 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] | 197 | item->node->schema->name, LYD_CANON_VALUE(lyd_child(item->node))); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 198 | } else if (((struct lyd_node_inner *)item->node)->schema->nodetype == LYS_LEAFLIST) { |
| 199 | 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] | 200 | item->node->schema->name, LYD_CANON_VALUE(item->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 201 | } else { |
| 202 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name); |
| 203 | } |
| 204 | break; |
| 205 | case LYXP_NODE_TEXT: |
| 206 | if (item->node->schema->nodetype & LYS_ANYDATA) { |
| 207 | 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] | 208 | item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 209 | } else { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 210 | 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] | 211 | } |
| 212 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 213 | case LYXP_NODE_META: |
| 214 | 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] | 215 | set->val.meta[i].meta->value); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 216 | break; |
| 217 | } |
| 218 | } |
| 219 | break; |
| 220 | |
| 221 | case LYXP_SET_SCNODE_SET: |
| 222 | LOGDBG(LY_LDGXPATH, "set SCNODE SET:"); |
| 223 | for (i = 0; i < set->used; ++i) { |
| 224 | sitem = &set->val.scnodes[i]; |
| 225 | |
| 226 | switch (sitem->type) { |
| 227 | case LYXP_NODE_ROOT: |
| 228 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx); |
| 229 | break; |
| 230 | case LYXP_NODE_ROOT_CONFIG: |
| 231 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx); |
| 232 | break; |
| 233 | case LYXP_NODE_ELEM: |
| 234 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name); |
| 235 | break; |
| 236 | default: |
| 237 | LOGINT(NULL); |
| 238 | break; |
| 239 | } |
| 240 | } |
| 241 | break; |
| 242 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 243 | case LYXP_SET_BOOLEAN: |
| 244 | LOGDBG(LY_LDGXPATH, "set BOOLEAN"); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 245 | LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false")); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 246 | break; |
| 247 | |
| 248 | case LYXP_SET_STRING: |
| 249 | LOGDBG(LY_LDGXPATH, "set STRING"); |
| 250 | LOGDBG(LY_LDGXPATH, "\t%s", set->val.str); |
| 251 | break; |
| 252 | |
| 253 | case LYXP_SET_NUMBER: |
| 254 | LOGDBG(LY_LDGXPATH, "set NUMBER"); |
| 255 | |
| 256 | if (isnan(set->val.num)) { |
| 257 | str = strdup("NaN"); |
| 258 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 259 | str = strdup("0"); |
| 260 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 261 | str = strdup("Infinity"); |
| 262 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 263 | str = strdup("-Infinity"); |
| 264 | } else if ((long long)set->val.num == set->val.num) { |
| 265 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 266 | str = NULL; |
| 267 | } |
| 268 | } else { |
| 269 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 270 | str = NULL; |
| 271 | } |
| 272 | } |
| 273 | LY_CHECK_ERR_RET(!str, LOGMEM(NULL), ); |
| 274 | |
| 275 | LOGDBG(LY_LDGXPATH, "\t%s", str); |
| 276 | free(str); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | #endif |
| 281 | |
| 282 | /** |
| 283 | * @brief Realloc the string \p str. |
| 284 | * |
| 285 | * @param[in] ctx libyang context for logging. |
| 286 | * @param[in] needed How much free space is required. |
| 287 | * @param[in,out] str Pointer to the string to use. |
| 288 | * @param[in,out] used Used bytes in \p str. |
| 289 | * @param[in,out] size Allocated bytes in \p str. |
| 290 | * @return LY_ERR |
| 291 | */ |
| 292 | static LY_ERR |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 293 | 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] | 294 | { |
| 295 | if (*size - *used < needed) { |
| 296 | do { |
| 297 | if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) { |
| 298 | LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX); |
| 299 | return LY_EINVAL; |
| 300 | } |
| 301 | *size += LYXP_STRING_CAST_SIZE_STEP; |
| 302 | } while (*size - *used < needed); |
| 303 | *str = ly_realloc(*str, *size * sizeof(char)); |
| 304 | LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM); |
| 305 | } |
| 306 | |
| 307 | return LY_SUCCESS; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * @brief Cast nodes recursively to one string @p str. |
| 312 | * |
| 313 | * @param[in] node Node to cast. |
| 314 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 315 | * @param[in] root_type Type of the XPath root. |
| 316 | * @param[in] indent Current indent. |
| 317 | * @param[in,out] str Resulting string. |
| 318 | * @param[in,out] used Used bytes in @p str. |
| 319 | * @param[in,out] size Allocated bytes in @p str. |
| 320 | * @return LY_ERR |
| 321 | */ |
| 322 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 323 | 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] | 324 | uint16_t *used, uint16_t *size) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 325 | { |
Radek Krejci | 7f769d7 | 2020-07-11 23:13:56 +0200 | [diff] [blame] | 326 | char *buf, *line, *ptr = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 327 | const char *value_str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 328 | const struct lyd_node *child; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 329 | struct lyd_node *tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 330 | struct lyd_node_any *any; |
| 331 | LY_ERR rc; |
| 332 | |
| 333 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) { |
| 334 | return LY_SUCCESS; |
| 335 | } |
| 336 | |
| 337 | if (fake_cont) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 338 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 339 | LY_CHECK_RET(rc); |
| 340 | strcpy(*str + (*used - 1), "\n"); |
| 341 | ++(*used); |
| 342 | |
| 343 | ++indent; |
| 344 | } |
| 345 | |
| 346 | switch (node->schema->nodetype) { |
| 347 | case LYS_CONTAINER: |
| 348 | case LYS_LIST: |
| 349 | case LYS_RPC: |
| 350 | case LYS_NOTIF: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 351 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 352 | LY_CHECK_RET(rc); |
| 353 | strcpy(*str + (*used - 1), "\n"); |
| 354 | ++(*used); |
| 355 | |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 356 | for (child = lyd_child(node); child; child = child->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 357 | rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size); |
| 358 | LY_CHECK_RET(rc); |
| 359 | } |
| 360 | |
| 361 | break; |
| 362 | |
| 363 | case LYS_LEAF: |
| 364 | case LYS_LEAFLIST: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 365 | value_str = LYD_CANON_VALUE(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 366 | |
| 367 | /* print indent */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 368 | 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] | 369 | memset(*str + (*used - 1), ' ', indent * 2); |
| 370 | *used += indent * 2; |
| 371 | |
| 372 | /* print value */ |
| 373 | if (*used == 1) { |
| 374 | sprintf(*str + (*used - 1), "%s", value_str); |
| 375 | *used += strlen(value_str); |
| 376 | } else { |
| 377 | sprintf(*str + (*used - 1), "%s\n", value_str); |
| 378 | *used += strlen(value_str) + 1; |
| 379 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 380 | |
| 381 | break; |
| 382 | |
| 383 | case LYS_ANYXML: |
| 384 | case LYS_ANYDATA: |
| 385 | any = (struct lyd_node_any *)node; |
| 386 | if (!(void *)any->value.tree) { |
| 387 | /* no content */ |
| 388 | buf = strdup(""); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 389 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 390 | } else { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 391 | struct ly_out *out; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 392 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 393 | if (any->value_type == LYD_ANYDATA_LYB) { |
| 394 | /* try to parse it into a data tree */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 395 | 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] | 396 | /* successfully parsed */ |
| 397 | free(any->value.mem); |
| 398 | any->value.tree = tree; |
| 399 | any->value_type = LYD_ANYDATA_DATATREE; |
| 400 | } |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 401 | /* 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] | 402 | } |
| 403 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 404 | switch (any->value_type) { |
| 405 | case LYD_ANYDATA_STRING: |
| 406 | case LYD_ANYDATA_XML: |
| 407 | case LYD_ANYDATA_JSON: |
| 408 | buf = strdup(any->value.json); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 409 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 410 | break; |
| 411 | case LYD_ANYDATA_DATATREE: |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 412 | LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 413 | rc = lyd_print_all(out, any->value.tree, LYD_XML, 0); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 414 | ly_out_free(out, NULL, 0); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 415 | LY_CHECK_RET(rc < 0, -rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 416 | break; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 417 | case LYD_ANYDATA_LYB: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 418 | LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string."); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 419 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | |
| 423 | line = strtok_r(buf, "\n", &ptr); |
| 424 | do { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 425 | 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] | 426 | if (rc != LY_SUCCESS) { |
| 427 | free(buf); |
| 428 | return rc; |
| 429 | } |
| 430 | memset(*str + (*used - 1), ' ', indent * 2); |
| 431 | *used += indent * 2; |
| 432 | |
| 433 | strcpy(*str + (*used - 1), line); |
| 434 | *used += strlen(line); |
| 435 | |
| 436 | strcpy(*str + (*used - 1), "\n"); |
| 437 | *used += 1; |
| 438 | } while ((line = strtok_r(NULL, "\n", &ptr))); |
| 439 | |
| 440 | free(buf); |
| 441 | break; |
| 442 | |
| 443 | default: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 444 | LOGINT_RET(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | if (fake_cont) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 448 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 449 | LY_CHECK_RET(rc); |
| 450 | strcpy(*str + (*used - 1), "\n"); |
| 451 | ++(*used); |
| 452 | |
| 453 | --indent; |
| 454 | } |
| 455 | |
| 456 | return LY_SUCCESS; |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * @brief Cast an element into a string. |
| 461 | * |
| 462 | * @param[in] node Node to cast. |
| 463 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 464 | * @param[in] root_type Type of the XPath root. |
| 465 | * @param[out] str Element cast to dynamically-allocated string. |
| 466 | * @return LY_ERR |
| 467 | */ |
| 468 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 469 | 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] | 470 | { |
| 471 | uint16_t used, size; |
| 472 | LY_ERR rc; |
| 473 | |
| 474 | *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char)); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 475 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 476 | (*str)[0] = '\0'; |
| 477 | used = 1; |
| 478 | size = LYXP_STRING_CAST_SIZE_START; |
| 479 | |
| 480 | rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size); |
| 481 | if (rc != LY_SUCCESS) { |
| 482 | free(*str); |
| 483 | return rc; |
| 484 | } |
| 485 | |
| 486 | if (size > used) { |
| 487 | *str = ly_realloc(*str, used * sizeof(char)); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 488 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 489 | } |
| 490 | return LY_SUCCESS; |
| 491 | } |
| 492 | |
| 493 | /** |
| 494 | * @brief Cast a LYXP_SET_NODE_SET set into a string. |
| 495 | * Context position aware. |
| 496 | * |
| 497 | * @param[in] set Set to cast. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 498 | * @param[out] str Cast dynamically-allocated string. |
| 499 | * @return LY_ERR |
| 500 | */ |
| 501 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 502 | cast_node_set_to_string(struct lyxp_set *set, char **str) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 503 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 504 | switch (set->val.nodes[0].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 505 | case LYXP_NODE_NONE: |
| 506 | /* invalid */ |
| 507 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 508 | case LYXP_NODE_ROOT: |
| 509 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 510 | 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] | 511 | case LYXP_NODE_ELEM: |
| 512 | case LYXP_NODE_TEXT: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 513 | 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] | 514 | case LYXP_NODE_META: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 515 | *str = strdup(set->val.meta[0].meta->value.canonical); |
| 516 | if (!*str) { |
| 517 | LOGMEM_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 518 | } |
| 519 | return LY_SUCCESS; |
| 520 | } |
| 521 | |
| 522 | LOGINT_RET(set->ctx); |
| 523 | } |
| 524 | |
| 525 | /** |
| 526 | * @brief Cast a string into an XPath number. |
| 527 | * |
| 528 | * @param[in] str String to use. |
| 529 | * @return Cast number. |
| 530 | */ |
| 531 | static long double |
| 532 | cast_string_to_number(const char *str) |
| 533 | { |
| 534 | long double num; |
| 535 | char *ptr; |
| 536 | |
| 537 | errno = 0; |
| 538 | num = strtold(str, &ptr); |
| 539 | if (errno || *ptr) { |
| 540 | num = NAN; |
| 541 | } |
| 542 | return num; |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * @brief Callback for checking value equality. |
| 547 | * |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 548 | * Implementation of ::values_equal_cb. |
| 549 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 550 | * @param[in] val1_p First value. |
| 551 | * @param[in] val2_p Second value. |
| 552 | * @param[in] mod Whether hash table is being modified. |
| 553 | * @param[in] cb_data Callback data. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 554 | * @return Boolean value whether values are equal or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 555 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 556 | static ly_bool |
| 557 | 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] | 558 | { |
| 559 | struct lyxp_set_hash_node *val1, *val2; |
| 560 | |
| 561 | val1 = (struct lyxp_set_hash_node *)val1_p; |
| 562 | val2 = (struct lyxp_set_hash_node *)val2_p; |
| 563 | |
| 564 | if ((val1->node == val2->node) && (val1->type == val2->type)) { |
| 565 | return 1; |
| 566 | } |
| 567 | |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * @brief Insert node and its hash into set. |
| 573 | * |
| 574 | * @param[in] set et to insert to. |
| 575 | * @param[in] node Node with hash. |
| 576 | * @param[in] type Node type. |
| 577 | */ |
| 578 | static void |
| 579 | set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 580 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 581 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 582 | uint32_t i, hash; |
| 583 | struct lyxp_set_hash_node hnode; |
| 584 | |
| 585 | if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) { |
| 586 | /* create hash table and add all the nodes */ |
| 587 | set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1); |
| 588 | for (i = 0; i < set->used; ++i) { |
| 589 | hnode.node = set->val.nodes[i].node; |
| 590 | hnode.type = set->val.nodes[i].type; |
| 591 | |
| 592 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 593 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 594 | hash = dict_hash_multi(hash, NULL, 0); |
| 595 | |
| 596 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 597 | assert(!r); |
| 598 | (void)r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 599 | |
Michal Vasko | 9d6befd | 2019-12-11 14:56:56 +0100 | [diff] [blame] | 600 | if (hnode.node == node) { |
| 601 | /* it was just added, do not add it twice */ |
| 602 | node = NULL; |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 608 | /* add the new node into hash table */ |
| 609 | hnode.node = node; |
| 610 | hnode.type = type; |
| 611 | |
| 612 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 613 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 614 | hash = dict_hash_multi(hash, NULL, 0); |
| 615 | |
| 616 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 617 | assert(!r); |
| 618 | (void)r; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | /** |
| 623 | * @brief Remove node and its hash from set. |
| 624 | * |
| 625 | * @param[in] set Set to remove from. |
| 626 | * @param[in] node Node to remove. |
| 627 | * @param[in] type Node type. |
| 628 | */ |
| 629 | static void |
| 630 | set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 631 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 632 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 633 | struct lyxp_set_hash_node hnode; |
| 634 | uint32_t hash; |
| 635 | |
| 636 | if (set->ht) { |
| 637 | hnode.node = node; |
| 638 | hnode.type = type; |
| 639 | |
| 640 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 641 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 642 | hash = dict_hash_multi(hash, NULL, 0); |
| 643 | |
| 644 | r = lyht_remove(set->ht, &hnode, hash); |
| 645 | assert(!r); |
| 646 | (void)r; |
| 647 | |
| 648 | if (!set->ht->used) { |
| 649 | lyht_free(set->ht); |
| 650 | set->ht = NULL; |
| 651 | } |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | /** |
| 656 | * @brief Check whether node is in set based on its hash. |
| 657 | * |
| 658 | * @param[in] set Set to search in. |
| 659 | * @param[in] node Node to search for. |
| 660 | * @param[in] type Node type. |
| 661 | * @param[in] skip_idx Index in @p set to skip. |
| 662 | * @return LY_ERR |
| 663 | */ |
| 664 | static LY_ERR |
| 665 | set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx) |
| 666 | { |
| 667 | struct lyxp_set_hash_node hnode, *match_p; |
| 668 | uint32_t hash; |
| 669 | |
| 670 | hnode.node = node; |
| 671 | hnode.type = type; |
| 672 | |
| 673 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 674 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 675 | hash = dict_hash_multi(hash, NULL, 0); |
| 676 | |
| 677 | if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) { |
| 678 | if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) { |
| 679 | /* we found it on the index that should be skipped, find another */ |
| 680 | hnode = *match_p; |
| 681 | if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) { |
| 682 | /* none other found */ |
| 683 | return LY_SUCCESS; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | return LY_EEXIST; |
| 688 | } |
| 689 | |
| 690 | /* not found */ |
| 691 | return LY_SUCCESS; |
| 692 | } |
| 693 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 694 | void |
| 695 | lyxp_set_free_content(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 696 | { |
| 697 | if (!set) { |
| 698 | return; |
| 699 | } |
| 700 | |
| 701 | if (set->type == LYXP_SET_NODE_SET) { |
| 702 | free(set->val.nodes); |
| 703 | lyht_free(set->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 704 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 705 | free(set->val.scnodes); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 706 | lyht_free(set->ht); |
| 707 | } else { |
| 708 | if (set->type == LYXP_SET_STRING) { |
| 709 | free(set->val.str); |
| 710 | } |
| 711 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 712 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 713 | |
| 714 | set->val.nodes = NULL; |
| 715 | set->used = 0; |
| 716 | set->size = 0; |
| 717 | set->ht = NULL; |
| 718 | set->ctx_pos = 0; |
| 719 | set->ctx_pos = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 720 | } |
| 721 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 722 | /** |
| 723 | * @brief Free dynamically-allocated set. |
| 724 | * |
| 725 | * @param[in] set Set to free. |
| 726 | */ |
| 727 | static void |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 728 | lyxp_set_free(struct lyxp_set *set) |
| 729 | { |
| 730 | if (!set) { |
| 731 | return; |
| 732 | } |
| 733 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 734 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 735 | free(set); |
| 736 | } |
| 737 | |
| 738 | /** |
| 739 | * @brief Initialize set context. |
| 740 | * |
| 741 | * @param[in] new Set to initialize. |
| 742 | * @param[in] set Arbitrary initialized set. |
| 743 | */ |
| 744 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 745 | set_init(struct lyxp_set *new, const struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 746 | { |
| 747 | memset(new, 0, sizeof *new); |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 748 | if (set) { |
| 749 | new->ctx = set->ctx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 750 | new->cur_node = set->cur_node; |
Michal Vasko | 588112f | 2019-12-10 14:51:53 +0100 | [diff] [blame] | 751 | new->root_type = set->root_type; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 752 | new->context_op = set->context_op; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 753 | new->tree = set->tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 754 | new->cur_mod = set->cur_mod; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 755 | new->format = set->format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 756 | new->prefix_data = set->prefix_data; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 757 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | /** |
| 761 | * @brief Create a deep copy of a set. |
| 762 | * |
| 763 | * @param[in] set Set to copy. |
| 764 | * @return Copy of @p set. |
| 765 | */ |
| 766 | static struct lyxp_set * |
| 767 | set_copy(struct lyxp_set *set) |
| 768 | { |
| 769 | struct lyxp_set *ret; |
| 770 | uint16_t i; |
| 771 | |
| 772 | if (!set) { |
| 773 | return NULL; |
| 774 | } |
| 775 | |
| 776 | ret = malloc(sizeof *ret); |
| 777 | LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL); |
| 778 | set_init(ret, set); |
| 779 | |
| 780 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 781 | ret->type = set->type; |
| 782 | |
| 783 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 784 | 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] | 785 | uint32_t idx; |
| 786 | LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx), |
| 787 | lyxp_set_free(ret), NULL); |
Michal Vasko | 3f27c52 | 2020-01-06 08:37:49 +0100 | [diff] [blame] | 788 | /* coverity seems to think scnodes can be NULL */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 789 | if (!ret->val.scnodes) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 790 | lyxp_set_free(ret); |
| 791 | return NULL; |
| 792 | } |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 793 | ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 794 | } |
| 795 | } |
| 796 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 797 | ret->type = set->type; |
| 798 | ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes); |
| 799 | LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL); |
| 800 | memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes); |
| 801 | |
| 802 | ret->used = ret->size = set->used; |
| 803 | ret->ctx_pos = set->ctx_pos; |
| 804 | ret->ctx_size = set->ctx_size; |
| 805 | ret->ht = lyht_dup(set->ht); |
| 806 | } else { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 807 | memcpy(ret, set, sizeof *ret); |
| 808 | if (set->type == LYXP_SET_STRING) { |
| 809 | ret->val.str = strdup(set->val.str); |
| 810 | LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL); |
| 811 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | return ret; |
| 815 | } |
| 816 | |
| 817 | /** |
| 818 | * @brief Fill XPath set with a string. Any current data are disposed of. |
| 819 | * |
| 820 | * @param[in] set Set to fill. |
| 821 | * @param[in] string String to fill into \p set. |
| 822 | * @param[in] str_len Length of \p string. 0 is a valid value! |
| 823 | */ |
| 824 | static void |
| 825 | set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len) |
| 826 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 827 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 828 | |
| 829 | set->type = LYXP_SET_STRING; |
| 830 | if ((str_len == 0) && (string[0] != '\0')) { |
| 831 | string = ""; |
| 832 | } |
| 833 | set->val.str = strndup(string, str_len); |
| 834 | } |
| 835 | |
| 836 | /** |
| 837 | * @brief Fill XPath set with a number. Any current data are disposed of. |
| 838 | * |
| 839 | * @param[in] set Set to fill. |
| 840 | * @param[in] number Number to fill into \p set. |
| 841 | */ |
| 842 | static void |
| 843 | set_fill_number(struct lyxp_set *set, long double number) |
| 844 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 845 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 846 | |
| 847 | set->type = LYXP_SET_NUMBER; |
| 848 | set->val.num = number; |
| 849 | } |
| 850 | |
| 851 | /** |
| 852 | * @brief Fill XPath set with a boolean. Any current data are disposed of. |
| 853 | * |
| 854 | * @param[in] set Set to fill. |
| 855 | * @param[in] boolean Boolean to fill into \p set. |
| 856 | */ |
| 857 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 858 | set_fill_boolean(struct lyxp_set *set, ly_bool boolean) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 859 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 860 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 861 | |
| 862 | set->type = LYXP_SET_BOOLEAN; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 863 | set->val.bln = boolean; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | /** |
| 867 | * @brief Fill XPath set with the value from another set (deep assign). |
| 868 | * Any current data are disposed of. |
| 869 | * |
| 870 | * @param[in] trg Set to fill. |
| 871 | * @param[in] src Source set to copy into \p trg. |
| 872 | */ |
| 873 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 874 | set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 875 | { |
| 876 | if (!trg || !src) { |
| 877 | return; |
| 878 | } |
| 879 | |
| 880 | if (trg->type == LYXP_SET_NODE_SET) { |
| 881 | free(trg->val.nodes); |
| 882 | } else if (trg->type == LYXP_SET_STRING) { |
| 883 | free(trg->val.str); |
| 884 | } |
| 885 | set_init(trg, src); |
| 886 | |
| 887 | if (src->type == LYXP_SET_SCNODE_SET) { |
| 888 | trg->type = LYXP_SET_SCNODE_SET; |
| 889 | trg->used = src->used; |
| 890 | trg->size = src->used; |
| 891 | |
| 892 | trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes); |
| 893 | LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 894 | memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes); |
| 895 | } else if (src->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 896 | set_fill_boolean(trg, src->val.bln); |
Michal Vasko | 44f3d2c | 2020-08-24 09:49:38 +0200 | [diff] [blame] | 897 | } else if (src->type == LYXP_SET_NUMBER) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 898 | set_fill_number(trg, src->val.num); |
| 899 | } else if (src->type == LYXP_SET_STRING) { |
| 900 | set_fill_string(trg, src->val.str, strlen(src->val.str)); |
| 901 | } else { |
| 902 | if (trg->type == LYXP_SET_NODE_SET) { |
| 903 | free(trg->val.nodes); |
| 904 | } else if (trg->type == LYXP_SET_STRING) { |
| 905 | free(trg->val.str); |
| 906 | } |
| 907 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 908 | assert(src->type == LYXP_SET_NODE_SET); |
| 909 | |
| 910 | trg->type = LYXP_SET_NODE_SET; |
| 911 | trg->used = src->used; |
| 912 | trg->size = src->used; |
| 913 | trg->ctx_pos = src->ctx_pos; |
| 914 | trg->ctx_size = src->ctx_size; |
| 915 | |
| 916 | trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes); |
| 917 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 918 | memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes); |
| 919 | if (src->ht) { |
| 920 | trg->ht = lyht_dup(src->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 921 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 922 | trg->ht = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 923 | } |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | /** |
| 928 | * @brief Clear context of all schema nodes. |
| 929 | * |
| 930 | * @param[in] set Set to clear. |
| 931 | */ |
| 932 | static void |
| 933 | set_scnode_clear_ctx(struct lyxp_set *set) |
| 934 | { |
| 935 | uint32_t i; |
| 936 | |
| 937 | for (i = 0; i < set->used; ++i) { |
| 938 | if (set->val.scnodes[i].in_ctx == 1) { |
| 939 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 940 | } else if (set->val.scnodes[i].in_ctx == -2) { |
| 941 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 942 | } |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | /** |
| 947 | * @brief Remove a node from a set. Removing last node changes |
| 948 | * set into LYXP_SET_EMPTY. Context position aware. |
| 949 | * |
| 950 | * @param[in] set Set to use. |
| 951 | * @param[in] idx Index from @p set of the node to be removed. |
| 952 | */ |
| 953 | static void |
| 954 | set_remove_node(struct lyxp_set *set, uint32_t idx) |
| 955 | { |
| 956 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 957 | assert(idx < set->used); |
| 958 | |
| 959 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 960 | |
| 961 | --set->used; |
| 962 | if (set->used) { |
| 963 | memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], |
| 964 | (set->used - idx) * sizeof *set->val.nodes); |
| 965 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 966 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 967 | } |
| 968 | } |
| 969 | |
| 970 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 971 | * @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] | 972 | * |
| 973 | * @param[in] set Set to use. |
| 974 | * @param[in] idx Index from @p set of the node to be removed. |
| 975 | */ |
| 976 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 977 | set_remove_node_none(struct lyxp_set *set, uint32_t idx) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 978 | { |
| 979 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 980 | assert(idx < set->used); |
| 981 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 982 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 983 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 984 | } |
| 985 | set->val.nodes[idx].type = LYXP_NODE_NONE; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 989 | * @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] | 990 | * set into LYXP_SET_EMPTY. Context position aware. |
| 991 | * |
| 992 | * @param[in] set Set to consolidate. |
| 993 | */ |
| 994 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 995 | set_remove_nodes_none(struct lyxp_set *set) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 996 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 997 | uint16_t i, orig_used, end = 0; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 998 | int32_t start; |
| 999 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1000 | assert(set); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1001 | |
| 1002 | orig_used = set->used; |
| 1003 | set->used = 0; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1004 | for (i = 0; i < orig_used; ) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1005 | start = -1; |
| 1006 | do { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1007 | if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1008 | start = i; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1009 | } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1010 | end = i; |
| 1011 | ++i; |
| 1012 | break; |
| 1013 | } |
| 1014 | |
| 1015 | ++i; |
| 1016 | if (i == orig_used) { |
| 1017 | end = i; |
| 1018 | } |
| 1019 | } while (i < orig_used); |
| 1020 | |
| 1021 | if (start > -1) { |
| 1022 | /* move the whole chunk of valid nodes together */ |
| 1023 | if (set->used != (unsigned)start) { |
| 1024 | memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes); |
| 1025 | } |
| 1026 | set->used += end - start; |
| 1027 | } |
| 1028 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1032 | * @brief Check for duplicates in a node set. |
| 1033 | * |
| 1034 | * @param[in] set Set to check. |
| 1035 | * @param[in] node Node to look for in @p set. |
| 1036 | * @param[in] node_type Type of @p node. |
| 1037 | * @param[in] skip_idx Index from @p set to skip. |
| 1038 | * @return LY_ERR |
| 1039 | */ |
| 1040 | static LY_ERR |
| 1041 | set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx) |
| 1042 | { |
| 1043 | uint32_t i; |
| 1044 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1045 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1046 | return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx); |
| 1047 | } |
| 1048 | |
| 1049 | for (i = 0; i < set->used; ++i) { |
| 1050 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1051 | continue; |
| 1052 | } |
| 1053 | |
| 1054 | if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) { |
| 1055 | return LY_EEXIST; |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | return LY_SUCCESS; |
| 1060 | } |
| 1061 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1062 | ly_bool |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1063 | lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx, |
| 1064 | uint32_t *index_p) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1065 | { |
| 1066 | uint32_t i; |
| 1067 | |
| 1068 | for (i = 0; i < set->used; ++i) { |
| 1069 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1070 | continue; |
| 1071 | } |
| 1072 | |
| 1073 | 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] | 1074 | if (index_p) { |
| 1075 | *index_p = i; |
| 1076 | } |
| 1077 | return 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1078 | } |
| 1079 | } |
| 1080 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1081 | return 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1082 | } |
| 1083 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1084 | void |
| 1085 | lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1086 | { |
| 1087 | uint32_t orig_used, i, j; |
| 1088 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1089 | 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] | 1090 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1091 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1092 | return; |
| 1093 | } |
| 1094 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1095 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1096 | memcpy(set1, set2, sizeof *set1); |
| 1097 | return; |
| 1098 | } |
| 1099 | |
| 1100 | if (set1->used + set2->used > set1->size) { |
| 1101 | set1->size = set1->used + set2->used; |
| 1102 | set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes); |
| 1103 | LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), ); |
| 1104 | } |
| 1105 | |
| 1106 | orig_used = set1->used; |
| 1107 | |
| 1108 | for (i = 0; i < set2->used; ++i) { |
| 1109 | for (j = 0; j < orig_used; ++j) { |
| 1110 | /* detect duplicities */ |
| 1111 | if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) { |
| 1112 | break; |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | if (j == orig_used) { |
| 1117 | memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes); |
| 1118 | ++set1->used; |
| 1119 | } |
| 1120 | } |
| 1121 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1122 | lyxp_set_free_content(set2); |
| 1123 | set2->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | /** |
| 1127 | * @brief Insert a node into a set. Context position aware. |
| 1128 | * |
| 1129 | * @param[in] set Set to use. |
| 1130 | * @param[in] node Node to insert to @p set. |
| 1131 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1132 | * @param[in] node_type Node type of @p node. |
| 1133 | * @param[in] idx Index in @p set to insert into. |
| 1134 | */ |
| 1135 | static void |
| 1136 | set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1137 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1138 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1139 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1140 | if (!set->size) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1141 | /* first item */ |
| 1142 | if (idx) { |
| 1143 | /* no real harm done, but it is a bug */ |
| 1144 | LOGINT(set->ctx); |
| 1145 | idx = 0; |
| 1146 | } |
| 1147 | set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes); |
| 1148 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1149 | set->type = LYXP_SET_NODE_SET; |
| 1150 | set->used = 0; |
| 1151 | set->size = LYXP_SET_SIZE_START; |
| 1152 | set->ctx_pos = 1; |
| 1153 | set->ctx_size = 1; |
| 1154 | set->ht = NULL; |
| 1155 | } else { |
| 1156 | /* not an empty set */ |
| 1157 | if (set->used == set->size) { |
| 1158 | |
| 1159 | /* set is full */ |
| 1160 | set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes); |
| 1161 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1162 | set->size += LYXP_SET_SIZE_STEP; |
| 1163 | } |
| 1164 | |
| 1165 | if (idx > set->used) { |
| 1166 | LOGINT(set->ctx); |
| 1167 | idx = set->used; |
| 1168 | } |
| 1169 | |
| 1170 | /* make space for the new node */ |
| 1171 | if (idx < set->used) { |
| 1172 | memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes); |
| 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | /* finally assign the value */ |
| 1177 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1178 | set->val.nodes[idx].type = node_type; |
| 1179 | set->val.nodes[idx].pos = pos; |
| 1180 | ++set->used; |
| 1181 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1182 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1183 | set_insert_node_hash(set, (struct lyd_node *)node, node_type); |
| 1184 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1185 | } |
| 1186 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1187 | LY_ERR |
| 1188 | 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] | 1189 | { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1190 | uint32_t index; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1191 | |
| 1192 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1193 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1194 | if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) { |
| 1195 | set->val.scnodes[index].in_ctx = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1196 | } else { |
| 1197 | if (set->used == set->size) { |
| 1198 | 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] | 1199 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1200 | set->size += LYXP_SET_SIZE_STEP; |
| 1201 | } |
| 1202 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1203 | index = set->used; |
| 1204 | set->val.scnodes[index].scnode = (struct lysc_node *)node; |
| 1205 | set->val.scnodes[index].type = node_type; |
| 1206 | set->val.scnodes[index].in_ctx = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1207 | ++set->used; |
| 1208 | } |
| 1209 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1210 | if (index_p) { |
| 1211 | *index_p = index; |
| 1212 | } |
| 1213 | |
| 1214 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | /** |
| 1218 | * @brief Replace a node in a set with another. Context position aware. |
| 1219 | * |
| 1220 | * @param[in] set Set to use. |
| 1221 | * @param[in] node Node to insert to @p set. |
| 1222 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1223 | * @param[in] node_type Node type of @p node. |
| 1224 | * @param[in] idx Index in @p set of the node to replace. |
| 1225 | */ |
| 1226 | static void |
| 1227 | set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1228 | { |
| 1229 | assert(set && (idx < set->used)); |
| 1230 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1231 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1232 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1233 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1234 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1235 | set->val.nodes[idx].type = node_type; |
| 1236 | set->val.nodes[idx].pos = pos; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1237 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1238 | set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1239 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | /** |
| 1243 | * @brief Set all nodes with ctx 1 to a new unique context value. |
| 1244 | * |
| 1245 | * @param[in] set Set to modify. |
| 1246 | * @return New context value. |
| 1247 | */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1248 | static int32_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1249 | set_scnode_new_in_ctx(struct lyxp_set *set) |
| 1250 | { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1251 | uint32_t i; |
| 1252 | int32_t ret_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1253 | |
| 1254 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1255 | |
| 1256 | ret_ctx = 3; |
| 1257 | retry: |
| 1258 | for (i = 0; i < set->used; ++i) { |
| 1259 | if (set->val.scnodes[i].in_ctx >= ret_ctx) { |
| 1260 | ret_ctx = set->val.scnodes[i].in_ctx + 1; |
| 1261 | goto retry; |
| 1262 | } |
| 1263 | } |
| 1264 | for (i = 0; i < set->used; ++i) { |
| 1265 | if (set->val.scnodes[i].in_ctx == 1) { |
| 1266 | set->val.scnodes[i].in_ctx = ret_ctx; |
| 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | return ret_ctx; |
| 1271 | } |
| 1272 | |
| 1273 | /** |
| 1274 | * @brief Get unique @p node position in the data. |
| 1275 | * |
| 1276 | * @param[in] node Node to find. |
| 1277 | * @param[in] node_type Node type of @p node. |
| 1278 | * @param[in] root Root node. |
| 1279 | * @param[in] root_type Type of the XPath @p root node. |
| 1280 | * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally |
| 1281 | * be used to increase efficiency and start the DFS from this node. |
| 1282 | * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set. |
| 1283 | * @return Node position. |
| 1284 | */ |
| 1285 | static uint32_t |
| 1286 | get_node_pos(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyd_node *root, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1287 | 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] | 1288 | { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1289 | const struct lyd_node *elem = NULL, *top_sibling; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1290 | uint32_t pos = 1; |
| 1291 | |
| 1292 | assert(prev && prev_pos && !root->prev->next); |
| 1293 | |
| 1294 | if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 1295 | return 0; |
| 1296 | } |
| 1297 | |
| 1298 | if (*prev) { |
| 1299 | /* start from the previous element instead from the root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1300 | pos = *prev_pos; |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1301 | 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] | 1302 | goto dfs_search; |
| 1303 | } |
| 1304 | |
| 1305 | for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1306 | LYD_TREE_DFS_BEGIN(top_sibling, elem) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1307 | dfs_search: |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1308 | if (*prev && !elem) { |
| 1309 | /* resume previous DFS */ |
| 1310 | elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev; |
| 1311 | LYD_TREE_DFS_continue = 0; |
| 1312 | } |
| 1313 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1314 | 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] | 1315 | /* skip */ |
| 1316 | LYD_TREE_DFS_continue = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1317 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1318 | if (elem == node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1319 | break; |
| 1320 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1321 | ++pos; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1322 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1323 | |
| 1324 | LYD_TREE_DFS_END(top_sibling, elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | /* node found */ |
| 1328 | if (elem) { |
| 1329 | break; |
| 1330 | } |
| 1331 | } |
| 1332 | |
| 1333 | if (!elem) { |
| 1334 | if (!(*prev)) { |
| 1335 | /* we went from root and failed to find it, cannot be */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1336 | LOGINT(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1337 | return 0; |
| 1338 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1339 | /* start the search again from the beginning */ |
| 1340 | *prev = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1341 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1342 | top_sibling = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1343 | pos = 1; |
| 1344 | goto dfs_search; |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | /* remember the last found node for next time */ |
| 1349 | *prev = node; |
| 1350 | *prev_pos = pos; |
| 1351 | |
| 1352 | return pos; |
| 1353 | } |
| 1354 | |
| 1355 | /** |
| 1356 | * @brief Assign (fill) missing node positions. |
| 1357 | * |
| 1358 | * @param[in] set Set to fill positions in. |
| 1359 | * @param[in] root Context root node. |
| 1360 | * @param[in] root_type Context root type. |
| 1361 | * @return LY_ERR |
| 1362 | */ |
| 1363 | static LY_ERR |
| 1364 | set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type) |
| 1365 | { |
| 1366 | const struct lyd_node *prev = NULL, *tmp_node; |
| 1367 | uint32_t i, tmp_pos = 0; |
| 1368 | |
| 1369 | for (i = 0; i < set->used; ++i) { |
| 1370 | if (!set->val.nodes[i].pos) { |
| 1371 | tmp_node = NULL; |
| 1372 | switch (set->val.nodes[i].type) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1373 | case LYXP_NODE_META: |
| 1374 | tmp_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1375 | if (!tmp_node) { |
| 1376 | LOGINT_RET(root->schema->module->ctx); |
| 1377 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1378 | /* fallthrough */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1379 | case LYXP_NODE_ELEM: |
| 1380 | case LYXP_NODE_TEXT: |
| 1381 | if (!tmp_node) { |
| 1382 | tmp_node = set->val.nodes[i].node; |
| 1383 | } |
| 1384 | set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos); |
| 1385 | break; |
| 1386 | default: |
| 1387 | /* all roots have position 0 */ |
| 1388 | break; |
| 1389 | } |
| 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | return LY_SUCCESS; |
| 1394 | } |
| 1395 | |
| 1396 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1397 | * @brief Get unique @p meta position in the parent metadata. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1398 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1399 | * @param[in] meta Metadata to use. |
| 1400 | * @return Metadata position. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1401 | */ |
| 1402 | static uint16_t |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1403 | get_meta_pos(struct lyd_meta *meta) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1404 | { |
| 1405 | uint16_t pos = 0; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1406 | struct lyd_meta *meta2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1407 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1408 | for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1409 | ++pos; |
| 1410 | } |
| 1411 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1412 | assert(meta2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1413 | return pos; |
| 1414 | } |
| 1415 | |
| 1416 | /** |
| 1417 | * @brief Compare 2 nodes in respect to XPath document order. |
| 1418 | * |
| 1419 | * @param[in] item1 1st node. |
| 1420 | * @param[in] item2 2nd node. |
| 1421 | * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1. |
| 1422 | */ |
| 1423 | static int |
| 1424 | set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2) |
| 1425 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1426 | uint32_t meta_pos1 = 0, meta_pos2 = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1427 | |
| 1428 | if (item1->pos < item2->pos) { |
| 1429 | return -1; |
| 1430 | } |
| 1431 | |
| 1432 | if (item1->pos > item2->pos) { |
| 1433 | return 1; |
| 1434 | } |
| 1435 | |
| 1436 | /* node positions are equal, the fun case */ |
| 1437 | |
| 1438 | /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */ |
| 1439 | /* special case since text nodes are actually saved as their parents */ |
| 1440 | if ((item1->node == item2->node) && (item1->type != item2->type)) { |
| 1441 | if (item1->type == LYXP_NODE_ELEM) { |
| 1442 | assert(item2->type == LYXP_NODE_TEXT); |
| 1443 | return -1; |
| 1444 | } else { |
| 1445 | assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM)); |
| 1446 | return 1; |
| 1447 | } |
| 1448 | } |
| 1449 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1450 | /* we need meta positions now */ |
| 1451 | if (item1->type == LYXP_NODE_META) { |
| 1452 | meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1453 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1454 | if (item2->type == LYXP_NODE_META) { |
| 1455 | meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1456 | } |
| 1457 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1458 | /* 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] | 1459 | /* check for duplicates */ |
| 1460 | if (item1->node == item2->node) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1461 | 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] | 1462 | return 0; |
| 1463 | } |
| 1464 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1465 | /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1466 | /* elem is always first, 2nd node is after it */ |
| 1467 | if (item1->type == LYXP_NODE_ELEM) { |
| 1468 | assert(item2->type != LYXP_NODE_ELEM); |
| 1469 | return -1; |
| 1470 | } |
| 1471 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1472 | /* 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] | 1473 | /* 2nd is before 1st */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1474 | if (((item1->type == LYXP_NODE_TEXT) && |
| 1475 | ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) || |
| 1476 | ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) || |
| 1477 | (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) && |
| 1478 | (meta_pos1 > meta_pos2))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1479 | return 1; |
| 1480 | } |
| 1481 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1482 | /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1483 | /* 2nd is after 1st */ |
| 1484 | return -1; |
| 1485 | } |
| 1486 | |
| 1487 | /** |
| 1488 | * @brief Set cast for comparisons. |
| 1489 | * |
| 1490 | * @param[in] trg Target set to cast source into. |
| 1491 | * @param[in] src Source set. |
| 1492 | * @param[in] type Target set type. |
| 1493 | * @param[in] src_idx Source set node index. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1494 | * @return LY_ERR |
| 1495 | */ |
| 1496 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1497 | 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] | 1498 | { |
| 1499 | assert(src->type == LYXP_SET_NODE_SET); |
| 1500 | |
| 1501 | set_init(trg, src); |
| 1502 | |
| 1503 | /* insert node into target set */ |
| 1504 | set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0); |
| 1505 | |
| 1506 | /* cast target set appropriately */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1507 | return lyxp_set_cast(trg, type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1508 | } |
| 1509 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1510 | /** |
| 1511 | * @brief Set content canonization for comparisons. |
| 1512 | * |
| 1513 | * @param[in] trg Target set to put the canononized source into. |
| 1514 | * @param[in] src Source set. |
| 1515 | * @param[in] xp_node Source XPath node/meta to use for canonization. |
| 1516 | * @return LY_ERR |
| 1517 | */ |
| 1518 | static LY_ERR |
| 1519 | set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node) |
| 1520 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1521 | const struct lysc_type *type = NULL; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1522 | struct lyd_value val; |
| 1523 | struct ly_err_item *err = NULL; |
| 1524 | char *str, *ptr; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1525 | ly_bool dynamic; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1526 | LY_ERR rc; |
| 1527 | |
| 1528 | /* is there anything to canonize even? */ |
| 1529 | if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) { |
| 1530 | /* do we have a type to use for canonization? */ |
| 1531 | if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) { |
| 1532 | type = ((struct lyd_node_term *)xp_node->node)->value.realtype; |
| 1533 | } else if (xp_node->type == LYXP_NODE_META) { |
| 1534 | type = ((struct lyd_meta *)xp_node->node)->value.realtype; |
| 1535 | } |
| 1536 | } |
| 1537 | if (!type) { |
| 1538 | goto fill; |
| 1539 | } |
| 1540 | |
| 1541 | if (src->type == LYXP_SET_NUMBER) { |
| 1542 | /* canonize number */ |
| 1543 | if (asprintf(&str, "%Lf", src->val.num) == -1) { |
| 1544 | LOGMEM(src->ctx); |
| 1545 | return LY_EMEM; |
| 1546 | } |
| 1547 | } else { |
| 1548 | /* canonize string */ |
| 1549 | str = strdup(src->val.str); |
| 1550 | } |
| 1551 | |
| 1552 | /* ignore errors, the value may not satisfy schema constraints */ |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1553 | rc = type->plugin->store(src->ctx, type, str, strlen(str), LY_TYPE_OPTS_DYNAMIC, LY_PREF_JSON, NULL, LYD_HINT_DATA, |
| 1554 | xp_node->node->schema, &val, &err); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1555 | ly_err_free(err); |
| 1556 | if (rc) { |
| 1557 | /* invalid value */ |
| 1558 | free(str); |
| 1559 | goto fill; |
| 1560 | } |
| 1561 | |
| 1562 | /* storing successful, now print the canonical value */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 1563 | str = (char *)type->plugin->print(&val, LY_PREF_JSON, NULL, &dynamic); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1564 | |
| 1565 | /* use the canonized value */ |
| 1566 | set_init(trg, src); |
| 1567 | trg->type = src->type; |
| 1568 | if (src->type == LYXP_SET_NUMBER) { |
| 1569 | trg->val.num = strtold(str, &ptr); |
| 1570 | if (dynamic) { |
| 1571 | free(str); |
| 1572 | } |
| 1573 | LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT); |
| 1574 | } else { |
| 1575 | trg->val.str = (dynamic ? str : strdup(str)); |
| 1576 | } |
| 1577 | type->plugin->free(src->ctx, &val); |
| 1578 | return LY_SUCCESS; |
| 1579 | |
| 1580 | fill: |
| 1581 | /* no canonization needed/possible */ |
| 1582 | set_fill_set(trg, src); |
| 1583 | return LY_SUCCESS; |
| 1584 | } |
| 1585 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1586 | #ifndef NDEBUG |
| 1587 | |
| 1588 | /** |
| 1589 | * @brief Bubble sort @p set into XPath document order. |
| 1590 | * Context position aware. Unused in the 'Release' build target. |
| 1591 | * |
| 1592 | * @param[in] set Set to sort. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1593 | * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0). |
| 1594 | */ |
| 1595 | static int |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1596 | set_sort(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1597 | { |
| 1598 | uint32_t i, j; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1599 | int ret = 0, cmp; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1600 | ly_bool inverted, change; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1601 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1602 | struct lyxp_set_node item; |
| 1603 | struct lyxp_set_hash_node hnode; |
| 1604 | uint64_t hash; |
| 1605 | |
Michal Vasko | 3cf8fbf | 2020-05-27 15:21:21 +0200 | [diff] [blame] | 1606 | if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1607 | return 0; |
| 1608 | } |
| 1609 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1610 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1611 | 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] | 1612 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1613 | |
| 1614 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1615 | if (set_assign_pos(set, root, set->root_type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1616 | return -1; |
| 1617 | } |
| 1618 | |
| 1619 | LOGDBG(LY_LDGXPATH, "SORT BEGIN"); |
| 1620 | print_set_debug(set); |
| 1621 | |
| 1622 | for (i = 0; i < set->used; ++i) { |
| 1623 | inverted = 0; |
| 1624 | change = 0; |
| 1625 | |
| 1626 | for (j = 1; j < set->used - i; ++j) { |
| 1627 | /* compare node positions */ |
| 1628 | if (inverted) { |
| 1629 | cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]); |
| 1630 | } else { |
| 1631 | cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]); |
| 1632 | } |
| 1633 | |
| 1634 | /* swap if needed */ |
| 1635 | if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) { |
| 1636 | change = 1; |
| 1637 | |
| 1638 | item = set->val.nodes[j - 1]; |
| 1639 | set->val.nodes[j - 1] = set->val.nodes[j]; |
| 1640 | set->val.nodes[j] = item; |
| 1641 | } else { |
| 1642 | /* whether node_pos1 should be smaller than node_pos2 or the other way around */ |
| 1643 | inverted = !inverted; |
| 1644 | } |
| 1645 | } |
| 1646 | |
| 1647 | ++ret; |
| 1648 | |
| 1649 | if (!change) { |
| 1650 | break; |
| 1651 | } |
| 1652 | } |
| 1653 | |
| 1654 | LOGDBG(LY_LDGXPATH, "SORT END %d", ret); |
| 1655 | print_set_debug(set); |
| 1656 | |
| 1657 | /* check node hashes */ |
| 1658 | if (set->used >= LYD_HT_MIN_ITEMS) { |
| 1659 | assert(set->ht); |
| 1660 | for (i = 0; i < set->used; ++i) { |
| 1661 | hnode.node = set->val.nodes[i].node; |
| 1662 | hnode.type = set->val.nodes[i].type; |
| 1663 | |
| 1664 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 1665 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 1666 | hash = dict_hash_multi(hash, NULL, 0); |
| 1667 | |
| 1668 | assert(!lyht_find(set->ht, &hnode, hash, NULL)); |
| 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | return ret - 1; |
| 1673 | } |
| 1674 | |
| 1675 | /** |
| 1676 | * @brief Remove duplicate entries in a sorted node set. |
| 1677 | * |
| 1678 | * @param[in] set Sorted set to check. |
| 1679 | * @return LY_ERR (LY_EEXIST if some duplicates are found) |
| 1680 | */ |
| 1681 | static LY_ERR |
| 1682 | set_sorted_dup_node_clean(struct lyxp_set *set) |
| 1683 | { |
| 1684 | uint32_t i = 0; |
| 1685 | LY_ERR ret = LY_SUCCESS; |
| 1686 | |
| 1687 | if (set->used > 1) { |
| 1688 | while (i < set->used - 1) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1689 | if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) && |
| 1690 | (set->val.nodes[i].type == set->val.nodes[i + 1].type)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1691 | set_remove_node_none(set, i + 1); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1692 | ret = LY_EEXIST; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1693 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1694 | ++i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1695 | } |
| 1696 | } |
| 1697 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1698 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1699 | return ret; |
| 1700 | } |
| 1701 | |
| 1702 | #endif |
| 1703 | |
| 1704 | /** |
| 1705 | * @brief Merge 2 sorted sets into one. |
| 1706 | * |
| 1707 | * @param[in,out] trg Set to merge into. Duplicates are removed. |
| 1708 | * @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] | 1709 | * @return LY_ERR |
| 1710 | */ |
| 1711 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1712 | set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1713 | { |
| 1714 | uint32_t i, j, k, count, dup_count; |
| 1715 | int cmp; |
| 1716 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1717 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1718 | 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] | 1719 | return LY_EINVAL; |
| 1720 | } |
| 1721 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1722 | if (!src->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1723 | return LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1724 | } else if (!trg->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1725 | set_fill_set(trg, src); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1726 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1727 | return LY_SUCCESS; |
| 1728 | } |
| 1729 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1730 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1731 | 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] | 1732 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1733 | |
| 1734 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1735 | 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] | 1736 | return LY_EINT; |
| 1737 | } |
| 1738 | |
| 1739 | #ifndef NDEBUG |
| 1740 | LOGDBG(LY_LDGXPATH, "MERGE target"); |
| 1741 | print_set_debug(trg); |
| 1742 | LOGDBG(LY_LDGXPATH, "MERGE source"); |
| 1743 | print_set_debug(src); |
| 1744 | #endif |
| 1745 | |
| 1746 | /* make memory for the merge (duplicates are not detected yet, so space |
| 1747 | * will likely be wasted on them, too bad) */ |
| 1748 | if (trg->size - trg->used < src->used) { |
| 1749 | trg->size = trg->used + src->used; |
| 1750 | |
| 1751 | trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes); |
| 1752 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM); |
| 1753 | } |
| 1754 | |
| 1755 | i = 0; |
| 1756 | j = 0; |
| 1757 | count = 0; |
| 1758 | dup_count = 0; |
| 1759 | do { |
| 1760 | cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]); |
| 1761 | if (!cmp) { |
| 1762 | if (!count) { |
| 1763 | /* duplicate, just skip it */ |
| 1764 | ++i; |
| 1765 | ++j; |
| 1766 | } else { |
| 1767 | /* we are copying something already, so let's copy the duplicate too, |
| 1768 | * we are hoping that afterwards there are some more nodes to |
| 1769 | * copy and this way we can copy them all together */ |
| 1770 | ++count; |
| 1771 | ++dup_count; |
| 1772 | ++i; |
| 1773 | ++j; |
| 1774 | } |
| 1775 | } else if (cmp < 0) { |
| 1776 | /* inserting src node into trg, just remember it for now */ |
| 1777 | ++count; |
| 1778 | ++i; |
| 1779 | |
| 1780 | /* insert the hash now */ |
| 1781 | set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type); |
| 1782 | } else if (count) { |
| 1783 | copy_nodes: |
| 1784 | /* time to actually copy the nodes, we have found the largest block of nodes */ |
| 1785 | memmove(&trg->val.nodes[j + (count - dup_count)], |
| 1786 | &trg->val.nodes[j], |
| 1787 | (trg->used - j) * sizeof *trg->val.nodes); |
| 1788 | memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes); |
| 1789 | |
| 1790 | trg->used += count - dup_count; |
| 1791 | /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */ |
| 1792 | j += count - dup_count; |
| 1793 | |
| 1794 | count = 0; |
| 1795 | dup_count = 0; |
| 1796 | } else { |
| 1797 | ++j; |
| 1798 | } |
| 1799 | } while ((i < src->used) && (j < trg->used)); |
| 1800 | |
| 1801 | if ((i < src->used) || count) { |
| 1802 | /* insert all the hashes first */ |
| 1803 | for (k = i; k < src->used; ++k) { |
| 1804 | set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type); |
| 1805 | } |
| 1806 | |
| 1807 | /* loop ended, but we need to copy something at trg end */ |
| 1808 | count += src->used - i; |
| 1809 | i = src->used; |
| 1810 | goto copy_nodes; |
| 1811 | } |
| 1812 | |
| 1813 | /* we are inserting hashes before the actual node insert, which causes |
| 1814 | * situations when there were initially not enough items for a hash table, |
| 1815 | * but even after some were inserted, hash table was not created (during |
| 1816 | * insertion the number of items is not updated yet) */ |
| 1817 | if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) { |
| 1818 | set_insert_node_hash(trg, NULL, 0); |
| 1819 | } |
| 1820 | |
| 1821 | #ifndef NDEBUG |
| 1822 | LOGDBG(LY_LDGXPATH, "MERGE result"); |
| 1823 | print_set_debug(trg); |
| 1824 | #endif |
| 1825 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1826 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1827 | return LY_SUCCESS; |
| 1828 | } |
| 1829 | |
| 1830 | /* |
| 1831 | * (re)parse functions |
| 1832 | * |
| 1833 | * Parse functions parse the expression into |
| 1834 | * tokens (syntactic analysis). |
| 1835 | * |
| 1836 | * Reparse functions perform semantic analysis |
| 1837 | * (do not save the result, just a check) of |
| 1838 | * the expression and fill repeat indices. |
| 1839 | */ |
| 1840 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1841 | LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1842 | 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] | 1843 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1844 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1845 | if (ctx) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1846 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1847 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1848 | return LY_EINCOMPLETE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1849 | } |
| 1850 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1851 | if (want_tok && (exp->tokens[tok_idx] != want_tok)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1852 | if (ctx) { |
Michal Vasko | 0b468e6 | 2020-10-19 09:33:04 +0200 | [diff] [blame] | 1853 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]), |
| 1854 | &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1855 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1856 | return LY_ENOT; |
| 1857 | } |
| 1858 | |
| 1859 | return LY_SUCCESS; |
| 1860 | } |
| 1861 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1862 | LY_ERR |
| 1863 | lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok) |
| 1864 | { |
| 1865 | LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok)); |
| 1866 | |
| 1867 | /* skip the token */ |
| 1868 | ++(*tok_idx); |
| 1869 | |
| 1870 | return LY_SUCCESS; |
| 1871 | } |
| 1872 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1873 | /* just like lyxp_check_token() but tests for 2 tokens */ |
| 1874 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame^] | 1875 | exp_check_token2(const struct ly_ctx *ctx, const 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] | 1876 | enum lyxp_token want_tok2) |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1877 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1878 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1879 | if (ctx) { |
| 1880 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1881 | } |
| 1882 | return LY_EINCOMPLETE; |
| 1883 | } |
| 1884 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1885 | 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] | 1886 | if (ctx) { |
Michal Vasko | 0b468e6 | 2020-10-19 09:33:04 +0200 | [diff] [blame] | 1887 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]), |
| 1888 | &exp->expr[exp->tok_pos[tok_idx]]); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1889 | } |
| 1890 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1891 | } |
| 1892 | |
| 1893 | return LY_SUCCESS; |
| 1894 | } |
| 1895 | |
| 1896 | /** |
| 1897 | * @brief Stack operation push on the repeat array. |
| 1898 | * |
| 1899 | * @param[in] exp Expression to use. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1900 | * @param[in] tok_idx Position in the expresion \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1901 | * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed. |
| 1902 | */ |
| 1903 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1904 | 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] | 1905 | { |
| 1906 | uint16_t i; |
| 1907 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1908 | if (exp->repeat[tok_idx]) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1909 | for (i = 0; exp->repeat[tok_idx][i]; ++i) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1910 | exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]); |
| 1911 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1912 | exp->repeat[tok_idx][i] = repeat_op_idx; |
| 1913 | exp->repeat[tok_idx][i + 1] = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1914 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1915 | exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]); |
| 1916 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1917 | exp->repeat[tok_idx][0] = repeat_op_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1918 | } |
| 1919 | } |
| 1920 | |
| 1921 | /** |
| 1922 | * @brief Reparse Predicate. Logs directly on error. |
| 1923 | * |
| 1924 | * [7] Predicate ::= '[' Expr ']' |
| 1925 | * |
| 1926 | * @param[in] ctx Context for logging. |
| 1927 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1928 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1929 | * @return LY_ERR |
| 1930 | */ |
| 1931 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1932 | 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] | 1933 | { |
| 1934 | LY_ERR rc; |
| 1935 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1936 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1937 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1938 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1939 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1940 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1941 | LY_CHECK_RET(rc); |
| 1942 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1943 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1944 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1945 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1946 | |
| 1947 | return LY_SUCCESS; |
| 1948 | } |
| 1949 | |
| 1950 | /** |
| 1951 | * @brief Reparse RelativeLocationPath. Logs directly on error. |
| 1952 | * |
| 1953 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 1954 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 1955 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 1956 | * |
| 1957 | * @param[in] ctx Context for logging. |
| 1958 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1959 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1960 | * @return LY_ERR (LY_EINCOMPLETE on forward reference) |
| 1961 | */ |
| 1962 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1963 | 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] | 1964 | { |
| 1965 | LY_ERR rc; |
| 1966 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1967 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1968 | LY_CHECK_RET(rc); |
| 1969 | |
| 1970 | goto step; |
| 1971 | do { |
| 1972 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1973 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1974 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1975 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1976 | LY_CHECK_RET(rc); |
| 1977 | step: |
| 1978 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1979 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1980 | case LYXP_TOKEN_DOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1981 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1982 | break; |
| 1983 | |
| 1984 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1985 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1986 | break; |
| 1987 | |
| 1988 | case LYXP_TOKEN_AT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1989 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1990 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1991 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1992 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1993 | 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] | 1994 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1995 | 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] | 1996 | return LY_EVALID; |
| 1997 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1998 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1999 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2000 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2001 | goto reparse_predicate; |
| 2002 | break; |
| 2003 | |
| 2004 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2005 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2006 | |
| 2007 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2008 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2009 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2010 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2011 | |
| 2012 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2013 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2014 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2015 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2016 | |
| 2017 | reparse_predicate: |
| 2018 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2019 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 2020 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2021 | LY_CHECK_RET(rc); |
| 2022 | } |
| 2023 | break; |
| 2024 | default: |
| 2025 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2026 | 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] | 2027 | return LY_EVALID; |
| 2028 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2029 | } 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] | 2030 | |
| 2031 | return LY_SUCCESS; |
| 2032 | } |
| 2033 | |
| 2034 | /** |
| 2035 | * @brief Reparse AbsoluteLocationPath. Logs directly on error. |
| 2036 | * |
| 2037 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 2038 | * |
| 2039 | * @param[in] ctx Context for logging. |
| 2040 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2041 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2042 | * @return LY_ERR |
| 2043 | */ |
| 2044 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2045 | 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] | 2046 | { |
| 2047 | LY_ERR rc; |
| 2048 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2049 | 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] | 2050 | |
| 2051 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2052 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2053 | /* '/' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2054 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2055 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2056 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2057 | return LY_SUCCESS; |
| 2058 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2059 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2060 | case LYXP_TOKEN_DOT: |
| 2061 | case LYXP_TOKEN_DDOT: |
| 2062 | case LYXP_TOKEN_AT: |
| 2063 | case LYXP_TOKEN_NAMETEST: |
| 2064 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2065 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2066 | LY_CHECK_RET(rc); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2067 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2068 | default: |
| 2069 | break; |
| 2070 | } |
| 2071 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2072 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 2073 | /* '//' RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2074 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2075 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2076 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2077 | LY_CHECK_RET(rc); |
| 2078 | } |
| 2079 | |
| 2080 | return LY_SUCCESS; |
| 2081 | } |
| 2082 | |
| 2083 | /** |
| 2084 | * @brief Reparse FunctionCall. Logs directly on error. |
| 2085 | * |
| 2086 | * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
| 2087 | * |
| 2088 | * @param[in] ctx Context for logging. |
| 2089 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2090 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2091 | * @return LY_ERR |
| 2092 | */ |
| 2093 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2094 | 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] | 2095 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2096 | int8_t min_arg_count = -1; |
| 2097 | uint32_t arg_count, max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2098 | uint16_t func_tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2099 | LY_ERR rc; |
| 2100 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2101 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2102 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2103 | func_tok_idx = *tok_idx; |
| 2104 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2105 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2106 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2107 | min_arg_count = 1; |
| 2108 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2109 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2110 | min_arg_count = 1; |
| 2111 | max_arg_count = 1; |
| 2112 | } |
| 2113 | break; |
| 2114 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2115 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2116 | min_arg_count = 1; |
| 2117 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2118 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2119 | min_arg_count = 0; |
| 2120 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2121 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2122 | min_arg_count = 0; |
| 2123 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2124 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2125 | min_arg_count = 0; |
| 2126 | max_arg_count = 0; |
| 2127 | } |
| 2128 | break; |
| 2129 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2130 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2131 | min_arg_count = 1; |
| 2132 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2133 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2134 | min_arg_count = 0; |
| 2135 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2136 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2137 | min_arg_count = 1; |
| 2138 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2139 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2140 | min_arg_count = 1; |
| 2141 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2142 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2143 | min_arg_count = 1; |
| 2144 | max_arg_count = 1; |
| 2145 | } |
| 2146 | break; |
| 2147 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2148 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2149 | min_arg_count = 2; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2150 | max_arg_count = UINT32_MAX; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2151 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2152 | min_arg_count = 0; |
| 2153 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2154 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2155 | min_arg_count = 0; |
| 2156 | max_arg_count = 1; |
| 2157 | } |
| 2158 | break; |
| 2159 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2160 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2161 | min_arg_count = 1; |
| 2162 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2163 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2164 | min_arg_count = 1; |
| 2165 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2166 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2167 | min_arg_count = 0; |
| 2168 | max_arg_count = 0; |
| 2169 | } |
| 2170 | break; |
| 2171 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2172 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2173 | min_arg_count = 2; |
| 2174 | max_arg_count = 2; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2175 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2176 | min_arg_count = 0; |
| 2177 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2178 | } 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] | 2179 | min_arg_count = 2; |
| 2180 | max_arg_count = 2; |
| 2181 | } |
| 2182 | break; |
| 2183 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2184 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2185 | min_arg_count = 2; |
| 2186 | max_arg_count = 3; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2187 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2188 | min_arg_count = 3; |
| 2189 | max_arg_count = 3; |
| 2190 | } |
| 2191 | break; |
| 2192 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2193 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2194 | min_arg_count = 0; |
| 2195 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2196 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2197 | min_arg_count = 1; |
| 2198 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2199 | } 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] | 2200 | min_arg_count = 2; |
| 2201 | max_arg_count = 2; |
| 2202 | } |
| 2203 | break; |
| 2204 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2205 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2206 | min_arg_count = 2; |
| 2207 | max_arg_count = 2; |
| 2208 | } |
| 2209 | break; |
| 2210 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2211 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2212 | min_arg_count = 2; |
| 2213 | max_arg_count = 2; |
| 2214 | } |
| 2215 | break; |
| 2216 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2217 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2218 | min_arg_count = 0; |
| 2219 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2220 | } 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] | 2221 | min_arg_count = 0; |
| 2222 | max_arg_count = 1; |
| 2223 | } |
| 2224 | break; |
| 2225 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2226 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2227 | min_arg_count = 0; |
| 2228 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2229 | } 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] | 2230 | min_arg_count = 2; |
| 2231 | max_arg_count = 2; |
| 2232 | } |
| 2233 | break; |
| 2234 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2235 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2236 | min_arg_count = 2; |
| 2237 | max_arg_count = 2; |
| 2238 | } |
| 2239 | break; |
| 2240 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2241 | 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] | 2242 | min_arg_count = 2; |
| 2243 | max_arg_count = 2; |
| 2244 | } |
| 2245 | break; |
| 2246 | } |
| 2247 | if (min_arg_count == -1) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2248 | 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] | 2249 | return LY_EINVAL; |
| 2250 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2251 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2252 | |
| 2253 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2254 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2255 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2256 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2257 | |
| 2258 | /* ( Expr ( ',' Expr )* )? */ |
| 2259 | arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2260 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2261 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2262 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2263 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2264 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2265 | LY_CHECK_RET(rc); |
| 2266 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2267 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
| 2268 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2269 | |
| 2270 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2271 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2272 | LY_CHECK_RET(rc); |
| 2273 | } |
| 2274 | |
| 2275 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2276 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2277 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2278 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2279 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2280 | 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] | 2281 | 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] | 2282 | &exp->expr[exp->tok_pos[func_tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2283 | return LY_EVALID; |
| 2284 | } |
| 2285 | |
| 2286 | return LY_SUCCESS; |
| 2287 | } |
| 2288 | |
| 2289 | /** |
| 2290 | * @brief Reparse PathExpr. Logs directly on error. |
| 2291 | * |
| 2292 | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
| 2293 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 2294 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 2295 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
| 2296 | * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
| 2297 | * |
| 2298 | * @param[in] ctx Context for logging. |
| 2299 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2300 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2301 | * @return LY_ERR |
| 2302 | */ |
| 2303 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2304 | 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] | 2305 | { |
| 2306 | LY_ERR rc; |
| 2307 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2308 | if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2309 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2310 | } |
| 2311 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2312 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2313 | case LYXP_TOKEN_PAR1: |
| 2314 | /* '(' Expr ')' Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2315 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2316 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2317 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2318 | LY_CHECK_RET(rc); |
| 2319 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2320 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2321 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2322 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2323 | goto predicate; |
| 2324 | break; |
| 2325 | case LYXP_TOKEN_DOT: |
| 2326 | case LYXP_TOKEN_DDOT: |
| 2327 | case LYXP_TOKEN_AT: |
| 2328 | case LYXP_TOKEN_NAMETEST: |
| 2329 | case LYXP_TOKEN_NODETYPE: |
| 2330 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2331 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2332 | LY_CHECK_RET(rc); |
| 2333 | break; |
| 2334 | case LYXP_TOKEN_FUNCNAME: |
| 2335 | /* FunctionCall */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2336 | rc = reparse_function_call(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2337 | LY_CHECK_RET(rc); |
| 2338 | goto predicate; |
| 2339 | break; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2340 | case LYXP_TOKEN_OPER_PATH: |
| 2341 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2342 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2343 | rc = reparse_absolute_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2344 | LY_CHECK_RET(rc); |
| 2345 | break; |
| 2346 | case LYXP_TOKEN_LITERAL: |
| 2347 | /* Literal */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2348 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2349 | goto predicate; |
| 2350 | break; |
| 2351 | case LYXP_TOKEN_NUMBER: |
| 2352 | /* Number */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2353 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2354 | goto predicate; |
| 2355 | break; |
| 2356 | default: |
| 2357 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2358 | 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] | 2359 | return LY_EVALID; |
| 2360 | } |
| 2361 | |
| 2362 | return LY_SUCCESS; |
| 2363 | |
| 2364 | predicate: |
| 2365 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2366 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 2367 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2368 | LY_CHECK_RET(rc); |
| 2369 | } |
| 2370 | |
| 2371 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2372 | 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] | 2373 | |
| 2374 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2375 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2376 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2377 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2378 | LY_CHECK_RET(rc); |
| 2379 | } |
| 2380 | |
| 2381 | return LY_SUCCESS; |
| 2382 | } |
| 2383 | |
| 2384 | /** |
| 2385 | * @brief Reparse UnaryExpr. Logs directly on error. |
| 2386 | * |
| 2387 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 2388 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 2389 | * |
| 2390 | * @param[in] ctx Context for logging. |
| 2391 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2392 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2393 | * @return LY_ERR |
| 2394 | */ |
| 2395 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2396 | 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] | 2397 | { |
| 2398 | uint16_t prev_exp; |
| 2399 | LY_ERR rc; |
| 2400 | |
| 2401 | /* ('-')* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2402 | prev_exp = *tok_idx; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2403 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2404 | (exp->expr[exp->tok_pos[*tok_idx]] == '-')) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2405 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2406 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2407 | } |
| 2408 | |
| 2409 | /* PathExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2410 | prev_exp = *tok_idx; |
| 2411 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2412 | LY_CHECK_RET(rc); |
| 2413 | |
| 2414 | /* ('|' PathExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2415 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2416 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2417 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2418 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2419 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2420 | LY_CHECK_RET(rc); |
| 2421 | } |
| 2422 | |
| 2423 | return LY_SUCCESS; |
| 2424 | } |
| 2425 | |
| 2426 | /** |
| 2427 | * @brief Reparse AdditiveExpr. Logs directly on error. |
| 2428 | * |
| 2429 | * [15] AdditiveExpr ::= MultiplicativeExpr |
| 2430 | * | AdditiveExpr '+' MultiplicativeExpr |
| 2431 | * | AdditiveExpr '-' MultiplicativeExpr |
| 2432 | * [16] MultiplicativeExpr ::= UnaryExpr |
| 2433 | * | MultiplicativeExpr '*' UnaryExpr |
| 2434 | * | MultiplicativeExpr 'div' UnaryExpr |
| 2435 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 2436 | * |
| 2437 | * @param[in] ctx Context for logging. |
| 2438 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2439 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2440 | * @return LY_ERR |
| 2441 | */ |
| 2442 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2443 | 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] | 2444 | { |
| 2445 | uint16_t prev_add_exp, prev_mul_exp; |
| 2446 | LY_ERR rc; |
| 2447 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2448 | prev_add_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2449 | goto reparse_multiplicative_expr; |
| 2450 | |
| 2451 | /* ('+' / '-' MultiplicativeExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2452 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2453 | ((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] | 2454 | exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2455 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2456 | |
| 2457 | reparse_multiplicative_expr: |
| 2458 | /* UnaryExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2459 | prev_mul_exp = *tok_idx; |
| 2460 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2461 | LY_CHECK_RET(rc); |
| 2462 | |
| 2463 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2464 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2465 | ((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] | 2466 | exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2467 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2468 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2469 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2470 | LY_CHECK_RET(rc); |
| 2471 | } |
| 2472 | } |
| 2473 | |
| 2474 | return LY_SUCCESS; |
| 2475 | } |
| 2476 | |
| 2477 | /** |
| 2478 | * @brief Reparse EqualityExpr. Logs directly on error. |
| 2479 | * |
| 2480 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
| 2481 | * | EqualityExpr '!=' RelationalExpr |
| 2482 | * [14] RelationalExpr ::= AdditiveExpr |
| 2483 | * | RelationalExpr '<' AdditiveExpr |
| 2484 | * | RelationalExpr '>' AdditiveExpr |
| 2485 | * | RelationalExpr '<=' AdditiveExpr |
| 2486 | * | RelationalExpr '>=' AdditiveExpr |
| 2487 | * |
| 2488 | * @param[in] ctx Context for logging. |
| 2489 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2490 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2491 | * @return LY_ERR |
| 2492 | */ |
| 2493 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2494 | 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] | 2495 | { |
| 2496 | uint16_t prev_eq_exp, prev_rel_exp; |
| 2497 | LY_ERR rc; |
| 2498 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2499 | prev_eq_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2500 | goto reparse_additive_expr; |
| 2501 | |
| 2502 | /* ('=' / '!=' RelationalExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2503 | 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] | 2504 | exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2505 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2506 | |
| 2507 | reparse_additive_expr: |
| 2508 | /* AdditiveExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2509 | prev_rel_exp = *tok_idx; |
| 2510 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2511 | LY_CHECK_RET(rc); |
| 2512 | |
| 2513 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2514 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2515 | exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2516 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2517 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2518 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2519 | LY_CHECK_RET(rc); |
| 2520 | } |
| 2521 | } |
| 2522 | |
| 2523 | return LY_SUCCESS; |
| 2524 | } |
| 2525 | |
| 2526 | /** |
| 2527 | * @brief Reparse OrExpr. Logs directly on error. |
| 2528 | * |
| 2529 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 2530 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 2531 | * |
| 2532 | * @param[in] ctx Context for logging. |
| 2533 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2534 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2535 | * @return LY_ERR |
| 2536 | */ |
| 2537 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2538 | 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] | 2539 | { |
| 2540 | uint16_t prev_or_exp, prev_and_exp; |
| 2541 | LY_ERR rc; |
| 2542 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2543 | prev_or_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2544 | goto reparse_equality_expr; |
| 2545 | |
| 2546 | /* ('or' AndExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2547 | 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] | 2548 | exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2549 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2550 | |
| 2551 | reparse_equality_expr: |
| 2552 | /* EqualityExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2553 | prev_and_exp = *tok_idx; |
| 2554 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2555 | LY_CHECK_RET(rc); |
| 2556 | |
| 2557 | /* ('and' EqualityExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2558 | 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] | 2559 | exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2560 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2561 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2562 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2563 | LY_CHECK_RET(rc); |
| 2564 | } |
| 2565 | } |
| 2566 | |
| 2567 | return LY_SUCCESS; |
| 2568 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2569 | |
| 2570 | /** |
| 2571 | * @brief Parse NCName. |
| 2572 | * |
| 2573 | * @param[in] ncname Name to parse. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2574 | * @return Length of @p ncname valid bytes. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2575 | */ |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2576 | static long int |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2577 | parse_ncname(const char *ncname) |
| 2578 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2579 | uint32_t uc; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2580 | size_t size; |
| 2581 | long int len = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2582 | |
| 2583 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0); |
| 2584 | if (!is_xmlqnamestartchar(uc) || (uc == ':')) { |
| 2585 | return len; |
| 2586 | } |
| 2587 | |
| 2588 | do { |
| 2589 | len += size; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2590 | if (!*ncname) { |
| 2591 | break; |
| 2592 | } |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2593 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2594 | } while (is_xmlqnamechar(uc) && (uc != ':')); |
| 2595 | |
| 2596 | return len; |
| 2597 | } |
| 2598 | |
| 2599 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2600 | * @brief Add @p token into the expression @p exp. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2601 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2602 | * @param[in] ctx Context for logging. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2603 | * @param[in] exp Expression to use. |
| 2604 | * @param[in] token Token to add. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2605 | * @param[in] tok_pos Token position in the XPath expression. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2606 | * @param[in] tok_len Token length in the XPath expression. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2607 | * @return LY_ERR |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2608 | */ |
| 2609 | static LY_ERR |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2610 | 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] | 2611 | { |
| 2612 | uint32_t prev; |
| 2613 | |
| 2614 | if (exp->used == exp->size) { |
| 2615 | prev = exp->size; |
| 2616 | exp->size += LYXP_EXPR_SIZE_STEP; |
| 2617 | if (prev > exp->size) { |
| 2618 | LOGINT(ctx); |
| 2619 | return LY_EINT; |
| 2620 | } |
| 2621 | |
| 2622 | exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens); |
| 2623 | LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM); |
| 2624 | exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos); |
| 2625 | LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM); |
| 2626 | exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len); |
| 2627 | LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM); |
| 2628 | } |
| 2629 | |
| 2630 | exp->tokens[exp->used] = token; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2631 | exp->tok_pos[exp->used] = tok_pos; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2632 | exp->tok_len[exp->used] = tok_len; |
| 2633 | ++exp->used; |
| 2634 | return LY_SUCCESS; |
| 2635 | } |
| 2636 | |
| 2637 | void |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2638 | lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2639 | { |
| 2640 | uint16_t i; |
| 2641 | |
| 2642 | if (!expr) { |
| 2643 | return; |
| 2644 | } |
| 2645 | |
| 2646 | lydict_remove(ctx, expr->expr); |
| 2647 | free(expr->tokens); |
| 2648 | free(expr->tok_pos); |
| 2649 | free(expr->tok_len); |
| 2650 | if (expr->repeat) { |
| 2651 | for (i = 0; i < expr->used; ++i) { |
| 2652 | free(expr->repeat[i]); |
| 2653 | } |
| 2654 | } |
| 2655 | free(expr->repeat); |
| 2656 | free(expr); |
| 2657 | } |
| 2658 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2659 | LY_ERR |
| 2660 | 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] | 2661 | { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2662 | LY_ERR ret = LY_SUCCESS; |
| 2663 | struct lyxp_expr *expr; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2664 | size_t parsed = 0, tok_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2665 | enum lyxp_token tok_type; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2666 | ly_bool prev_function_check = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2667 | uint16_t tok_idx = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2668 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2669 | assert(expr_p); |
| 2670 | |
| 2671 | if (!expr_str[0]) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2672 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2673 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2674 | } |
| 2675 | |
| 2676 | if (!expr_len) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2677 | expr_len = strlen(expr_str); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2678 | } |
| 2679 | if (expr_len > UINT16_MAX) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2680 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "XPath expression cannot be longer than %ud characters.", UINT16_MAX); |
| 2681 | return LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2682 | } |
| 2683 | |
| 2684 | /* init lyxp_expr structure */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2685 | expr = calloc(1, sizeof *expr); |
| 2686 | LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error); |
| 2687 | LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error); |
| 2688 | expr->used = 0; |
| 2689 | expr->size = LYXP_EXPR_SIZE_START; |
| 2690 | expr->tokens = malloc(expr->size * sizeof *expr->tokens); |
| 2691 | LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2692 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2693 | expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos); |
| 2694 | 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] | 2695 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2696 | expr->tok_len = malloc(expr->size * sizeof *expr->tok_len); |
| 2697 | 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] | 2698 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2699 | /* make expr 0-terminated */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2700 | expr_str = expr->expr; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2701 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2702 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2703 | ++parsed; |
| 2704 | } |
| 2705 | |
| 2706 | do { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2707 | if (expr_str[parsed] == '(') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2708 | |
| 2709 | /* '(' */ |
| 2710 | tok_len = 1; |
| 2711 | tok_type = LYXP_TOKEN_PAR1; |
| 2712 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2713 | 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] | 2714 | /* it is a NodeType/FunctionName after all */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2715 | if (((expr->tok_len[expr->used - 1] == 4) && |
| 2716 | (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) || |
| 2717 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) || |
| 2718 | ((expr->tok_len[expr->used - 1] == 7) && |
| 2719 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2720 | expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2721 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2722 | expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2723 | } |
| 2724 | prev_function_check = 0; |
| 2725 | } |
| 2726 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2727 | } else if (expr_str[parsed] == ')') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2728 | |
| 2729 | /* ')' */ |
| 2730 | tok_len = 1; |
| 2731 | tok_type = LYXP_TOKEN_PAR2; |
| 2732 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2733 | } else if (expr_str[parsed] == '[') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2734 | |
| 2735 | /* '[' */ |
| 2736 | tok_len = 1; |
| 2737 | tok_type = LYXP_TOKEN_BRACK1; |
| 2738 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2739 | } else if (expr_str[parsed] == ']') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2740 | |
| 2741 | /* ']' */ |
| 2742 | tok_len = 1; |
| 2743 | tok_type = LYXP_TOKEN_BRACK2; |
| 2744 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2745 | } else if (!strncmp(&expr_str[parsed], "..", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2746 | |
| 2747 | /* '..' */ |
| 2748 | tok_len = 2; |
| 2749 | tok_type = LYXP_TOKEN_DDOT; |
| 2750 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2751 | } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2752 | |
| 2753 | /* '.' */ |
| 2754 | tok_len = 1; |
| 2755 | tok_type = LYXP_TOKEN_DOT; |
| 2756 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2757 | } else if (expr_str[parsed] == '@') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2758 | |
| 2759 | /* '@' */ |
| 2760 | tok_len = 1; |
| 2761 | tok_type = LYXP_TOKEN_AT; |
| 2762 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2763 | } else if (expr_str[parsed] == ',') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2764 | |
| 2765 | /* ',' */ |
| 2766 | tok_len = 1; |
| 2767 | tok_type = LYXP_TOKEN_COMMA; |
| 2768 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2769 | } else if (expr_str[parsed] == '\'') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2770 | |
| 2771 | /* Literal with ' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2772 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {} |
| 2773 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2774 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID, |
| 2775 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2776 | ++tok_len; |
| 2777 | tok_type = LYXP_TOKEN_LITERAL; |
| 2778 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2779 | } else if (expr_str[parsed] == '\"') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2780 | |
| 2781 | /* Literal with " */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2782 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {} |
| 2783 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2784 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID, |
| 2785 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2786 | ++tok_len; |
| 2787 | tok_type = LYXP_TOKEN_LITERAL; |
| 2788 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2789 | } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2790 | |
| 2791 | /* Number */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2792 | for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
| 2793 | if (expr_str[parsed + tok_len] == '.') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2794 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2795 | for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2796 | } |
| 2797 | tok_type = LYXP_TOKEN_NUMBER; |
| 2798 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2799 | } else if (expr_str[parsed] == '/') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2800 | |
| 2801 | /* Operator '/', '//' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2802 | if (!strncmp(&expr_str[parsed], "//", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2803 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2804 | tok_type = LYXP_TOKEN_OPER_RPATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2805 | } else { |
| 2806 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2807 | tok_type = LYXP_TOKEN_OPER_PATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2808 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2809 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2810 | } else if (!strncmp(&expr_str[parsed], "!=", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2811 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2812 | /* Operator '!=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2813 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2814 | tok_type = LYXP_TOKEN_OPER_NEQUAL; |
| 2815 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2816 | } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2817 | |
| 2818 | /* Operator '<=', '>=' */ |
| 2819 | tok_len = 2; |
| 2820 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2821 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2822 | } else if (expr_str[parsed] == '|') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2823 | |
| 2824 | /* Operator '|' */ |
| 2825 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2826 | tok_type = LYXP_TOKEN_OPER_UNI; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2827 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2828 | } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2829 | |
| 2830 | /* Operator '+', '-' */ |
| 2831 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2832 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2833 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2834 | } else if (expr_str[parsed] == '=') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2835 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2836 | /* Operator '=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2837 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2838 | tok_type = LYXP_TOKEN_OPER_EQUAL; |
| 2839 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2840 | } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2841 | |
| 2842 | /* Operator '<', '>' */ |
| 2843 | tok_len = 1; |
| 2844 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2845 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2846 | } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) && |
| 2847 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) && |
| 2848 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) && |
| 2849 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) && |
| 2850 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) && |
| 2851 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) && |
| 2852 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) && |
| 2853 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) && |
| 2854 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) && |
| 2855 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) && |
| 2856 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) && |
| 2857 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2858 | |
| 2859 | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2860 | if (expr_str[parsed] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2861 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2862 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2863 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2864 | } else if (!strncmp(&expr_str[parsed], "or", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2865 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2866 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2867 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2868 | } else if (!strncmp(&expr_str[parsed], "and", 3)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2869 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2870 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2871 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2872 | } 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] | 2873 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2874 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2875 | |
| 2876 | } else if (prev_function_check) { |
Michal Vasko | 5307857 | 2019-05-24 08:50:15 +0200 | [diff] [blame] | 2877 | 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] | 2878 | 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] | 2879 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2880 | goto error; |
| 2881 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2882 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed + 1, expr_str); |
| 2883 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2884 | goto error; |
| 2885 | } |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2886 | } else if (expr_str[parsed] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2887 | |
| 2888 | /* NameTest '*' */ |
| 2889 | tok_len = 1; |
| 2890 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2891 | |
| 2892 | } else { |
| 2893 | |
| 2894 | /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2895 | long int ncname_len = parse_ncname(&expr_str[parsed]); |
| 2896 | LY_CHECK_ERR_GOTO(ncname_len < 0, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2897 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID, |
| 2898 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2899 | tok_len = ncname_len; |
| 2900 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2901 | if (expr_str[parsed + tok_len] == ':') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2902 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2903 | if (expr_str[parsed + tok_len] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2904 | ++tok_len; |
| 2905 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2906 | ncname_len = parse_ncname(&expr_str[parsed + tok_len]); |
| 2907 | LY_CHECK_ERR_GOTO(ncname_len < 0, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2908 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID, |
| 2909 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2910 | tok_len += ncname_len; |
| 2911 | } |
| 2912 | /* remove old flag to prevent ambiguities */ |
| 2913 | prev_function_check = 0; |
| 2914 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2915 | } else { |
| 2916 | /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */ |
| 2917 | prev_function_check = 1; |
| 2918 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2919 | } |
| 2920 | } |
| 2921 | |
| 2922 | /* store the token, move on to the next one */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2923 | 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] | 2924 | parsed += tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2925 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2926 | ++parsed; |
| 2927 | } |
| 2928 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2929 | } while (expr_str[parsed]); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2930 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2931 | if (reparse) { |
| 2932 | /* prealloc repeat */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2933 | expr->repeat = calloc(expr->size, sizeof *expr->repeat); |
| 2934 | LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2935 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2936 | /* fill repeat */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2937 | LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx), ret = LY_EVALID, error); |
| 2938 | if (expr->used > tok_idx) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2939 | 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] | 2940 | &expr->expr[expr->tok_pos[tok_idx]]); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2941 | ret = LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2942 | goto error; |
| 2943 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2944 | } |
| 2945 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2946 | print_expr_struct_debug(expr); |
| 2947 | *expr_p = expr; |
| 2948 | return LY_SUCCESS; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2949 | |
| 2950 | error: |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2951 | lyxp_expr_free(ctx, expr); |
| 2952 | return ret; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2953 | } |
| 2954 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2955 | LY_ERR |
| 2956 | 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] | 2957 | { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2958 | LY_ERR ret = LY_SUCCESS; |
| 2959 | struct lyxp_expr *dup = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2960 | uint32_t i, j; |
| 2961 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2962 | if (!exp) { |
| 2963 | goto cleanup; |
| 2964 | } |
| 2965 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2966 | dup = calloc(1, sizeof *dup); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2967 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2968 | |
| 2969 | dup->tokens = malloc(exp->used * sizeof *dup->tokens); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2970 | LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2971 | memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens); |
| 2972 | |
| 2973 | dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2974 | 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] | 2975 | memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos); |
| 2976 | |
| 2977 | dup->tok_len = malloc(exp->used * sizeof *dup->tok_len); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2978 | 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] | 2979 | memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len); |
| 2980 | |
| 2981 | dup->repeat = malloc(exp->used * sizeof *dup->repeat); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2982 | LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2983 | for (i = 0; i < exp->used; ++i) { |
| 2984 | if (!exp->repeat[i]) { |
| 2985 | dup->repeat[i] = NULL; |
| 2986 | } else { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2987 | for (j = 0; exp->repeat[i][j]; ++j) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2988 | /* the ending 0 as well */ |
| 2989 | ++j; |
| 2990 | |
Michal Vasko | 99c7164 | 2020-07-03 13:33:36 +0200 | [diff] [blame] | 2991 | dup->repeat[i] = malloc(j * sizeof **dup->repeat); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2992 | 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] | 2993 | memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat); |
| 2994 | dup->repeat[i][j - 1] = 0; |
| 2995 | } |
| 2996 | } |
| 2997 | |
| 2998 | dup->used = exp->used; |
| 2999 | dup->size = exp->used; |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3000 | 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] | 3001 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3002 | cleanup: |
| 3003 | if (ret) { |
| 3004 | lyxp_expr_free(ctx, dup); |
| 3005 | } else { |
| 3006 | *dup_p = dup; |
| 3007 | } |
| 3008 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3009 | } |
| 3010 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3011 | /* |
| 3012 | * warn functions |
| 3013 | * |
| 3014 | * Warn functions check specific reasonable conditions for schema XPath |
| 3015 | * and print a warning if they are not satisfied. |
| 3016 | */ |
| 3017 | |
| 3018 | /** |
| 3019 | * @brief Get the last-added schema node that is currently in the context. |
| 3020 | * |
| 3021 | * @param[in] set Set to search in. |
| 3022 | * @return Last-added schema context node, NULL if no node is in context. |
| 3023 | */ |
| 3024 | static struct lysc_node * |
| 3025 | warn_get_scnode_in_ctx(struct lyxp_set *set) |
| 3026 | { |
| 3027 | uint32_t i; |
| 3028 | |
| 3029 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
| 3030 | return NULL; |
| 3031 | } |
| 3032 | |
| 3033 | i = set->used; |
| 3034 | do { |
| 3035 | --i; |
| 3036 | if (set->val.scnodes[i].in_ctx == 1) { |
| 3037 | /* if there are more, simply return the first found (last added) */ |
| 3038 | return set->val.scnodes[i].scnode; |
| 3039 | } |
| 3040 | } while (i); |
| 3041 | |
| 3042 | return NULL; |
| 3043 | } |
| 3044 | |
| 3045 | /** |
| 3046 | * @brief Test whether a type is numeric - integer type or decimal64. |
| 3047 | * |
| 3048 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3049 | * @return Boolean value whether @p type is numeric type or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3050 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3051 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3052 | warn_is_numeric_type(struct lysc_type *type) |
| 3053 | { |
| 3054 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3055 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3056 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3057 | |
| 3058 | switch (type->basetype) { |
| 3059 | case LY_TYPE_DEC64: |
| 3060 | case LY_TYPE_INT8: |
| 3061 | case LY_TYPE_UINT8: |
| 3062 | case LY_TYPE_INT16: |
| 3063 | case LY_TYPE_UINT16: |
| 3064 | case LY_TYPE_INT32: |
| 3065 | case LY_TYPE_UINT32: |
| 3066 | case LY_TYPE_INT64: |
| 3067 | case LY_TYPE_UINT64: |
| 3068 | return 1; |
| 3069 | case LY_TYPE_UNION: |
| 3070 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3071 | LY_ARRAY_FOR(uni->types, u) { |
| 3072 | ret = warn_is_numeric_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3073 | if (ret) { |
| 3074 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3075 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3076 | } |
| 3077 | } |
| 3078 | /* did not find any suitable type */ |
| 3079 | return 0; |
| 3080 | case LY_TYPE_LEAFREF: |
| 3081 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
| 3082 | default: |
| 3083 | return 0; |
| 3084 | } |
| 3085 | } |
| 3086 | |
| 3087 | /** |
| 3088 | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
| 3089 | * |
| 3090 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3091 | * @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] | 3092 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3093 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3094 | warn_is_string_type(struct lysc_type *type) |
| 3095 | { |
| 3096 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3097 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3098 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3099 | |
| 3100 | switch (type->basetype) { |
| 3101 | case LY_TYPE_BITS: |
| 3102 | case LY_TYPE_ENUM: |
| 3103 | case LY_TYPE_IDENT: |
| 3104 | case LY_TYPE_INST: |
| 3105 | case LY_TYPE_STRING: |
| 3106 | return 1; |
| 3107 | case LY_TYPE_UNION: |
| 3108 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3109 | LY_ARRAY_FOR(uni->types, u) { |
| 3110 | ret = warn_is_string_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3111 | if (ret) { |
| 3112 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3113 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3114 | } |
| 3115 | } |
| 3116 | /* did not find any suitable type */ |
| 3117 | return 0; |
| 3118 | case LY_TYPE_LEAFREF: |
| 3119 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
| 3120 | default: |
| 3121 | return 0; |
| 3122 | } |
| 3123 | } |
| 3124 | |
| 3125 | /** |
| 3126 | * @brief Test whether a type is one specific type. |
| 3127 | * |
| 3128 | * @param[in] type Type to test. |
| 3129 | * @param[in] base Expected type. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3130 | * @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] | 3131 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3132 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3133 | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
| 3134 | { |
| 3135 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3136 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3137 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3138 | |
| 3139 | if (type->basetype == base) { |
| 3140 | return 1; |
| 3141 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3142 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3143 | LY_ARRAY_FOR(uni->types, u) { |
| 3144 | ret = warn_is_specific_type(uni->types[u], base); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3145 | if (ret) { |
| 3146 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3147 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3148 | } |
| 3149 | } |
| 3150 | /* did not find any suitable type */ |
| 3151 | return 0; |
| 3152 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
| 3153 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
| 3154 | } |
| 3155 | |
| 3156 | return 0; |
| 3157 | } |
| 3158 | |
| 3159 | /** |
| 3160 | * @brief Get next type of a (union) type. |
| 3161 | * |
| 3162 | * @param[in] type Base type. |
| 3163 | * @param[in] prev_type Previously returned type. |
| 3164 | * @return Next type or NULL. |
| 3165 | */ |
| 3166 | static struct lysc_type * |
| 3167 | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type) |
| 3168 | { |
| 3169 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3170 | ly_bool found = 0; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3171 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3172 | |
| 3173 | switch (type->basetype) { |
| 3174 | case LY_TYPE_UNION: |
| 3175 | uni = (struct lysc_type_union *)type; |
| 3176 | if (!prev_type) { |
| 3177 | return uni->types[0]; |
| 3178 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3179 | LY_ARRAY_FOR(uni->types, u) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3180 | if (found) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3181 | return uni->types[u]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3182 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3183 | if (prev_type == uni->types[u]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3184 | found = 1; |
| 3185 | } |
| 3186 | } |
| 3187 | return NULL; |
| 3188 | default: |
| 3189 | if (prev_type) { |
| 3190 | assert(type == prev_type); |
| 3191 | return NULL; |
| 3192 | } else { |
| 3193 | return type; |
| 3194 | } |
| 3195 | } |
| 3196 | } |
| 3197 | |
| 3198 | /** |
| 3199 | * @brief Test whether 2 types have a common type. |
| 3200 | * |
| 3201 | * @param[in] type1 First type. |
| 3202 | * @param[in] type2 Second type. |
| 3203 | * @return 1 if they do, 0 otherwise. |
| 3204 | */ |
| 3205 | static int |
| 3206 | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
| 3207 | { |
| 3208 | struct lysc_type *t1, *rt1, *t2, *rt2; |
| 3209 | |
| 3210 | t1 = NULL; |
| 3211 | while ((t1 = warn_is_equal_type_next_type(type1, t1))) { |
| 3212 | if (t1->basetype == LY_TYPE_LEAFREF) { |
| 3213 | rt1 = ((struct lysc_type_leafref *)t1)->realtype; |
| 3214 | } else { |
| 3215 | rt1 = t1; |
| 3216 | } |
| 3217 | |
| 3218 | t2 = NULL; |
| 3219 | while ((t2 = warn_is_equal_type_next_type(type2, t2))) { |
| 3220 | if (t2->basetype == LY_TYPE_LEAFREF) { |
| 3221 | rt2 = ((struct lysc_type_leafref *)t2)->realtype; |
| 3222 | } else { |
| 3223 | rt2 = t2; |
| 3224 | } |
| 3225 | |
| 3226 | if (rt2->basetype == rt1->basetype) { |
| 3227 | /* match found */ |
| 3228 | return 1; |
| 3229 | } |
| 3230 | } |
| 3231 | } |
| 3232 | |
| 3233 | return 0; |
| 3234 | } |
| 3235 | |
| 3236 | /** |
| 3237 | * @brief Check both operands of comparison operators. |
| 3238 | * |
| 3239 | * @param[in] ctx Context for errors. |
| 3240 | * @param[in] set1 First operand set. |
| 3241 | * @param[in] set2 Second operand set. |
| 3242 | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
| 3243 | * @param[in] expr Start of the expression to print with the warning. |
| 3244 | * @param[in] tok_pos Token position. |
| 3245 | */ |
| 3246 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3247 | 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] | 3248 | { |
| 3249 | struct lysc_node_leaf *node1, *node2; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3250 | ly_bool leaves = 1, warning = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3251 | |
| 3252 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
| 3253 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
| 3254 | |
| 3255 | if (!node1 && !node2) { |
| 3256 | /* no node-sets involved, nothing to do */ |
| 3257 | return; |
| 3258 | } |
| 3259 | |
| 3260 | if (node1) { |
| 3261 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3262 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
| 3263 | warning = 1; |
| 3264 | leaves = 0; |
| 3265 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
| 3266 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
| 3267 | warning = 1; |
| 3268 | } |
| 3269 | } |
| 3270 | |
| 3271 | if (node2) { |
| 3272 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3273 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
| 3274 | warning = 1; |
| 3275 | leaves = 0; |
| 3276 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
| 3277 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
| 3278 | warning = 1; |
| 3279 | } |
| 3280 | } |
| 3281 | |
| 3282 | if (node1 && node2 && leaves && !numbers_only) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3283 | if ((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_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) && |
| 3286 | !warn_is_equal_type(node1->type, node2->type))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3287 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
| 3288 | warning = 1; |
| 3289 | } |
| 3290 | } |
| 3291 | |
| 3292 | if (warning) { |
| 3293 | LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos); |
| 3294 | } |
| 3295 | } |
| 3296 | |
| 3297 | /** |
| 3298 | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
| 3299 | * |
| 3300 | * @param[in] exp Parsed XPath expression. |
| 3301 | * @param[in] set Set with the leaf/leaf-list. |
| 3302 | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
| 3303 | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
| 3304 | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
| 3305 | */ |
| 3306 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame^] | 3307 | warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, |
| 3308 | uint16_t last_equal_exp) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3309 | { |
| 3310 | struct lysc_node *scnode; |
| 3311 | struct lysc_type *type; |
| 3312 | char *value; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3313 | struct lyd_value storage; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3314 | LY_ERR rc; |
| 3315 | struct ly_err_item *err = NULL; |
| 3316 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3317 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && |
| 3318 | ((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] | 3319 | /* check that the node can have the specified value */ |
| 3320 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
| 3321 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
| 3322 | } else { |
| 3323 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
| 3324 | } |
| 3325 | if (!value) { |
| 3326 | LOGMEM(set->ctx); |
| 3327 | return; |
| 3328 | } |
| 3329 | |
| 3330 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
| 3331 | 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] | 3332 | " 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] | 3333 | 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] | 3334 | (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] | 3335 | exp->expr + exp->tok_pos[equal_exp]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3336 | } |
| 3337 | |
| 3338 | type = ((struct lysc_node_leaf *)scnode)->type; |
| 3339 | if (type->basetype != LY_TYPE_IDENT) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3340 | 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] | 3341 | LYD_HINT_DATA, scnode, &storage, &err); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3342 | |
| 3343 | if (err) { |
| 3344 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
| 3345 | ly_err_free(err); |
| 3346 | } else if (rc != LY_SUCCESS) { |
| 3347 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
| 3348 | } |
| 3349 | if (rc != LY_SUCCESS) { |
| 3350 | 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] | 3351 | (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] | 3352 | exp->expr + exp->tok_pos[equal_exp]); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3353 | } else { |
| 3354 | type->plugin->free(set->ctx, &storage); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3355 | } |
| 3356 | } |
| 3357 | free(value); |
| 3358 | } |
| 3359 | } |
| 3360 | |
| 3361 | /* |
| 3362 | * XPath functions |
| 3363 | */ |
| 3364 | |
| 3365 | /** |
| 3366 | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
| 3367 | * depending on whether the first node bit value from the second argument is set. |
| 3368 | * |
| 3369 | * @param[in] args Array of arguments. |
| 3370 | * @param[in] arg_count Count of elements in @p args. |
| 3371 | * @param[in,out] set Context and result set at the same time. |
| 3372 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3373 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3374 | */ |
| 3375 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3376 | 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] | 3377 | { |
| 3378 | struct lyd_node_term *leaf; |
| 3379 | struct lysc_node_leaf *sleaf; |
| 3380 | struct lysc_type_bits *bits; |
| 3381 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3382 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3383 | |
| 3384 | if (options & LYXP_SCNODE_ALL) { |
| 3385 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3386 | 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] | 3387 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3388 | 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] | 3389 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
| 3390 | 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] | 3391 | } |
| 3392 | |
| 3393 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3394 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3395 | 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] | 3396 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3397 | 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] | 3398 | } |
| 3399 | } |
| 3400 | set_scnode_clear_ctx(set); |
| 3401 | return rc; |
| 3402 | } |
| 3403 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3404 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3405 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 3406 | "bit-is-set(node-set, string)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3407 | return LY_EVALID; |
| 3408 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3409 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3410 | LY_CHECK_RET(rc); |
| 3411 | |
| 3412 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3413 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3414 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3415 | if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && |
| 3416 | (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3417 | bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3418 | LY_ARRAY_FOR(bits->bits, u) { |
| 3419 | if (!strcmp(bits->bits[u].name, args[1]->val.str)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3420 | set_fill_boolean(set, 1); |
| 3421 | break; |
| 3422 | } |
| 3423 | } |
| 3424 | } |
| 3425 | } |
| 3426 | |
| 3427 | return LY_SUCCESS; |
| 3428 | } |
| 3429 | |
| 3430 | /** |
| 3431 | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
| 3432 | * with the argument converted to boolean. |
| 3433 | * |
| 3434 | * @param[in] args Array of arguments. |
| 3435 | * @param[in] arg_count Count of elements in @p args. |
| 3436 | * @param[in,out] set Context and result set at the same time. |
| 3437 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3438 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3439 | */ |
| 3440 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3441 | 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] | 3442 | { |
| 3443 | LY_ERR rc; |
| 3444 | |
| 3445 | if (options & LYXP_SCNODE_ALL) { |
| 3446 | set_scnode_clear_ctx(set); |
| 3447 | return LY_SUCCESS; |
| 3448 | } |
| 3449 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3450 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3451 | LY_CHECK_RET(rc); |
| 3452 | set_fill_set(set, args[0]); |
| 3453 | |
| 3454 | return LY_SUCCESS; |
| 3455 | } |
| 3456 | |
| 3457 | /** |
| 3458 | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
| 3459 | * with the first argument rounded up to the nearest integer. |
| 3460 | * |
| 3461 | * @param[in] args Array of arguments. |
| 3462 | * @param[in] arg_count Count of elements in @p args. |
| 3463 | * @param[in,out] set Context and result set at the same time. |
| 3464 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3465 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3466 | */ |
| 3467 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3468 | 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] | 3469 | { |
| 3470 | struct lysc_node_leaf *sleaf; |
| 3471 | LY_ERR rc = LY_SUCCESS; |
| 3472 | |
| 3473 | if (options & LYXP_SCNODE_ALL) { |
| 3474 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3475 | 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] | 3476 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3477 | 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] | 3478 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 3479 | 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] | 3480 | } |
| 3481 | set_scnode_clear_ctx(set); |
| 3482 | return rc; |
| 3483 | } |
| 3484 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3485 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3486 | LY_CHECK_RET(rc); |
| 3487 | if ((long long)args[0]->val.num != args[0]->val.num) { |
| 3488 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
| 3489 | } else { |
| 3490 | set_fill_number(set, args[0]->val.num); |
| 3491 | } |
| 3492 | |
| 3493 | return LY_SUCCESS; |
| 3494 | } |
| 3495 | |
| 3496 | /** |
| 3497 | * @brief Execute the XPath concat(string, string, string*) function. |
| 3498 | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
| 3499 | * |
| 3500 | * @param[in] args Array of arguments. |
| 3501 | * @param[in] arg_count Count of elements in @p args. |
| 3502 | * @param[in,out] set Context and result set at the same time. |
| 3503 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3504 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3505 | */ |
| 3506 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3507 | 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] | 3508 | { |
| 3509 | uint16_t i; |
| 3510 | char *str = NULL; |
| 3511 | size_t used = 1; |
| 3512 | LY_ERR rc = LY_SUCCESS; |
| 3513 | struct lysc_node_leaf *sleaf; |
| 3514 | |
| 3515 | if (options & LYXP_SCNODE_ALL) { |
| 3516 | for (i = 0; i < arg_count; ++i) { |
| 3517 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
| 3518 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3519 | LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3520 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3521 | } else if (!warn_is_string_type(sleaf->type)) { |
Radek Krejci | 70124c8 | 2020-08-14 22:17:03 +0200 | [diff] [blame] | 3522 | 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] | 3523 | } |
| 3524 | } |
| 3525 | } |
| 3526 | set_scnode_clear_ctx(set); |
| 3527 | return rc; |
| 3528 | } |
| 3529 | |
| 3530 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3531 | rc = lyxp_set_cast(args[i], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3532 | if (rc != LY_SUCCESS) { |
| 3533 | free(str); |
| 3534 | return rc; |
| 3535 | } |
| 3536 | |
| 3537 | str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char)); |
| 3538 | LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM); |
| 3539 | strcpy(str + used - 1, args[i]->val.str); |
| 3540 | used += strlen(args[i]->val.str); |
| 3541 | } |
| 3542 | |
| 3543 | /* free, kind of */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3544 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3545 | set->type = LYXP_SET_STRING; |
| 3546 | set->val.str = str; |
| 3547 | |
| 3548 | return LY_SUCCESS; |
| 3549 | } |
| 3550 | |
| 3551 | /** |
| 3552 | * @brief Execute the XPath contains(string, string) function. |
| 3553 | * Returns LYXP_SET_BOOLEAN whether the second argument can |
| 3554 | * be found in the first or not. |
| 3555 | * |
| 3556 | * @param[in] args Array of arguments. |
| 3557 | * @param[in] arg_count Count of elements in @p args. |
| 3558 | * @param[in,out] set Context and result set at the same time. |
| 3559 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3560 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3561 | */ |
| 3562 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3563 | 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] | 3564 | { |
| 3565 | struct lysc_node_leaf *sleaf; |
| 3566 | LY_ERR rc = LY_SUCCESS; |
| 3567 | |
| 3568 | if (options & LYXP_SCNODE_ALL) { |
| 3569 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3570 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3571 | 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] | 3572 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3573 | 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] | 3574 | } |
| 3575 | } |
| 3576 | |
| 3577 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3578 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3579 | 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] | 3580 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3581 | 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] | 3582 | } |
| 3583 | } |
| 3584 | set_scnode_clear_ctx(set); |
| 3585 | return rc; |
| 3586 | } |
| 3587 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3588 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3589 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3590 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3591 | LY_CHECK_RET(rc); |
| 3592 | |
| 3593 | if (strstr(args[0]->val.str, args[1]->val.str)) { |
| 3594 | set_fill_boolean(set, 1); |
| 3595 | } else { |
| 3596 | set_fill_boolean(set, 0); |
| 3597 | } |
| 3598 | |
| 3599 | return LY_SUCCESS; |
| 3600 | } |
| 3601 | |
| 3602 | /** |
| 3603 | * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER |
| 3604 | * with the size of the node-set from the argument. |
| 3605 | * |
| 3606 | * @param[in] args Array of arguments. |
| 3607 | * @param[in] arg_count Count of elements in @p args. |
| 3608 | * @param[in,out] set Context and result set at the same time. |
| 3609 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3610 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3611 | */ |
| 3612 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3613 | 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] | 3614 | { |
| 3615 | struct lysc_node *scnode = NULL; |
| 3616 | LY_ERR rc = LY_SUCCESS; |
| 3617 | |
| 3618 | if (options & LYXP_SCNODE_ALL) { |
| 3619 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) { |
| 3620 | 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] | 3621 | } |
| 3622 | set_scnode_clear_ctx(set); |
| 3623 | return rc; |
| 3624 | } |
| 3625 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3626 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3627 | 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] | 3628 | return LY_EVALID; |
| 3629 | } |
| 3630 | |
| 3631 | set_fill_number(set, args[0]->used); |
| 3632 | return LY_SUCCESS; |
| 3633 | } |
| 3634 | |
| 3635 | /** |
| 3636 | * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET |
| 3637 | * with the context with the intial node. |
| 3638 | * |
| 3639 | * @param[in] args Array of arguments. |
| 3640 | * @param[in] arg_count Count of elements in @p args. |
| 3641 | * @param[in,out] set Context and result set at the same time. |
| 3642 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3643 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3644 | */ |
| 3645 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3646 | 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] | 3647 | { |
| 3648 | if (arg_count || args) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3649 | 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] | 3650 | return LY_EVALID; |
| 3651 | } |
| 3652 | |
| 3653 | if (options & LYXP_SCNODE_ALL) { |
| 3654 | set_scnode_clear_ctx(set); |
| 3655 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3656 | 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] | 3657 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3658 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3659 | |
| 3660 | /* position is filled later */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3661 | set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3662 | } |
| 3663 | |
| 3664 | return LY_SUCCESS; |
| 3665 | } |
| 3666 | |
| 3667 | /** |
| 3668 | * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either |
| 3669 | * leafref or instance-identifier target node(s). |
| 3670 | * |
| 3671 | * @param[in] args Array of arguments. |
| 3672 | * @param[in] arg_count Count of elements in @p args. |
| 3673 | * @param[in,out] set Context and result set at the same time. |
| 3674 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3675 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3676 | */ |
| 3677 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3678 | 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] | 3679 | { |
| 3680 | struct lyd_node_term *leaf; |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3681 | struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3682 | struct lysc_type_leafref *lref; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3683 | const struct lysc_node *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3684 | struct ly_path *p; |
| 3685 | struct lyd_node *node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3686 | char *errmsg = NULL; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3687 | uint8_t oper; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3688 | LY_ERR rc = LY_SUCCESS; |
| 3689 | |
| 3690 | if (options & LYXP_SCNODE_ALL) { |
| 3691 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3692 | 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] | 3693 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3694 | 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] | 3695 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
| 3696 | 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] | 3697 | __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3698 | } |
| 3699 | set_scnode_clear_ctx(set); |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3700 | if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3701 | lref = (struct lysc_type_leafref *)sleaf->type; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3702 | 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] | 3703 | |
| 3704 | /* it was already evaluated on schema, it must succeed */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3705 | rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, |
| 3706 | 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] | 3707 | assert(!rc); |
| 3708 | |
| 3709 | /* get the target node */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3710 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3711 | ly_path_free(set->ctx, p); |
| 3712 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 3713 | 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] | 3714 | } |
| 3715 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3716 | return rc; |
| 3717 | } |
| 3718 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3719 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3720 | 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] | 3721 | return LY_EVALID; |
| 3722 | } |
| 3723 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3724 | lyxp_set_free_content(set); |
| 3725 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3726 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3727 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3728 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 3729 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3730 | /* find leafref target */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3731 | 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] | 3732 | &leaf->value, set->tree, &node, &errmsg)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3733 | LOGERR(set->ctx, LY_EVALID, errmsg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3734 | free(errmsg); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3735 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3736 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3737 | } else { |
| 3738 | assert(sleaf->type->basetype == LY_TYPE_INST); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3739 | if (ly_path_eval(leaf->value.target, set->tree, &node)) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3740 | 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] | 3741 | LYD_CANON_VALUE(leaf)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3742 | return LY_EVALID; |
| 3743 | } |
| 3744 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3745 | |
| 3746 | /* insert it */ |
| 3747 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3748 | } |
| 3749 | } |
| 3750 | |
| 3751 | return LY_SUCCESS; |
| 3752 | } |
| 3753 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3754 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3755 | 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] | 3756 | { |
| 3757 | uint16_t i; |
| 3758 | LY_ARRAY_COUNT_TYPE u; |
| 3759 | struct lyd_node_term *leaf; |
| 3760 | struct lysc_node_leaf *sleaf; |
| 3761 | struct lyd_meta *meta; |
| 3762 | struct lyd_value data = {0}, *val; |
| 3763 | struct ly_err_item *err = NULL; |
| 3764 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3765 | ly_bool found; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3766 | |
| 3767 | if (options & LYXP_SCNODE_ALL) { |
| 3768 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3769 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func); |
| 3770 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3771 | 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] | 3772 | sleaf->name); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3773 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 3774 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name); |
| 3775 | } |
| 3776 | |
| 3777 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3778 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3779 | 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] | 3780 | sleaf->name); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3781 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3782 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name); |
| 3783 | } |
| 3784 | } |
| 3785 | set_scnode_clear_ctx(set); |
| 3786 | return rc; |
| 3787 | } |
| 3788 | |
| 3789 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3790 | 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] | 3791 | "derived-from(-or-self)(node-set, string)"); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3792 | return LY_EVALID; |
| 3793 | } |
| 3794 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
| 3795 | LY_CHECK_RET(rc); |
| 3796 | |
| 3797 | set_fill_boolean(set, 0); |
| 3798 | found = 0; |
| 3799 | for (i = 0; i < args[0]->used; ++i) { |
| 3800 | if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) { |
| 3801 | continue; |
| 3802 | } |
| 3803 | |
| 3804 | if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 3805 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 3806 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3807 | val = &leaf->value; |
| 3808 | if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) { |
| 3809 | /* uninteresting */ |
| 3810 | continue; |
| 3811 | } |
| 3812 | |
| 3813 | /* store args[1] as ident */ |
| 3814 | 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] | 3815 | 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] | 3816 | } else { |
| 3817 | meta = args[0]->val.meta[i].meta; |
| 3818 | val = &meta->value; |
| 3819 | if (val->realtype->basetype != LY_TYPE_IDENT) { |
| 3820 | /* uninteresting */ |
| 3821 | continue; |
| 3822 | } |
| 3823 | |
| 3824 | /* store args[1] as ident */ |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 3825 | 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] | 3826 | 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] | 3827 | } |
| 3828 | |
| 3829 | if (err) { |
| 3830 | ly_err_print(err); |
| 3831 | ly_err_free(err); |
| 3832 | } |
| 3833 | LY_CHECK_RET(rc); |
| 3834 | |
| 3835 | /* finally check the identity itself */ |
| 3836 | if (self_match && (data.ident == val->ident)) { |
| 3837 | set_fill_boolean(set, 1); |
| 3838 | found = 1; |
| 3839 | } |
| 3840 | if (!found) { |
| 3841 | LY_ARRAY_FOR(data.ident->derived, u) { |
| 3842 | if (data.ident->derived[u] == val->ident) { |
| 3843 | set_fill_boolean(set, 1); |
| 3844 | found = 1; |
| 3845 | break; |
| 3846 | } |
| 3847 | } |
| 3848 | } |
| 3849 | |
| 3850 | /* free temporary value */ |
| 3851 | val->realtype->plugin->free(set->ctx, &data); |
| 3852 | if (found) { |
| 3853 | break; |
| 3854 | } |
| 3855 | } |
| 3856 | |
| 3857 | return LY_SUCCESS; |
| 3858 | } |
| 3859 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3860 | /** |
| 3861 | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3862 | * on whether the first argument nodes contain a node of an identity derived from the second |
| 3863 | * argument identity. |
| 3864 | * |
| 3865 | * @param[in] args Array of arguments. |
| 3866 | * @param[in] arg_count Count of elements in @p args. |
| 3867 | * @param[in,out] set Context and result set at the same time. |
| 3868 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3869 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3870 | */ |
| 3871 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3872 | 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] | 3873 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3874 | return xpath_derived_(args, set, options, 0, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3875 | } |
| 3876 | |
| 3877 | /** |
| 3878 | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3879 | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
| 3880 | * the second argument identity. |
| 3881 | * |
| 3882 | * @param[in] args Array of arguments. |
| 3883 | * @param[in] arg_count Count of elements in @p args. |
| 3884 | * @param[in,out] set Context and result set at the same time. |
| 3885 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3886 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3887 | */ |
| 3888 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3889 | 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] | 3890 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3891 | return xpath_derived_(args, set, options, 1, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3892 | } |
| 3893 | |
| 3894 | /** |
| 3895 | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
| 3896 | * with the integer value of the first node's enum value, otherwise NaN. |
| 3897 | * |
| 3898 | * @param[in] args Array of arguments. |
| 3899 | * @param[in] arg_count Count of elements in @p args. |
| 3900 | * @param[in,out] set Context and result set at the same time. |
| 3901 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3902 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3903 | */ |
| 3904 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3905 | 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] | 3906 | { |
| 3907 | struct lyd_node_term *leaf; |
| 3908 | struct lysc_node_leaf *sleaf; |
| 3909 | LY_ERR rc = LY_SUCCESS; |
| 3910 | |
| 3911 | if (options & LYXP_SCNODE_ALL) { |
| 3912 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3913 | 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] | 3914 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3915 | 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] | 3916 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
| 3917 | 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] | 3918 | } |
| 3919 | set_scnode_clear_ctx(set); |
| 3920 | return rc; |
| 3921 | } |
| 3922 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3923 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3924 | 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] | 3925 | return LY_EVALID; |
| 3926 | } |
| 3927 | |
| 3928 | set_fill_number(set, NAN); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3929 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3930 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3931 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3932 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
| 3933 | set_fill_number(set, leaf->value.enum_item->value); |
| 3934 | } |
| 3935 | } |
| 3936 | |
| 3937 | return LY_SUCCESS; |
| 3938 | } |
| 3939 | |
| 3940 | /** |
| 3941 | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
| 3942 | * with false value. |
| 3943 | * |
| 3944 | * @param[in] args Array of arguments. |
| 3945 | * @param[in] arg_count Count of elements in @p args. |
| 3946 | * @param[in,out] set Context and result set at the same time. |
| 3947 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3948 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3949 | */ |
| 3950 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3951 | 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] | 3952 | { |
| 3953 | if (options & LYXP_SCNODE_ALL) { |
| 3954 | set_scnode_clear_ctx(set); |
| 3955 | return LY_SUCCESS; |
| 3956 | } |
| 3957 | |
| 3958 | set_fill_boolean(set, 0); |
| 3959 | return LY_SUCCESS; |
| 3960 | } |
| 3961 | |
| 3962 | /** |
| 3963 | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
| 3964 | * with the first argument floored (truncated). |
| 3965 | * |
| 3966 | * @param[in] args Array of arguments. |
| 3967 | * @param[in] arg_count Count of elements in @p args. |
| 3968 | * @param[in,out] set Context and result set at the same time. |
| 3969 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3970 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3971 | */ |
| 3972 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3973 | 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] | 3974 | { |
| 3975 | LY_ERR rc; |
| 3976 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3977 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3978 | LY_CHECK_RET(rc); |
| 3979 | if (isfinite(args[0]->val.num)) { |
| 3980 | set_fill_number(set, (long long)args[0]->val.num); |
| 3981 | } |
| 3982 | |
| 3983 | return LY_SUCCESS; |
| 3984 | } |
| 3985 | |
| 3986 | /** |
| 3987 | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
| 3988 | * whether the language of the text matches the one from the argument. |
| 3989 | * |
| 3990 | * @param[in] args Array of arguments. |
| 3991 | * @param[in] arg_count Count of elements in @p args. |
| 3992 | * @param[in,out] set Context and result set at the same time. |
| 3993 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3994 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3995 | */ |
| 3996 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3997 | 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] | 3998 | { |
| 3999 | const struct lyd_node *node; |
| 4000 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4001 | struct lyd_meta *meta = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4002 | const char *val; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4003 | LY_ERR rc = LY_SUCCESS; |
| 4004 | |
| 4005 | if (options & LYXP_SCNODE_ALL) { |
| 4006 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4007 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4008 | 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] | 4009 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4010 | 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] | 4011 | } |
| 4012 | } |
| 4013 | set_scnode_clear_ctx(set); |
| 4014 | return rc; |
| 4015 | } |
| 4016 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4017 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4018 | LY_CHECK_RET(rc); |
| 4019 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4020 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4021 | 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] | 4022 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4023 | } else if (!set->used) { |
| 4024 | set_fill_boolean(set, 0); |
| 4025 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4026 | } |
| 4027 | |
| 4028 | switch (set->val.nodes[0].type) { |
| 4029 | case LYXP_NODE_ELEM: |
| 4030 | case LYXP_NODE_TEXT: |
| 4031 | node = set->val.nodes[0].node; |
| 4032 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4033 | case LYXP_NODE_META: |
| 4034 | node = set->val.meta[0].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4035 | break; |
| 4036 | default: |
| 4037 | /* nothing to do with roots */ |
| 4038 | set_fill_boolean(set, 0); |
| 4039 | return LY_SUCCESS; |
| 4040 | } |
| 4041 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4042 | /* find lang metadata */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 4043 | for ( ; node; node = (struct lyd_node *)node->parent) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4044 | for (meta = node->meta; meta; meta = meta->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4045 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4046 | 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] | 4047 | break; |
| 4048 | } |
| 4049 | } |
| 4050 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4051 | if (meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4052 | break; |
| 4053 | } |
| 4054 | } |
| 4055 | |
| 4056 | /* compare languages */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4057 | if (!meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4058 | set_fill_boolean(set, 0); |
| 4059 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4060 | uint64_t i; |
| 4061 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4062 | val = meta->value.canonical; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4063 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 4064 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
| 4065 | set_fill_boolean(set, 0); |
| 4066 | break; |
| 4067 | } |
| 4068 | } |
| 4069 | if (!args[0]->val.str[i]) { |
| 4070 | if (!val[i] || (val[i] == '-')) { |
| 4071 | set_fill_boolean(set, 1); |
| 4072 | } else { |
| 4073 | set_fill_boolean(set, 0); |
| 4074 | } |
| 4075 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4076 | } |
| 4077 | |
| 4078 | return LY_SUCCESS; |
| 4079 | } |
| 4080 | |
| 4081 | /** |
| 4082 | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
| 4083 | * with the context size. |
| 4084 | * |
| 4085 | * @param[in] args Array of arguments. |
| 4086 | * @param[in] arg_count Count of elements in @p args. |
| 4087 | * @param[in,out] set Context and result set at the same time. |
| 4088 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4089 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4090 | */ |
| 4091 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4092 | 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] | 4093 | { |
| 4094 | if (options & LYXP_SCNODE_ALL) { |
| 4095 | set_scnode_clear_ctx(set); |
| 4096 | return LY_SUCCESS; |
| 4097 | } |
| 4098 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4099 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4100 | 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] | 4101 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4102 | } else if (!set->used) { |
| 4103 | set_fill_number(set, 0); |
| 4104 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4105 | } |
| 4106 | |
| 4107 | set_fill_number(set, set->ctx_size); |
| 4108 | return LY_SUCCESS; |
| 4109 | } |
| 4110 | |
| 4111 | /** |
| 4112 | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
| 4113 | * with the node name without namespace from the argument or the context. |
| 4114 | * |
| 4115 | * @param[in] args Array of arguments. |
| 4116 | * @param[in] arg_count Count of elements in @p args. |
| 4117 | * @param[in,out] set Context and result set at the same time. |
| 4118 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4119 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4120 | */ |
| 4121 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4122 | 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] | 4123 | { |
| 4124 | struct lyxp_set_node *item; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4125 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4126 | /* suppress unused variable warning */ |
| 4127 | (void)options; |
| 4128 | |
| 4129 | if (options & LYXP_SCNODE_ALL) { |
| 4130 | set_scnode_clear_ctx(set); |
| 4131 | return LY_SUCCESS; |
| 4132 | } |
| 4133 | |
| 4134 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4135 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4136 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 4137 | "local-name(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4138 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4139 | } else if (!args[0]->used) { |
| 4140 | set_fill_string(set, "", 0); |
| 4141 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4142 | } |
| 4143 | |
| 4144 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4145 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4146 | |
| 4147 | item = &args[0]->val.nodes[0]; |
| 4148 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4149 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4150 | 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] | 4151 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4152 | } else if (!set->used) { |
| 4153 | set_fill_string(set, "", 0); |
| 4154 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4155 | } |
| 4156 | |
| 4157 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4158 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4159 | |
| 4160 | item = &set->val.nodes[0]; |
| 4161 | } |
| 4162 | |
| 4163 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4164 | case LYXP_NODE_NONE: |
| 4165 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4166 | case LYXP_NODE_ROOT: |
| 4167 | case LYXP_NODE_ROOT_CONFIG: |
| 4168 | case LYXP_NODE_TEXT: |
| 4169 | set_fill_string(set, "", 0); |
| 4170 | break; |
| 4171 | case LYXP_NODE_ELEM: |
| 4172 | set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name)); |
| 4173 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4174 | case LYXP_NODE_META: |
| 4175 | 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] | 4176 | break; |
| 4177 | } |
| 4178 | |
| 4179 | return LY_SUCCESS; |
| 4180 | } |
| 4181 | |
| 4182 | /** |
| 4183 | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
| 4184 | * with the node name fully qualified (with namespace) from the argument or the context. |
| 4185 | * |
| 4186 | * @param[in] args Array of arguments. |
| 4187 | * @param[in] arg_count Count of elements in @p args. |
| 4188 | * @param[in,out] set Context and result set at the same time. |
| 4189 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4190 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4191 | */ |
| 4192 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4193 | 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] | 4194 | { |
| 4195 | struct lyxp_set_node *item; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4196 | struct lys_module *mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4197 | char *str; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4198 | const char *name = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4199 | |
| 4200 | if (options & LYXP_SCNODE_ALL) { |
| 4201 | set_scnode_clear_ctx(set); |
| 4202 | return LY_SUCCESS; |
| 4203 | } |
| 4204 | |
| 4205 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4206 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4207 | 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] | 4208 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4209 | } else if (!args[0]->used) { |
| 4210 | set_fill_string(set, "", 0); |
| 4211 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4212 | } |
| 4213 | |
| 4214 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4215 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4216 | |
| 4217 | item = &args[0]->val.nodes[0]; |
| 4218 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4219 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4220 | 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] | 4221 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4222 | } else if (!set->used) { |
| 4223 | set_fill_string(set, "", 0); |
| 4224 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4225 | } |
| 4226 | |
| 4227 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4228 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4229 | |
| 4230 | item = &set->val.nodes[0]; |
| 4231 | } |
| 4232 | |
| 4233 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4234 | case LYXP_NODE_NONE: |
| 4235 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4236 | case LYXP_NODE_ROOT: |
| 4237 | case LYXP_NODE_ROOT_CONFIG: |
| 4238 | case LYXP_NODE_TEXT: |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4239 | /* keep NULL */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4240 | break; |
| 4241 | case LYXP_NODE_ELEM: |
| 4242 | mod = item->node->schema->module; |
| 4243 | name = item->node->schema->name; |
| 4244 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4245 | case LYXP_NODE_META: |
| 4246 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
| 4247 | name = ((struct lyd_meta *)item->node)->name; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4248 | break; |
| 4249 | } |
| 4250 | |
| 4251 | if (mod && name) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4252 | 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] | 4253 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
| 4254 | set_fill_string(set, str, strlen(str)); |
| 4255 | free(str); |
| 4256 | } else { |
| 4257 | set_fill_string(set, "", 0); |
| 4258 | } |
| 4259 | |
| 4260 | return LY_SUCCESS; |
| 4261 | } |
| 4262 | |
| 4263 | /** |
| 4264 | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
| 4265 | * with the namespace of the node from the argument or the context. |
| 4266 | * |
| 4267 | * @param[in] args Array of arguments. |
| 4268 | * @param[in] arg_count Count of elements in @p args. |
| 4269 | * @param[in,out] set Context and result set at the same time. |
| 4270 | * @param[in] options XPath options. |
| 4271 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4272 | */ |
| 4273 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4274 | 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] | 4275 | { |
| 4276 | struct lyxp_set_node *item; |
| 4277 | struct lys_module *mod; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4278 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4279 | /* suppress unused variable warning */ |
| 4280 | (void)options; |
| 4281 | |
| 4282 | if (options & LYXP_SCNODE_ALL) { |
| 4283 | set_scnode_clear_ctx(set); |
| 4284 | return LY_SUCCESS; |
| 4285 | } |
| 4286 | |
| 4287 | if (arg_count) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4288 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4289 | 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] | 4290 | "namespace-uri(node-set?)"); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4291 | return LY_EVALID; |
| 4292 | } else if (!args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4293 | set_fill_string(set, "", 0); |
| 4294 | return LY_SUCCESS; |
| 4295 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4296 | |
| 4297 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4298 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4299 | |
| 4300 | item = &args[0]->val.nodes[0]; |
| 4301 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4302 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4303 | 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] | 4304 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4305 | } else if (!set->used) { |
| 4306 | set_fill_string(set, "", 0); |
| 4307 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4308 | } |
| 4309 | |
| 4310 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4311 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4312 | |
| 4313 | item = &set->val.nodes[0]; |
| 4314 | } |
| 4315 | |
| 4316 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4317 | case LYXP_NODE_NONE: |
| 4318 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4319 | case LYXP_NODE_ROOT: |
| 4320 | case LYXP_NODE_ROOT_CONFIG: |
| 4321 | case LYXP_NODE_TEXT: |
| 4322 | set_fill_string(set, "", 0); |
| 4323 | break; |
| 4324 | case LYXP_NODE_ELEM: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4325 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4326 | if (item->type == LYXP_NODE_ELEM) { |
| 4327 | mod = item->node->schema->module; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4328 | } else { /* LYXP_NODE_META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4329 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4330 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4331 | } |
| 4332 | |
| 4333 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
| 4334 | break; |
| 4335 | } |
| 4336 | |
| 4337 | return LY_SUCCESS; |
| 4338 | } |
| 4339 | |
| 4340 | /** |
| 4341 | * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET |
| 4342 | * with only nodes from the context. In practice it either leaves the context |
| 4343 | * as it is or returns an empty node set. |
| 4344 | * |
| 4345 | * @param[in] args Array of arguments. |
| 4346 | * @param[in] arg_count Count of elements in @p args. |
| 4347 | * @param[in,out] set Context and result set at the same time. |
| 4348 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4349 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4350 | */ |
| 4351 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4352 | 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] | 4353 | { |
| 4354 | if (options & LYXP_SCNODE_ALL) { |
| 4355 | set_scnode_clear_ctx(set); |
| 4356 | return LY_SUCCESS; |
| 4357 | } |
| 4358 | |
| 4359 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4360 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4361 | } |
| 4362 | return LY_SUCCESS; |
| 4363 | } |
| 4364 | |
| 4365 | /** |
| 4366 | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
| 4367 | * with normalized value (no leading, trailing, double white spaces) of the node |
| 4368 | * from the argument or the context. |
| 4369 | * |
| 4370 | * @param[in] args Array of arguments. |
| 4371 | * @param[in] arg_count Count of elements in @p args. |
| 4372 | * @param[in,out] set Context and result set at the same time. |
| 4373 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4374 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4375 | */ |
| 4376 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4377 | 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] | 4378 | { |
| 4379 | uint16_t i, new_used; |
| 4380 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4381 | ly_bool have_spaces = 0, space_before = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4382 | struct lysc_node_leaf *sleaf; |
| 4383 | LY_ERR rc = LY_SUCCESS; |
| 4384 | |
| 4385 | if (options & LYXP_SCNODE_ALL) { |
| 4386 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4387 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4388 | 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] | 4389 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4390 | 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] | 4391 | } |
| 4392 | } |
| 4393 | set_scnode_clear_ctx(set); |
| 4394 | return rc; |
| 4395 | } |
| 4396 | |
| 4397 | if (arg_count) { |
| 4398 | set_fill_set(set, args[0]); |
| 4399 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4400 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4401 | LY_CHECK_RET(rc); |
| 4402 | |
| 4403 | /* is there any normalization necessary? */ |
| 4404 | for (i = 0; set->val.str[i]; ++i) { |
| 4405 | if (is_xmlws(set->val.str[i])) { |
| 4406 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
| 4407 | have_spaces = 1; |
| 4408 | break; |
| 4409 | } |
| 4410 | space_before = 1; |
| 4411 | } else { |
| 4412 | space_before = 0; |
| 4413 | } |
| 4414 | } |
| 4415 | |
| 4416 | /* yep, there is */ |
| 4417 | if (have_spaces) { |
| 4418 | /* it's enough, at least one character will go, makes space for ending '\0' */ |
| 4419 | new = malloc(strlen(set->val.str) * sizeof(char)); |
| 4420 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4421 | new_used = 0; |
| 4422 | |
| 4423 | space_before = 0; |
| 4424 | for (i = 0; set->val.str[i]; ++i) { |
| 4425 | if (is_xmlws(set->val.str[i])) { |
| 4426 | if ((i == 0) || space_before) { |
| 4427 | space_before = 1; |
| 4428 | continue; |
| 4429 | } else { |
| 4430 | space_before = 1; |
| 4431 | } |
| 4432 | } else { |
| 4433 | space_before = 0; |
| 4434 | } |
| 4435 | |
| 4436 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
| 4437 | ++new_used; |
| 4438 | } |
| 4439 | |
| 4440 | /* at worst there is one trailing space now */ |
| 4441 | if (new_used && is_xmlws(new[new_used - 1])) { |
| 4442 | --new_used; |
| 4443 | } |
| 4444 | |
| 4445 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 4446 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4447 | new[new_used] = '\0'; |
| 4448 | |
| 4449 | free(set->val.str); |
| 4450 | set->val.str = new; |
| 4451 | } |
| 4452 | |
| 4453 | return LY_SUCCESS; |
| 4454 | } |
| 4455 | |
| 4456 | /** |
| 4457 | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
| 4458 | * with the argument converted to boolean and logically inverted. |
| 4459 | * |
| 4460 | * @param[in] args Array of arguments. |
| 4461 | * @param[in] arg_count Count of elements in @p args. |
| 4462 | * @param[in,out] set Context and result set at the same time. |
| 4463 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4464 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4465 | */ |
| 4466 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4467 | 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] | 4468 | { |
| 4469 | if (options & LYXP_SCNODE_ALL) { |
| 4470 | set_scnode_clear_ctx(set); |
| 4471 | return LY_SUCCESS; |
| 4472 | } |
| 4473 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4474 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4475 | if (args[0]->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4476 | set_fill_boolean(set, 0); |
| 4477 | } else { |
| 4478 | set_fill_boolean(set, 1); |
| 4479 | } |
| 4480 | |
| 4481 | return LY_SUCCESS; |
| 4482 | } |
| 4483 | |
| 4484 | /** |
| 4485 | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
| 4486 | * with the number representation of either the argument or the context. |
| 4487 | * |
| 4488 | * @param[in] args Array of arguments. |
| 4489 | * @param[in] arg_count Count of elements in @p args. |
| 4490 | * @param[in,out] set Context and result set at the same time. |
| 4491 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4492 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4493 | */ |
| 4494 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4495 | 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] | 4496 | { |
| 4497 | LY_ERR rc; |
| 4498 | |
| 4499 | if (options & LYXP_SCNODE_ALL) { |
| 4500 | set_scnode_clear_ctx(set); |
| 4501 | return LY_SUCCESS; |
| 4502 | } |
| 4503 | |
| 4504 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4505 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4506 | LY_CHECK_RET(rc); |
| 4507 | set_fill_set(set, args[0]); |
| 4508 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4509 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4510 | LY_CHECK_RET(rc); |
| 4511 | } |
| 4512 | |
| 4513 | return LY_SUCCESS; |
| 4514 | } |
| 4515 | |
| 4516 | /** |
| 4517 | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
| 4518 | * with the context position. |
| 4519 | * |
| 4520 | * @param[in] args Array of arguments. |
| 4521 | * @param[in] arg_count Count of elements in @p args. |
| 4522 | * @param[in,out] set Context and result set at the same time. |
| 4523 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4524 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4525 | */ |
| 4526 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4527 | 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] | 4528 | { |
| 4529 | if (options & LYXP_SCNODE_ALL) { |
| 4530 | set_scnode_clear_ctx(set); |
| 4531 | return LY_SUCCESS; |
| 4532 | } |
| 4533 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4534 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4535 | 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] | 4536 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4537 | } else if (!set->used) { |
| 4538 | set_fill_number(set, 0); |
| 4539 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4540 | } |
| 4541 | |
| 4542 | set_fill_number(set, set->ctx_pos); |
| 4543 | |
| 4544 | /* UNUSED in 'Release' build type */ |
| 4545 | (void)options; |
| 4546 | return LY_SUCCESS; |
| 4547 | } |
| 4548 | |
| 4549 | /** |
| 4550 | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
| 4551 | * depending on whether the second argument regex matches the first argument string. For details refer to |
| 4552 | * YANG 1.1 RFC section 10.2.1. |
| 4553 | * |
| 4554 | * @param[in] args Array of arguments. |
| 4555 | * @param[in] arg_count Count of elements in @p args. |
| 4556 | * @param[in,out] set Context and result set at the same time. |
| 4557 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4558 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4559 | */ |
| 4560 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4561 | 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] | 4562 | { |
| 4563 | struct lysc_pattern **patterns = NULL, **pattern; |
| 4564 | struct lysc_node_leaf *sleaf; |
| 4565 | char *path; |
| 4566 | LY_ERR rc = LY_SUCCESS; |
| 4567 | struct ly_err_item *err; |
| 4568 | |
| 4569 | if (options & LYXP_SCNODE_ALL) { |
| 4570 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4571 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4572 | 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] | 4573 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4574 | 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] | 4575 | } |
| 4576 | } |
| 4577 | |
| 4578 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4579 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4580 | 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] | 4581 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4582 | 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] | 4583 | } |
| 4584 | } |
| 4585 | set_scnode_clear_ctx(set); |
| 4586 | return rc; |
| 4587 | } |
| 4588 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4589 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4590 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4591 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4592 | LY_CHECK_RET(rc); |
| 4593 | |
| 4594 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
| 4595 | *pattern = malloc(sizeof **pattern); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4596 | path = lyd_path(set->cur_node, LYD_PATH_LOG, NULL, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4597 | rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code); |
| 4598 | free(path); |
| 4599 | if (rc != LY_SUCCESS) { |
| 4600 | LY_ARRAY_FREE(patterns); |
| 4601 | return rc; |
| 4602 | } |
| 4603 | |
| 4604 | rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err); |
| 4605 | pcre2_code_free((*pattern)->code); |
| 4606 | free(*pattern); |
| 4607 | LY_ARRAY_FREE(patterns); |
| 4608 | if (rc && (rc != LY_EVALID)) { |
| 4609 | ly_err_print(err); |
| 4610 | ly_err_free(err); |
| 4611 | return rc; |
| 4612 | } |
| 4613 | |
| 4614 | if (rc == LY_EVALID) { |
| 4615 | ly_err_free(err); |
| 4616 | set_fill_boolean(set, 0); |
| 4617 | } else { |
| 4618 | set_fill_boolean(set, 1); |
| 4619 | } |
| 4620 | |
| 4621 | return LY_SUCCESS; |
| 4622 | } |
| 4623 | |
| 4624 | /** |
| 4625 | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
| 4626 | * with the rounded first argument. For details refer to |
| 4627 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
| 4628 | * |
| 4629 | * @param[in] args Array of arguments. |
| 4630 | * @param[in] arg_count Count of elements in @p args. |
| 4631 | * @param[in,out] set Context and result set at the same time. |
| 4632 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4633 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4634 | */ |
| 4635 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4636 | 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] | 4637 | { |
| 4638 | struct lysc_node_leaf *sleaf; |
| 4639 | LY_ERR rc = LY_SUCCESS; |
| 4640 | |
| 4641 | if (options & LYXP_SCNODE_ALL) { |
| 4642 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4643 | 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] | 4644 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4645 | 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] | 4646 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4647 | 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] | 4648 | } |
| 4649 | set_scnode_clear_ctx(set); |
| 4650 | return rc; |
| 4651 | } |
| 4652 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4653 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4654 | LY_CHECK_RET(rc); |
| 4655 | |
| 4656 | /* cover only the cases where floor can't be used */ |
| 4657 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
| 4658 | set_fill_number(set, -0.0f); |
| 4659 | } else { |
| 4660 | args[0]->val.num += 0.5; |
| 4661 | rc = xpath_floor(args, 1, args[0], options); |
| 4662 | LY_CHECK_RET(rc); |
| 4663 | set_fill_number(set, args[0]->val.num); |
| 4664 | } |
| 4665 | |
| 4666 | return LY_SUCCESS; |
| 4667 | } |
| 4668 | |
| 4669 | /** |
| 4670 | * @brief Execute the XPath starts-with(string, string) function. |
| 4671 | * Returns LYXP_SET_BOOLEAN whether the second argument is |
| 4672 | * the prefix of the first or not. |
| 4673 | * |
| 4674 | * @param[in] args Array of arguments. |
| 4675 | * @param[in] arg_count Count of elements in @p args. |
| 4676 | * @param[in,out] set Context and result set at the same time. |
| 4677 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4678 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4679 | */ |
| 4680 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4681 | 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] | 4682 | { |
| 4683 | struct lysc_node_leaf *sleaf; |
| 4684 | LY_ERR rc = LY_SUCCESS; |
| 4685 | |
| 4686 | if (options & LYXP_SCNODE_ALL) { |
| 4687 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4688 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4689 | 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] | 4690 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4691 | 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] | 4692 | } |
| 4693 | } |
| 4694 | |
| 4695 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4696 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4697 | 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] | 4698 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4699 | 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] | 4700 | } |
| 4701 | } |
| 4702 | set_scnode_clear_ctx(set); |
| 4703 | return rc; |
| 4704 | } |
| 4705 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4706 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4707 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4708 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4709 | LY_CHECK_RET(rc); |
| 4710 | |
| 4711 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
| 4712 | set_fill_boolean(set, 0); |
| 4713 | } else { |
| 4714 | set_fill_boolean(set, 1); |
| 4715 | } |
| 4716 | |
| 4717 | return LY_SUCCESS; |
| 4718 | } |
| 4719 | |
| 4720 | /** |
| 4721 | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
| 4722 | * with the string representation of either the argument or the context. |
| 4723 | * |
| 4724 | * @param[in] args Array of arguments. |
| 4725 | * @param[in] arg_count Count of elements in @p args. |
| 4726 | * @param[in,out] set Context and result set at the same time. |
| 4727 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4728 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4729 | */ |
| 4730 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4731 | 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] | 4732 | { |
| 4733 | LY_ERR rc; |
| 4734 | |
| 4735 | if (options & LYXP_SCNODE_ALL) { |
| 4736 | set_scnode_clear_ctx(set); |
| 4737 | return LY_SUCCESS; |
| 4738 | } |
| 4739 | |
| 4740 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4741 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4742 | LY_CHECK_RET(rc); |
| 4743 | set_fill_set(set, args[0]); |
| 4744 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4745 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4746 | LY_CHECK_RET(rc); |
| 4747 | } |
| 4748 | |
| 4749 | return LY_SUCCESS; |
| 4750 | } |
| 4751 | |
| 4752 | /** |
| 4753 | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
| 4754 | * with the length of the string in either the argument or the context. |
| 4755 | * |
| 4756 | * @param[in] args Array of arguments. |
| 4757 | * @param[in] arg_count Count of elements in @p args. |
| 4758 | * @param[in,out] set Context and result set at the same time. |
| 4759 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4760 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4761 | */ |
| 4762 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4763 | 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] | 4764 | { |
| 4765 | struct lysc_node_leaf *sleaf; |
| 4766 | LY_ERR rc = LY_SUCCESS; |
| 4767 | |
| 4768 | if (options & LYXP_SCNODE_ALL) { |
| 4769 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4770 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4771 | 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] | 4772 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4773 | 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] | 4774 | } |
| 4775 | } |
| 4776 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
| 4777 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4778 | 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] | 4779 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4780 | 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] | 4781 | } |
| 4782 | } |
| 4783 | set_scnode_clear_ctx(set); |
| 4784 | return rc; |
| 4785 | } |
| 4786 | |
| 4787 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4788 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4789 | LY_CHECK_RET(rc); |
| 4790 | set_fill_number(set, strlen(args[0]->val.str)); |
| 4791 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4792 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4793 | LY_CHECK_RET(rc); |
| 4794 | set_fill_number(set, strlen(set->val.str)); |
| 4795 | } |
| 4796 | |
| 4797 | return LY_SUCCESS; |
| 4798 | } |
| 4799 | |
| 4800 | /** |
| 4801 | * @brief Execute the XPath substring(string, number, number?) function. |
| 4802 | * Returns LYXP_SET_STRING substring of the first argument starting |
| 4803 | * on the second argument index ending on the third argument index, |
| 4804 | * indexed from 1. For exact definition refer to |
| 4805 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
| 4806 | * |
| 4807 | * @param[in] args Array of arguments. |
| 4808 | * @param[in] arg_count Count of elements in @p args. |
| 4809 | * @param[in,out] set Context and result set at the same time. |
| 4810 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4811 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4812 | */ |
| 4813 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4814 | 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] | 4815 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4816 | int32_t start, len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4817 | uint16_t str_start, str_len, pos; |
| 4818 | struct lysc_node_leaf *sleaf; |
| 4819 | LY_ERR rc = LY_SUCCESS; |
| 4820 | |
| 4821 | if (options & LYXP_SCNODE_ALL) { |
| 4822 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4823 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4824 | 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] | 4825 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4826 | 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] | 4827 | } |
| 4828 | } |
| 4829 | |
| 4830 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4831 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4832 | 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] | 4833 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4834 | 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] | 4835 | } |
| 4836 | } |
| 4837 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4838 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) && |
| 4839 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4840 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4841 | 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] | 4842 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4843 | 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] | 4844 | } |
| 4845 | } |
| 4846 | set_scnode_clear_ctx(set); |
| 4847 | return rc; |
| 4848 | } |
| 4849 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4850 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4851 | LY_CHECK_RET(rc); |
| 4852 | |
| 4853 | /* start */ |
| 4854 | if (xpath_round(&args[1], 1, args[1], options)) { |
| 4855 | return -1; |
| 4856 | } |
| 4857 | if (isfinite(args[1]->val.num)) { |
| 4858 | start = args[1]->val.num - 1; |
| 4859 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4860 | start = INT32_MIN; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4861 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4862 | start = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4863 | } |
| 4864 | |
| 4865 | /* len */ |
| 4866 | if (arg_count == 3) { |
| 4867 | rc = xpath_round(&args[2], 1, args[2], options); |
| 4868 | LY_CHECK_RET(rc); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4869 | if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4870 | len = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4871 | } else if (isfinite(args[2]->val.num)) { |
| 4872 | len = args[2]->val.num; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4873 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4874 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4875 | } |
| 4876 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4877 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4878 | } |
| 4879 | |
| 4880 | /* find matching character positions */ |
| 4881 | str_start = 0; |
| 4882 | str_len = 0; |
| 4883 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
| 4884 | if (pos < start) { |
| 4885 | ++str_start; |
| 4886 | } else if (pos < start + len) { |
| 4887 | ++str_len; |
| 4888 | } else { |
| 4889 | break; |
| 4890 | } |
| 4891 | } |
| 4892 | |
| 4893 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
| 4894 | return LY_SUCCESS; |
| 4895 | } |
| 4896 | |
| 4897 | /** |
| 4898 | * @brief Execute the XPath substring-after(string, string) function. |
| 4899 | * Returns LYXP_SET_STRING with the string succeeding the occurance |
| 4900 | * of the second argument in the first or an empty string. |
| 4901 | * |
| 4902 | * @param[in] args Array of arguments. |
| 4903 | * @param[in] arg_count Count of elements in @p args. |
| 4904 | * @param[in,out] set Context and result set at the same time. |
| 4905 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4906 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4907 | */ |
| 4908 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4909 | 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] | 4910 | { |
| 4911 | char *ptr; |
| 4912 | struct lysc_node_leaf *sleaf; |
| 4913 | LY_ERR rc = LY_SUCCESS; |
| 4914 | |
| 4915 | if (options & LYXP_SCNODE_ALL) { |
| 4916 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4917 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4918 | 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] | 4919 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4920 | 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] | 4921 | } |
| 4922 | } |
| 4923 | |
| 4924 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4925 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4926 | 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] | 4927 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4928 | 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] | 4929 | } |
| 4930 | } |
| 4931 | set_scnode_clear_ctx(set); |
| 4932 | return rc; |
| 4933 | } |
| 4934 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4935 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4936 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4937 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4938 | LY_CHECK_RET(rc); |
| 4939 | |
| 4940 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4941 | if (ptr) { |
| 4942 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
| 4943 | } else { |
| 4944 | set_fill_string(set, "", 0); |
| 4945 | } |
| 4946 | |
| 4947 | return LY_SUCCESS; |
| 4948 | } |
| 4949 | |
| 4950 | /** |
| 4951 | * @brief Execute the XPath substring-before(string, string) function. |
| 4952 | * Returns LYXP_SET_STRING with the string preceding the occurance |
| 4953 | * of the second argument in the first or an empty string. |
| 4954 | * |
| 4955 | * @param[in] args Array of arguments. |
| 4956 | * @param[in] arg_count Count of elements in @p args. |
| 4957 | * @param[in,out] set Context and result set at the same time. |
| 4958 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4959 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4960 | */ |
| 4961 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4962 | 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] | 4963 | { |
| 4964 | char *ptr; |
| 4965 | struct lysc_node_leaf *sleaf; |
| 4966 | LY_ERR rc = LY_SUCCESS; |
| 4967 | |
| 4968 | if (options & LYXP_SCNODE_ALL) { |
| 4969 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4970 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4971 | 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] | 4972 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4973 | 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] | 4974 | } |
| 4975 | } |
| 4976 | |
| 4977 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4978 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4979 | 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] | 4980 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4981 | 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] | 4982 | } |
| 4983 | } |
| 4984 | set_scnode_clear_ctx(set); |
| 4985 | return rc; |
| 4986 | } |
| 4987 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4988 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4989 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4990 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4991 | LY_CHECK_RET(rc); |
| 4992 | |
| 4993 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4994 | if (ptr) { |
| 4995 | set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str); |
| 4996 | } else { |
| 4997 | set_fill_string(set, "", 0); |
| 4998 | } |
| 4999 | |
| 5000 | return LY_SUCCESS; |
| 5001 | } |
| 5002 | |
| 5003 | /** |
| 5004 | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
| 5005 | * with the sum of all the nodes in the context. |
| 5006 | * |
| 5007 | * @param[in] args Array of arguments. |
| 5008 | * @param[in] arg_count Count of elements in @p args. |
| 5009 | * @param[in,out] set Context and result set at the same time. |
| 5010 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5011 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5012 | */ |
| 5013 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5014 | 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] | 5015 | { |
| 5016 | long double num; |
| 5017 | char *str; |
| 5018 | uint16_t i; |
| 5019 | struct lyxp_set set_item; |
| 5020 | struct lysc_node_leaf *sleaf; |
| 5021 | LY_ERR rc = LY_SUCCESS; |
| 5022 | |
| 5023 | if (options & LYXP_SCNODE_ALL) { |
| 5024 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
| 5025 | for (i = 0; i < args[0]->used; ++i) { |
| 5026 | if (args[0]->val.scnodes[i].in_ctx == 1) { |
| 5027 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
| 5028 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5029 | 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] | 5030 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5031 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5032 | 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] | 5033 | } |
| 5034 | } |
| 5035 | } |
| 5036 | } |
| 5037 | set_scnode_clear_ctx(set); |
| 5038 | return rc; |
| 5039 | } |
| 5040 | |
| 5041 | set_fill_number(set, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5042 | |
| 5043 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5044 | 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] | 5045 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5046 | } else if (!args[0]->used) { |
| 5047 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5048 | } |
| 5049 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5050 | set_init(&set_item, set); |
| 5051 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5052 | set_item.type = LYXP_SET_NODE_SET; |
| 5053 | set_item.val.nodes = malloc(sizeof *set_item.val.nodes); |
| 5054 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
| 5055 | |
| 5056 | set_item.used = 1; |
| 5057 | set_item.size = 1; |
| 5058 | |
| 5059 | for (i = 0; i < args[0]->used; ++i) { |
| 5060 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
| 5061 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5062 | rc = cast_node_set_to_string(&set_item, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5063 | LY_CHECK_RET(rc); |
| 5064 | num = cast_string_to_number(str); |
| 5065 | free(str); |
| 5066 | set->val.num += num; |
| 5067 | } |
| 5068 | |
| 5069 | free(set_item.val.nodes); |
| 5070 | |
| 5071 | return LY_SUCCESS; |
| 5072 | } |
| 5073 | |
| 5074 | /** |
| 5075 | * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET |
| 5076 | * with the text content of the nodes in the context. |
| 5077 | * |
| 5078 | * @param[in] args Array of arguments. |
| 5079 | * @param[in] arg_count Count of elements in @p args. |
| 5080 | * @param[in,out] set Context and result set at the same time. |
| 5081 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5082 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5083 | */ |
| 5084 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5085 | 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] | 5086 | { |
| 5087 | uint32_t i; |
| 5088 | |
| 5089 | if (options & LYXP_SCNODE_ALL) { |
| 5090 | set_scnode_clear_ctx(set); |
| 5091 | return LY_SUCCESS; |
| 5092 | } |
| 5093 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5094 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5095 | 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] | 5096 | return LY_EVALID; |
| 5097 | } |
| 5098 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 5099 | for (i = 0; i < set->used; ) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5100 | switch (set->val.nodes[i].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 5101 | case LYXP_NODE_NONE: |
| 5102 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5103 | case LYXP_NODE_ELEM: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5104 | if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 5105 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
| 5106 | ++i; |
| 5107 | break; |
| 5108 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5109 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5110 | case LYXP_NODE_ROOT: |
| 5111 | case LYXP_NODE_ROOT_CONFIG: |
| 5112 | case LYXP_NODE_TEXT: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5113 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5114 | set_remove_node(set, i); |
| 5115 | break; |
| 5116 | } |
| 5117 | } |
| 5118 | |
| 5119 | return LY_SUCCESS; |
| 5120 | } |
| 5121 | |
| 5122 | /** |
| 5123 | * @brief Execute the XPath translate(string, string, string) function. |
| 5124 | * Returns LYXP_SET_STRING with the first argument with the characters |
| 5125 | * from the second argument replaced by those on the corresponding |
| 5126 | * positions in the third argument. |
| 5127 | * |
| 5128 | * @param[in] args Array of arguments. |
| 5129 | * @param[in] arg_count Count of elements in @p args. |
| 5130 | * @param[in,out] set Context and result set at the same time. |
| 5131 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5132 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5133 | */ |
| 5134 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5135 | 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] | 5136 | { |
| 5137 | uint16_t i, j, new_used; |
| 5138 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5139 | ly_bool have_removed; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5140 | struct lysc_node_leaf *sleaf; |
| 5141 | LY_ERR rc = LY_SUCCESS; |
| 5142 | |
| 5143 | if (options & LYXP_SCNODE_ALL) { |
| 5144 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5145 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5146 | 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] | 5147 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5148 | 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] | 5149 | } |
| 5150 | } |
| 5151 | |
| 5152 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5153 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5154 | 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] | 5155 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5156 | 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] | 5157 | } |
| 5158 | } |
| 5159 | |
| 5160 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 5161 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5162 | 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] | 5163 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5164 | 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] | 5165 | } |
| 5166 | } |
| 5167 | set_scnode_clear_ctx(set); |
| 5168 | return rc; |
| 5169 | } |
| 5170 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5171 | rc = lyxp_set_cast(args[0], 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[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5174 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5175 | rc = lyxp_set_cast(args[2], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5176 | LY_CHECK_RET(rc); |
| 5177 | |
| 5178 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
| 5179 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5180 | new_used = 0; |
| 5181 | |
| 5182 | have_removed = 0; |
| 5183 | for (i = 0; args[0]->val.str[i]; ++i) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5184 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5185 | |
| 5186 | for (j = 0; args[1]->val.str[j]; ++j) { |
| 5187 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
| 5188 | /* removing this char */ |
| 5189 | if (j >= strlen(args[2]->val.str)) { |
| 5190 | have_removed = 1; |
| 5191 | found = 1; |
| 5192 | break; |
| 5193 | } |
| 5194 | /* replacing this char */ |
| 5195 | new[new_used] = args[2]->val.str[j]; |
| 5196 | ++new_used; |
| 5197 | found = 1; |
| 5198 | break; |
| 5199 | } |
| 5200 | } |
| 5201 | |
| 5202 | /* copying this char */ |
| 5203 | if (!found) { |
| 5204 | new[new_used] = args[0]->val.str[i]; |
| 5205 | ++new_used; |
| 5206 | } |
| 5207 | } |
| 5208 | |
| 5209 | if (have_removed) { |
| 5210 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 5211 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5212 | } |
| 5213 | new[new_used] = '\0'; |
| 5214 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5215 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5216 | set->type = LYXP_SET_STRING; |
| 5217 | set->val.str = new; |
| 5218 | |
| 5219 | return LY_SUCCESS; |
| 5220 | } |
| 5221 | |
| 5222 | /** |
| 5223 | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
| 5224 | * with true value. |
| 5225 | * |
| 5226 | * @param[in] args Array of arguments. |
| 5227 | * @param[in] arg_count Count of elements in @p args. |
| 5228 | * @param[in,out] set Context and result set at the same time. |
| 5229 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5230 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5231 | */ |
| 5232 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5233 | 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] | 5234 | { |
| 5235 | if (options & LYXP_SCNODE_ALL) { |
| 5236 | set_scnode_clear_ctx(set); |
| 5237 | return LY_SUCCESS; |
| 5238 | } |
| 5239 | |
| 5240 | set_fill_boolean(set, 1); |
| 5241 | return LY_SUCCESS; |
| 5242 | } |
| 5243 | |
| 5244 | /* |
| 5245 | * moveto functions |
| 5246 | * |
| 5247 | * They and only they actually change the context (set). |
| 5248 | */ |
| 5249 | |
| 5250 | /** |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5251 | * @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] | 5252 | * |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5253 | * XPath @p set is expected to be a (sc)node set! |
| 5254 | * |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5255 | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
| 5256 | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5257 | * @param[in] set Set with general XPath context. |
| 5258 | * @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] | 5259 | * @param[out] moveto_mod Expected module of a matching node. |
| 5260 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5261 | */ |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5262 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5263 | moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set, |
| 5264 | const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5265 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 5266 | const struct lys_module *mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5267 | const char *ptr; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5268 | size_t pref_len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5269 | |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5270 | assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET)); |
| 5271 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5272 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
| 5273 | /* specific module */ |
| 5274 | pref_len = ptr - *qname; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5275 | 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] | 5276 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5277 | /* check for errors and non-implemented modules, as they are not valid */ |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5278 | if (!mod || !mod->implemented) { |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5279 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5280 | 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] | 5281 | } else { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5282 | 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] | 5283 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5284 | return LY_EVALID; |
| 5285 | } |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5286 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5287 | *qname += pref_len + 1; |
| 5288 | *qname_len -= pref_len + 1; |
| 5289 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
| 5290 | /* all modules - special case */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5291 | mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5292 | } else { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5293 | switch (set->format) { |
| 5294 | case LY_PREF_SCHEMA: |
| 5295 | case LY_PREF_SCHEMA_RESOLVED: |
| 5296 | /* current module */ |
| 5297 | mod = set->cur_mod; |
| 5298 | break; |
| 5299 | case LY_PREF_JSON: |
| 5300 | /* inherit parent (context node) module */ |
| 5301 | if (ctx_scnode) { |
| 5302 | mod = ctx_scnode->module; |
| 5303 | } else { |
| 5304 | mod = NULL; |
| 5305 | } |
| 5306 | break; |
| 5307 | case LY_PREF_XML: |
| 5308 | /* not defined */ |
| 5309 | LOGINT_RET(set->ctx); |
| 5310 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5311 | } |
| 5312 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5313 | *moveto_mod = mod; |
| 5314 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5315 | } |
| 5316 | |
| 5317 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5318 | * @brief Move context @p set to the root. Handles absolute path. |
| 5319 | * Result is LYXP_SET_NODE_SET. |
| 5320 | * |
| 5321 | * @param[in,out] set Set to use. |
| 5322 | * @param[in] options Xpath options. |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5323 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5324 | */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5325 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5326 | moveto_root(struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5327 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5328 | if (!set) { |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5329 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5330 | } |
| 5331 | |
| 5332 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5333 | set_scnode_clear_ctx(set); |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5334 | 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] | 5335 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5336 | set->type = LYXP_SET_NODE_SET; |
| 5337 | set->used = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5338 | set_insert_node(set, NULL, 0, set->root_type, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5339 | } |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5340 | |
| 5341 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5342 | } |
| 5343 | |
| 5344 | /** |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5345 | * @brief Check whether a node has some unresolved "when". |
| 5346 | * |
| 5347 | * @param[in] node Node to check. |
| 5348 | * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when") |
| 5349 | */ |
| 5350 | static LY_ERR |
| 5351 | moveto_when_check(const struct lyd_node *node) |
| 5352 | { |
| 5353 | const struct lysc_node *schema; |
| 5354 | |
| 5355 | if (!node) { |
| 5356 | return LY_SUCCESS; |
| 5357 | } |
| 5358 | |
| 5359 | schema = node->schema; |
| 5360 | do { |
| 5361 | if (schema->when && !(node->flags & LYD_WHEN_TRUE)) { |
| 5362 | return LY_EINCOMPLETE; |
| 5363 | } |
| 5364 | schema = schema->parent; |
| 5365 | } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 5366 | |
| 5367 | return LY_SUCCESS; |
| 5368 | } |
| 5369 | |
| 5370 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5371 | * @brief Check @p node as a part of NameTest processing. |
| 5372 | * |
| 5373 | * @param[in] node Node to check. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5374 | * @param[in] ctx_node Context node. |
| 5375 | * @param[in] set Set to read general context from. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5376 | * @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] | 5377 | * @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] | 5378 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when, |
| 5379 | * LY_EINVAL if netither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5380 | */ |
| 5381 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5382 | 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] | 5383 | const char *node_name, const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5384 | { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5385 | if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) { |
| 5386 | switch (set->format) { |
| 5387 | case LY_PREF_SCHEMA: |
| 5388 | case LY_PREF_SCHEMA_RESOLVED: |
| 5389 | /* use current module */ |
| 5390 | moveto_mod = set->cur_mod; |
| 5391 | break; |
| 5392 | case LY_PREF_JSON: |
| 5393 | /* inherit module of the context node, if any */ |
| 5394 | if (ctx_node) { |
| 5395 | moveto_mod = ctx_node->schema->module; |
| 5396 | } |
| 5397 | break; |
| 5398 | case LY_PREF_XML: |
| 5399 | /* not defined */ |
| 5400 | LOGINT(set->ctx); |
| 5401 | return LY_EINVAL; |
| 5402 | } |
| 5403 | } |
| 5404 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5405 | /* module check */ |
| 5406 | if (moveto_mod && (node->schema->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5407 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5408 | } |
| 5409 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5410 | /* context check */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5411 | 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] | 5412 | return LY_EINVAL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5413 | } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 5414 | (node->schema != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5415 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5416 | } |
| 5417 | |
| 5418 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5419 | if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5420 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5421 | } |
| 5422 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5423 | /* when check */ |
| 5424 | if (moveto_when_check(node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5425 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5426 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5427 | |
| 5428 | /* match */ |
| 5429 | return LY_SUCCESS; |
| 5430 | } |
| 5431 | |
| 5432 | /** |
| 5433 | * @brief Check @p node as a part of schema NameTest processing. |
| 5434 | * |
| 5435 | * @param[in] node Schema node to check. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5436 | * @param[in] ctx_scnode Context node. |
| 5437 | * @param[in] set Set to read general context from. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5438 | * @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] | 5439 | * @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] | 5440 | * @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] | 5441 | */ |
| 5442 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5443 | 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] | 5444 | const char *node_name, const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5445 | { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5446 | if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) { |
| 5447 | switch (set->format) { |
| 5448 | case LY_PREF_SCHEMA: |
| 5449 | case LY_PREF_SCHEMA_RESOLVED: |
| 5450 | /* use current module */ |
| 5451 | moveto_mod = set->cur_mod; |
| 5452 | break; |
| 5453 | case LY_PREF_JSON: |
| 5454 | /* inherit module of the context node, if any */ |
| 5455 | if (ctx_scnode) { |
| 5456 | moveto_mod = ctx_scnode->module; |
| 5457 | } |
| 5458 | break; |
| 5459 | case LY_PREF_XML: |
| 5460 | /* not defined */ |
| 5461 | LOGINT(set->ctx); |
| 5462 | return LY_EINVAL; |
| 5463 | } |
| 5464 | } |
| 5465 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5466 | /* module check */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5467 | if (moveto_mod && (node->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5468 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5469 | } |
| 5470 | |
| 5471 | /* context check */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5472 | 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] | 5473 | return LY_EINVAL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5474 | } 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] | 5475 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5476 | } |
| 5477 | |
| 5478 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5479 | if (node_name && strcmp(node_name, "*") && (node->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5480 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5481 | } |
| 5482 | |
| 5483 | /* match */ |
| 5484 | return LY_SUCCESS; |
| 5485 | } |
| 5486 | |
| 5487 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5488 | * @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] | 5489 | * |
| 5490 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5491 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5492 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5493 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5494 | */ |
| 5495 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5496 | 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] | 5497 | { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 5498 | uint32_t i; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5499 | const struct lyd_node *siblings, *sub, *ctx_node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5500 | LY_ERR rc; |
| 5501 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5502 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5503 | return LY_SUCCESS; |
| 5504 | } |
| 5505 | |
| 5506 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5507 | 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] | 5508 | return LY_EVALID; |
| 5509 | } |
| 5510 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5511 | for (i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5512 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5513 | |
| 5514 | 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] | 5515 | assert(!set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5516 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5517 | /* search in all the trees */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5518 | ctx_node = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5519 | siblings = set->tree; |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5520 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5521 | /* search in children */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5522 | ctx_node = set->val.nodes[i].node; |
| 5523 | siblings = lyd_child(ctx_node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5524 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5525 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5526 | for (sub = siblings; sub; sub = sub->next) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5527 | rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5528 | if (rc == LY_SUCCESS) { |
| 5529 | if (!replaced) { |
| 5530 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
| 5531 | replaced = 1; |
| 5532 | } else { |
| 5533 | set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5534 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5535 | ++i; |
| 5536 | } else if (rc == LY_EINCOMPLETE) { |
| 5537 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5538 | } |
| 5539 | } |
| 5540 | |
| 5541 | if (!replaced) { |
| 5542 | /* no match */ |
| 5543 | set_remove_node(set, i); |
| 5544 | } |
| 5545 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5546 | |
| 5547 | return LY_SUCCESS; |
| 5548 | } |
| 5549 | |
| 5550 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5551 | * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). |
| 5552 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5553 | * |
| 5554 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5555 | * @param[in] scnode Matching node schema. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5556 | * @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] | 5557 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5558 | */ |
| 5559 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5560 | 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] | 5561 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5562 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5563 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5564 | const struct lyd_node *siblings; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5565 | struct lyd_node *sub, *inst = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5566 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5567 | assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5568 | |
| 5569 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5570 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5571 | } |
| 5572 | |
| 5573 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5574 | 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] | 5575 | ret = LY_EVALID; |
| 5576 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5577 | } |
| 5578 | |
| 5579 | /* context check for all the nodes since we have the schema node */ |
| 5580 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 5581 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5582 | goto cleanup; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 5583 | } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 5584 | (scnode != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5585 | lyxp_set_free_content(set); |
| 5586 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5587 | } |
| 5588 | |
| 5589 | /* create specific data instance if needed */ |
| 5590 | if (scnode->nodetype == LYS_LIST) { |
| 5591 | LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup); |
| 5592 | } else if (scnode->nodetype == LYS_LEAFLIST) { |
| 5593 | 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] | 5594 | } |
| 5595 | |
| 5596 | for (i = 0; i < set->used; ) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5597 | siblings = NULL; |
| 5598 | |
| 5599 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
| 5600 | assert(!set->val.nodes[i].node); |
| 5601 | |
| 5602 | /* search in all the trees */ |
| 5603 | siblings = set->tree; |
| 5604 | } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 5605 | /* search in children */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 5606 | siblings = lyd_child(set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5607 | } |
| 5608 | |
| 5609 | /* find the node using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5610 | if (inst) { |
| 5611 | lyd_find_sibling_first(siblings, inst, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5612 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5613 | lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5614 | } |
| 5615 | |
| 5616 | /* when check */ |
| 5617 | if (sub && moveto_when_check(sub)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5618 | ret = LY_EINCOMPLETE; |
| 5619 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5620 | } |
| 5621 | |
| 5622 | if (sub) { |
| 5623 | /* pos filled later */ |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5624 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5625 | ++i; |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5626 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5627 | /* no match */ |
| 5628 | set_remove_node(set, i); |
| 5629 | } |
| 5630 | } |
| 5631 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5632 | cleanup: |
| 5633 | lyd_free_tree(inst); |
| 5634 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5635 | } |
| 5636 | |
| 5637 | /** |
| 5638 | * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
| 5639 | * |
| 5640 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5641 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5642 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5643 | * @param[in] options XPath options. |
| 5644 | * @return LY_ERR |
| 5645 | */ |
| 5646 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5647 | 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] | 5648 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5649 | ly_bool temp_ctx = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5650 | uint32_t getnext_opts; |
| 5651 | uint32_t orig_used, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5652 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5653 | const struct lysc_node *iter, *start_parent; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5654 | const struct lys_module *mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5655 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5656 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5657 | return LY_SUCCESS; |
| 5658 | } |
| 5659 | |
| 5660 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5661 | 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] | 5662 | return LY_EVALID; |
| 5663 | } |
| 5664 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5665 | /* getnext opts */ |
| 5666 | getnext_opts = LYS_GETNEXT_NOSTATECHECK; |
| 5667 | if (options & LYXP_SCNODE_OUTPUT) { |
| 5668 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 5669 | } |
| 5670 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5671 | orig_used = set->used; |
| 5672 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5673 | uint32_t idx; |
| 5674 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5675 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5676 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5677 | continue; |
| 5678 | } |
| 5679 | |
| 5680 | /* remember context node */ |
| 5681 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5682 | } else { |
| 5683 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5684 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5685 | |
| 5686 | start_parent = set->val.scnodes[i].scnode; |
| 5687 | |
| 5688 | 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] | 5689 | /* 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] | 5690 | * 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] | 5691 | mod_idx = 0; |
Michal Vasko | 9e9f26d | 2020-10-12 16:31:33 +0200 | [diff] [blame] | 5692 | 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] | 5693 | iter = NULL; |
Michal Vasko | 509de4d | 2019-12-10 14:51:30 +0100 | [diff] [blame] | 5694 | /* module may not be implemented */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5695 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5696 | if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5697 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx)); |
| 5698 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5699 | /* we need to prevent these nodes from being considered in this moveto */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5700 | if ((idx < orig_used) && (idx > i)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5701 | set->val.scnodes[idx].in_ctx = 2; |
| 5702 | temp_ctx = 1; |
| 5703 | } |
| 5704 | } |
| 5705 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5706 | } |
| 5707 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5708 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 5709 | iter = NULL; |
| 5710 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5711 | if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5712 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx)); |
| 5713 | |
| 5714 | if ((idx < orig_used) && (idx > i)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5715 | set->val.scnodes[idx].in_ctx = 2; |
| 5716 | temp_ctx = 1; |
| 5717 | } |
| 5718 | } |
| 5719 | } |
| 5720 | } |
| 5721 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5722 | |
| 5723 | /* correct temporary in_ctx values */ |
| 5724 | if (temp_ctx) { |
| 5725 | for (i = 0; i < orig_used; ++i) { |
| 5726 | if (set->val.scnodes[i].in_ctx == 2) { |
| 5727 | set->val.scnodes[i].in_ctx = 1; |
| 5728 | } |
| 5729 | } |
| 5730 | } |
| 5731 | |
| 5732 | return LY_SUCCESS; |
| 5733 | } |
| 5734 | |
| 5735 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5736 | * @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] | 5737 | * Context position aware. |
| 5738 | * |
| 5739 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5740 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5741 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5742 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5743 | */ |
| 5744 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5745 | 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] | 5746 | { |
| 5747 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5748 | const struct lyd_node *next, *elem, *start; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5749 | struct lyxp_set ret_set; |
| 5750 | LY_ERR rc; |
| 5751 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5752 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5753 | return LY_SUCCESS; |
| 5754 | } |
| 5755 | |
| 5756 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5757 | 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] | 5758 | return LY_EVALID; |
| 5759 | } |
| 5760 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5761 | /* 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] | 5762 | rc = moveto_node(set, NULL, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5763 | LY_CHECK_RET(rc); |
| 5764 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5765 | /* 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] | 5766 | set_init(&ret_set, set); |
| 5767 | for (i = 0; i < set->used; ++i) { |
| 5768 | |
| 5769 | /* TREE DFS */ |
| 5770 | start = set->val.nodes[i].node; |
| 5771 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5772 | rc = moveto_node_check(elem, start, set, ncname, moveto_mod); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5773 | if (!rc) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5774 | /* add matching node into result set */ |
| 5775 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
| 5776 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
| 5777 | /* the node is a duplicate, we'll process it later in the set */ |
| 5778 | goto skip_children; |
| 5779 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5780 | } else if (rc == LY_EINCOMPLETE) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5781 | return rc; |
| 5782 | } else if (rc == LY_EINVAL) { |
| 5783 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5784 | } |
| 5785 | |
| 5786 | /* TREE DFS NEXT ELEM */ |
| 5787 | /* select element for the next run - children first */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 5788 | next = lyd_child(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5789 | if (!next) { |
| 5790 | skip_children: |
| 5791 | /* no children, so try siblings, but only if it's not the start, |
| 5792 | * that is considered to be the root and it's siblings are not traversed */ |
| 5793 | if (elem != start) { |
| 5794 | next = elem->next; |
| 5795 | } else { |
| 5796 | break; |
| 5797 | } |
| 5798 | } |
| 5799 | while (!next) { |
| 5800 | /* no siblings, go back through the parents */ |
| 5801 | if ((struct lyd_node *)elem->parent == start) { |
| 5802 | /* we are done, no next element to process */ |
| 5803 | break; |
| 5804 | } |
| 5805 | /* parent is already processed, go to its sibling */ |
| 5806 | elem = (struct lyd_node *)elem->parent; |
| 5807 | next = elem->next; |
| 5808 | } |
| 5809 | } |
| 5810 | } |
| 5811 | |
| 5812 | /* make the temporary set the current one */ |
| 5813 | ret_set.ctx_pos = set->ctx_pos; |
| 5814 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5815 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5816 | memcpy(set, &ret_set, sizeof *set); |
| 5817 | |
| 5818 | return LY_SUCCESS; |
| 5819 | } |
| 5820 | |
| 5821 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5822 | * @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] | 5823 | * |
| 5824 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5825 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5826 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5827 | * @param[in] options XPath options. |
| 5828 | * @return LY_ERR |
| 5829 | */ |
| 5830 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5831 | 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] | 5832 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5833 | uint32_t i, orig_used; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5834 | const struct lysc_node *next, *elem, *start; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5835 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5836 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5837 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5838 | return LY_SUCCESS; |
| 5839 | } |
| 5840 | |
| 5841 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5842 | 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] | 5843 | return LY_EVALID; |
| 5844 | } |
| 5845 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5846 | orig_used = set->used; |
| 5847 | for (i = 0; i < orig_used; ++i) { |
| 5848 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5849 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5850 | continue; |
| 5851 | } |
| 5852 | |
| 5853 | /* remember context node */ |
| 5854 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5855 | } else { |
| 5856 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5857 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5858 | |
| 5859 | /* TREE DFS */ |
| 5860 | start = set->val.scnodes[i].scnode; |
| 5861 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5862 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 5863 | /* schema-only nodes, skip root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5864 | goto next_iter; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5865 | } |
| 5866 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5867 | rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5868 | if (!rc) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5869 | uint32_t idx; |
| 5870 | |
| 5871 | if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5872 | set->val.scnodes[idx].in_ctx = 1; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5873 | if ((uint32_t)idx > i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5874 | /* we will process it later in the set */ |
| 5875 | goto skip_children; |
| 5876 | } |
| 5877 | } else { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5878 | 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] | 5879 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5880 | } else if (rc == LY_EINVAL) { |
| 5881 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5882 | } |
| 5883 | |
| 5884 | next_iter: |
| 5885 | /* TREE DFS NEXT ELEM */ |
| 5886 | /* select element for the next run - children first */ |
| 5887 | 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] | 5888 | if (!next) { |
| 5889 | skip_children: |
| 5890 | /* no children, so try siblings, but only if it's not the start, |
| 5891 | * that is considered to be the root and it's siblings are not traversed */ |
| 5892 | if (elem != start) { |
| 5893 | next = elem->next; |
| 5894 | } else { |
| 5895 | break; |
| 5896 | } |
| 5897 | } |
| 5898 | while (!next) { |
| 5899 | /* no siblings, go back through the parents */ |
| 5900 | if (elem->parent == start) { |
| 5901 | /* we are done, no next element to process */ |
| 5902 | break; |
| 5903 | } |
| 5904 | /* parent is already processed, go to its sibling */ |
| 5905 | elem = elem->parent; |
| 5906 | next = elem->next; |
| 5907 | } |
| 5908 | } |
| 5909 | } |
| 5910 | |
| 5911 | return LY_SUCCESS; |
| 5912 | } |
| 5913 | |
| 5914 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5915 | * @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] | 5916 | * Indirectly context position aware. |
| 5917 | * |
| 5918 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5919 | * @param[in] mod Matching metadata module, NULL for any. |
| 5920 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5921 | * @return LY_ERR |
| 5922 | */ |
| 5923 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5924 | 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] | 5925 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5926 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5927 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5928 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5929 | return LY_SUCCESS; |
| 5930 | } |
| 5931 | |
| 5932 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5933 | 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] | 5934 | return LY_EVALID; |
| 5935 | } |
| 5936 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5937 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5938 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5939 | |
| 5940 | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
| 5941 | * our attributes are always qualified */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5942 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5943 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5944 | |
| 5945 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5946 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5947 | continue; |
| 5948 | } |
| 5949 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5950 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5951 | /* match */ |
| 5952 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5953 | set->val.meta[i].meta = sub; |
| 5954 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5955 | /* pos does not change */ |
| 5956 | replaced = 1; |
| 5957 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5958 | 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] | 5959 | } |
| 5960 | ++i; |
| 5961 | } |
| 5962 | } |
| 5963 | } |
| 5964 | |
| 5965 | if (!replaced) { |
| 5966 | /* no match */ |
| 5967 | set_remove_node(set, i); |
| 5968 | } |
| 5969 | } |
| 5970 | |
| 5971 | return LY_SUCCESS; |
| 5972 | } |
| 5973 | |
| 5974 | /** |
| 5975 | * @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] | 5976 | * Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5977 | * |
| 5978 | * @param[in,out] set1 Set to use for the result. |
| 5979 | * @param[in] set2 Set that is copied to @p set1. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5980 | * @return LY_ERR |
| 5981 | */ |
| 5982 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5983 | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5984 | { |
| 5985 | LY_ERR rc; |
| 5986 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5987 | 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] | 5988 | 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] | 5989 | return LY_EVALID; |
| 5990 | } |
| 5991 | |
| 5992 | /* set2 is empty or both set1 and set2 */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5993 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5994 | return LY_SUCCESS; |
| 5995 | } |
| 5996 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5997 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5998 | memcpy(set1, set2, sizeof *set1); |
| 5999 | /* dynamic memory belongs to set1 now, do not free */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6000 | memset(set2, 0, sizeof *set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6001 | return LY_SUCCESS; |
| 6002 | } |
| 6003 | |
| 6004 | /* we assume sets are sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6005 | assert(!set_sort(set1) && !set_sort(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6006 | |
| 6007 | /* sort, remove duplicates */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6008 | rc = set_sorted_merge(set1, set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6009 | LY_CHECK_RET(rc); |
| 6010 | |
| 6011 | /* final set must be sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6012 | assert(!set_sort(set1)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6013 | |
| 6014 | return LY_SUCCESS; |
| 6015 | } |
| 6016 | |
| 6017 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6018 | * @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] | 6019 | * Context position aware. |
| 6020 | * |
| 6021 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6022 | * @param[in] mod Matching metadata module, NULL for any. |
| 6023 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6024 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6025 | */ |
| 6026 | static int |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6027 | 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] | 6028 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6029 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6030 | struct lyxp_set *set_all_desc = NULL; |
| 6031 | LY_ERR rc; |
| 6032 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6033 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6034 | return LY_SUCCESS; |
| 6035 | } |
| 6036 | |
| 6037 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6038 | 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] | 6039 | return LY_EVALID; |
| 6040 | } |
| 6041 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6042 | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
| 6043 | * but it likely won't be used much, so it's a waste of time */ |
| 6044 | /* copy the context */ |
| 6045 | set_all_desc = set_copy(set); |
| 6046 | /* get all descendant nodes (the original context nodes are removed) */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6047 | rc = moveto_node_alldesc(set_all_desc, NULL, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6048 | if (rc != LY_SUCCESS) { |
| 6049 | lyxp_set_free(set_all_desc); |
| 6050 | return rc; |
| 6051 | } |
| 6052 | /* prepend the original context nodes */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6053 | rc = moveto_union(set, set_all_desc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6054 | if (rc != LY_SUCCESS) { |
| 6055 | lyxp_set_free(set_all_desc); |
| 6056 | return rc; |
| 6057 | } |
| 6058 | lyxp_set_free(set_all_desc); |
| 6059 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6060 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6061 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6062 | |
| 6063 | /* only attributes of an elem can be in the result, skip all the rest, |
| 6064 | * we have all attributes qualified in lyd tree */ |
| 6065 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6066 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6067 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6068 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6069 | continue; |
| 6070 | } |
| 6071 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6072 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6073 | /* match */ |
| 6074 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6075 | set->val.meta[i].meta = sub; |
| 6076 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6077 | /* pos does not change */ |
| 6078 | replaced = 1; |
| 6079 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6080 | 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] | 6081 | } |
| 6082 | ++i; |
| 6083 | } |
| 6084 | } |
| 6085 | } |
| 6086 | |
| 6087 | if (!replaced) { |
| 6088 | /* no match */ |
| 6089 | set_remove_node(set, i); |
| 6090 | } |
| 6091 | } |
| 6092 | |
| 6093 | return LY_SUCCESS; |
| 6094 | } |
| 6095 | |
| 6096 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6097 | * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET. |
| 6098 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6099 | * |
| 6100 | * @param[in] parent Current parent. |
| 6101 | * @param[in] parent_pos Position of @p parent. |
| 6102 | * @param[in] parent_type Node type of @p parent. |
| 6103 | * @param[in,out] to_set Set to use. |
| 6104 | * @param[in] dup_check_set Set for checking duplicities. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6105 | * @param[in] options XPath options. |
| 6106 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6107 | */ |
| 6108 | static LY_ERR |
| 6109 | 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] | 6110 | 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] | 6111 | { |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6112 | const struct lyd_node *iter, *first; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6113 | LY_ERR rc; |
| 6114 | |
| 6115 | switch (parent_type) { |
| 6116 | case LYXP_NODE_ROOT: |
| 6117 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6118 | assert(!parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6119 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6120 | /* add all top-level nodes as elements */ |
| 6121 | first = to_set->tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6122 | break; |
| 6123 | case LYXP_NODE_ELEM: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6124 | /* add just the text node of this term element node */ |
| 6125 | if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6126 | if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) { |
| 6127 | set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used); |
| 6128 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6129 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6130 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6131 | |
| 6132 | /* add all the children of this node */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 6133 | first = lyd_child(parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6134 | break; |
| 6135 | default: |
| 6136 | LOGINT_RET(parent->schema->module->ctx); |
| 6137 | } |
| 6138 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6139 | /* add all top-level nodes as elements */ |
| 6140 | LY_LIST_FOR(first, iter) { |
| 6141 | /* context check */ |
| 6142 | if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) { |
| 6143 | continue; |
| 6144 | } |
| 6145 | |
| 6146 | /* when check */ |
| 6147 | if (moveto_when_check(iter)) { |
| 6148 | return LY_EINCOMPLETE; |
| 6149 | } |
| 6150 | |
| 6151 | if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) { |
| 6152 | set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used); |
| 6153 | |
| 6154 | /* also add all the children of this node, recursively */ |
| 6155 | rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options); |
| 6156 | LY_CHECK_RET(rc); |
| 6157 | } |
| 6158 | } |
| 6159 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6160 | return LY_SUCCESS; |
| 6161 | } |
| 6162 | |
| 6163 | /** |
| 6164 | * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET |
| 6165 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6166 | * |
| 6167 | * @param[in,out] set Set to use. |
| 6168 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6169 | * @param[in] options XPath options. |
| 6170 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6171 | */ |
| 6172 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6173 | 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] | 6174 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6175 | struct lyxp_set ret_set; |
| 6176 | LY_ERR rc; |
| 6177 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6178 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6179 | return LY_SUCCESS; |
| 6180 | } |
| 6181 | |
| 6182 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6183 | 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] | 6184 | return LY_EVALID; |
| 6185 | } |
| 6186 | |
| 6187 | /* nothing to do */ |
| 6188 | if (!all_desc) { |
| 6189 | return LY_SUCCESS; |
| 6190 | } |
| 6191 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6192 | /* add all the children, they get added recursively */ |
| 6193 | set_init(&ret_set, set); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6194 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6195 | /* copy the current node to tmp */ |
| 6196 | set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used); |
| 6197 | |
| 6198 | /* do not touch attributes and text nodes */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6199 | 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] | 6200 | continue; |
| 6201 | } |
| 6202 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6203 | /* add all the children */ |
| 6204 | 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] | 6205 | set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6206 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6207 | lyxp_set_free_content(&ret_set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6208 | return rc; |
| 6209 | } |
| 6210 | } |
| 6211 | |
| 6212 | /* use the temporary set as the current one */ |
| 6213 | ret_set.ctx_pos = set->ctx_pos; |
| 6214 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6215 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6216 | memcpy(set, &ret_set, sizeof *set); |
| 6217 | |
| 6218 | return LY_SUCCESS; |
| 6219 | } |
| 6220 | |
| 6221 | /** |
| 6222 | * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET |
| 6223 | * (or LYXP_SET_EMPTY). |
| 6224 | * |
| 6225 | * @param[in,out] set Set to use. |
| 6226 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6227 | * @param[in] options XPath options. |
| 6228 | * @return LY_ERR |
| 6229 | */ |
| 6230 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6231 | 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] | 6232 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6233 | uint32_t getnext_opts; |
| 6234 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6235 | const struct lysc_node *iter, *start_parent; |
| 6236 | const struct lys_module *mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6237 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6238 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6239 | return LY_SUCCESS; |
| 6240 | } |
| 6241 | |
| 6242 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6243 | 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] | 6244 | return LY_EVALID; |
| 6245 | } |
| 6246 | |
| 6247 | /* nothing to do */ |
| 6248 | if (!all_desc) { |
| 6249 | return LY_SUCCESS; |
| 6250 | } |
| 6251 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6252 | /* getnext opts */ |
| 6253 | getnext_opts = LYS_GETNEXT_NOSTATECHECK; |
| 6254 | if (options & LYXP_SCNODE_OUTPUT) { |
| 6255 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 6256 | } |
| 6257 | |
| 6258 | /* 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] | 6259 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6260 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6261 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6262 | continue; |
| 6263 | } |
| 6264 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6265 | /* remember context node */ |
| 6266 | set->val.scnodes[i].in_ctx = -1; |
| 6267 | } else { |
| 6268 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6269 | } |
| 6270 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6271 | start_parent = set->val.scnodes[i].scnode; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6272 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6273 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
| 6274 | /* it can actually be in any module, it's all <running> */ |
| 6275 | mod_idx = 0; |
| 6276 | while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
| 6277 | iter = NULL; |
| 6278 | /* module may not be implemented */ |
| 6279 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
| 6280 | /* context check */ |
| 6281 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) { |
| 6282 | continue; |
| 6283 | } |
| 6284 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6285 | 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] | 6286 | /* throw away the insert index, we want to consider that node again, recursively */ |
| 6287 | } |
| 6288 | } |
| 6289 | |
| 6290 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 6291 | iter = NULL; |
| 6292 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6293 | /* context check */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6294 | 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] | 6295 | continue; |
| 6296 | } |
| 6297 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6298 | 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] | 6299 | } |
| 6300 | } |
| 6301 | } |
| 6302 | |
| 6303 | return LY_SUCCESS; |
| 6304 | } |
| 6305 | |
| 6306 | /** |
| 6307 | * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET |
| 6308 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6309 | * |
| 6310 | * @param[in] set Set to use. |
| 6311 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6312 | * @param[in] options XPath options. |
| 6313 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6314 | */ |
| 6315 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6316 | 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] | 6317 | { |
| 6318 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6319 | struct lyd_node *node, *new_node; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6320 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6321 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6322 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6323 | return LY_SUCCESS; |
| 6324 | } |
| 6325 | |
| 6326 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6327 | 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] | 6328 | return LY_EVALID; |
| 6329 | } |
| 6330 | |
| 6331 | if (all_desc) { |
| 6332 | /* <path>//.. == <path>//./.. */ |
| 6333 | rc = moveto_self(set, 1, options); |
| 6334 | LY_CHECK_RET(rc); |
| 6335 | } |
| 6336 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6337 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6338 | node = set->val.nodes[i].node; |
| 6339 | |
| 6340 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 6341 | new_node = (struct lyd_node *)node->parent; |
| 6342 | } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) { |
| 6343 | new_node = node; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6344 | } else if (set->val.nodes[i].type == LYXP_NODE_META) { |
| 6345 | new_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6346 | if (!new_node) { |
| 6347 | LOGINT_RET(set->ctx); |
| 6348 | } |
| 6349 | } else { |
| 6350 | /* root does not have a parent */ |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6351 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6352 | continue; |
| 6353 | } |
| 6354 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6355 | /* when check */ |
| 6356 | if (moveto_when_check(new_node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6357 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6358 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6359 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6360 | if (!new_node) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6361 | /* node already there can also be the root */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6362 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6363 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6364 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6365 | /* 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] | 6366 | new_type = LYXP_NODE_ELEM; |
| 6367 | } |
| 6368 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6369 | if (set_dup_node_check(set, new_node, new_type, -1)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6370 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6371 | } else { |
| 6372 | set_replace_node(set, new_node, 0, new_type, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6373 | } |
| 6374 | } |
| 6375 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6376 | set_remove_nodes_none(set); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6377 | assert(!set_sort(set) && !set_sorted_dup_node_clean(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6378 | |
| 6379 | return LY_SUCCESS; |
| 6380 | } |
| 6381 | |
| 6382 | /** |
| 6383 | * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET |
| 6384 | * (or LYXP_SET_EMPTY). |
| 6385 | * |
| 6386 | * @param[in] set Set to use. |
| 6387 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6388 | * @param[in] options XPath options. |
| 6389 | * @return LY_ERR |
| 6390 | */ |
| 6391 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6392 | 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] | 6393 | { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6394 | uint32_t i, orig_used, idx; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6395 | ly_bool temp_ctx = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6396 | const struct lysc_node *node, *new_node; |
| 6397 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6398 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6399 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6400 | return LY_SUCCESS; |
| 6401 | } |
| 6402 | |
| 6403 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6404 | 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] | 6405 | return LY_EVALID; |
| 6406 | } |
| 6407 | |
| 6408 | if (all_desc) { |
| 6409 | /* <path>//.. == <path>//./.. */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6410 | LY_CHECK_RET(moveto_scnode_self(set, 1, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6411 | } |
| 6412 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6413 | orig_used = set->used; |
| 6414 | for (i = 0; i < orig_used; ++i) { |
| 6415 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6416 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6417 | continue; |
| 6418 | } |
| 6419 | |
| 6420 | /* remember context node */ |
| 6421 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 6422 | } else { |
| 6423 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6424 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6425 | |
| 6426 | node = set->val.scnodes[i].scnode; |
| 6427 | |
| 6428 | if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6429 | new_node = lysc_data_parent(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6430 | } else { |
| 6431 | /* root does not have a parent */ |
| 6432 | continue; |
| 6433 | } |
| 6434 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6435 | if (!new_node) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6436 | /* node has no parent */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6437 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6438 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6439 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6440 | /* 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] | 6441 | new_type = LYXP_NODE_ELEM; |
| 6442 | } |
| 6443 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6444 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx)); |
| 6445 | if ((idx < orig_used) && (idx > i)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6446 | set->val.scnodes[idx].in_ctx = 2; |
| 6447 | temp_ctx = 1; |
| 6448 | } |
| 6449 | } |
| 6450 | |
| 6451 | if (temp_ctx) { |
| 6452 | for (i = 0; i < orig_used; ++i) { |
| 6453 | if (set->val.scnodes[i].in_ctx == 2) { |
| 6454 | set->val.scnodes[i].in_ctx = 1; |
| 6455 | } |
| 6456 | } |
| 6457 | } |
| 6458 | |
| 6459 | return LY_SUCCESS; |
| 6460 | } |
| 6461 | |
| 6462 | /** |
| 6463 | * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'. |
| 6464 | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
| 6465 | * |
| 6466 | * @param[in,out] set1 Set to use for the result. |
| 6467 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6468 | * @param[in] op Comparison operator to process. |
| 6469 | * @param[in] options XPath options. |
| 6470 | * @return LY_ERR |
| 6471 | */ |
| 6472 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6473 | 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] | 6474 | { |
| 6475 | /* |
| 6476 | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
| 6477 | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
| 6478 | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6479 | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6480 | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
| 6481 | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6482 | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6483 | * |
| 6484 | * '=' or '!=' |
| 6485 | * BOOLEAN + BOOLEAN |
| 6486 | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6487 | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6488 | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6489 | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6490 | * NUMBER + NUMBER |
| 6491 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6492 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6493 | * STRING + STRING |
| 6494 | * |
| 6495 | * '<=', '<', '>=', '>' |
| 6496 | * NUMBER + NUMBER |
| 6497 | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6498 | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6499 | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6500 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6501 | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6502 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6503 | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6504 | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6505 | */ |
| 6506 | struct lyxp_set iter1, iter2; |
| 6507 | int result; |
| 6508 | int64_t i; |
| 6509 | LY_ERR rc; |
| 6510 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6511 | memset(&iter1, 0, sizeof iter1); |
| 6512 | memset(&iter2, 0, sizeof iter2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6513 | |
| 6514 | /* iterative evaluation with node-sets */ |
| 6515 | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
| 6516 | if (set1->type == LYXP_SET_NODE_SET) { |
| 6517 | for (i = 0; i < set1->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6518 | /* cast set1 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6519 | switch (set2->type) { |
| 6520 | case LYXP_SET_NUMBER: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6521 | rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6522 | break; |
| 6523 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6524 | rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6525 | break; |
| 6526 | default: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6527 | rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6528 | break; |
| 6529 | } |
| 6530 | LY_CHECK_RET(rc); |
| 6531 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6532 | /* canonize set2 */ |
| 6533 | LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc); |
| 6534 | |
| 6535 | /* compare recursively */ |
| 6536 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
| 6537 | lyxp_set_free_content(&iter2); |
| 6538 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6539 | |
| 6540 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6541 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6542 | set_fill_boolean(set1, 1); |
| 6543 | return LY_SUCCESS; |
| 6544 | } |
| 6545 | } |
| 6546 | } else { |
| 6547 | for (i = 0; i < set2->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6548 | /* set set2 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6549 | switch (set1->type) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6550 | case LYXP_SET_NUMBER: |
| 6551 | rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i); |
| 6552 | break; |
| 6553 | case LYXP_SET_BOOLEAN: |
| 6554 | rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i); |
| 6555 | break; |
| 6556 | default: |
| 6557 | rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i); |
| 6558 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6559 | } |
| 6560 | LY_CHECK_RET(rc); |
| 6561 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6562 | /* canonize set1 */ |
| 6563 | 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] | 6564 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6565 | /* compare recursively */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6566 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6567 | lyxp_set_free_content(&iter2); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6568 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6569 | |
| 6570 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6571 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6572 | set_fill_boolean(set1, 1); |
| 6573 | return LY_SUCCESS; |
| 6574 | } |
| 6575 | } |
| 6576 | } |
| 6577 | |
| 6578 | /* false for all nodes */ |
| 6579 | set_fill_boolean(set1, 0); |
| 6580 | return LY_SUCCESS; |
| 6581 | } |
| 6582 | |
| 6583 | /* first convert properly */ |
| 6584 | if ((op[0] == '=') || (op[0] == '!')) { |
| 6585 | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6586 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
| 6587 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6588 | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6589 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6590 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6591 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6592 | LY_CHECK_RET(rc); |
| 6593 | } /* else we have 2 strings */ |
| 6594 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6595 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6596 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6597 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6598 | LY_CHECK_RET(rc); |
| 6599 | } |
| 6600 | |
| 6601 | assert(set1->type == set2->type); |
| 6602 | |
| 6603 | /* compute result */ |
| 6604 | if (op[0] == '=') { |
| 6605 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6606 | result = (set1->val.bln == set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6607 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6608 | result = (set1->val.num == set2->val.num); |
| 6609 | } else { |
| 6610 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6611 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6612 | } |
| 6613 | } else if (op[0] == '!') { |
| 6614 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6615 | result = (set1->val.bln != set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6616 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6617 | result = (set1->val.num != set2->val.num); |
| 6618 | } else { |
| 6619 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6620 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6621 | } |
| 6622 | } else { |
| 6623 | assert(set1->type == LYXP_SET_NUMBER); |
| 6624 | if (op[0] == '<') { |
| 6625 | if (op[1] == '=') { |
| 6626 | result = (set1->val.num <= set2->val.num); |
| 6627 | } else { |
| 6628 | result = (set1->val.num < set2->val.num); |
| 6629 | } |
| 6630 | } else { |
| 6631 | if (op[1] == '=') { |
| 6632 | result = (set1->val.num >= set2->val.num); |
| 6633 | } else { |
| 6634 | result = (set1->val.num > set2->val.num); |
| 6635 | } |
| 6636 | } |
| 6637 | } |
| 6638 | |
| 6639 | /* assign result */ |
| 6640 | if (result) { |
| 6641 | set_fill_boolean(set1, 1); |
| 6642 | } else { |
| 6643 | set_fill_boolean(set1, 0); |
| 6644 | } |
| 6645 | |
| 6646 | return LY_SUCCESS; |
| 6647 | } |
| 6648 | |
| 6649 | /** |
| 6650 | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
| 6651 | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
| 6652 | * |
| 6653 | * @param[in,out] set1 Set to use for the result. |
| 6654 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6655 | * @param[in] op Operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6656 | * @return LY_ERR |
| 6657 | */ |
| 6658 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6659 | 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] | 6660 | { |
| 6661 | LY_ERR rc; |
| 6662 | |
| 6663 | /* unary '-' */ |
| 6664 | if (!set2 && (op[0] == '-')) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6665 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6666 | LY_CHECK_RET(rc); |
| 6667 | set1->val.num *= -1; |
| 6668 | lyxp_set_free(set2); |
| 6669 | return LY_SUCCESS; |
| 6670 | } |
| 6671 | |
| 6672 | assert(set1 && set2); |
| 6673 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6674 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6675 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6676 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6677 | LY_CHECK_RET(rc); |
| 6678 | |
| 6679 | switch (op[0]) { |
| 6680 | /* '+' */ |
| 6681 | case '+': |
| 6682 | set1->val.num += set2->val.num; |
| 6683 | break; |
| 6684 | |
| 6685 | /* '-' */ |
| 6686 | case '-': |
| 6687 | set1->val.num -= set2->val.num; |
| 6688 | break; |
| 6689 | |
| 6690 | /* '*' */ |
| 6691 | case '*': |
| 6692 | set1->val.num *= set2->val.num; |
| 6693 | break; |
| 6694 | |
| 6695 | /* 'div' */ |
| 6696 | case 'd': |
| 6697 | set1->val.num /= set2->val.num; |
| 6698 | break; |
| 6699 | |
| 6700 | /* 'mod' */ |
| 6701 | case 'm': |
| 6702 | set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num); |
| 6703 | break; |
| 6704 | |
| 6705 | default: |
| 6706 | LOGINT_RET(set1->ctx); |
| 6707 | } |
| 6708 | |
| 6709 | return LY_SUCCESS; |
| 6710 | } |
| 6711 | |
| 6712 | /* |
| 6713 | * eval functions |
| 6714 | * |
| 6715 | * They execute a parsed XPath expression on some data subtree. |
| 6716 | */ |
| 6717 | |
| 6718 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6719 | * @brief Evaluate Predicate. Logs directly on error. |
| 6720 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6721 | * [9] Predicate ::= '[' Expr ']' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6722 | * |
| 6723 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6724 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6725 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6726 | * @param[in] options XPath options. |
| 6727 | * @param[in] parent_pos_pred Whether parent predicate was a positional one. |
| 6728 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6729 | */ |
| 6730 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame^] | 6731 | eval_predicate(const 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] | 6732 | { |
| 6733 | LY_ERR rc; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 6734 | uint16_t i, orig_exp; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6735 | uint32_t orig_pos, orig_size; |
| 6736 | int32_t pred_in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6737 | struct lyxp_set set2; |
| 6738 | struct lyd_node *orig_parent; |
| 6739 | |
| 6740 | /* '[' */ |
| 6741 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6742 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6743 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6744 | |
| 6745 | if (!set) { |
| 6746 | only_parse: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6747 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6748 | LY_CHECK_RET(rc); |
| 6749 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 6750 | /* 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] | 6751 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6752 | |
| 6753 | /* empty set, nothing to evaluate */ |
| 6754 | if (!set->used) { |
| 6755 | goto only_parse; |
| 6756 | } |
| 6757 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6758 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6759 | orig_pos = 0; |
| 6760 | orig_size = set->used; |
| 6761 | orig_parent = NULL; |
Michal Vasko | 39dbf35 | 2020-05-21 10:08:59 +0200 | [diff] [blame] | 6762 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6763 | set_init(&set2, set); |
| 6764 | set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0); |
| 6765 | /* remember the node context position for position() and context size for last(), |
| 6766 | * predicates should always be evaluated with respect to the child axis (since we do |
| 6767 | * not support explicit axes) so we assign positions based on their parents */ |
| 6768 | if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) { |
| 6769 | orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent; |
| 6770 | orig_pos = 1; |
| 6771 | } else { |
| 6772 | ++orig_pos; |
| 6773 | } |
| 6774 | |
| 6775 | set2.ctx_pos = orig_pos; |
| 6776 | set2.ctx_size = orig_size; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6777 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6778 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6779 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6780 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6781 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6782 | return rc; |
| 6783 | } |
| 6784 | |
| 6785 | /* number is a position */ |
| 6786 | if (set2.type == LYXP_SET_NUMBER) { |
| 6787 | if ((long long)set2.val.num == orig_pos) { |
| 6788 | set2.val.num = 1; |
| 6789 | } else { |
| 6790 | set2.val.num = 0; |
| 6791 | } |
| 6792 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6793 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6794 | |
| 6795 | /* predicate satisfied or not? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6796 | if (!set2.val.bln) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6797 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6798 | } |
| 6799 | } |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6800 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6801 | |
| 6802 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 6803 | for (i = 0; i < set->used; ++i) { |
| 6804 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6805 | /* there is a currently-valid node */ |
| 6806 | break; |
| 6807 | } |
| 6808 | } |
| 6809 | /* empty set, nothing to evaluate */ |
| 6810 | if (i == set->used) { |
| 6811 | goto only_parse; |
| 6812 | } |
| 6813 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6814 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6815 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6816 | /* set special in_ctx to all the valid snodes */ |
| 6817 | pred_in_ctx = set_scnode_new_in_ctx(set); |
| 6818 | |
| 6819 | /* use the valid snodes one-by-one */ |
| 6820 | for (i = 0; i < set->used; ++i) { |
| 6821 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
| 6822 | continue; |
| 6823 | } |
| 6824 | set->val.scnodes[i].in_ctx = 1; |
| 6825 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6826 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6827 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6828 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6829 | LY_CHECK_RET(rc); |
| 6830 | |
| 6831 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
| 6832 | } |
| 6833 | |
| 6834 | /* restore the state as it was before the predicate */ |
| 6835 | for (i = 0; i < set->used; ++i) { |
| 6836 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6837 | set->val.scnodes[i].in_ctx = 0; |
| 6838 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
| 6839 | set->val.scnodes[i].in_ctx = 1; |
| 6840 | } |
| 6841 | } |
| 6842 | |
| 6843 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6844 | set2.type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6845 | set_fill_set(&set2, set); |
| 6846 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6847 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6848 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6849 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6850 | return rc; |
| 6851 | } |
| 6852 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6853 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6854 | if (!set2.val.bln) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6855 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6856 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6857 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6858 | } |
| 6859 | |
| 6860 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6861 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6862 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6863 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6864 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6865 | |
| 6866 | return LY_SUCCESS; |
| 6867 | } |
| 6868 | |
| 6869 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6870 | * @brief Evaluate Literal. Logs directly on error. |
| 6871 | * |
| 6872 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6873 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6874 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6875 | */ |
| 6876 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame^] | 6877 | eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6878 | { |
| 6879 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6880 | if (exp->tok_len[*tok_idx] == 2) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6881 | set_fill_string(set, "", 0); |
| 6882 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6883 | 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] | 6884 | } |
| 6885 | } |
| 6886 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6887 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6888 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6889 | } |
| 6890 | |
| 6891 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6892 | * @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] | 6893 | * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6894 | * @param[in] exp Full parsed XPath expression. |
| 6895 | * @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] | 6896 | * @param[in] ctx_node Found schema node as the context for the predicate. |
| 6897 | * @param[in] cur_mod Current module for the expression. |
| 6898 | * @param[in] cur_node Current (original context) node. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6899 | * @param[in] format Format of any prefixes in key names/values. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6900 | * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix). |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6901 | * @param[out] predicates Parsed predicates. |
| 6902 | * @param[out] pred_type Type of @p predicates. |
| 6903 | * @return LY_SUCCESS on success, |
| 6904 | * @return LY_ERR on any error. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6905 | */ |
| 6906 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame^] | 6907 | eval_name_test_try_compile_predicates(const struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *ctx_node, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6908 | const struct lys_module *cur_mod, const struct lysc_node *cur_node, LY_PREFIX_FORMAT format, void *prefix_data, |
| 6909 | struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6910 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6911 | LY_ERR ret = LY_SUCCESS; |
| 6912 | uint16_t key_count, e_idx, pred_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6913 | const struct lysc_node *key; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6914 | size_t pred_len; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6915 | uint32_t prev_lo; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6916 | struct lyxp_expr *exp2 = NULL; |
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 | assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6919 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6920 | if (ctx_node->nodetype == LYS_LIST) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6921 | /* get key count */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6922 | if (ctx_node->flags & LYS_KEYLESS) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6923 | return LY_EINVAL; |
| 6924 | } |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6925 | 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] | 6926 | assert(key_count); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6927 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6928 | /* learn where the predicates end */ |
| 6929 | e_idx = *tok_idx; |
| 6930 | while (key_count) { |
| 6931 | /* '[' */ |
| 6932 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6933 | return LY_EINVAL; |
| 6934 | } |
| 6935 | ++e_idx; |
| 6936 | |
| 6937 | /* ']' */ |
| 6938 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6939 | ++e_idx; |
| 6940 | } |
| 6941 | ++e_idx; |
| 6942 | |
| 6943 | /* another presumably key predicate parsed */ |
| 6944 | --key_count; |
| 6945 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6946 | } else { |
| 6947 | /* learn just where this single predicate ends */ |
| 6948 | e_idx = *tok_idx; |
| 6949 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6950 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6951 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6952 | return LY_EINVAL; |
| 6953 | } |
| 6954 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6955 | |
| 6956 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6957 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6958 | ++e_idx; |
| 6959 | } |
| 6960 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6961 | } |
| 6962 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6963 | /* get the joined length of all the keys predicates/of the single leaf-list predicate */ |
| 6964 | pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx]; |
| 6965 | |
| 6966 | /* turn logging off */ |
| 6967 | prev_lo = ly_log_options(0); |
| 6968 | |
| 6969 | /* parse the predicate(s) */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6970 | LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx], |
| 6971 | pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6972 | |
| 6973 | /* compile */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6974 | ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format, |
| 6975 | prefix_data, predicates, pred_type); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6976 | LY_CHECK_GOTO(ret, cleanup); |
| 6977 | |
| 6978 | /* success, the predicate must include all the needed information for hash-based search */ |
| 6979 | *tok_idx = e_idx; |
| 6980 | |
| 6981 | cleanup: |
| 6982 | ly_log_options(prev_lo); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6983 | lyxp_expr_free(ctx_node->module->ctx, exp2); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6984 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6985 | } |
| 6986 | |
| 6987 | /** |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6988 | * @brief Search for/check the next schema node that could be the only matching schema node meaning the |
| 6989 | * data node(s) could be found using a single hash-based search. |
| 6990 | * |
| 6991 | * @param[in] node Next context node to check. |
| 6992 | * @param[in] name Expected node name. |
| 6993 | * @param[in] name_len Length of @p name. |
| 6994 | * @param[in] moveto_mod Expected node module. |
| 6995 | * @param[in] root_type XPath root type. |
| 6996 | * @param[in] no_prefix Whether the node name was unprefixed. |
| 6997 | * @param[in] format Prefix format. |
| 6998 | * @param[in,out] found Previously found node, is updated. |
| 6999 | * @return LY_SUCCESS on success, |
| 7000 | * @return LY_ENOT if the whole check failed and hashes cannot be used. |
| 7001 | */ |
| 7002 | static LY_ERR |
| 7003 | eval_name_test_with_predicate_get_scnode(const struct lyd_node *node, const char *name, uint16_t name_len, |
| 7004 | const struct lys_module *moveto_mod, enum lyxp_node_type root_type, ly_bool no_prefix, LY_PREFIX_FORMAT format, |
| 7005 | const struct lysc_node **found) |
| 7006 | { |
| 7007 | const struct lysc_node *scnode; |
| 7008 | const struct lys_module *mod; |
| 7009 | uint32_t idx = 0; |
| 7010 | |
| 7011 | continue_search: |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7012 | scnode = NULL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7013 | if (!node) { |
| 7014 | if (no_prefix && (format == LY_PREF_JSON)) { |
| 7015 | /* search all modules for a single match */ |
| 7016 | while ((mod = ly_ctx_get_module_iter(moveto_mod->ctx, &idx))) { |
| 7017 | scnode = lys_find_child(NULL, mod, name, name_len, 0, 0); |
| 7018 | if (scnode) { |
| 7019 | /* we have found a match */ |
| 7020 | break; |
| 7021 | } |
| 7022 | } |
| 7023 | |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7024 | if (!scnode) { |
| 7025 | /* all modules searched */ |
| 7026 | idx = 0; |
| 7027 | } |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7028 | } else { |
| 7029 | /* search in top-level */ |
| 7030 | scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0); |
| 7031 | } |
| 7032 | } else if (!*found || (lysc_data_parent(*found) != node->schema)) { |
| 7033 | if (no_prefix && (format == LY_PREF_JSON)) { |
| 7034 | /* we must adjust the module to inherit the one from the context node */ |
| 7035 | moveto_mod = node->schema->module; |
| 7036 | } |
| 7037 | |
| 7038 | /* search in children, do not repeat the same search */ |
| 7039 | 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] | 7040 | } /* else skip redundant search */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7041 | |
| 7042 | /* additional context check */ |
| 7043 | if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 7044 | scnode = NULL; |
| 7045 | } |
| 7046 | |
| 7047 | if (scnode) { |
| 7048 | if (*found) { |
| 7049 | /* we found a schema node with the same name but at different level, give up, too complicated |
| 7050 | * (more hash-based searches would be required, not supported) */ |
| 7051 | return LY_ENOT; |
| 7052 | } else { |
| 7053 | /* remember the found schema node and continue to make sure it can be used */ |
| 7054 | *found = scnode; |
| 7055 | } |
| 7056 | } |
| 7057 | |
| 7058 | if (idx) { |
| 7059 | /* continue searching all the following models */ |
| 7060 | goto continue_search; |
| 7061 | } |
| 7062 | |
| 7063 | return LY_SUCCESS; |
| 7064 | } |
| 7065 | |
| 7066 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7067 | * @brief Evaluate NameTest and any following Predicates. Logs directly on error. |
| 7068 | * |
| 7069 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7070 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7071 | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
| 7072 | * |
| 7073 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7074 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7075 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 7076 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7077 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7078 | * @param[in] options XPath options. |
| 7079 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7080 | */ |
| 7081 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame^] | 7082 | eval_name_test_with_predicate(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool attr_axis, ly_bool all_desc, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7083 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7084 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7085 | char *path; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7086 | const char *ncname, *ncname_dict = NULL; |
| 7087 | uint16_t ncname_len; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7088 | const struct lys_module *moveto_mod = NULL; |
| 7089 | const struct lysc_node *scnode = NULL, *ctx_scnode; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7090 | struct ly_path_predicate *predicates = NULL; |
| 7091 | enum ly_path_pred_type pred_type = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7092 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7093 | ly_bool no_prefix; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7094 | |
| 7095 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7096 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7097 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7098 | |
| 7099 | if (!set) { |
| 7100 | goto moveto; |
| 7101 | } |
| 7102 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7103 | ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]]; |
| 7104 | ncname_len = exp->tok_len[*tok_idx - 1]; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7105 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7106 | /* get the first schema context node */ |
| 7107 | if (set->used) { |
| 7108 | if (set->type == LYXP_SET_NODE_SET) { |
| 7109 | ctx_scnode = set->val.nodes[0].node ? set->val.nodes[0].node->schema : NULL; |
| 7110 | } else { |
| 7111 | ctx_scnode = set->val.scnodes[0].scnode; |
| 7112 | } |
| 7113 | } else { |
| 7114 | ctx_scnode = NULL; |
| 7115 | } |
| 7116 | |
| 7117 | /* parse (and skip) module name, use any context node (if available) just so we get some moveto_mod if there |
| 7118 | * is no prefix for ::LY_PREF_JSON */ |
| 7119 | rc = moveto_resolve_model(&ncname, &ncname_len, set, ctx_scnode, &moveto_mod); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7120 | LY_CHECK_GOTO(rc, cleanup); |
| 7121 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7122 | if (ncname_len == exp->tok_len[*tok_idx - 1]) { |
| 7123 | /* remember that there is no prefix */ |
| 7124 | no_prefix = 1; |
| 7125 | } else { |
| 7126 | no_prefix = 0; |
| 7127 | } |
| 7128 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7129 | if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) { |
| 7130 | /* find the matching schema node in some parent in the context */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7131 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7132 | if (eval_name_test_with_predicate_get_scnode(set->val.nodes[i].node, ncname, ncname_len, moveto_mod, |
| 7133 | set->root_type, no_prefix, set->format, &scnode)) { |
| 7134 | /* check failed */ |
| 7135 | scnode = NULL; |
| 7136 | break; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7137 | } |
| 7138 | } |
| 7139 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7140 | if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 7141 | /* try to create the predicates */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7142 | if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ? |
| 7143 | 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] | 7144 | /* hashes cannot be used */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7145 | scnode = NULL; |
| 7146 | } |
| 7147 | } |
| 7148 | } |
| 7149 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7150 | if (!scnode && moveto_mod) { |
| 7151 | /* we are not able to match based on a schema node and not all the modules match, |
| 7152 | * use dictionary for efficient comparison */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 7153 | 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] | 7154 | } |
| 7155 | |
| 7156 | moveto: |
| 7157 | /* move to the attribute(s), data node(s), or schema node(s) */ |
| 7158 | if (attr_axis) { |
| 7159 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7160 | set_scnode_clear_ctx(set); |
| 7161 | } else { |
| 7162 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7163 | rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7164 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7165 | rc = moveto_attr(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7166 | } |
| 7167 | LY_CHECK_GOTO(rc, cleanup); |
| 7168 | } |
| 7169 | } else { |
| 7170 | if (set && (options & LYXP_SCNODE_ALL)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7171 | int64_t i; |
| 7172 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7173 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7174 | rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7175 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7176 | rc = moveto_scnode(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7177 | } |
| 7178 | LY_CHECK_GOTO(rc, cleanup); |
| 7179 | |
| 7180 | for (i = set->used - 1; i > -1; --i) { |
| 7181 | if (set->val.scnodes[i].in_ctx > 0) { |
| 7182 | break; |
| 7183 | } |
| 7184 | } |
| 7185 | if (i == -1) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7186 | path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7187 | 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] | 7188 | ncname_len, ncname, ncname - exp->expr, exp->expr, path); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7189 | free(path); |
| 7190 | } |
| 7191 | } else { |
| 7192 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7193 | rc = moveto_node_alldesc(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7194 | } else { |
| 7195 | if (scnode) { |
| 7196 | /* we can find the nodes using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7197 | rc = moveto_node_hash(set, scnode, predicates); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7198 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7199 | rc = moveto_node(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7200 | } |
| 7201 | } |
| 7202 | LY_CHECK_GOTO(rc, cleanup); |
| 7203 | } |
| 7204 | } |
| 7205 | |
| 7206 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7207 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7208 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7209 | LY_CHECK_RET(rc); |
| 7210 | } |
| 7211 | |
| 7212 | cleanup: |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7213 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7214 | lydict_remove(set->ctx, ncname_dict); |
| 7215 | ly_path_predicates_free(set->ctx, pred_type, scnode, predicates); |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7216 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7217 | return rc; |
| 7218 | } |
| 7219 | |
| 7220 | /** |
| 7221 | * @brief Evaluate NodeType and any following Predicates. Logs directly on error. |
| 7222 | * |
| 7223 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7224 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7225 | * [8] NodeType ::= 'text' | 'node' |
| 7226 | * |
| 7227 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7228 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7229 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 7230 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7231 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7232 | * @param[in] options XPath options. |
| 7233 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7234 | */ |
| 7235 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame^] | 7236 | eval_node_type_with_predicate(const 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] | 7237 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7238 | { |
| 7239 | LY_ERR rc; |
| 7240 | |
| 7241 | /* TODO */ |
| 7242 | (void)attr_axis; |
| 7243 | (void)all_desc; |
| 7244 | |
| 7245 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7246 | assert(exp->tok_len[*tok_idx] == 4); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7247 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 7248 | set_scnode_clear_ctx(set); |
| 7249 | /* just for the debug message below */ |
| 7250 | set = NULL; |
| 7251 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7252 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7253 | rc = xpath_node(NULL, 0, set, options); |
| 7254 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7255 | assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7256 | rc = xpath_text(NULL, 0, set, options); |
| 7257 | } |
| 7258 | LY_CHECK_RET(rc); |
| 7259 | } |
| 7260 | } |
| 7261 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7262 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7263 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7264 | |
| 7265 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7266 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7267 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7268 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7269 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7270 | |
| 7271 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7272 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7273 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7274 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7275 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7276 | |
| 7277 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7278 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7279 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7280 | LY_CHECK_RET(rc); |
| 7281 | } |
| 7282 | |
| 7283 | return LY_SUCCESS; |
| 7284 | } |
| 7285 | |
| 7286 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7287 | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
| 7288 | * |
| 7289 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 7290 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7291 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7292 | * |
| 7293 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7294 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7295 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7296 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7297 | * @param[in] options XPath options. |
| 7298 | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
| 7299 | */ |
| 7300 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame^] | 7301 | eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set, |
| 7302 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7303 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7304 | ly_bool attr_axis; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7305 | LY_ERR rc; |
| 7306 | |
| 7307 | goto step; |
| 7308 | do { |
| 7309 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7310 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7311 | all_desc = 0; |
| 7312 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7313 | assert(exp->tok_len[*tok_idx] == 2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7314 | all_desc = 1; |
| 7315 | } |
| 7316 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7317 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7318 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7319 | |
| 7320 | step: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7321 | /* evaluate abbreviated axis '@'? if any */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7322 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7323 | attr_axis = 1; |
| 7324 | |
| 7325 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7326 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7327 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7328 | } else { |
| 7329 | attr_axis = 0; |
| 7330 | } |
| 7331 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7332 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7333 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7334 | case LYXP_TOKEN_DOT: |
| 7335 | /* evaluate '.' */ |
| 7336 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7337 | rc = moveto_scnode_self(set, all_desc, options); |
| 7338 | } else { |
| 7339 | rc = moveto_self(set, all_desc, options); |
| 7340 | } |
| 7341 | LY_CHECK_RET(rc); |
| 7342 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7343 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7344 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7345 | break; |
| 7346 | |
| 7347 | case LYXP_TOKEN_DDOT: |
| 7348 | /* evaluate '..' */ |
| 7349 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7350 | rc = moveto_scnode_parent(set, all_desc, options); |
| 7351 | } else { |
| 7352 | rc = moveto_parent(set, all_desc, options); |
| 7353 | } |
| 7354 | LY_CHECK_RET(rc); |
| 7355 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7356 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7357 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7358 | break; |
| 7359 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7360 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7361 | /* evaluate NameTest Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7362 | 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] | 7363 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7364 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7365 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7366 | case LYXP_TOKEN_NODETYPE: |
| 7367 | /* evaluate NodeType Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7368 | 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] | 7369 | LY_CHECK_RET(rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7370 | break; |
| 7371 | |
| 7372 | default: |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 7373 | LOGINT_RET(set ? set->ctx : NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7374 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7375 | } 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] | 7376 | |
| 7377 | return LY_SUCCESS; |
| 7378 | } |
| 7379 | |
| 7380 | /** |
| 7381 | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
| 7382 | * |
| 7383 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 7384 | * |
| 7385 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7386 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7387 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7388 | * @param[in] options XPath options. |
| 7389 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7390 | */ |
| 7391 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame^] | 7392 | eval_absolute_location_path(const 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] | 7393 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7394 | ly_bool all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7395 | |
| 7396 | if (set) { |
| 7397 | /* no matter what tokens follow, we need to be at the root */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7398 | LY_CHECK_RET(moveto_root(set, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7399 | } |
| 7400 | |
| 7401 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7402 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7403 | /* evaluate '/' - deferred */ |
| 7404 | all_desc = 0; |
| 7405 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7406 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7407 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7408 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7409 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7410 | return LY_SUCCESS; |
| 7411 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7412 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7413 | case LYXP_TOKEN_DOT: |
| 7414 | case LYXP_TOKEN_DDOT: |
| 7415 | case LYXP_TOKEN_AT: |
| 7416 | case LYXP_TOKEN_NAMETEST: |
| 7417 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7418 | 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] | 7419 | break; |
| 7420 | default: |
| 7421 | break; |
| 7422 | } |
| 7423 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7424 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 7425 | /* '//' RelativeLocationPath */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7426 | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
| 7427 | all_desc = 1; |
| 7428 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7429 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7430 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7431 | |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7432 | 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] | 7433 | } |
| 7434 | |
| 7435 | return LY_SUCCESS; |
| 7436 | } |
| 7437 | |
| 7438 | /** |
| 7439 | * @brief Evaluate FunctionCall. Logs directly on error. |
| 7440 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7441 | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7442 | * |
| 7443 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7444 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7445 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7446 | * @param[in] options XPath options. |
| 7447 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7448 | */ |
| 7449 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame^] | 7450 | eval_function_call(const 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] | 7451 | { |
| 7452 | LY_ERR rc; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7453 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7454 | 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] | 7455 | uint16_t arg_count = 0, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7456 | struct lyxp_set **args = NULL, **args_aux; |
| 7457 | |
| 7458 | if (set) { |
| 7459 | /* FunctionName */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7460 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7461 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7462 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7463 | xpath_func = &xpath_not; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7464 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7465 | xpath_func = &xpath_sum; |
| 7466 | } |
| 7467 | break; |
| 7468 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7469 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7470 | xpath_func = &xpath_lang; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7471 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7472 | xpath_func = &xpath_last; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7473 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7474 | xpath_func = &xpath_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7475 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7476 | xpath_func = &xpath_true; |
| 7477 | } |
| 7478 | break; |
| 7479 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7480 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7481 | xpath_func = &xpath_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7482 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7483 | xpath_func = &xpath_false; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7484 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7485 | xpath_func = &xpath_floor; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7486 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7487 | xpath_func = &xpath_round; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7488 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7489 | xpath_func = &xpath_deref; |
| 7490 | } |
| 7491 | break; |
| 7492 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7493 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7494 | xpath_func = &xpath_concat; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7495 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7496 | xpath_func = &xpath_number; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7497 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7498 | xpath_func = &xpath_string; |
| 7499 | } |
| 7500 | break; |
| 7501 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7502 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7503 | xpath_func = &xpath_boolean; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7504 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7505 | xpath_func = &xpath_ceiling; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7506 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7507 | xpath_func = &xpath_current; |
| 7508 | } |
| 7509 | break; |
| 7510 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7511 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7512 | xpath_func = &xpath_contains; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7513 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7514 | xpath_func = &xpath_position; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7515 | } 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] | 7516 | xpath_func = &xpath_re_match; |
| 7517 | } |
| 7518 | break; |
| 7519 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7520 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7521 | xpath_func = &xpath_substring; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7522 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7523 | xpath_func = &xpath_translate; |
| 7524 | } |
| 7525 | break; |
| 7526 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7527 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7528 | xpath_func = &xpath_local_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7529 | } 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] | 7530 | xpath_func = &xpath_enum_value; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7531 | } 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] | 7532 | xpath_func = &xpath_bit_is_set; |
| 7533 | } |
| 7534 | break; |
| 7535 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7536 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7537 | xpath_func = &xpath_starts_with; |
| 7538 | } |
| 7539 | break; |
| 7540 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7541 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7542 | xpath_func = &xpath_derived_from; |
| 7543 | } |
| 7544 | break; |
| 7545 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7546 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7547 | xpath_func = &xpath_namespace_uri; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7548 | } 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] | 7549 | xpath_func = &xpath_string_length; |
| 7550 | } |
| 7551 | break; |
| 7552 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7553 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7554 | xpath_func = &xpath_normalize_space; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7555 | } 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] | 7556 | xpath_func = &xpath_substring_after; |
| 7557 | } |
| 7558 | break; |
| 7559 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7560 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7561 | xpath_func = &xpath_substring_before; |
| 7562 | } |
| 7563 | break; |
| 7564 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7565 | 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] | 7566 | xpath_func = &xpath_derived_from_or_self; |
| 7567 | } |
| 7568 | break; |
| 7569 | } |
| 7570 | |
| 7571 | if (!xpath_func) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7572 | 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] | 7573 | return LY_EVALID; |
| 7574 | } |
| 7575 | } |
| 7576 | |
| 7577 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7578 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7579 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7580 | |
| 7581 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7582 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7583 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7584 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7585 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7586 | |
| 7587 | /* ( Expr ( ',' Expr )* )? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7588 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7589 | if (set) { |
| 7590 | args = malloc(sizeof *args); |
| 7591 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7592 | arg_count = 1; |
| 7593 | args[0] = set_copy(set); |
| 7594 | if (!args[0]) { |
| 7595 | rc = LY_EMEM; |
| 7596 | goto cleanup; |
| 7597 | } |
| 7598 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7599 | rc = eval_expr_select(exp, tok_idx, 0, args[0], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7600 | LY_CHECK_GOTO(rc, cleanup); |
| 7601 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7602 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7603 | LY_CHECK_GOTO(rc, cleanup); |
| 7604 | } |
| 7605 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7606 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7607 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7608 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7609 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7610 | |
| 7611 | if (set) { |
| 7612 | ++arg_count; |
| 7613 | args_aux = realloc(args, arg_count * sizeof *args); |
| 7614 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7615 | args = args_aux; |
| 7616 | args[arg_count - 1] = set_copy(set); |
| 7617 | if (!args[arg_count - 1]) { |
| 7618 | rc = LY_EMEM; |
| 7619 | goto cleanup; |
| 7620 | } |
| 7621 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7622 | 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] | 7623 | LY_CHECK_GOTO(rc, cleanup); |
| 7624 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7625 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7626 | LY_CHECK_GOTO(rc, cleanup); |
| 7627 | } |
| 7628 | } |
| 7629 | |
| 7630 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7631 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7632 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7633 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7634 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7635 | |
| 7636 | if (set) { |
| 7637 | /* evaluate function */ |
| 7638 | rc = xpath_func(args, arg_count, set, options); |
| 7639 | |
| 7640 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7641 | /* merge all nodes from arg evaluations */ |
| 7642 | for (i = 0; i < arg_count; ++i) { |
| 7643 | set_scnode_clear_ctx(args[i]); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7644 | lyxp_set_scnode_merge(set, args[i]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7645 | } |
| 7646 | } |
| 7647 | } else { |
| 7648 | rc = LY_SUCCESS; |
| 7649 | } |
| 7650 | |
| 7651 | cleanup: |
| 7652 | for (i = 0; i < arg_count; ++i) { |
| 7653 | lyxp_set_free(args[i]); |
| 7654 | } |
| 7655 | free(args); |
| 7656 | |
| 7657 | return rc; |
| 7658 | } |
| 7659 | |
| 7660 | /** |
| 7661 | * @brief Evaluate Number. Logs directly on error. |
| 7662 | * |
| 7663 | * @param[in] ctx Context for errors. |
| 7664 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7665 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7666 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7667 | * @return LY_ERR |
| 7668 | */ |
| 7669 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame^] | 7670 | eval_number(struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7671 | { |
| 7672 | long double num; |
| 7673 | char *endptr; |
| 7674 | |
| 7675 | if (set) { |
| 7676 | errno = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7677 | num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7678 | if (errno) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7679 | LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 7680 | 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] | 7681 | 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] | 7682 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7683 | } 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] | 7684 | LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 7685 | 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] | 7686 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7687 | return LY_EVALID; |
| 7688 | } |
| 7689 | |
| 7690 | set_fill_number(set, num); |
| 7691 | } |
| 7692 | |
| 7693 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7694 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7695 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7696 | return LY_SUCCESS; |
| 7697 | } |
| 7698 | |
| 7699 | /** |
| 7700 | * @brief Evaluate PathExpr. Logs directly on error. |
| 7701 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7702 | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7703 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 7704 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 7705 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7706 | * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7707 | * |
| 7708 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7709 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7710 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7711 | * @param[in] options XPath options. |
| 7712 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7713 | */ |
| 7714 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame^] | 7715 | eval_path_expr(const 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] | 7716 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7717 | ly_bool all_desc, parent_pos_pred; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7718 | LY_ERR rc; |
| 7719 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7720 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7721 | case LYXP_TOKEN_PAR1: |
| 7722 | /* '(' Expr ')' */ |
| 7723 | |
| 7724 | /* '(' */ |
| 7725 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7726 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7727 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7728 | |
| 7729 | /* Expr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7730 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7731 | LY_CHECK_RET(rc); |
| 7732 | |
| 7733 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7734 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7735 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7736 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7737 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7738 | |
| 7739 | parent_pos_pred = 0; |
| 7740 | goto predicate; |
| 7741 | |
| 7742 | case LYXP_TOKEN_DOT: |
| 7743 | case LYXP_TOKEN_DDOT: |
| 7744 | case LYXP_TOKEN_AT: |
| 7745 | case LYXP_TOKEN_NAMETEST: |
| 7746 | case LYXP_TOKEN_NODETYPE: |
| 7747 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7748 | rc = eval_relative_location_path(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7749 | LY_CHECK_RET(rc); |
| 7750 | break; |
| 7751 | |
| 7752 | case LYXP_TOKEN_FUNCNAME: |
| 7753 | /* FunctionCall */ |
| 7754 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7755 | rc = eval_function_call(exp, tok_idx, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7756 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7757 | rc = eval_function_call(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7758 | } |
| 7759 | LY_CHECK_RET(rc); |
| 7760 | |
| 7761 | parent_pos_pred = 1; |
| 7762 | goto predicate; |
| 7763 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 7764 | case LYXP_TOKEN_OPER_PATH: |
| 7765 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7766 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7767 | rc = eval_absolute_location_path(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7768 | LY_CHECK_RET(rc); |
| 7769 | break; |
| 7770 | |
| 7771 | case LYXP_TOKEN_LITERAL: |
| 7772 | /* Literal */ |
| 7773 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7774 | if (set) { |
| 7775 | set_scnode_clear_ctx(set); |
| 7776 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7777 | eval_literal(exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7778 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7779 | eval_literal(exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7780 | } |
| 7781 | |
| 7782 | parent_pos_pred = 1; |
| 7783 | goto predicate; |
| 7784 | |
| 7785 | case LYXP_TOKEN_NUMBER: |
| 7786 | /* Number */ |
| 7787 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7788 | if (set) { |
| 7789 | set_scnode_clear_ctx(set); |
| 7790 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7791 | rc = eval_number(NULL, exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7792 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7793 | rc = eval_number(set->ctx, exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7794 | } |
| 7795 | LY_CHECK_RET(rc); |
| 7796 | |
| 7797 | parent_pos_pred = 1; |
| 7798 | goto predicate; |
| 7799 | |
| 7800 | default: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7801 | 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] | 7802 | &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7803 | return LY_EVALID; |
| 7804 | } |
| 7805 | |
| 7806 | return LY_SUCCESS; |
| 7807 | |
| 7808 | predicate: |
| 7809 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7810 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7811 | rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7812 | LY_CHECK_RET(rc); |
| 7813 | } |
| 7814 | |
| 7815 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7816 | 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] | 7817 | |
| 7818 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7819 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7820 | all_desc = 0; |
| 7821 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7822 | all_desc = 1; |
| 7823 | } |
| 7824 | |
| 7825 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7826 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7827 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7828 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7829 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7830 | LY_CHECK_RET(rc); |
| 7831 | } |
| 7832 | |
| 7833 | return LY_SUCCESS; |
| 7834 | } |
| 7835 | |
| 7836 | /** |
| 7837 | * @brief Evaluate UnionExpr. Logs directly on error. |
| 7838 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7839 | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7840 | * |
| 7841 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7842 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7843 | * @param[in] repeat How many times this expression is repeated. |
| 7844 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7845 | * @param[in] options XPath options. |
| 7846 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7847 | */ |
| 7848 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame^] | 7849 | eval_union_expr(const 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] | 7850 | { |
| 7851 | LY_ERR rc = LY_SUCCESS; |
| 7852 | struct lyxp_set orig_set, set2; |
| 7853 | uint16_t i; |
| 7854 | |
| 7855 | assert(repeat); |
| 7856 | |
| 7857 | set_init(&orig_set, set); |
| 7858 | set_init(&set2, set); |
| 7859 | |
| 7860 | set_fill_set(&orig_set, set); |
| 7861 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7862 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7863 | LY_CHECK_GOTO(rc, cleanup); |
| 7864 | |
| 7865 | /* ('|' PathExpr)* */ |
| 7866 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7867 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7868 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7869 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7870 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7871 | |
| 7872 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7873 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7874 | LY_CHECK_GOTO(rc, cleanup); |
| 7875 | continue; |
| 7876 | } |
| 7877 | |
| 7878 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7879 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7880 | LY_CHECK_GOTO(rc, cleanup); |
| 7881 | |
| 7882 | /* eval */ |
| 7883 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7884 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7885 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7886 | rc = moveto_union(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7887 | LY_CHECK_GOTO(rc, cleanup); |
| 7888 | } |
| 7889 | } |
| 7890 | |
| 7891 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7892 | lyxp_set_free_content(&orig_set); |
| 7893 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7894 | return rc; |
| 7895 | } |
| 7896 | |
| 7897 | /** |
| 7898 | * @brief Evaluate UnaryExpr. Logs directly on error. |
| 7899 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7900 | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7901 | * |
| 7902 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7903 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7904 | * @param[in] repeat How many times this expression is repeated. |
| 7905 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7906 | * @param[in] options XPath options. |
| 7907 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7908 | */ |
| 7909 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame^] | 7910 | eval_unary_expr(const 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] | 7911 | { |
| 7912 | LY_ERR rc; |
| 7913 | uint16_t this_op, i; |
| 7914 | |
| 7915 | assert(repeat); |
| 7916 | |
| 7917 | /* ('-')+ */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7918 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7919 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7920 | 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] | 7921 | |
| 7922 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7923 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7924 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7925 | } |
| 7926 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7927 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7928 | LY_CHECK_RET(rc); |
| 7929 | |
| 7930 | if (set && (repeat % 2)) { |
| 7931 | if (options & LYXP_SCNODE_ALL) { |
| 7932 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
| 7933 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7934 | 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] | 7935 | LY_CHECK_RET(rc); |
| 7936 | } |
| 7937 | } |
| 7938 | |
| 7939 | return LY_SUCCESS; |
| 7940 | } |
| 7941 | |
| 7942 | /** |
| 7943 | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
| 7944 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7945 | * [18] MultiplicativeExpr ::= UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7946 | * | MultiplicativeExpr '*' UnaryExpr |
| 7947 | * | MultiplicativeExpr 'div' UnaryExpr |
| 7948 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 7949 | * |
| 7950 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7951 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7952 | * @param[in] repeat How many times this expression is repeated. |
| 7953 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7954 | * @param[in] options XPath options. |
| 7955 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7956 | */ |
| 7957 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame^] | 7958 | eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, |
| 7959 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7960 | { |
| 7961 | LY_ERR rc; |
| 7962 | uint16_t this_op; |
| 7963 | struct lyxp_set orig_set, set2; |
| 7964 | uint16_t i; |
| 7965 | |
| 7966 | assert(repeat); |
| 7967 | |
| 7968 | set_init(&orig_set, set); |
| 7969 | set_init(&set2, set); |
| 7970 | |
| 7971 | set_fill_set(&orig_set, set); |
| 7972 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7973 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7974 | LY_CHECK_GOTO(rc, cleanup); |
| 7975 | |
| 7976 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 7977 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7978 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7979 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7980 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7981 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7982 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7983 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7984 | |
| 7985 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7986 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7987 | LY_CHECK_GOTO(rc, cleanup); |
| 7988 | continue; |
| 7989 | } |
| 7990 | |
| 7991 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7992 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7993 | LY_CHECK_GOTO(rc, cleanup); |
| 7994 | |
| 7995 | /* eval */ |
| 7996 | if (options & LYXP_SCNODE_ALL) { |
| 7997 | 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] | 7998 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7999 | set_scnode_clear_ctx(set); |
| 8000 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8001 | 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] | 8002 | LY_CHECK_GOTO(rc, cleanup); |
| 8003 | } |
| 8004 | } |
| 8005 | |
| 8006 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8007 | lyxp_set_free_content(&orig_set); |
| 8008 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8009 | return rc; |
| 8010 | } |
| 8011 | |
| 8012 | /** |
| 8013 | * @brief Evaluate AdditiveExpr. Logs directly on error. |
| 8014 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8015 | * [17] AdditiveExpr ::= MultiplicativeExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8016 | * | AdditiveExpr '+' MultiplicativeExpr |
| 8017 | * | AdditiveExpr '-' MultiplicativeExpr |
| 8018 | * |
| 8019 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8020 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8021 | * @param[in] repeat How many times this expression is repeated. |
| 8022 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8023 | * @param[in] options XPath options. |
| 8024 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8025 | */ |
| 8026 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame^] | 8027 | eval_additive_expr(const 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] | 8028 | { |
| 8029 | LY_ERR rc; |
| 8030 | uint16_t this_op; |
| 8031 | struct lyxp_set orig_set, set2; |
| 8032 | uint16_t i; |
| 8033 | |
| 8034 | assert(repeat); |
| 8035 | |
| 8036 | set_init(&orig_set, set); |
| 8037 | set_init(&set2, set); |
| 8038 | |
| 8039 | set_fill_set(&orig_set, set); |
| 8040 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8041 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8042 | LY_CHECK_GOTO(rc, cleanup); |
| 8043 | |
| 8044 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 8045 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8046 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8047 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8048 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8049 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8050 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8051 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8052 | |
| 8053 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8054 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8055 | LY_CHECK_GOTO(rc, cleanup); |
| 8056 | continue; |
| 8057 | } |
| 8058 | |
| 8059 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8060 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8061 | LY_CHECK_GOTO(rc, cleanup); |
| 8062 | |
| 8063 | /* eval */ |
| 8064 | if (options & LYXP_SCNODE_ALL) { |
| 8065 | 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] | 8066 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8067 | set_scnode_clear_ctx(set); |
| 8068 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8069 | 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] | 8070 | LY_CHECK_GOTO(rc, cleanup); |
| 8071 | } |
| 8072 | } |
| 8073 | |
| 8074 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8075 | lyxp_set_free_content(&orig_set); |
| 8076 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8077 | return rc; |
| 8078 | } |
| 8079 | |
| 8080 | /** |
| 8081 | * @brief Evaluate RelationalExpr. Logs directly on error. |
| 8082 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8083 | * [16] RelationalExpr ::= AdditiveExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8084 | * | RelationalExpr '<' AdditiveExpr |
| 8085 | * | RelationalExpr '>' AdditiveExpr |
| 8086 | * | RelationalExpr '<=' AdditiveExpr |
| 8087 | * | RelationalExpr '>=' AdditiveExpr |
| 8088 | * |
| 8089 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8090 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8091 | * @param[in] repeat How many times this expression is repeated. |
| 8092 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8093 | * @param[in] options XPath options. |
| 8094 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8095 | */ |
| 8096 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame^] | 8097 | eval_relational_expr(const 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] | 8098 | { |
| 8099 | LY_ERR rc; |
| 8100 | uint16_t this_op; |
| 8101 | struct lyxp_set orig_set, set2; |
| 8102 | uint16_t i; |
| 8103 | |
| 8104 | assert(repeat); |
| 8105 | |
| 8106 | set_init(&orig_set, set); |
| 8107 | set_init(&set2, set); |
| 8108 | |
| 8109 | set_fill_set(&orig_set, set); |
| 8110 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8111 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8112 | LY_CHECK_GOTO(rc, cleanup); |
| 8113 | |
| 8114 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 8115 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8116 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8117 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8118 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8119 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8120 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8121 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8122 | |
| 8123 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8124 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8125 | LY_CHECK_GOTO(rc, cleanup); |
| 8126 | continue; |
| 8127 | } |
| 8128 | |
| 8129 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8130 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8131 | LY_CHECK_GOTO(rc, cleanup); |
| 8132 | |
| 8133 | /* eval */ |
| 8134 | if (options & LYXP_SCNODE_ALL) { |
| 8135 | 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] | 8136 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8137 | set_scnode_clear_ctx(set); |
| 8138 | } else { |
| 8139 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8140 | LY_CHECK_GOTO(rc, cleanup); |
| 8141 | } |
| 8142 | } |
| 8143 | |
| 8144 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8145 | lyxp_set_free_content(&orig_set); |
| 8146 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8147 | return rc; |
| 8148 | } |
| 8149 | |
| 8150 | /** |
| 8151 | * @brief Evaluate EqualityExpr. Logs directly on error. |
| 8152 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8153 | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8154 | * | EqualityExpr '!=' RelationalExpr |
| 8155 | * |
| 8156 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8157 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8158 | * @param[in] repeat How many times this expression is repeated. |
| 8159 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8160 | * @param[in] options XPath options. |
| 8161 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8162 | */ |
| 8163 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame^] | 8164 | eval_equality_expr(const 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] | 8165 | { |
| 8166 | LY_ERR rc; |
| 8167 | uint16_t this_op; |
| 8168 | struct lyxp_set orig_set, set2; |
| 8169 | uint16_t i; |
| 8170 | |
| 8171 | assert(repeat); |
| 8172 | |
| 8173 | set_init(&orig_set, set); |
| 8174 | set_init(&set2, set); |
| 8175 | |
| 8176 | set_fill_set(&orig_set, set); |
| 8177 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8178 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8179 | LY_CHECK_GOTO(rc, cleanup); |
| 8180 | |
| 8181 | /* ('=' / '!=' RelationalExpr)* */ |
| 8182 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8183 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8184 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8185 | 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] | 8186 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8187 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8188 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8189 | |
| 8190 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8191 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8192 | LY_CHECK_GOTO(rc, cleanup); |
| 8193 | continue; |
| 8194 | } |
| 8195 | |
| 8196 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8197 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8198 | LY_CHECK_GOTO(rc, cleanup); |
| 8199 | |
| 8200 | /* eval */ |
| 8201 | if (options & LYXP_SCNODE_ALL) { |
| 8202 | 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] | 8203 | warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1); |
| 8204 | 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] | 8205 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8206 | set_scnode_clear_ctx(set); |
| 8207 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8208 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8209 | LY_CHECK_GOTO(rc, cleanup); |
| 8210 | } |
| 8211 | } |
| 8212 | |
| 8213 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8214 | lyxp_set_free_content(&orig_set); |
| 8215 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8216 | return rc; |
| 8217 | } |
| 8218 | |
| 8219 | /** |
| 8220 | * @brief Evaluate AndExpr. Logs directly on error. |
| 8221 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8222 | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8223 | * |
| 8224 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8225 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8226 | * @param[in] repeat How many times this expression is repeated. |
| 8227 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8228 | * @param[in] options XPath options. |
| 8229 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8230 | */ |
| 8231 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame^] | 8232 | eval_and_expr(const 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] | 8233 | { |
| 8234 | LY_ERR rc; |
| 8235 | struct lyxp_set orig_set, set2; |
| 8236 | uint16_t i; |
| 8237 | |
| 8238 | assert(repeat); |
| 8239 | |
| 8240 | set_init(&orig_set, set); |
| 8241 | set_init(&set2, set); |
| 8242 | |
| 8243 | set_fill_set(&orig_set, set); |
| 8244 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8245 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8246 | LY_CHECK_GOTO(rc, cleanup); |
| 8247 | |
| 8248 | /* cast to boolean, we know that will be the final result */ |
| 8249 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8250 | set_scnode_clear_ctx(set); |
| 8251 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8252 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8253 | } |
| 8254 | |
| 8255 | /* ('and' EqualityExpr)* */ |
| 8256 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8257 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8258 | 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] | 8259 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8260 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8261 | |
| 8262 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8263 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) { |
| 8264 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8265 | LY_CHECK_GOTO(rc, cleanup); |
| 8266 | continue; |
| 8267 | } |
| 8268 | |
| 8269 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8270 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8271 | LY_CHECK_GOTO(rc, cleanup); |
| 8272 | |
| 8273 | /* eval - just get boolean value actually */ |
| 8274 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8275 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8276 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8277 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8278 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8279 | set_fill_set(set, &set2); |
| 8280 | } |
| 8281 | } |
| 8282 | |
| 8283 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8284 | lyxp_set_free_content(&orig_set); |
| 8285 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8286 | return rc; |
| 8287 | } |
| 8288 | |
| 8289 | /** |
| 8290 | * @brief Evaluate OrExpr. Logs directly on error. |
| 8291 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8292 | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8293 | * |
| 8294 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8295 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8296 | * @param[in] repeat How many times this expression is repeated. |
| 8297 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8298 | * @param[in] options XPath options. |
| 8299 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8300 | */ |
| 8301 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame^] | 8302 | eval_or_expr(const 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] | 8303 | { |
| 8304 | LY_ERR rc; |
| 8305 | struct lyxp_set orig_set, set2; |
| 8306 | uint16_t i; |
| 8307 | |
| 8308 | assert(repeat); |
| 8309 | |
| 8310 | set_init(&orig_set, set); |
| 8311 | set_init(&set2, set); |
| 8312 | |
| 8313 | set_fill_set(&orig_set, set); |
| 8314 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8315 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8316 | LY_CHECK_GOTO(rc, cleanup); |
| 8317 | |
| 8318 | /* cast to boolean, we know that will be the final result */ |
| 8319 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8320 | set_scnode_clear_ctx(set); |
| 8321 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8322 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8323 | } |
| 8324 | |
| 8325 | /* ('or' AndExpr)* */ |
| 8326 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8327 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8328 | 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] | 8329 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8330 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8331 | |
| 8332 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8333 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) { |
| 8334 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8335 | LY_CHECK_GOTO(rc, cleanup); |
| 8336 | continue; |
| 8337 | } |
| 8338 | |
| 8339 | set_fill_set(&set2, &orig_set); |
| 8340 | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
| 8341 | * but it does not matter */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8342 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8343 | LY_CHECK_GOTO(rc, cleanup); |
| 8344 | |
| 8345 | /* eval - just get boolean value actually */ |
| 8346 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8347 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8348 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8349 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8350 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8351 | set_fill_set(set, &set2); |
| 8352 | } |
| 8353 | } |
| 8354 | |
| 8355 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8356 | lyxp_set_free_content(&orig_set); |
| 8357 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8358 | return rc; |
| 8359 | } |
| 8360 | |
| 8361 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8362 | * @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] | 8363 | * |
| 8364 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8365 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8366 | * @param[in] etype Expression type to evaluate. |
| 8367 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8368 | * @param[in] options XPath options. |
| 8369 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8370 | */ |
| 8371 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame^] | 8372 | eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, |
| 8373 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8374 | { |
| 8375 | uint16_t i, count; |
| 8376 | enum lyxp_expr_type next_etype; |
| 8377 | LY_ERR rc; |
| 8378 | |
| 8379 | /* process operator repeats */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8380 | if (!exp->repeat[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8381 | next_etype = LYXP_EXPR_NONE; |
| 8382 | } else { |
| 8383 | /* find etype repeat */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8384 | for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8385 | |
| 8386 | /* select one-priority lower because etype expression called us */ |
| 8387 | if (i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8388 | next_etype = exp->repeat[*tok_idx][i - 1]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8389 | /* count repeats for that expression */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8390 | 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] | 8391 | } else { |
| 8392 | next_etype = LYXP_EXPR_NONE; |
| 8393 | } |
| 8394 | } |
| 8395 | |
| 8396 | /* decide what expression are we parsing based on the repeat */ |
| 8397 | switch (next_etype) { |
| 8398 | case LYXP_EXPR_OR: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8399 | rc = eval_or_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8400 | break; |
| 8401 | case LYXP_EXPR_AND: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8402 | rc = eval_and_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8403 | break; |
| 8404 | case LYXP_EXPR_EQUALITY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8405 | rc = eval_equality_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8406 | break; |
| 8407 | case LYXP_EXPR_RELATIONAL: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8408 | rc = eval_relational_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8409 | break; |
| 8410 | case LYXP_EXPR_ADDITIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8411 | rc = eval_additive_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8412 | break; |
| 8413 | case LYXP_EXPR_MULTIPLICATIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8414 | rc = eval_multiplicative_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8415 | break; |
| 8416 | case LYXP_EXPR_UNARY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8417 | rc = eval_unary_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8418 | break; |
| 8419 | case LYXP_EXPR_UNION: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8420 | rc = eval_union_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8421 | break; |
| 8422 | case LYXP_EXPR_NONE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8423 | rc = eval_path_expr(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8424 | break; |
| 8425 | default: |
| 8426 | LOGINT_RET(set->ctx); |
| 8427 | } |
| 8428 | |
| 8429 | return rc; |
| 8430 | } |
| 8431 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8432 | /** |
| 8433 | * @brief Get root type. |
| 8434 | * |
| 8435 | * @param[in] ctx_node Context node. |
| 8436 | * @param[in] ctx_scnode Schema context node. |
| 8437 | * @param[in] options XPath options. |
| 8438 | * @return Root type. |
| 8439 | */ |
| 8440 | static enum lyxp_node_type |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8441 | 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] | 8442 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8443 | const struct lysc_node *op; |
| 8444 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8445 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8446 | /* schema */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8447 | 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] | 8448 | |
| 8449 | if (op || (options & LYXP_SCNODE)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8450 | /* general root that can access everything */ |
| 8451 | return LYXP_NODE_ROOT; |
| 8452 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
| 8453 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8454 | return LYXP_NODE_ROOT_CONFIG; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8455 | } |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8456 | return LYXP_NODE_ROOT; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8457 | } |
| 8458 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8459 | /* data */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8460 | op = ctx_node ? ctx_node->schema : NULL; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 8461 | 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] | 8462 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8463 | if (op || !(options & LYXP_SCHEMA)) { |
| 8464 | /* general root that can access everything */ |
| 8465 | return LYXP_NODE_ROOT; |
| 8466 | } else if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8467 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8468 | return LYXP_NODE_ROOT_CONFIG; |
| 8469 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8470 | return LYXP_NODE_ROOT; |
| 8471 | } |
| 8472 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8473 | LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame^] | 8474 | lyxp_eval(const struct lyxp_expr *exp, const struct lys_module *cur_mod, LY_PREFIX_FORMAT format, void *prefix_data, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8475 | 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] | 8476 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8477 | uint16_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8478 | LY_ERR rc; |
| 8479 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8480 | LY_CHECK_ARG_RET(NULL, exp, set, LY_EINVAL); |
| 8481 | if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) { |
| 8482 | LOGARG(NULL, "Current module must be set if schema format is used."); |
| 8483 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8484 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8485 | |
| 8486 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8487 | memset(set, 0, sizeof *set); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8488 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8489 | set->root_type = lyxp_get_root_type(ctx_node, NULL, options); |
| 8490 | set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0); |
| 8491 | |
| 8492 | set->ctx = (struct ly_ctx *)(cur_mod ? cur_mod->ctx : (ctx_node ? LYD_CTX(ctx_node) : (tree ? LYD_CTX(tree) : NULL))); |
| 8493 | set->cur_node = ctx_node; |
| 8494 | for (set->context_op = ctx_node ? ctx_node->schema : NULL; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 8495 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8496 | set->context_op = set->context_op->parent) {} |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 8497 | set->tree = tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8498 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8499 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8500 | set->prefix_data = prefix_data; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8501 | |
| 8502 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8503 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8504 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8505 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8506 | } |
| 8507 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8508 | return rc; |
| 8509 | } |
| 8510 | |
| 8511 | #if 0 |
| 8512 | |
| 8513 | /* full xml printing of set elements, not used currently */ |
| 8514 | |
| 8515 | void |
| 8516 | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
| 8517 | { |
| 8518 | uint32_t i; |
| 8519 | char *str_num; |
| 8520 | struct lyout out; |
| 8521 | |
| 8522 | memset(&out, 0, sizeof out); |
| 8523 | |
| 8524 | out.type = LYOUT_STREAM; |
| 8525 | out.method.f = f; |
| 8526 | |
| 8527 | switch (set->type) { |
| 8528 | case LYXP_SET_EMPTY: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8529 | ly_print_(&out, "Empty XPath set\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8530 | break; |
| 8531 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8532 | ly_print_(&out, "Boolean XPath set:\n"); |
| 8533 | ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8534 | break; |
| 8535 | case LYXP_SET_STRING: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8536 | ly_print_(&out, "String XPath set:\n"); |
| 8537 | ly_print_(&out, "\"%s\"\n\n", set->value.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8538 | break; |
| 8539 | case LYXP_SET_NUMBER: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8540 | ly_print_(&out, "Number XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8541 | |
| 8542 | if (isnan(set->value.num)) { |
| 8543 | str_num = strdup("NaN"); |
| 8544 | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
| 8545 | str_num = strdup("0"); |
| 8546 | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
| 8547 | str_num = strdup("Infinity"); |
| 8548 | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
| 8549 | str_num = strdup("-Infinity"); |
| 8550 | } else if ((long long)set->value.num == set->value.num) { |
| 8551 | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
| 8552 | str_num = NULL; |
| 8553 | } |
| 8554 | } else { |
| 8555 | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
| 8556 | str_num = NULL; |
| 8557 | } |
| 8558 | } |
| 8559 | if (!str_num) { |
| 8560 | LOGMEM; |
| 8561 | return; |
| 8562 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8563 | ly_print_(&out, "%s\n\n", str_num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8564 | free(str_num); |
| 8565 | break; |
| 8566 | case LYXP_SET_NODE_SET: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8567 | ly_print_(&out, "Node XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8568 | |
| 8569 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8570 | ly_print_(&out, "%d. ", i + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8571 | switch (set->node_type[i]) { |
| 8572 | case LYXP_NODE_ROOT_ALL: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8573 | ly_print_(&out, "ROOT all\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8574 | break; |
| 8575 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8576 | ly_print_(&out, "ROOT config\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8577 | break; |
| 8578 | case LYXP_NODE_ROOT_STATE: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8579 | ly_print_(&out, "ROOT state\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8580 | break; |
| 8581 | case LYXP_NODE_ROOT_NOTIF: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8582 | 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] | 8583 | break; |
| 8584 | case LYXP_NODE_ROOT_RPC: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8585 | 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] | 8586 | break; |
| 8587 | case LYXP_NODE_ROOT_OUTPUT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8588 | 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] | 8589 | break; |
| 8590 | case LYXP_NODE_ELEM: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8591 | ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8592 | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8593 | ly_print_(&out, "\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8594 | break; |
| 8595 | case LYXP_NODE_TEXT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8596 | 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] | 8597 | break; |
| 8598 | case LYXP_NODE_ATTR: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8599 | 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] | 8600 | break; |
| 8601 | } |
| 8602 | } |
| 8603 | break; |
| 8604 | } |
| 8605 | } |
| 8606 | |
| 8607 | #endif |
| 8608 | |
| 8609 | LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8610 | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8611 | { |
| 8612 | long double num; |
| 8613 | char *str; |
| 8614 | LY_ERR rc; |
| 8615 | |
| 8616 | if (!set || (set->type == target)) { |
| 8617 | return LY_SUCCESS; |
| 8618 | } |
| 8619 | |
| 8620 | /* it's not possible to convert anything into a node set */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8621 | assert(target != LYXP_SET_NODE_SET); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8622 | |
| 8623 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8624 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8625 | return LY_EINVAL; |
| 8626 | } |
| 8627 | |
| 8628 | /* to STRING */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8629 | 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] | 8630 | switch (set->type) { |
| 8631 | case LYXP_SET_NUMBER: |
| 8632 | if (isnan(set->val.num)) { |
| 8633 | set->val.str = strdup("NaN"); |
| 8634 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8635 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 8636 | set->val.str = strdup("0"); |
| 8637 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8638 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 8639 | set->val.str = strdup("Infinity"); |
| 8640 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8641 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 8642 | set->val.str = strdup("-Infinity"); |
| 8643 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8644 | } else if ((long long)set->val.num == set->val.num) { |
| 8645 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 8646 | LOGMEM_RET(set->ctx); |
| 8647 | } |
| 8648 | set->val.str = str; |
| 8649 | } else { |
| 8650 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 8651 | LOGMEM_RET(set->ctx); |
| 8652 | } |
| 8653 | set->val.str = str; |
| 8654 | } |
| 8655 | break; |
| 8656 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8657 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8658 | set->val.str = strdup("true"); |
| 8659 | } else { |
| 8660 | set->val.str = strdup("false"); |
| 8661 | } |
| 8662 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 8663 | break; |
| 8664 | case LYXP_SET_NODE_SET: |
| 8665 | assert(set->used); |
| 8666 | |
| 8667 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8668 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8669 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8670 | rc = cast_node_set_to_string(set, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8671 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8672 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8673 | set->val.str = str; |
| 8674 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8675 | default: |
| 8676 | LOGINT_RET(set->ctx); |
| 8677 | } |
| 8678 | set->type = LYXP_SET_STRING; |
| 8679 | } |
| 8680 | |
| 8681 | /* to NUMBER */ |
| 8682 | if (target == LYXP_SET_NUMBER) { |
| 8683 | switch (set->type) { |
| 8684 | case LYXP_SET_STRING: |
| 8685 | num = cast_string_to_number(set->val.str); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8686 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8687 | set->val.num = num; |
| 8688 | break; |
| 8689 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8690 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8691 | set->val.num = 1; |
| 8692 | } else { |
| 8693 | set->val.num = 0; |
| 8694 | } |
| 8695 | break; |
| 8696 | default: |
| 8697 | LOGINT_RET(set->ctx); |
| 8698 | } |
| 8699 | set->type = LYXP_SET_NUMBER; |
| 8700 | } |
| 8701 | |
| 8702 | /* to BOOLEAN */ |
| 8703 | if (target == LYXP_SET_BOOLEAN) { |
| 8704 | switch (set->type) { |
| 8705 | case LYXP_SET_NUMBER: |
| 8706 | 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] | 8707 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8708 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8709 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8710 | } |
| 8711 | break; |
| 8712 | case LYXP_SET_STRING: |
| 8713 | if (set->val.str[0]) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8714 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8715 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8716 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8717 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8718 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8719 | } |
| 8720 | break; |
| 8721 | case LYXP_SET_NODE_SET: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8722 | if (set->used) { |
| 8723 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8724 | set->val.bln = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8725 | } else { |
| 8726 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8727 | set->val.bln = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8728 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8729 | break; |
| 8730 | default: |
| 8731 | LOGINT_RET(set->ctx); |
| 8732 | } |
| 8733 | set->type = LYXP_SET_BOOLEAN; |
| 8734 | } |
| 8735 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8736 | return LY_SUCCESS; |
| 8737 | } |
| 8738 | |
| 8739 | LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame^] | 8740 | lyxp_atomize(const struct lyxp_expr *exp, const struct lys_module *cur_mod, LY_PREFIX_FORMAT format, void *prefix_data, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8741 | 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] | 8742 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8743 | uint16_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8744 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8745 | LY_CHECK_ARG_RET(NULL, exp, set, LY_EINVAL); |
| 8746 | if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) { |
| 8747 | LOGARG(NULL, "Current module must be set if schema format is used."); |
| 8748 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8749 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8750 | |
| 8751 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8752 | memset(set, 0, sizeof *set); |
| 8753 | set->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8754 | set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options); |
| 8755 | 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] | 8756 | set->val.scnodes[0].in_ctx = -2; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8757 | |
| 8758 | set->ctx = cur_mod ? cur_mod->ctx : (ctx_scnode ? ctx_scnode->module->ctx : NULL); |
| 8759 | set->cur_scnode = ctx_scnode; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8760 | for (set->context_op = ctx_scnode; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 8761 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8762 | set->context_op = set->context_op->parent) {} |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8763 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8764 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8765 | set->prefix_data = prefix_data; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8766 | |
| 8767 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8768 | return eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8769 | } |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 8770 | |
| 8771 | API const char * |
| 8772 | lyxp_get_expr(const struct lyxp_expr *path) |
| 8773 | { |
| 8774 | if (!path) { |
| 8775 | return NULL; |
| 8776 | } |
| 8777 | |
| 8778 | return path->expr; |
| 8779 | } |