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> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 24 | #include <math.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 25 | #include <stdint.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 28 | #include <string.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 29 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 30 | #include "common.h" |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 31 | #include "compat.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 32 | #include "context.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 33 | #include "dict.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 34 | #include "hash_table.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 35 | #include "out.h" |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 36 | #include "parser_data.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 37 | #include "path.h" |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 38 | #include "plugins_types.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 39 | #include "printer_data.h" |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 40 | #include "schema_compile_node.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 41 | #include "tree.h" |
| 42 | #include "tree_data_internal.h" |
| 43 | #include "tree_schema_internal.h" |
| 44 | #include "xml.h" |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 45 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 46 | static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx); |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 47 | static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, |
| 48 | struct lyxp_set *set, uint32_t options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * @brief Print the type of an XPath \p set. |
| 52 | * |
| 53 | * @param[in] set Set to use. |
| 54 | * @return Set type string. |
| 55 | */ |
| 56 | static const char * |
| 57 | print_set_type(struct lyxp_set *set) |
| 58 | { |
| 59 | switch (set->type) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 60 | case LYXP_SET_NODE_SET: |
| 61 | return "node set"; |
| 62 | case LYXP_SET_SCNODE_SET: |
| 63 | return "schema node set"; |
| 64 | case LYXP_SET_BOOLEAN: |
| 65 | return "boolean"; |
| 66 | case LYXP_SET_NUMBER: |
| 67 | return "number"; |
| 68 | case LYXP_SET_STRING: |
| 69 | return "string"; |
| 70 | } |
| 71 | |
| 72 | return NULL; |
| 73 | } |
| 74 | |
Michal Vasko | 24cddf8 | 2020-06-01 08:17:01 +0200 | [diff] [blame] | 75 | const char * |
| 76 | lyxp_print_token(enum lyxp_token tok) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 77 | { |
| 78 | switch (tok) { |
| 79 | case LYXP_TOKEN_PAR1: |
| 80 | return "("; |
| 81 | case LYXP_TOKEN_PAR2: |
| 82 | return ")"; |
| 83 | case LYXP_TOKEN_BRACK1: |
| 84 | return "["; |
| 85 | case LYXP_TOKEN_BRACK2: |
| 86 | return "]"; |
| 87 | case LYXP_TOKEN_DOT: |
| 88 | return "."; |
| 89 | case LYXP_TOKEN_DDOT: |
| 90 | return ".."; |
| 91 | case LYXP_TOKEN_AT: |
| 92 | return "@"; |
| 93 | case LYXP_TOKEN_COMMA: |
| 94 | return ","; |
| 95 | case LYXP_TOKEN_NAMETEST: |
| 96 | return "NameTest"; |
| 97 | case LYXP_TOKEN_NODETYPE: |
| 98 | return "NodeType"; |
| 99 | case LYXP_TOKEN_FUNCNAME: |
| 100 | return "FunctionName"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 101 | case LYXP_TOKEN_OPER_LOG: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 102 | return "Operator(Logic)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 103 | case LYXP_TOKEN_OPER_EQUAL: |
| 104 | return "Operator(Equal)"; |
| 105 | case LYXP_TOKEN_OPER_NEQUAL: |
| 106 | return "Operator(Non-equal)"; |
| 107 | case LYXP_TOKEN_OPER_COMP: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 108 | return "Operator(Comparison)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 109 | case LYXP_TOKEN_OPER_MATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 110 | return "Operator(Math)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 111 | case LYXP_TOKEN_OPER_UNI: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 112 | return "Operator(Union)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 113 | case LYXP_TOKEN_OPER_PATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 114 | return "Operator(Path)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 115 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 116 | return "Operator(Recursive Path)"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 117 | case LYXP_TOKEN_LITERAL: |
| 118 | return "Literal"; |
| 119 | case LYXP_TOKEN_NUMBER: |
| 120 | return "Number"; |
| 121 | default: |
| 122 | LOGINT(NULL); |
| 123 | return ""; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @brief Print the whole expression \p exp to debug output. |
| 129 | * |
| 130 | * @param[in] exp Expression to use. |
| 131 | */ |
| 132 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 133 | print_expr_struct_debug(const struct lyxp_expr *exp) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 134 | { |
| 135 | uint16_t i, j; |
| 136 | char tmp[128]; |
| 137 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 138 | if (!exp || (ly_ll < LY_LLDBG)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 139 | return; |
| 140 | } |
| 141 | |
| 142 | LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr); |
| 143 | for (i = 0; i < exp->used; ++i) { |
Michal Vasko | 24cddf8 | 2020-06-01 08:17:01 +0200 | [diff] [blame] | 144 | sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i], |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 145 | &exp->expr[exp->tok_pos[i]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 146 | if (exp->repeat[i]) { |
| 147 | sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]); |
| 148 | for (j = 1; exp->repeat[i][j]; ++j) { |
| 149 | sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]); |
| 150 | } |
| 151 | strcat(tmp, ")"); |
| 152 | } |
| 153 | LOGDBG(LY_LDGXPATH, tmp); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | #ifndef NDEBUG |
| 158 | |
| 159 | /** |
| 160 | * @brief Print XPath set content to debug output. |
| 161 | * |
| 162 | * @param[in] set Set to print. |
| 163 | */ |
| 164 | static void |
| 165 | print_set_debug(struct lyxp_set *set) |
| 166 | { |
| 167 | uint32_t i; |
| 168 | char *str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 169 | struct lyxp_set_node *item; |
| 170 | struct lyxp_set_scnode *sitem; |
| 171 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 172 | if (ly_ll < LY_LLDBG) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 173 | return; |
| 174 | } |
| 175 | |
| 176 | switch (set->type) { |
| 177 | case LYXP_SET_NODE_SET: |
| 178 | LOGDBG(LY_LDGXPATH, "set NODE SET:"); |
| 179 | for (i = 0; i < set->used; ++i) { |
| 180 | item = &set->val.nodes[i]; |
| 181 | |
| 182 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 183 | case LYXP_NODE_NONE: |
| 184 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos); |
| 185 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 186 | case LYXP_NODE_ROOT: |
| 187 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos); |
| 188 | break; |
| 189 | case LYXP_NODE_ROOT_CONFIG: |
| 190 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos); |
| 191 | break; |
| 192 | case LYXP_NODE_ELEM: |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 193 | if ((item->node->schema->nodetype == LYS_LIST) && |
| 194 | (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 195 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 196 | item->node->schema->name, LYD_CANON_VALUE(lyd_child(item->node))); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 197 | } else if (((struct lyd_node_inner *)item->node)->schema->nodetype == LYS_LEAFLIST) { |
| 198 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 199 | item->node->schema->name, LYD_CANON_VALUE(item->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 200 | } else { |
| 201 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name); |
| 202 | } |
| 203 | break; |
| 204 | case LYXP_NODE_TEXT: |
| 205 | if (item->node->schema->nodetype & LYS_ANYDATA) { |
| 206 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 207 | item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 208 | } else { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 209 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos, LYD_CANON_VALUE(item->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 210 | } |
| 211 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 212 | case LYXP_NODE_META: |
| 213 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 214 | set->val.meta[i].meta->value); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 215 | break; |
| 216 | } |
| 217 | } |
| 218 | break; |
| 219 | |
| 220 | case LYXP_SET_SCNODE_SET: |
| 221 | LOGDBG(LY_LDGXPATH, "set SCNODE SET:"); |
| 222 | for (i = 0; i < set->used; ++i) { |
| 223 | sitem = &set->val.scnodes[i]; |
| 224 | |
| 225 | switch (sitem->type) { |
| 226 | case LYXP_NODE_ROOT: |
| 227 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx); |
| 228 | break; |
| 229 | case LYXP_NODE_ROOT_CONFIG: |
| 230 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx); |
| 231 | break; |
| 232 | case LYXP_NODE_ELEM: |
| 233 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name); |
| 234 | break; |
| 235 | default: |
| 236 | LOGINT(NULL); |
| 237 | break; |
| 238 | } |
| 239 | } |
| 240 | break; |
| 241 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 242 | case LYXP_SET_BOOLEAN: |
| 243 | LOGDBG(LY_LDGXPATH, "set BOOLEAN"); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 244 | LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false")); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 245 | break; |
| 246 | |
| 247 | case LYXP_SET_STRING: |
| 248 | LOGDBG(LY_LDGXPATH, "set STRING"); |
| 249 | LOGDBG(LY_LDGXPATH, "\t%s", set->val.str); |
| 250 | break; |
| 251 | |
| 252 | case LYXP_SET_NUMBER: |
| 253 | LOGDBG(LY_LDGXPATH, "set NUMBER"); |
| 254 | |
| 255 | if (isnan(set->val.num)) { |
| 256 | str = strdup("NaN"); |
| 257 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 258 | str = strdup("0"); |
| 259 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 260 | str = strdup("Infinity"); |
| 261 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 262 | str = strdup("-Infinity"); |
| 263 | } else if ((long long)set->val.num == set->val.num) { |
| 264 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 265 | str = NULL; |
| 266 | } |
| 267 | } else { |
| 268 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 269 | str = NULL; |
| 270 | } |
| 271 | } |
| 272 | LY_CHECK_ERR_RET(!str, LOGMEM(NULL), ); |
| 273 | |
| 274 | LOGDBG(LY_LDGXPATH, "\t%s", str); |
| 275 | free(str); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | #endif |
| 280 | |
| 281 | /** |
| 282 | * @brief Realloc the string \p str. |
| 283 | * |
| 284 | * @param[in] ctx libyang context for logging. |
| 285 | * @param[in] needed How much free space is required. |
| 286 | * @param[in,out] str Pointer to the string to use. |
| 287 | * @param[in,out] used Used bytes in \p str. |
| 288 | * @param[in,out] size Allocated bytes in \p str. |
| 289 | * @return LY_ERR |
| 290 | */ |
| 291 | static LY_ERR |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 292 | cast_string_realloc(const struct ly_ctx *ctx, uint16_t needed, char **str, uint16_t *used, uint16_t *size) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 293 | { |
| 294 | if (*size - *used < needed) { |
| 295 | do { |
| 296 | if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) { |
| 297 | LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX); |
| 298 | return LY_EINVAL; |
| 299 | } |
| 300 | *size += LYXP_STRING_CAST_SIZE_STEP; |
| 301 | } while (*size - *used < needed); |
| 302 | *str = ly_realloc(*str, *size * sizeof(char)); |
| 303 | LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM); |
| 304 | } |
| 305 | |
| 306 | return LY_SUCCESS; |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * @brief Cast nodes recursively to one string @p str. |
| 311 | * |
| 312 | * @param[in] node Node to cast. |
| 313 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 314 | * @param[in] root_type Type of the XPath root. |
| 315 | * @param[in] indent Current indent. |
| 316 | * @param[in,out] str Resulting string. |
| 317 | * @param[in,out] used Used bytes in @p str. |
| 318 | * @param[in,out] size Allocated bytes in @p str. |
| 319 | * @return LY_ERR |
| 320 | */ |
| 321 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 322 | cast_string_recursive(const struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 323 | uint16_t *used, uint16_t *size) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 324 | { |
Radek Krejci | 7f769d7 | 2020-07-11 23:13:56 +0200 | [diff] [blame] | 325 | char *buf, *line, *ptr = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 326 | const char *value_str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 327 | const struct lyd_node *child; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 328 | struct lyd_node *tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 329 | struct lyd_node_any *any; |
| 330 | LY_ERR rc; |
| 331 | |
| 332 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) { |
| 333 | return LY_SUCCESS; |
| 334 | } |
| 335 | |
| 336 | if (fake_cont) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 337 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 338 | LY_CHECK_RET(rc); |
| 339 | strcpy(*str + (*used - 1), "\n"); |
| 340 | ++(*used); |
| 341 | |
| 342 | ++indent; |
| 343 | } |
| 344 | |
| 345 | switch (node->schema->nodetype) { |
| 346 | case LYS_CONTAINER: |
| 347 | case LYS_LIST: |
| 348 | case LYS_RPC: |
| 349 | case LYS_NOTIF: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 350 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 351 | LY_CHECK_RET(rc); |
| 352 | strcpy(*str + (*used - 1), "\n"); |
| 353 | ++(*used); |
| 354 | |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 355 | for (child = lyd_child(node); child; child = child->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 356 | rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size); |
| 357 | LY_CHECK_RET(rc); |
| 358 | } |
| 359 | |
| 360 | break; |
| 361 | |
| 362 | case LYS_LEAF: |
| 363 | case LYS_LEAFLIST: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 364 | value_str = LYD_CANON_VALUE(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 365 | |
| 366 | /* print indent */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 367 | LY_CHECK_RET(cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(value_str) + 1, str, used, size)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 368 | memset(*str + (*used - 1), ' ', indent * 2); |
| 369 | *used += indent * 2; |
| 370 | |
| 371 | /* print value */ |
| 372 | if (*used == 1) { |
| 373 | sprintf(*str + (*used - 1), "%s", value_str); |
| 374 | *used += strlen(value_str); |
| 375 | } else { |
| 376 | sprintf(*str + (*used - 1), "%s\n", value_str); |
| 377 | *used += strlen(value_str) + 1; |
| 378 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 379 | |
| 380 | break; |
| 381 | |
| 382 | case LYS_ANYXML: |
| 383 | case LYS_ANYDATA: |
| 384 | any = (struct lyd_node_any *)node; |
| 385 | if (!(void *)any->value.tree) { |
| 386 | /* no content */ |
| 387 | buf = strdup(""); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 388 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 389 | } else { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 390 | struct ly_out *out; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 391 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 392 | if (any->value_type == LYD_ANYDATA_LYB) { |
| 393 | /* try to parse it into a data tree */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 394 | if (lyd_parse_data_mem((struct ly_ctx *)LYD_CTX(node), any->value.mem, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 395 | /* successfully parsed */ |
| 396 | free(any->value.mem); |
| 397 | any->value.tree = tree; |
| 398 | any->value_type = LYD_ANYDATA_DATATREE; |
| 399 | } |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 400 | /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */ |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 401 | } |
| 402 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 403 | switch (any->value_type) { |
| 404 | case LYD_ANYDATA_STRING: |
| 405 | case LYD_ANYDATA_XML: |
| 406 | case LYD_ANYDATA_JSON: |
| 407 | buf = strdup(any->value.json); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 408 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 409 | break; |
| 410 | case LYD_ANYDATA_DATATREE: |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 411 | LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 412 | rc = lyd_print_all(out, any->value.tree, LYD_XML, 0); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 413 | ly_out_free(out, NULL, 0); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 414 | LY_CHECK_RET(rc < 0, -rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 415 | break; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 416 | case LYD_ANYDATA_LYB: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 417 | LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string."); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 418 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
| 422 | line = strtok_r(buf, "\n", &ptr); |
| 423 | do { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 424 | rc = cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(line) + 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 425 | if (rc != LY_SUCCESS) { |
| 426 | free(buf); |
| 427 | return rc; |
| 428 | } |
| 429 | memset(*str + (*used - 1), ' ', indent * 2); |
| 430 | *used += indent * 2; |
| 431 | |
| 432 | strcpy(*str + (*used - 1), line); |
| 433 | *used += strlen(line); |
| 434 | |
| 435 | strcpy(*str + (*used - 1), "\n"); |
| 436 | *used += 1; |
| 437 | } while ((line = strtok_r(NULL, "\n", &ptr))); |
| 438 | |
| 439 | free(buf); |
| 440 | break; |
| 441 | |
| 442 | default: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 443 | LOGINT_RET(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | if (fake_cont) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 447 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 448 | LY_CHECK_RET(rc); |
| 449 | strcpy(*str + (*used - 1), "\n"); |
| 450 | ++(*used); |
| 451 | |
| 452 | --indent; |
| 453 | } |
| 454 | |
| 455 | return LY_SUCCESS; |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * @brief Cast an element into a string. |
| 460 | * |
| 461 | * @param[in] node Node to cast. |
| 462 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 463 | * @param[in] root_type Type of the XPath root. |
| 464 | * @param[out] str Element cast to dynamically-allocated string. |
| 465 | * @return LY_ERR |
| 466 | */ |
| 467 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 468 | cast_string_elem(struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, char **str) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 469 | { |
| 470 | uint16_t used, size; |
| 471 | LY_ERR rc; |
| 472 | |
| 473 | *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char)); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 474 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 475 | (*str)[0] = '\0'; |
| 476 | used = 1; |
| 477 | size = LYXP_STRING_CAST_SIZE_START; |
| 478 | |
| 479 | rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size); |
| 480 | if (rc != LY_SUCCESS) { |
| 481 | free(*str); |
| 482 | return rc; |
| 483 | } |
| 484 | |
| 485 | if (size > used) { |
| 486 | *str = ly_realloc(*str, used * sizeof(char)); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 487 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 488 | } |
| 489 | return LY_SUCCESS; |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * @brief Cast a LYXP_SET_NODE_SET set into a string. |
| 494 | * Context position aware. |
| 495 | * |
| 496 | * @param[in] set Set to cast. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 497 | * @param[out] str Cast dynamically-allocated string. |
| 498 | * @return LY_ERR |
| 499 | */ |
| 500 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 501 | cast_node_set_to_string(struct lyxp_set *set, char **str) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 502 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 503 | switch (set->val.nodes[0].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 504 | case LYXP_NODE_NONE: |
| 505 | /* invalid */ |
| 506 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 507 | case LYXP_NODE_ROOT: |
| 508 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 509 | return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 510 | case LYXP_NODE_ELEM: |
| 511 | case LYXP_NODE_TEXT: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 512 | return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 513 | case LYXP_NODE_META: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 514 | *str = strdup(set->val.meta[0].meta->value.canonical); |
| 515 | if (!*str) { |
| 516 | LOGMEM_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 517 | } |
| 518 | return LY_SUCCESS; |
| 519 | } |
| 520 | |
| 521 | LOGINT_RET(set->ctx); |
| 522 | } |
| 523 | |
| 524 | /** |
| 525 | * @brief Cast a string into an XPath number. |
| 526 | * |
| 527 | * @param[in] str String to use. |
| 528 | * @return Cast number. |
| 529 | */ |
| 530 | static long double |
| 531 | cast_string_to_number(const char *str) |
| 532 | { |
| 533 | long double num; |
| 534 | char *ptr; |
| 535 | |
| 536 | errno = 0; |
| 537 | num = strtold(str, &ptr); |
| 538 | if (errno || *ptr) { |
| 539 | num = NAN; |
| 540 | } |
| 541 | return num; |
| 542 | } |
| 543 | |
| 544 | /** |
| 545 | * @brief Callback for checking value equality. |
| 546 | * |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 547 | * Implementation of ::values_equal_cb. |
| 548 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 549 | * @param[in] val1_p First value. |
| 550 | * @param[in] val2_p Second value. |
| 551 | * @param[in] mod Whether hash table is being modified. |
| 552 | * @param[in] cb_data Callback data. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 553 | * @return Boolean value whether values are equal or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 554 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 555 | static ly_bool |
| 556 | set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data)) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 557 | { |
| 558 | struct lyxp_set_hash_node *val1, *val2; |
| 559 | |
| 560 | val1 = (struct lyxp_set_hash_node *)val1_p; |
| 561 | val2 = (struct lyxp_set_hash_node *)val2_p; |
| 562 | |
| 563 | if ((val1->node == val2->node) && (val1->type == val2->type)) { |
| 564 | return 1; |
| 565 | } |
| 566 | |
| 567 | return 0; |
| 568 | } |
| 569 | |
| 570 | /** |
| 571 | * @brief Insert node and its hash into set. |
| 572 | * |
| 573 | * @param[in] set et to insert to. |
| 574 | * @param[in] node Node with hash. |
| 575 | * @param[in] type Node type. |
| 576 | */ |
| 577 | static void |
| 578 | set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 579 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 580 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 581 | uint32_t i, hash; |
| 582 | struct lyxp_set_hash_node hnode; |
| 583 | |
| 584 | if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) { |
| 585 | /* create hash table and add all the nodes */ |
| 586 | set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1); |
| 587 | for (i = 0; i < set->used; ++i) { |
| 588 | hnode.node = set->val.nodes[i].node; |
| 589 | hnode.type = set->val.nodes[i].type; |
| 590 | |
| 591 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 592 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 593 | hash = dict_hash_multi(hash, NULL, 0); |
| 594 | |
| 595 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 596 | assert(!r); |
| 597 | (void)r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 598 | |
Michal Vasko | 9d6befd | 2019-12-11 14:56:56 +0100 | [diff] [blame] | 599 | if (hnode.node == node) { |
| 600 | /* it was just added, do not add it twice */ |
| 601 | node = NULL; |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 607 | /* add the new node into hash table */ |
| 608 | hnode.node = node; |
| 609 | hnode.type = type; |
| 610 | |
| 611 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 612 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 613 | hash = dict_hash_multi(hash, NULL, 0); |
| 614 | |
| 615 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 616 | assert(!r); |
| 617 | (void)r; |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | /** |
| 622 | * @brief Remove node and its hash from set. |
| 623 | * |
| 624 | * @param[in] set Set to remove from. |
| 625 | * @param[in] node Node to remove. |
| 626 | * @param[in] type Node type. |
| 627 | */ |
| 628 | static void |
| 629 | set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 630 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 631 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 632 | struct lyxp_set_hash_node hnode; |
| 633 | uint32_t hash; |
| 634 | |
| 635 | if (set->ht) { |
| 636 | hnode.node = node; |
| 637 | hnode.type = type; |
| 638 | |
| 639 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 640 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 641 | hash = dict_hash_multi(hash, NULL, 0); |
| 642 | |
| 643 | r = lyht_remove(set->ht, &hnode, hash); |
| 644 | assert(!r); |
| 645 | (void)r; |
| 646 | |
| 647 | if (!set->ht->used) { |
| 648 | lyht_free(set->ht); |
| 649 | set->ht = NULL; |
| 650 | } |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | /** |
| 655 | * @brief Check whether node is in set based on its hash. |
| 656 | * |
| 657 | * @param[in] set Set to search in. |
| 658 | * @param[in] node Node to search for. |
| 659 | * @param[in] type Node type. |
| 660 | * @param[in] skip_idx Index in @p set to skip. |
| 661 | * @return LY_ERR |
| 662 | */ |
| 663 | static LY_ERR |
| 664 | set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx) |
| 665 | { |
| 666 | struct lyxp_set_hash_node hnode, *match_p; |
| 667 | uint32_t hash; |
| 668 | |
| 669 | hnode.node = node; |
| 670 | hnode.type = type; |
| 671 | |
| 672 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 673 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 674 | hash = dict_hash_multi(hash, NULL, 0); |
| 675 | |
| 676 | if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) { |
| 677 | if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) { |
| 678 | /* we found it on the index that should be skipped, find another */ |
| 679 | hnode = *match_p; |
| 680 | if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) { |
| 681 | /* none other found */ |
| 682 | return LY_SUCCESS; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | return LY_EEXIST; |
| 687 | } |
| 688 | |
| 689 | /* not found */ |
| 690 | return LY_SUCCESS; |
| 691 | } |
| 692 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 693 | void |
| 694 | lyxp_set_free_content(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 695 | { |
| 696 | if (!set) { |
| 697 | return; |
| 698 | } |
| 699 | |
| 700 | if (set->type == LYXP_SET_NODE_SET) { |
| 701 | free(set->val.nodes); |
| 702 | lyht_free(set->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 703 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 704 | free(set->val.scnodes); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 705 | lyht_free(set->ht); |
| 706 | } else { |
| 707 | if (set->type == LYXP_SET_STRING) { |
| 708 | free(set->val.str); |
| 709 | } |
| 710 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 711 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 712 | |
| 713 | set->val.nodes = NULL; |
| 714 | set->used = 0; |
| 715 | set->size = 0; |
| 716 | set->ht = NULL; |
| 717 | set->ctx_pos = 0; |
| 718 | set->ctx_pos = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 719 | } |
| 720 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 721 | /** |
| 722 | * @brief Free dynamically-allocated set. |
| 723 | * |
| 724 | * @param[in] set Set to free. |
| 725 | */ |
| 726 | static void |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 727 | lyxp_set_free(struct lyxp_set *set) |
| 728 | { |
| 729 | if (!set) { |
| 730 | return; |
| 731 | } |
| 732 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 733 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 734 | free(set); |
| 735 | } |
| 736 | |
| 737 | /** |
| 738 | * @brief Initialize set context. |
| 739 | * |
| 740 | * @param[in] new Set to initialize. |
| 741 | * @param[in] set Arbitrary initialized set. |
| 742 | */ |
| 743 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 744 | set_init(struct lyxp_set *new, const struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 745 | { |
| 746 | memset(new, 0, sizeof *new); |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 747 | if (set) { |
| 748 | new->ctx = set->ctx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 749 | new->cur_node = set->cur_node; |
Michal Vasko | 588112f | 2019-12-10 14:51:53 +0100 | [diff] [blame] | 750 | new->root_type = set->root_type; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 751 | new->context_op = set->context_op; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 752 | new->tree = set->tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 753 | new->cur_mod = set->cur_mod; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 754 | new->format = set->format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 755 | new->prefix_data = set->prefix_data; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 756 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | /** |
| 760 | * @brief Create a deep copy of a set. |
| 761 | * |
| 762 | * @param[in] set Set to copy. |
| 763 | * @return Copy of @p set. |
| 764 | */ |
| 765 | static struct lyxp_set * |
| 766 | set_copy(struct lyxp_set *set) |
| 767 | { |
| 768 | struct lyxp_set *ret; |
| 769 | uint16_t i; |
| 770 | |
| 771 | if (!set) { |
| 772 | return NULL; |
| 773 | } |
| 774 | |
| 775 | ret = malloc(sizeof *ret); |
| 776 | LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL); |
| 777 | set_init(ret, set); |
| 778 | |
| 779 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 780 | ret->type = set->type; |
| 781 | |
| 782 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 783 | if ((set->val.scnodes[i].in_ctx == 1) || (set->val.scnodes[i].in_ctx == -2)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 784 | uint32_t idx; |
| 785 | LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx), |
| 786 | lyxp_set_free(ret), NULL); |
Michal Vasko | 3f27c52 | 2020-01-06 08:37:49 +0100 | [diff] [blame] | 787 | /* coverity seems to think scnodes can be NULL */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 788 | if (!ret->val.scnodes) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 789 | lyxp_set_free(ret); |
| 790 | return NULL; |
| 791 | } |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 792 | ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 793 | } |
| 794 | } |
| 795 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 796 | ret->type = set->type; |
| 797 | ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes); |
| 798 | LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL); |
| 799 | memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes); |
| 800 | |
| 801 | ret->used = ret->size = set->used; |
| 802 | ret->ctx_pos = set->ctx_pos; |
| 803 | ret->ctx_size = set->ctx_size; |
| 804 | ret->ht = lyht_dup(set->ht); |
| 805 | } else { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 806 | memcpy(ret, set, sizeof *ret); |
| 807 | if (set->type == LYXP_SET_STRING) { |
| 808 | ret->val.str = strdup(set->val.str); |
| 809 | LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL); |
| 810 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | return ret; |
| 814 | } |
| 815 | |
| 816 | /** |
| 817 | * @brief Fill XPath set with a string. Any current data are disposed of. |
| 818 | * |
| 819 | * @param[in] set Set to fill. |
| 820 | * @param[in] string String to fill into \p set. |
| 821 | * @param[in] str_len Length of \p string. 0 is a valid value! |
| 822 | */ |
| 823 | static void |
| 824 | set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len) |
| 825 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 826 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 827 | |
| 828 | set->type = LYXP_SET_STRING; |
| 829 | if ((str_len == 0) && (string[0] != '\0')) { |
| 830 | string = ""; |
| 831 | } |
| 832 | set->val.str = strndup(string, str_len); |
| 833 | } |
| 834 | |
| 835 | /** |
| 836 | * @brief Fill XPath set with a number. Any current data are disposed of. |
| 837 | * |
| 838 | * @param[in] set Set to fill. |
| 839 | * @param[in] number Number to fill into \p set. |
| 840 | */ |
| 841 | static void |
| 842 | set_fill_number(struct lyxp_set *set, long double number) |
| 843 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 844 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 845 | |
| 846 | set->type = LYXP_SET_NUMBER; |
| 847 | set->val.num = number; |
| 848 | } |
| 849 | |
| 850 | /** |
| 851 | * @brief Fill XPath set with a boolean. Any current data are disposed of. |
| 852 | * |
| 853 | * @param[in] set Set to fill. |
| 854 | * @param[in] boolean Boolean to fill into \p set. |
| 855 | */ |
| 856 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 857 | set_fill_boolean(struct lyxp_set *set, ly_bool boolean) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 858 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 859 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 860 | |
| 861 | set->type = LYXP_SET_BOOLEAN; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 862 | set->val.bln = boolean; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | /** |
| 866 | * @brief Fill XPath set with the value from another set (deep assign). |
| 867 | * Any current data are disposed of. |
| 868 | * |
| 869 | * @param[in] trg Set to fill. |
| 870 | * @param[in] src Source set to copy into \p trg. |
| 871 | */ |
| 872 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 873 | set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 874 | { |
| 875 | if (!trg || !src) { |
| 876 | return; |
| 877 | } |
| 878 | |
| 879 | if (trg->type == LYXP_SET_NODE_SET) { |
| 880 | free(trg->val.nodes); |
| 881 | } else if (trg->type == LYXP_SET_STRING) { |
| 882 | free(trg->val.str); |
| 883 | } |
| 884 | set_init(trg, src); |
| 885 | |
| 886 | if (src->type == LYXP_SET_SCNODE_SET) { |
| 887 | trg->type = LYXP_SET_SCNODE_SET; |
| 888 | trg->used = src->used; |
| 889 | trg->size = src->used; |
| 890 | |
| 891 | trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes); |
| 892 | LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 893 | memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes); |
| 894 | } else if (src->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 895 | set_fill_boolean(trg, src->val.bln); |
Michal Vasko | 44f3d2c | 2020-08-24 09:49:38 +0200 | [diff] [blame] | 896 | } else if (src->type == LYXP_SET_NUMBER) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 897 | set_fill_number(trg, src->val.num); |
| 898 | } else if (src->type == LYXP_SET_STRING) { |
| 899 | set_fill_string(trg, src->val.str, strlen(src->val.str)); |
| 900 | } else { |
| 901 | if (trg->type == LYXP_SET_NODE_SET) { |
| 902 | free(trg->val.nodes); |
| 903 | } else if (trg->type == LYXP_SET_STRING) { |
| 904 | free(trg->val.str); |
| 905 | } |
| 906 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 907 | assert(src->type == LYXP_SET_NODE_SET); |
| 908 | |
| 909 | trg->type = LYXP_SET_NODE_SET; |
| 910 | trg->used = src->used; |
| 911 | trg->size = src->used; |
| 912 | trg->ctx_pos = src->ctx_pos; |
| 913 | trg->ctx_size = src->ctx_size; |
| 914 | |
| 915 | trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes); |
| 916 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 917 | memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes); |
| 918 | if (src->ht) { |
| 919 | trg->ht = lyht_dup(src->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 920 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 921 | trg->ht = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 922 | } |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | /** |
| 927 | * @brief Clear context of all schema nodes. |
| 928 | * |
| 929 | * @param[in] set Set to clear. |
| 930 | */ |
| 931 | static void |
| 932 | set_scnode_clear_ctx(struct lyxp_set *set) |
| 933 | { |
| 934 | uint32_t i; |
| 935 | |
| 936 | for (i = 0; i < set->used; ++i) { |
| 937 | if (set->val.scnodes[i].in_ctx == 1) { |
| 938 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 939 | } else if (set->val.scnodes[i].in_ctx == -2) { |
| 940 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 941 | } |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | /** |
| 946 | * @brief Remove a node from a set. Removing last node changes |
| 947 | * set into LYXP_SET_EMPTY. Context position aware. |
| 948 | * |
| 949 | * @param[in] set Set to use. |
| 950 | * @param[in] idx Index from @p set of the node to be removed. |
| 951 | */ |
| 952 | static void |
| 953 | set_remove_node(struct lyxp_set *set, uint32_t idx) |
| 954 | { |
| 955 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 956 | assert(idx < set->used); |
| 957 | |
| 958 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 959 | |
| 960 | --set->used; |
| 961 | if (set->used) { |
| 962 | memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], |
| 963 | (set->used - idx) * sizeof *set->val.nodes); |
| 964 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 965 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 966 | } |
| 967 | } |
| 968 | |
| 969 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 970 | * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE. |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 971 | * |
| 972 | * @param[in] set Set to use. |
| 973 | * @param[in] idx Index from @p set of the node to be removed. |
| 974 | */ |
| 975 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 976 | set_remove_node_none(struct lyxp_set *set, uint32_t idx) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 977 | { |
| 978 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 979 | assert(idx < set->used); |
| 980 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 981 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 982 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 983 | } |
| 984 | set->val.nodes[idx].type = LYXP_NODE_NONE; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 988 | * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 989 | * set into LYXP_SET_EMPTY. Context position aware. |
| 990 | * |
| 991 | * @param[in] set Set to consolidate. |
| 992 | */ |
| 993 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 994 | set_remove_nodes_none(struct lyxp_set *set) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 995 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 996 | uint16_t i, orig_used, end = 0; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 997 | int32_t start; |
| 998 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 999 | assert(set); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1000 | |
| 1001 | orig_used = set->used; |
| 1002 | set->used = 0; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1003 | for (i = 0; i < orig_used; ) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1004 | start = -1; |
| 1005 | do { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1006 | if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1007 | start = i; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1008 | } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1009 | end = i; |
| 1010 | ++i; |
| 1011 | break; |
| 1012 | } |
| 1013 | |
| 1014 | ++i; |
| 1015 | if (i == orig_used) { |
| 1016 | end = i; |
| 1017 | } |
| 1018 | } while (i < orig_used); |
| 1019 | |
| 1020 | if (start > -1) { |
| 1021 | /* move the whole chunk of valid nodes together */ |
| 1022 | if (set->used != (unsigned)start) { |
| 1023 | memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes); |
| 1024 | } |
| 1025 | set->used += end - start; |
| 1026 | } |
| 1027 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1031 | * @brief Check for duplicates in a node set. |
| 1032 | * |
| 1033 | * @param[in] set Set to check. |
| 1034 | * @param[in] node Node to look for in @p set. |
| 1035 | * @param[in] node_type Type of @p node. |
| 1036 | * @param[in] skip_idx Index from @p set to skip. |
| 1037 | * @return LY_ERR |
| 1038 | */ |
| 1039 | static LY_ERR |
| 1040 | set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx) |
| 1041 | { |
| 1042 | uint32_t i; |
| 1043 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1044 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1045 | return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx); |
| 1046 | } |
| 1047 | |
| 1048 | for (i = 0; i < set->used; ++i) { |
| 1049 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1050 | continue; |
| 1051 | } |
| 1052 | |
| 1053 | if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) { |
| 1054 | return LY_EEXIST; |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | return LY_SUCCESS; |
| 1059 | } |
| 1060 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1061 | ly_bool |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1062 | lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx, |
| 1063 | uint32_t *index_p) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1064 | { |
| 1065 | uint32_t i; |
| 1066 | |
| 1067 | for (i = 0; i < set->used; ++i) { |
| 1068 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1069 | continue; |
| 1070 | } |
| 1071 | |
| 1072 | if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1073 | if (index_p) { |
| 1074 | *index_p = i; |
| 1075 | } |
| 1076 | return 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1077 | } |
| 1078 | } |
| 1079 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1080 | return 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1081 | } |
| 1082 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1083 | void |
| 1084 | lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1085 | { |
| 1086 | uint32_t orig_used, i, j; |
| 1087 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1088 | assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1089 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1090 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1091 | return; |
| 1092 | } |
| 1093 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1094 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1095 | memcpy(set1, set2, sizeof *set1); |
| 1096 | return; |
| 1097 | } |
| 1098 | |
| 1099 | if (set1->used + set2->used > set1->size) { |
| 1100 | set1->size = set1->used + set2->used; |
| 1101 | set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes); |
| 1102 | LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), ); |
| 1103 | } |
| 1104 | |
| 1105 | orig_used = set1->used; |
| 1106 | |
| 1107 | for (i = 0; i < set2->used; ++i) { |
| 1108 | for (j = 0; j < orig_used; ++j) { |
| 1109 | /* detect duplicities */ |
| 1110 | if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) { |
| 1111 | break; |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | if (j == orig_used) { |
| 1116 | memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes); |
| 1117 | ++set1->used; |
| 1118 | } |
| 1119 | } |
| 1120 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1121 | lyxp_set_free_content(set2); |
| 1122 | set2->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | /** |
| 1126 | * @brief Insert a node into a set. Context position aware. |
| 1127 | * |
| 1128 | * @param[in] set Set to use. |
| 1129 | * @param[in] node Node to insert to @p set. |
| 1130 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1131 | * @param[in] node_type Node type of @p node. |
| 1132 | * @param[in] idx Index in @p set to insert into. |
| 1133 | */ |
| 1134 | static void |
| 1135 | set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1136 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1137 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1138 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1139 | if (!set->size) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1140 | /* first item */ |
| 1141 | if (idx) { |
| 1142 | /* no real harm done, but it is a bug */ |
| 1143 | LOGINT(set->ctx); |
| 1144 | idx = 0; |
| 1145 | } |
| 1146 | set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes); |
| 1147 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1148 | set->type = LYXP_SET_NODE_SET; |
| 1149 | set->used = 0; |
| 1150 | set->size = LYXP_SET_SIZE_START; |
| 1151 | set->ctx_pos = 1; |
| 1152 | set->ctx_size = 1; |
| 1153 | set->ht = NULL; |
| 1154 | } else { |
| 1155 | /* not an empty set */ |
| 1156 | if (set->used == set->size) { |
| 1157 | |
| 1158 | /* set is full */ |
| 1159 | set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes); |
| 1160 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1161 | set->size += LYXP_SET_SIZE_STEP; |
| 1162 | } |
| 1163 | |
| 1164 | if (idx > set->used) { |
| 1165 | LOGINT(set->ctx); |
| 1166 | idx = set->used; |
| 1167 | } |
| 1168 | |
| 1169 | /* make space for the new node */ |
| 1170 | if (idx < set->used) { |
| 1171 | memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes); |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | /* finally assign the value */ |
| 1176 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1177 | set->val.nodes[idx].type = node_type; |
| 1178 | set->val.nodes[idx].pos = pos; |
| 1179 | ++set->used; |
| 1180 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1181 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1182 | set_insert_node_hash(set, (struct lyd_node *)node, node_type); |
| 1183 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1184 | } |
| 1185 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1186 | LY_ERR |
| 1187 | lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, uint32_t *index_p) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1188 | { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1189 | uint32_t index; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1190 | |
| 1191 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1192 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1193 | if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) { |
| 1194 | set->val.scnodes[index].in_ctx = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1195 | } else { |
| 1196 | if (set->used == set->size) { |
| 1197 | set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes); |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1198 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1199 | set->size += LYXP_SET_SIZE_STEP; |
| 1200 | } |
| 1201 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1202 | index = set->used; |
| 1203 | set->val.scnodes[index].scnode = (struct lysc_node *)node; |
| 1204 | set->val.scnodes[index].type = node_type; |
| 1205 | set->val.scnodes[index].in_ctx = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1206 | ++set->used; |
| 1207 | } |
| 1208 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1209 | if (index_p) { |
| 1210 | *index_p = index; |
| 1211 | } |
| 1212 | |
| 1213 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | /** |
| 1217 | * @brief Replace a node in a set with another. Context position aware. |
| 1218 | * |
| 1219 | * @param[in] set Set to use. |
| 1220 | * @param[in] node Node to insert to @p set. |
| 1221 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1222 | * @param[in] node_type Node type of @p node. |
| 1223 | * @param[in] idx Index in @p set of the node to replace. |
| 1224 | */ |
| 1225 | static void |
| 1226 | set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1227 | { |
| 1228 | assert(set && (idx < set->used)); |
| 1229 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1230 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1231 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1232 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1233 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1234 | set->val.nodes[idx].type = node_type; |
| 1235 | set->val.nodes[idx].pos = pos; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1236 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1237 | set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1238 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | /** |
| 1242 | * @brief Set all nodes with ctx 1 to a new unique context value. |
| 1243 | * |
| 1244 | * @param[in] set Set to modify. |
| 1245 | * @return New context value. |
| 1246 | */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1247 | static int32_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1248 | set_scnode_new_in_ctx(struct lyxp_set *set) |
| 1249 | { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1250 | uint32_t i; |
| 1251 | int32_t ret_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1252 | |
| 1253 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1254 | |
| 1255 | ret_ctx = 3; |
| 1256 | retry: |
| 1257 | for (i = 0; i < set->used; ++i) { |
| 1258 | if (set->val.scnodes[i].in_ctx >= ret_ctx) { |
| 1259 | ret_ctx = set->val.scnodes[i].in_ctx + 1; |
| 1260 | goto retry; |
| 1261 | } |
| 1262 | } |
| 1263 | for (i = 0; i < set->used; ++i) { |
| 1264 | if (set->val.scnodes[i].in_ctx == 1) { |
| 1265 | set->val.scnodes[i].in_ctx = ret_ctx; |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | return ret_ctx; |
| 1270 | } |
| 1271 | |
| 1272 | /** |
| 1273 | * @brief Get unique @p node position in the data. |
| 1274 | * |
| 1275 | * @param[in] node Node to find. |
| 1276 | * @param[in] node_type Node type of @p node. |
| 1277 | * @param[in] root Root node. |
| 1278 | * @param[in] root_type Type of the XPath @p root node. |
| 1279 | * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally |
| 1280 | * be used to increase efficiency and start the DFS from this node. |
| 1281 | * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set. |
| 1282 | * @return Node position. |
| 1283 | */ |
| 1284 | static uint32_t |
| 1285 | get_node_pos(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyd_node *root, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1286 | enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1287 | { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1288 | const struct lyd_node *elem = NULL, *top_sibling; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1289 | uint32_t pos = 1; |
| 1290 | |
| 1291 | assert(prev && prev_pos && !root->prev->next); |
| 1292 | |
| 1293 | if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 1294 | return 0; |
| 1295 | } |
| 1296 | |
| 1297 | if (*prev) { |
| 1298 | /* start from the previous element instead from the root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1299 | pos = *prev_pos; |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1300 | for (top_sibling = *prev; top_sibling->parent; top_sibling = (struct lyd_node *)top_sibling->parent) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1301 | goto dfs_search; |
| 1302 | } |
| 1303 | |
| 1304 | for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1305 | LYD_TREE_DFS_BEGIN(top_sibling, elem) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1306 | dfs_search: |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1307 | if (*prev && !elem) { |
| 1308 | /* resume previous DFS */ |
| 1309 | elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev; |
| 1310 | LYD_TREE_DFS_continue = 0; |
| 1311 | } |
| 1312 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1313 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1314 | /* skip */ |
| 1315 | LYD_TREE_DFS_continue = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1316 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1317 | if (elem == node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1318 | break; |
| 1319 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1320 | ++pos; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1321 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1322 | |
| 1323 | LYD_TREE_DFS_END(top_sibling, elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | /* node found */ |
| 1327 | if (elem) { |
| 1328 | break; |
| 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | if (!elem) { |
| 1333 | if (!(*prev)) { |
| 1334 | /* we went from root and failed to find it, cannot be */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1335 | LOGINT(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1336 | return 0; |
| 1337 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1338 | /* start the search again from the beginning */ |
| 1339 | *prev = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1340 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1341 | top_sibling = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1342 | pos = 1; |
| 1343 | goto dfs_search; |
| 1344 | } |
| 1345 | } |
| 1346 | |
| 1347 | /* remember the last found node for next time */ |
| 1348 | *prev = node; |
| 1349 | *prev_pos = pos; |
| 1350 | |
| 1351 | return pos; |
| 1352 | } |
| 1353 | |
| 1354 | /** |
| 1355 | * @brief Assign (fill) missing node positions. |
| 1356 | * |
| 1357 | * @param[in] set Set to fill positions in. |
| 1358 | * @param[in] root Context root node. |
| 1359 | * @param[in] root_type Context root type. |
| 1360 | * @return LY_ERR |
| 1361 | */ |
| 1362 | static LY_ERR |
| 1363 | set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type) |
| 1364 | { |
| 1365 | const struct lyd_node *prev = NULL, *tmp_node; |
| 1366 | uint32_t i, tmp_pos = 0; |
| 1367 | |
| 1368 | for (i = 0; i < set->used; ++i) { |
| 1369 | if (!set->val.nodes[i].pos) { |
| 1370 | tmp_node = NULL; |
| 1371 | switch (set->val.nodes[i].type) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1372 | case LYXP_NODE_META: |
| 1373 | tmp_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1374 | if (!tmp_node) { |
| 1375 | LOGINT_RET(root->schema->module->ctx); |
| 1376 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1377 | /* fallthrough */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1378 | case LYXP_NODE_ELEM: |
| 1379 | case LYXP_NODE_TEXT: |
| 1380 | if (!tmp_node) { |
| 1381 | tmp_node = set->val.nodes[i].node; |
| 1382 | } |
| 1383 | set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos); |
| 1384 | break; |
| 1385 | default: |
| 1386 | /* all roots have position 0 */ |
| 1387 | break; |
| 1388 | } |
| 1389 | } |
| 1390 | } |
| 1391 | |
| 1392 | return LY_SUCCESS; |
| 1393 | } |
| 1394 | |
| 1395 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1396 | * @brief Get unique @p meta position in the parent metadata. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1397 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1398 | * @param[in] meta Metadata to use. |
| 1399 | * @return Metadata position. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1400 | */ |
| 1401 | static uint16_t |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1402 | get_meta_pos(struct lyd_meta *meta) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1403 | { |
| 1404 | uint16_t pos = 0; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1405 | struct lyd_meta *meta2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1406 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1407 | for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1408 | ++pos; |
| 1409 | } |
| 1410 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1411 | assert(meta2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1412 | return pos; |
| 1413 | } |
| 1414 | |
| 1415 | /** |
| 1416 | * @brief Compare 2 nodes in respect to XPath document order. |
| 1417 | * |
| 1418 | * @param[in] item1 1st node. |
| 1419 | * @param[in] item2 2nd node. |
| 1420 | * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1. |
| 1421 | */ |
| 1422 | static int |
| 1423 | set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2) |
| 1424 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1425 | uint32_t meta_pos1 = 0, meta_pos2 = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1426 | |
| 1427 | if (item1->pos < item2->pos) { |
| 1428 | return -1; |
| 1429 | } |
| 1430 | |
| 1431 | if (item1->pos > item2->pos) { |
| 1432 | return 1; |
| 1433 | } |
| 1434 | |
| 1435 | /* node positions are equal, the fun case */ |
| 1436 | |
| 1437 | /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */ |
| 1438 | /* special case since text nodes are actually saved as their parents */ |
| 1439 | if ((item1->node == item2->node) && (item1->type != item2->type)) { |
| 1440 | if (item1->type == LYXP_NODE_ELEM) { |
| 1441 | assert(item2->type == LYXP_NODE_TEXT); |
| 1442 | return -1; |
| 1443 | } else { |
| 1444 | assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM)); |
| 1445 | return 1; |
| 1446 | } |
| 1447 | } |
| 1448 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1449 | /* we need meta positions now */ |
| 1450 | if (item1->type == LYXP_NODE_META) { |
| 1451 | meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1452 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1453 | if (item2->type == LYXP_NODE_META) { |
| 1454 | meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1455 | } |
| 1456 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1457 | /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1458 | /* check for duplicates */ |
| 1459 | if (item1->node == item2->node) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1460 | assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2))); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1461 | return 0; |
| 1462 | } |
| 1463 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1464 | /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1465 | /* elem is always first, 2nd node is after it */ |
| 1466 | if (item1->type == LYXP_NODE_ELEM) { |
| 1467 | assert(item2->type != LYXP_NODE_ELEM); |
| 1468 | return -1; |
| 1469 | } |
| 1470 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1471 | /* 1st TEXT - 2nd ELEM, 1st TEXT - any pos - 2nd META, 1st META - any pos - 2nd ELEM, 1st META - >pos> - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1472 | /* 2nd is before 1st */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1473 | if (((item1->type == LYXP_NODE_TEXT) && |
| 1474 | ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) || |
| 1475 | ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) || |
| 1476 | (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) && |
| 1477 | (meta_pos1 > meta_pos2))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1478 | return 1; |
| 1479 | } |
| 1480 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1481 | /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1482 | /* 2nd is after 1st */ |
| 1483 | return -1; |
| 1484 | } |
| 1485 | |
| 1486 | /** |
| 1487 | * @brief Set cast for comparisons. |
| 1488 | * |
| 1489 | * @param[in] trg Target set to cast source into. |
| 1490 | * @param[in] src Source set. |
| 1491 | * @param[in] type Target set type. |
| 1492 | * @param[in] src_idx Source set node index. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1493 | * @return LY_ERR |
| 1494 | */ |
| 1495 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1496 | set_comp_cast(struct lyxp_set *trg, struct lyxp_set *src, enum lyxp_set_type type, uint32_t src_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1497 | { |
| 1498 | assert(src->type == LYXP_SET_NODE_SET); |
| 1499 | |
| 1500 | set_init(trg, src); |
| 1501 | |
| 1502 | /* insert node into target set */ |
| 1503 | set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0); |
| 1504 | |
| 1505 | /* cast target set appropriately */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1506 | return lyxp_set_cast(trg, type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1507 | } |
| 1508 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1509 | /** |
| 1510 | * @brief Set content canonization for comparisons. |
| 1511 | * |
| 1512 | * @param[in] trg Target set to put the canononized source into. |
| 1513 | * @param[in] src Source set. |
| 1514 | * @param[in] xp_node Source XPath node/meta to use for canonization. |
| 1515 | * @return LY_ERR |
| 1516 | */ |
| 1517 | static LY_ERR |
| 1518 | set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node) |
| 1519 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1520 | const struct lysc_type *type = NULL; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1521 | struct lyd_value val; |
| 1522 | struct ly_err_item *err = NULL; |
| 1523 | char *str, *ptr; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1524 | LY_ERR rc; |
| 1525 | |
| 1526 | /* is there anything to canonize even? */ |
| 1527 | if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) { |
| 1528 | /* do we have a type to use for canonization? */ |
| 1529 | if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) { |
| 1530 | type = ((struct lyd_node_term *)xp_node->node)->value.realtype; |
| 1531 | } else if (xp_node->type == LYXP_NODE_META) { |
| 1532 | type = ((struct lyd_meta *)xp_node->node)->value.realtype; |
| 1533 | } |
| 1534 | } |
| 1535 | if (!type) { |
| 1536 | goto fill; |
| 1537 | } |
| 1538 | |
| 1539 | if (src->type == LYXP_SET_NUMBER) { |
| 1540 | /* canonize number */ |
| 1541 | if (asprintf(&str, "%Lf", src->val.num) == -1) { |
| 1542 | LOGMEM(src->ctx); |
| 1543 | return LY_EMEM; |
| 1544 | } |
| 1545 | } else { |
| 1546 | /* canonize string */ |
| 1547 | str = strdup(src->val.str); |
| 1548 | } |
| 1549 | |
| 1550 | /* ignore errors, the value may not satisfy schema constraints */ |
Michal Vasko | 0fdb0d9 | 2020-11-06 17:25:50 +0100 | [diff] [blame^] | 1551 | rc = type->plugin->store(src->ctx, type, str, strlen(str), LY_TYPE_STORE_DYNAMIC, src->format, src->prefix_data, |
| 1552 | LYD_HINT_DATA, xp_node->node->schema, &val, &err); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1553 | ly_err_free(err); |
| 1554 | if (rc) { |
| 1555 | /* invalid value */ |
| 1556 | free(str); |
| 1557 | goto fill; |
| 1558 | } |
| 1559 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1560 | /* use the canonized value */ |
| 1561 | set_init(trg, src); |
| 1562 | trg->type = src->type; |
| 1563 | if (src->type == LYXP_SET_NUMBER) { |
Michal Vasko | 0fdb0d9 | 2020-11-06 17:25:50 +0100 | [diff] [blame^] | 1564 | trg->val.num = strtold(val.canonical, &ptr); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1565 | LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT); |
| 1566 | } else { |
Michal Vasko | 0fdb0d9 | 2020-11-06 17:25:50 +0100 | [diff] [blame^] | 1567 | trg->val.str = strdup(val.canonical); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1568 | } |
| 1569 | type->plugin->free(src->ctx, &val); |
| 1570 | return LY_SUCCESS; |
| 1571 | |
| 1572 | fill: |
| 1573 | /* no canonization needed/possible */ |
| 1574 | set_fill_set(trg, src); |
| 1575 | return LY_SUCCESS; |
| 1576 | } |
| 1577 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1578 | #ifndef NDEBUG |
| 1579 | |
| 1580 | /** |
| 1581 | * @brief Bubble sort @p set into XPath document order. |
| 1582 | * Context position aware. Unused in the 'Release' build target. |
| 1583 | * |
| 1584 | * @param[in] set Set to sort. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1585 | * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0). |
| 1586 | */ |
| 1587 | static int |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1588 | set_sort(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1589 | { |
| 1590 | uint32_t i, j; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1591 | int ret = 0, cmp; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1592 | ly_bool inverted, change; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1593 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1594 | struct lyxp_set_node item; |
| 1595 | struct lyxp_set_hash_node hnode; |
| 1596 | uint64_t hash; |
| 1597 | |
Michal Vasko | 3cf8fbf | 2020-05-27 15:21:21 +0200 | [diff] [blame] | 1598 | if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1599 | return 0; |
| 1600 | } |
| 1601 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1602 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1603 | 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] | 1604 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1605 | |
| 1606 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1607 | if (set_assign_pos(set, root, set->root_type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1608 | return -1; |
| 1609 | } |
| 1610 | |
| 1611 | LOGDBG(LY_LDGXPATH, "SORT BEGIN"); |
| 1612 | print_set_debug(set); |
| 1613 | |
| 1614 | for (i = 0; i < set->used; ++i) { |
| 1615 | inverted = 0; |
| 1616 | change = 0; |
| 1617 | |
| 1618 | for (j = 1; j < set->used - i; ++j) { |
| 1619 | /* compare node positions */ |
| 1620 | if (inverted) { |
| 1621 | cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]); |
| 1622 | } else { |
| 1623 | cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]); |
| 1624 | } |
| 1625 | |
| 1626 | /* swap if needed */ |
| 1627 | if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) { |
| 1628 | change = 1; |
| 1629 | |
| 1630 | item = set->val.nodes[j - 1]; |
| 1631 | set->val.nodes[j - 1] = set->val.nodes[j]; |
| 1632 | set->val.nodes[j] = item; |
| 1633 | } else { |
| 1634 | /* whether node_pos1 should be smaller than node_pos2 or the other way around */ |
| 1635 | inverted = !inverted; |
| 1636 | } |
| 1637 | } |
| 1638 | |
| 1639 | ++ret; |
| 1640 | |
| 1641 | if (!change) { |
| 1642 | break; |
| 1643 | } |
| 1644 | } |
| 1645 | |
| 1646 | LOGDBG(LY_LDGXPATH, "SORT END %d", ret); |
| 1647 | print_set_debug(set); |
| 1648 | |
| 1649 | /* check node hashes */ |
| 1650 | if (set->used >= LYD_HT_MIN_ITEMS) { |
| 1651 | assert(set->ht); |
| 1652 | for (i = 0; i < set->used; ++i) { |
| 1653 | hnode.node = set->val.nodes[i].node; |
| 1654 | hnode.type = set->val.nodes[i].type; |
| 1655 | |
| 1656 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 1657 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 1658 | hash = dict_hash_multi(hash, NULL, 0); |
| 1659 | |
| 1660 | assert(!lyht_find(set->ht, &hnode, hash, NULL)); |
| 1661 | } |
| 1662 | } |
| 1663 | |
| 1664 | return ret - 1; |
| 1665 | } |
| 1666 | |
| 1667 | /** |
| 1668 | * @brief Remove duplicate entries in a sorted node set. |
| 1669 | * |
| 1670 | * @param[in] set Sorted set to check. |
| 1671 | * @return LY_ERR (LY_EEXIST if some duplicates are found) |
| 1672 | */ |
| 1673 | static LY_ERR |
| 1674 | set_sorted_dup_node_clean(struct lyxp_set *set) |
| 1675 | { |
| 1676 | uint32_t i = 0; |
| 1677 | LY_ERR ret = LY_SUCCESS; |
| 1678 | |
| 1679 | if (set->used > 1) { |
| 1680 | while (i < set->used - 1) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1681 | if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) && |
| 1682 | (set->val.nodes[i].type == set->val.nodes[i + 1].type)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1683 | set_remove_node_none(set, i + 1); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1684 | ret = LY_EEXIST; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1685 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1686 | ++i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1687 | } |
| 1688 | } |
| 1689 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1690 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1691 | return ret; |
| 1692 | } |
| 1693 | |
| 1694 | #endif |
| 1695 | |
| 1696 | /** |
| 1697 | * @brief Merge 2 sorted sets into one. |
| 1698 | * |
| 1699 | * @param[in,out] trg Set to merge into. Duplicates are removed. |
| 1700 | * @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] | 1701 | * @return LY_ERR |
| 1702 | */ |
| 1703 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1704 | set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1705 | { |
| 1706 | uint32_t i, j, k, count, dup_count; |
| 1707 | int cmp; |
| 1708 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1709 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1710 | 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] | 1711 | return LY_EINVAL; |
| 1712 | } |
| 1713 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1714 | if (!src->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1715 | return LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1716 | } else if (!trg->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1717 | set_fill_set(trg, src); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1718 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1719 | return LY_SUCCESS; |
| 1720 | } |
| 1721 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1722 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1723 | 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] | 1724 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1725 | |
| 1726 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1727 | 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] | 1728 | return LY_EINT; |
| 1729 | } |
| 1730 | |
| 1731 | #ifndef NDEBUG |
| 1732 | LOGDBG(LY_LDGXPATH, "MERGE target"); |
| 1733 | print_set_debug(trg); |
| 1734 | LOGDBG(LY_LDGXPATH, "MERGE source"); |
| 1735 | print_set_debug(src); |
| 1736 | #endif |
| 1737 | |
| 1738 | /* make memory for the merge (duplicates are not detected yet, so space |
| 1739 | * will likely be wasted on them, too bad) */ |
| 1740 | if (trg->size - trg->used < src->used) { |
| 1741 | trg->size = trg->used + src->used; |
| 1742 | |
| 1743 | trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes); |
| 1744 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM); |
| 1745 | } |
| 1746 | |
| 1747 | i = 0; |
| 1748 | j = 0; |
| 1749 | count = 0; |
| 1750 | dup_count = 0; |
| 1751 | do { |
| 1752 | cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]); |
| 1753 | if (!cmp) { |
| 1754 | if (!count) { |
| 1755 | /* duplicate, just skip it */ |
| 1756 | ++i; |
| 1757 | ++j; |
| 1758 | } else { |
| 1759 | /* we are copying something already, so let's copy the duplicate too, |
| 1760 | * we are hoping that afterwards there are some more nodes to |
| 1761 | * copy and this way we can copy them all together */ |
| 1762 | ++count; |
| 1763 | ++dup_count; |
| 1764 | ++i; |
| 1765 | ++j; |
| 1766 | } |
| 1767 | } else if (cmp < 0) { |
| 1768 | /* inserting src node into trg, just remember it for now */ |
| 1769 | ++count; |
| 1770 | ++i; |
| 1771 | |
| 1772 | /* insert the hash now */ |
| 1773 | set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type); |
| 1774 | } else if (count) { |
| 1775 | copy_nodes: |
| 1776 | /* time to actually copy the nodes, we have found the largest block of nodes */ |
| 1777 | memmove(&trg->val.nodes[j + (count - dup_count)], |
| 1778 | &trg->val.nodes[j], |
| 1779 | (trg->used - j) * sizeof *trg->val.nodes); |
| 1780 | memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes); |
| 1781 | |
| 1782 | trg->used += count - dup_count; |
| 1783 | /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */ |
| 1784 | j += count - dup_count; |
| 1785 | |
| 1786 | count = 0; |
| 1787 | dup_count = 0; |
| 1788 | } else { |
| 1789 | ++j; |
| 1790 | } |
| 1791 | } while ((i < src->used) && (j < trg->used)); |
| 1792 | |
| 1793 | if ((i < src->used) || count) { |
| 1794 | /* insert all the hashes first */ |
| 1795 | for (k = i; k < src->used; ++k) { |
| 1796 | set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type); |
| 1797 | } |
| 1798 | |
| 1799 | /* loop ended, but we need to copy something at trg end */ |
| 1800 | count += src->used - i; |
| 1801 | i = src->used; |
| 1802 | goto copy_nodes; |
| 1803 | } |
| 1804 | |
| 1805 | /* we are inserting hashes before the actual node insert, which causes |
| 1806 | * situations when there were initially not enough items for a hash table, |
| 1807 | * but even after some were inserted, hash table was not created (during |
| 1808 | * insertion the number of items is not updated yet) */ |
| 1809 | if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) { |
| 1810 | set_insert_node_hash(trg, NULL, 0); |
| 1811 | } |
| 1812 | |
| 1813 | #ifndef NDEBUG |
| 1814 | LOGDBG(LY_LDGXPATH, "MERGE result"); |
| 1815 | print_set_debug(trg); |
| 1816 | #endif |
| 1817 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1818 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1819 | return LY_SUCCESS; |
| 1820 | } |
| 1821 | |
| 1822 | /* |
| 1823 | * (re)parse functions |
| 1824 | * |
| 1825 | * Parse functions parse the expression into |
| 1826 | * tokens (syntactic analysis). |
| 1827 | * |
| 1828 | * Reparse functions perform semantic analysis |
| 1829 | * (do not save the result, just a check) of |
| 1830 | * the expression and fill repeat indices. |
| 1831 | */ |
| 1832 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1833 | LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1834 | 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] | 1835 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1836 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1837 | if (ctx) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1838 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1839 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1840 | return LY_EINCOMPLETE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1841 | } |
| 1842 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1843 | if (want_tok && (exp->tokens[tok_idx] != want_tok)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1844 | if (ctx) { |
Michal Vasko | 0b468e6 | 2020-10-19 09:33:04 +0200 | [diff] [blame] | 1845 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]), |
| 1846 | &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1847 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1848 | return LY_ENOT; |
| 1849 | } |
| 1850 | |
| 1851 | return LY_SUCCESS; |
| 1852 | } |
| 1853 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1854 | LY_ERR |
| 1855 | lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok) |
| 1856 | { |
| 1857 | LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok)); |
| 1858 | |
| 1859 | /* skip the token */ |
| 1860 | ++(*tok_idx); |
| 1861 | |
| 1862 | return LY_SUCCESS; |
| 1863 | } |
| 1864 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1865 | /* just like lyxp_check_token() but tests for 2 tokens */ |
| 1866 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 1867 | 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] | 1868 | enum lyxp_token want_tok2) |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1869 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1870 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1871 | if (ctx) { |
| 1872 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1873 | } |
| 1874 | return LY_EINCOMPLETE; |
| 1875 | } |
| 1876 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1877 | 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] | 1878 | if (ctx) { |
Michal Vasko | 0b468e6 | 2020-10-19 09:33:04 +0200 | [diff] [blame] | 1879 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]), |
| 1880 | &exp->expr[exp->tok_pos[tok_idx]]); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1881 | } |
| 1882 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1883 | } |
| 1884 | |
| 1885 | return LY_SUCCESS; |
| 1886 | } |
| 1887 | |
| 1888 | /** |
| 1889 | * @brief Stack operation push on the repeat array. |
| 1890 | * |
| 1891 | * @param[in] exp Expression to use. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1892 | * @param[in] tok_idx Position in the expresion \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1893 | * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed. |
| 1894 | */ |
| 1895 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1896 | 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] | 1897 | { |
| 1898 | uint16_t i; |
| 1899 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1900 | if (exp->repeat[tok_idx]) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1901 | for (i = 0; exp->repeat[tok_idx][i]; ++i) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1902 | exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]); |
| 1903 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1904 | exp->repeat[tok_idx][i] = repeat_op_idx; |
| 1905 | exp->repeat[tok_idx][i + 1] = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1906 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1907 | exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]); |
| 1908 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1909 | exp->repeat[tok_idx][0] = repeat_op_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1910 | } |
| 1911 | } |
| 1912 | |
| 1913 | /** |
| 1914 | * @brief Reparse Predicate. Logs directly on error. |
| 1915 | * |
| 1916 | * [7] Predicate ::= '[' Expr ']' |
| 1917 | * |
| 1918 | * @param[in] ctx Context for logging. |
| 1919 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1920 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1921 | * @return LY_ERR |
| 1922 | */ |
| 1923 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1924 | 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] | 1925 | { |
| 1926 | LY_ERR rc; |
| 1927 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1928 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1929 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1930 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1931 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1932 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1933 | LY_CHECK_RET(rc); |
| 1934 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1935 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1936 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1937 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1938 | |
| 1939 | return LY_SUCCESS; |
| 1940 | } |
| 1941 | |
| 1942 | /** |
| 1943 | * @brief Reparse RelativeLocationPath. Logs directly on error. |
| 1944 | * |
| 1945 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 1946 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 1947 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 1948 | * |
| 1949 | * @param[in] ctx Context for logging. |
| 1950 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1951 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1952 | * @return LY_ERR (LY_EINCOMPLETE on forward reference) |
| 1953 | */ |
| 1954 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1955 | 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] | 1956 | { |
| 1957 | LY_ERR rc; |
| 1958 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1959 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1960 | LY_CHECK_RET(rc); |
| 1961 | |
| 1962 | goto step; |
| 1963 | do { |
| 1964 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1965 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 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 | step: |
| 1970 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1971 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1972 | case LYXP_TOKEN_DOT: |
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 | break; |
| 1975 | |
| 1976 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1977 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1978 | break; |
| 1979 | |
| 1980 | case LYXP_TOKEN_AT: |
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 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1983 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1984 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1985 | 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] | 1986 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1987 | 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] | 1988 | return LY_EVALID; |
| 1989 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1990 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1991 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1992 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1993 | goto reparse_predicate; |
| 1994 | break; |
| 1995 | |
| 1996 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1997 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1998 | |
| 1999 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2000 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2001 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2002 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2003 | |
| 2004 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2005 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2006 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2007 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2008 | |
| 2009 | reparse_predicate: |
| 2010 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2011 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 2012 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2013 | LY_CHECK_RET(rc); |
| 2014 | } |
| 2015 | break; |
| 2016 | default: |
| 2017 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2018 | 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] | 2019 | return LY_EVALID; |
| 2020 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2021 | } 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] | 2022 | |
| 2023 | return LY_SUCCESS; |
| 2024 | } |
| 2025 | |
| 2026 | /** |
| 2027 | * @brief Reparse AbsoluteLocationPath. Logs directly on error. |
| 2028 | * |
| 2029 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 2030 | * |
| 2031 | * @param[in] ctx Context for logging. |
| 2032 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2033 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2034 | * @return LY_ERR |
| 2035 | */ |
| 2036 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2037 | 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] | 2038 | { |
| 2039 | LY_ERR rc; |
| 2040 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2041 | 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] | 2042 | |
| 2043 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2044 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2045 | /* '/' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2046 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2047 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2048 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2049 | return LY_SUCCESS; |
| 2050 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2051 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2052 | case LYXP_TOKEN_DOT: |
| 2053 | case LYXP_TOKEN_DDOT: |
| 2054 | case LYXP_TOKEN_AT: |
| 2055 | case LYXP_TOKEN_NAMETEST: |
| 2056 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2057 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2058 | LY_CHECK_RET(rc); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2059 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2060 | default: |
| 2061 | break; |
| 2062 | } |
| 2063 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2064 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 2065 | /* '//' RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2066 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2067 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2068 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2069 | LY_CHECK_RET(rc); |
| 2070 | } |
| 2071 | |
| 2072 | return LY_SUCCESS; |
| 2073 | } |
| 2074 | |
| 2075 | /** |
| 2076 | * @brief Reparse FunctionCall. Logs directly on error. |
| 2077 | * |
| 2078 | * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
| 2079 | * |
| 2080 | * @param[in] ctx Context for logging. |
| 2081 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2082 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2083 | * @return LY_ERR |
| 2084 | */ |
| 2085 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2086 | 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] | 2087 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2088 | int8_t min_arg_count = -1; |
| 2089 | uint32_t arg_count, max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2090 | uint16_t func_tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2091 | LY_ERR rc; |
| 2092 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2093 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2094 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2095 | func_tok_idx = *tok_idx; |
| 2096 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2097 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2098 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2099 | min_arg_count = 1; |
| 2100 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2101 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2102 | min_arg_count = 1; |
| 2103 | max_arg_count = 1; |
| 2104 | } |
| 2105 | break; |
| 2106 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2107 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2108 | min_arg_count = 1; |
| 2109 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2110 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2111 | min_arg_count = 0; |
| 2112 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2113 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2114 | min_arg_count = 0; |
| 2115 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2116 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2117 | min_arg_count = 0; |
| 2118 | max_arg_count = 0; |
| 2119 | } |
| 2120 | break; |
| 2121 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2122 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2123 | min_arg_count = 1; |
| 2124 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2125 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2126 | min_arg_count = 0; |
| 2127 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2128 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2129 | min_arg_count = 1; |
| 2130 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2131 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2132 | min_arg_count = 1; |
| 2133 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2134 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2135 | min_arg_count = 1; |
| 2136 | max_arg_count = 1; |
| 2137 | } |
| 2138 | break; |
| 2139 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2140 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2141 | min_arg_count = 2; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2142 | max_arg_count = UINT32_MAX; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2143 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2144 | min_arg_count = 0; |
| 2145 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2146 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2147 | min_arg_count = 0; |
| 2148 | max_arg_count = 1; |
| 2149 | } |
| 2150 | break; |
| 2151 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2152 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2153 | min_arg_count = 1; |
| 2154 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2155 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2156 | min_arg_count = 1; |
| 2157 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2158 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2159 | min_arg_count = 0; |
| 2160 | max_arg_count = 0; |
| 2161 | } |
| 2162 | break; |
| 2163 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2164 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2165 | min_arg_count = 2; |
| 2166 | max_arg_count = 2; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2167 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2168 | min_arg_count = 0; |
| 2169 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2170 | } 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] | 2171 | min_arg_count = 2; |
| 2172 | max_arg_count = 2; |
| 2173 | } |
| 2174 | break; |
| 2175 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2176 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2177 | min_arg_count = 2; |
| 2178 | max_arg_count = 3; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2179 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2180 | min_arg_count = 3; |
| 2181 | max_arg_count = 3; |
| 2182 | } |
| 2183 | break; |
| 2184 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2185 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2186 | min_arg_count = 0; |
| 2187 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2188 | } 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] | 2189 | min_arg_count = 1; |
| 2190 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2191 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2192 | min_arg_count = 2; |
| 2193 | max_arg_count = 2; |
| 2194 | } |
| 2195 | break; |
| 2196 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2197 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2198 | min_arg_count = 2; |
| 2199 | max_arg_count = 2; |
| 2200 | } |
| 2201 | break; |
| 2202 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2203 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2204 | min_arg_count = 2; |
| 2205 | max_arg_count = 2; |
| 2206 | } |
| 2207 | break; |
| 2208 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2209 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2210 | min_arg_count = 0; |
| 2211 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2212 | } 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] | 2213 | min_arg_count = 0; |
| 2214 | max_arg_count = 1; |
| 2215 | } |
| 2216 | break; |
| 2217 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2218 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2219 | min_arg_count = 0; |
| 2220 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2221 | } 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] | 2222 | min_arg_count = 2; |
| 2223 | max_arg_count = 2; |
| 2224 | } |
| 2225 | break; |
| 2226 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2227 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2228 | min_arg_count = 2; |
| 2229 | max_arg_count = 2; |
| 2230 | } |
| 2231 | break; |
| 2232 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2233 | 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] | 2234 | min_arg_count = 2; |
| 2235 | max_arg_count = 2; |
| 2236 | } |
| 2237 | break; |
| 2238 | } |
| 2239 | if (min_arg_count == -1) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2240 | 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] | 2241 | return LY_EINVAL; |
| 2242 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2243 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2244 | |
| 2245 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2246 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2247 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2248 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2249 | |
| 2250 | /* ( Expr ( ',' Expr )* )? */ |
| 2251 | arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2252 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2253 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2254 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2255 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2256 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2257 | LY_CHECK_RET(rc); |
| 2258 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2259 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
| 2260 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2261 | |
| 2262 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2263 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2264 | LY_CHECK_RET(rc); |
| 2265 | } |
| 2266 | |
| 2267 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2268 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2269 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2270 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2271 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2272 | 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] | 2273 | 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] | 2274 | &exp->expr[exp->tok_pos[func_tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2275 | return LY_EVALID; |
| 2276 | } |
| 2277 | |
| 2278 | return LY_SUCCESS; |
| 2279 | } |
| 2280 | |
| 2281 | /** |
| 2282 | * @brief Reparse PathExpr. Logs directly on error. |
| 2283 | * |
| 2284 | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
| 2285 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 2286 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 2287 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
| 2288 | * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
| 2289 | * |
| 2290 | * @param[in] ctx Context for logging. |
| 2291 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2292 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2293 | * @return LY_ERR |
| 2294 | */ |
| 2295 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2296 | 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] | 2297 | { |
| 2298 | LY_ERR rc; |
| 2299 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2300 | if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2301 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2302 | } |
| 2303 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2304 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2305 | case LYXP_TOKEN_PAR1: |
| 2306 | /* '(' Expr ')' Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2307 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2308 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2309 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2310 | LY_CHECK_RET(rc); |
| 2311 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2312 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2313 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2314 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2315 | goto predicate; |
| 2316 | break; |
| 2317 | case LYXP_TOKEN_DOT: |
| 2318 | case LYXP_TOKEN_DDOT: |
| 2319 | case LYXP_TOKEN_AT: |
| 2320 | case LYXP_TOKEN_NAMETEST: |
| 2321 | case LYXP_TOKEN_NODETYPE: |
| 2322 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2323 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2324 | LY_CHECK_RET(rc); |
| 2325 | break; |
| 2326 | case LYXP_TOKEN_FUNCNAME: |
| 2327 | /* FunctionCall */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2328 | rc = reparse_function_call(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2329 | LY_CHECK_RET(rc); |
| 2330 | goto predicate; |
| 2331 | break; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2332 | case LYXP_TOKEN_OPER_PATH: |
| 2333 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2334 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2335 | rc = reparse_absolute_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2336 | LY_CHECK_RET(rc); |
| 2337 | break; |
| 2338 | case LYXP_TOKEN_LITERAL: |
| 2339 | /* Literal */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2340 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2341 | goto predicate; |
| 2342 | break; |
| 2343 | case LYXP_TOKEN_NUMBER: |
| 2344 | /* Number */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2345 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2346 | goto predicate; |
| 2347 | break; |
| 2348 | default: |
| 2349 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2350 | 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] | 2351 | return LY_EVALID; |
| 2352 | } |
| 2353 | |
| 2354 | return LY_SUCCESS; |
| 2355 | |
| 2356 | predicate: |
| 2357 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2358 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 2359 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2360 | LY_CHECK_RET(rc); |
| 2361 | } |
| 2362 | |
| 2363 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2364 | 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] | 2365 | |
| 2366 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2367 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2368 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2369 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2370 | LY_CHECK_RET(rc); |
| 2371 | } |
| 2372 | |
| 2373 | return LY_SUCCESS; |
| 2374 | } |
| 2375 | |
| 2376 | /** |
| 2377 | * @brief Reparse UnaryExpr. Logs directly on error. |
| 2378 | * |
| 2379 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 2380 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 2381 | * |
| 2382 | * @param[in] ctx Context for logging. |
| 2383 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2384 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2385 | * @return LY_ERR |
| 2386 | */ |
| 2387 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2388 | 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] | 2389 | { |
| 2390 | uint16_t prev_exp; |
| 2391 | LY_ERR rc; |
| 2392 | |
| 2393 | /* ('-')* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2394 | prev_exp = *tok_idx; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2395 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2396 | (exp->expr[exp->tok_pos[*tok_idx]] == '-')) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2397 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2398 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2399 | } |
| 2400 | |
| 2401 | /* PathExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2402 | prev_exp = *tok_idx; |
| 2403 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2404 | LY_CHECK_RET(rc); |
| 2405 | |
| 2406 | /* ('|' PathExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2407 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2408 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2409 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2410 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 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 | |
| 2415 | return LY_SUCCESS; |
| 2416 | } |
| 2417 | |
| 2418 | /** |
| 2419 | * @brief Reparse AdditiveExpr. Logs directly on error. |
| 2420 | * |
| 2421 | * [15] AdditiveExpr ::= MultiplicativeExpr |
| 2422 | * | AdditiveExpr '+' MultiplicativeExpr |
| 2423 | * | AdditiveExpr '-' MultiplicativeExpr |
| 2424 | * [16] MultiplicativeExpr ::= UnaryExpr |
| 2425 | * | MultiplicativeExpr '*' UnaryExpr |
| 2426 | * | MultiplicativeExpr 'div' UnaryExpr |
| 2427 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 2428 | * |
| 2429 | * @param[in] ctx Context for logging. |
| 2430 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2431 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2432 | * @return LY_ERR |
| 2433 | */ |
| 2434 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2435 | 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] | 2436 | { |
| 2437 | uint16_t prev_add_exp, prev_mul_exp; |
| 2438 | LY_ERR rc; |
| 2439 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2440 | prev_add_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2441 | goto reparse_multiplicative_expr; |
| 2442 | |
| 2443 | /* ('+' / '-' MultiplicativeExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2444 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2445 | ((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] | 2446 | exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2447 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2448 | |
| 2449 | reparse_multiplicative_expr: |
| 2450 | /* UnaryExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2451 | prev_mul_exp = *tok_idx; |
| 2452 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2453 | LY_CHECK_RET(rc); |
| 2454 | |
| 2455 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2456 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2457 | ((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] | 2458 | exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2459 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2460 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2461 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2462 | LY_CHECK_RET(rc); |
| 2463 | } |
| 2464 | } |
| 2465 | |
| 2466 | return LY_SUCCESS; |
| 2467 | } |
| 2468 | |
| 2469 | /** |
| 2470 | * @brief Reparse EqualityExpr. Logs directly on error. |
| 2471 | * |
| 2472 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
| 2473 | * | EqualityExpr '!=' RelationalExpr |
| 2474 | * [14] RelationalExpr ::= AdditiveExpr |
| 2475 | * | RelationalExpr '<' AdditiveExpr |
| 2476 | * | RelationalExpr '>' AdditiveExpr |
| 2477 | * | RelationalExpr '<=' AdditiveExpr |
| 2478 | * | RelationalExpr '>=' AdditiveExpr |
| 2479 | * |
| 2480 | * @param[in] ctx Context for logging. |
| 2481 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2482 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2483 | * @return LY_ERR |
| 2484 | */ |
| 2485 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2486 | 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] | 2487 | { |
| 2488 | uint16_t prev_eq_exp, prev_rel_exp; |
| 2489 | LY_ERR rc; |
| 2490 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2491 | prev_eq_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2492 | goto reparse_additive_expr; |
| 2493 | |
| 2494 | /* ('=' / '!=' RelationalExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2495 | 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] | 2496 | exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2497 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2498 | |
| 2499 | reparse_additive_expr: |
| 2500 | /* AdditiveExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2501 | prev_rel_exp = *tok_idx; |
| 2502 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2503 | LY_CHECK_RET(rc); |
| 2504 | |
| 2505 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2506 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2507 | exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2508 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2509 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 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 | } |
| 2514 | |
| 2515 | return LY_SUCCESS; |
| 2516 | } |
| 2517 | |
| 2518 | /** |
| 2519 | * @brief Reparse OrExpr. Logs directly on error. |
| 2520 | * |
| 2521 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 2522 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 2523 | * |
| 2524 | * @param[in] ctx Context for logging. |
| 2525 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2526 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2527 | * @return LY_ERR |
| 2528 | */ |
| 2529 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2530 | 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] | 2531 | { |
| 2532 | uint16_t prev_or_exp, prev_and_exp; |
| 2533 | LY_ERR rc; |
| 2534 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2535 | prev_or_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2536 | goto reparse_equality_expr; |
| 2537 | |
| 2538 | /* ('or' AndExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2539 | 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] | 2540 | exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2541 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2542 | |
| 2543 | reparse_equality_expr: |
| 2544 | /* EqualityExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2545 | prev_and_exp = *tok_idx; |
| 2546 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2547 | LY_CHECK_RET(rc); |
| 2548 | |
| 2549 | /* ('and' EqualityExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2550 | 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] | 2551 | exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2552 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2553 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 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 | } |
| 2558 | |
| 2559 | return LY_SUCCESS; |
| 2560 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2561 | |
| 2562 | /** |
| 2563 | * @brief Parse NCName. |
| 2564 | * |
| 2565 | * @param[in] ncname Name to parse. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2566 | * @return Length of @p ncname valid bytes. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2567 | */ |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2568 | static long int |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2569 | parse_ncname(const char *ncname) |
| 2570 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2571 | uint32_t uc; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2572 | size_t size; |
| 2573 | long int len = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2574 | |
| 2575 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0); |
| 2576 | if (!is_xmlqnamestartchar(uc) || (uc == ':')) { |
| 2577 | return len; |
| 2578 | } |
| 2579 | |
| 2580 | do { |
| 2581 | len += size; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2582 | if (!*ncname) { |
| 2583 | break; |
| 2584 | } |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2585 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2586 | } while (is_xmlqnamechar(uc) && (uc != ':')); |
| 2587 | |
| 2588 | return len; |
| 2589 | } |
| 2590 | |
| 2591 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2592 | * @brief Add @p token into the expression @p exp. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2593 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2594 | * @param[in] ctx Context for logging. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2595 | * @param[in] exp Expression to use. |
| 2596 | * @param[in] token Token to add. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2597 | * @param[in] tok_pos Token position in the XPath expression. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2598 | * @param[in] tok_len Token length in the XPath expression. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2599 | * @return LY_ERR |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2600 | */ |
| 2601 | static LY_ERR |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2602 | 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] | 2603 | { |
| 2604 | uint32_t prev; |
| 2605 | |
| 2606 | if (exp->used == exp->size) { |
| 2607 | prev = exp->size; |
| 2608 | exp->size += LYXP_EXPR_SIZE_STEP; |
| 2609 | if (prev > exp->size) { |
| 2610 | LOGINT(ctx); |
| 2611 | return LY_EINT; |
| 2612 | } |
| 2613 | |
| 2614 | exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens); |
| 2615 | LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM); |
| 2616 | exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos); |
| 2617 | LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM); |
| 2618 | exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len); |
| 2619 | LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM); |
| 2620 | } |
| 2621 | |
| 2622 | exp->tokens[exp->used] = token; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2623 | exp->tok_pos[exp->used] = tok_pos; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2624 | exp->tok_len[exp->used] = tok_len; |
| 2625 | ++exp->used; |
| 2626 | return LY_SUCCESS; |
| 2627 | } |
| 2628 | |
| 2629 | void |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2630 | lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2631 | { |
| 2632 | uint16_t i; |
| 2633 | |
| 2634 | if (!expr) { |
| 2635 | return; |
| 2636 | } |
| 2637 | |
| 2638 | lydict_remove(ctx, expr->expr); |
| 2639 | free(expr->tokens); |
| 2640 | free(expr->tok_pos); |
| 2641 | free(expr->tok_len); |
| 2642 | if (expr->repeat) { |
| 2643 | for (i = 0; i < expr->used; ++i) { |
| 2644 | free(expr->repeat[i]); |
| 2645 | } |
| 2646 | } |
| 2647 | free(expr->repeat); |
| 2648 | free(expr); |
| 2649 | } |
| 2650 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2651 | LY_ERR |
| 2652 | 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] | 2653 | { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2654 | LY_ERR ret = LY_SUCCESS; |
| 2655 | struct lyxp_expr *expr; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2656 | size_t parsed = 0, tok_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2657 | enum lyxp_token tok_type; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2658 | ly_bool prev_function_check = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2659 | uint16_t tok_idx = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2660 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2661 | assert(expr_p); |
| 2662 | |
| 2663 | if (!expr_str[0]) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2664 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2665 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2666 | } |
| 2667 | |
| 2668 | if (!expr_len) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2669 | expr_len = strlen(expr_str); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2670 | } |
| 2671 | if (expr_len > UINT16_MAX) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2672 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "XPath expression cannot be longer than %ud characters.", UINT16_MAX); |
| 2673 | return LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2674 | } |
| 2675 | |
| 2676 | /* init lyxp_expr structure */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2677 | expr = calloc(1, sizeof *expr); |
| 2678 | LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error); |
| 2679 | LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error); |
| 2680 | expr->used = 0; |
| 2681 | expr->size = LYXP_EXPR_SIZE_START; |
| 2682 | expr->tokens = malloc(expr->size * sizeof *expr->tokens); |
| 2683 | LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2684 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2685 | expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos); |
| 2686 | 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] | 2687 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2688 | expr->tok_len = malloc(expr->size * sizeof *expr->tok_len); |
| 2689 | 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] | 2690 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2691 | /* make expr 0-terminated */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2692 | expr_str = expr->expr; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2693 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2694 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2695 | ++parsed; |
| 2696 | } |
| 2697 | |
| 2698 | do { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2699 | if (expr_str[parsed] == '(') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2700 | |
| 2701 | /* '(' */ |
| 2702 | tok_len = 1; |
| 2703 | tok_type = LYXP_TOKEN_PAR1; |
| 2704 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2705 | 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] | 2706 | /* it is a NodeType/FunctionName after all */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2707 | if (((expr->tok_len[expr->used - 1] == 4) && |
| 2708 | (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) || |
| 2709 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) || |
| 2710 | ((expr->tok_len[expr->used - 1] == 7) && |
| 2711 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2712 | expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2713 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2714 | expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2715 | } |
| 2716 | prev_function_check = 0; |
| 2717 | } |
| 2718 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2719 | } else if (expr_str[parsed] == ')') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2720 | |
| 2721 | /* ')' */ |
| 2722 | tok_len = 1; |
| 2723 | tok_type = LYXP_TOKEN_PAR2; |
| 2724 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2725 | } else if (expr_str[parsed] == '[') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2726 | |
| 2727 | /* '[' */ |
| 2728 | tok_len = 1; |
| 2729 | tok_type = LYXP_TOKEN_BRACK1; |
| 2730 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2731 | } else if (expr_str[parsed] == ']') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2732 | |
| 2733 | /* ']' */ |
| 2734 | tok_len = 1; |
| 2735 | tok_type = LYXP_TOKEN_BRACK2; |
| 2736 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2737 | } else if (!strncmp(&expr_str[parsed], "..", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2738 | |
| 2739 | /* '..' */ |
| 2740 | tok_len = 2; |
| 2741 | tok_type = LYXP_TOKEN_DDOT; |
| 2742 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2743 | } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2744 | |
| 2745 | /* '.' */ |
| 2746 | tok_len = 1; |
| 2747 | tok_type = LYXP_TOKEN_DOT; |
| 2748 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2749 | } else if (expr_str[parsed] == '@') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2750 | |
| 2751 | /* '@' */ |
| 2752 | tok_len = 1; |
| 2753 | tok_type = LYXP_TOKEN_AT; |
| 2754 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2755 | } else if (expr_str[parsed] == ',') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2756 | |
| 2757 | /* ',' */ |
| 2758 | tok_len = 1; |
| 2759 | tok_type = LYXP_TOKEN_COMMA; |
| 2760 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2761 | } else if (expr_str[parsed] == '\'') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2762 | |
| 2763 | /* Literal with ' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2764 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {} |
| 2765 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2766 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID, |
| 2767 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2768 | ++tok_len; |
| 2769 | tok_type = LYXP_TOKEN_LITERAL; |
| 2770 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2771 | } else if (expr_str[parsed] == '\"') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2772 | |
| 2773 | /* Literal with " */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2774 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {} |
| 2775 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2776 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID, |
| 2777 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2778 | ++tok_len; |
| 2779 | tok_type = LYXP_TOKEN_LITERAL; |
| 2780 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2781 | } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2782 | |
| 2783 | /* Number */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2784 | for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
| 2785 | if (expr_str[parsed + tok_len] == '.') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2786 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2787 | for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2788 | } |
| 2789 | tok_type = LYXP_TOKEN_NUMBER; |
| 2790 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2791 | } else if (expr_str[parsed] == '/') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2792 | |
| 2793 | /* Operator '/', '//' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2794 | if (!strncmp(&expr_str[parsed], "//", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2795 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2796 | tok_type = LYXP_TOKEN_OPER_RPATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2797 | } else { |
| 2798 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2799 | tok_type = LYXP_TOKEN_OPER_PATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2800 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2801 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2802 | } else if (!strncmp(&expr_str[parsed], "!=", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2803 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2804 | /* Operator '!=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2805 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2806 | tok_type = LYXP_TOKEN_OPER_NEQUAL; |
| 2807 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2808 | } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2809 | |
| 2810 | /* Operator '<=', '>=' */ |
| 2811 | tok_len = 2; |
| 2812 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2813 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2814 | } else if (expr_str[parsed] == '|') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2815 | |
| 2816 | /* Operator '|' */ |
| 2817 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2818 | tok_type = LYXP_TOKEN_OPER_UNI; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2819 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2820 | } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2821 | |
| 2822 | /* Operator '+', '-' */ |
| 2823 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2824 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2825 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2826 | } else if (expr_str[parsed] == '=') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2827 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2828 | /* Operator '=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2829 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2830 | tok_type = LYXP_TOKEN_OPER_EQUAL; |
| 2831 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2832 | } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2833 | |
| 2834 | /* Operator '<', '>' */ |
| 2835 | tok_len = 1; |
| 2836 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2837 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2838 | } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) && |
| 2839 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) && |
| 2840 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) && |
| 2841 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) && |
| 2842 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) && |
| 2843 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) && |
| 2844 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) && |
| 2845 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) && |
| 2846 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) && |
| 2847 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) && |
| 2848 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) && |
| 2849 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2850 | |
| 2851 | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2852 | if (expr_str[parsed] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2853 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2854 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2855 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2856 | } else if (!strncmp(&expr_str[parsed], "or", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2857 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2858 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2859 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2860 | } else if (!strncmp(&expr_str[parsed], "and", 3)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2861 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2862 | tok_type = LYXP_TOKEN_OPER_LOG; |
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], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2865 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2866 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2867 | |
| 2868 | } else if (prev_function_check) { |
Michal Vasko | 5307857 | 2019-05-24 08:50:15 +0200 | [diff] [blame] | 2869 | 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] | 2870 | 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] | 2871 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2872 | goto error; |
| 2873 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2874 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed + 1, expr_str); |
| 2875 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2876 | goto error; |
| 2877 | } |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2878 | } else if (expr_str[parsed] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2879 | |
| 2880 | /* NameTest '*' */ |
| 2881 | tok_len = 1; |
| 2882 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2883 | |
| 2884 | } else { |
| 2885 | |
| 2886 | /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2887 | long int ncname_len = parse_ncname(&expr_str[parsed]); |
| 2888 | LY_CHECK_ERR_GOTO(ncname_len < 0, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2889 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID, |
| 2890 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2891 | tok_len = ncname_len; |
| 2892 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2893 | if (expr_str[parsed + tok_len] == ':') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2894 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2895 | if (expr_str[parsed + tok_len] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2896 | ++tok_len; |
| 2897 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2898 | ncname_len = parse_ncname(&expr_str[parsed + tok_len]); |
| 2899 | LY_CHECK_ERR_GOTO(ncname_len < 0, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2900 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID, |
| 2901 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2902 | tok_len += ncname_len; |
| 2903 | } |
| 2904 | /* remove old flag to prevent ambiguities */ |
| 2905 | prev_function_check = 0; |
| 2906 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2907 | } else { |
| 2908 | /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */ |
| 2909 | prev_function_check = 1; |
| 2910 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2911 | } |
| 2912 | } |
| 2913 | |
| 2914 | /* store the token, move on to the next one */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2915 | 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] | 2916 | parsed += tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2917 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2918 | ++parsed; |
| 2919 | } |
| 2920 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2921 | } while (expr_str[parsed]); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2922 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2923 | if (reparse) { |
| 2924 | /* prealloc repeat */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2925 | expr->repeat = calloc(expr->size, sizeof *expr->repeat); |
| 2926 | LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2927 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2928 | /* fill repeat */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2929 | LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx), ret = LY_EVALID, error); |
| 2930 | if (expr->used > tok_idx) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2931 | 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] | 2932 | &expr->expr[expr->tok_pos[tok_idx]]); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2933 | ret = LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2934 | goto error; |
| 2935 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2936 | } |
| 2937 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2938 | print_expr_struct_debug(expr); |
| 2939 | *expr_p = expr; |
| 2940 | return LY_SUCCESS; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2941 | |
| 2942 | error: |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2943 | lyxp_expr_free(ctx, expr); |
| 2944 | return ret; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2945 | } |
| 2946 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2947 | LY_ERR |
| 2948 | 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] | 2949 | { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2950 | LY_ERR ret = LY_SUCCESS; |
| 2951 | struct lyxp_expr *dup = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2952 | uint32_t i, j; |
| 2953 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2954 | if (!exp) { |
| 2955 | goto cleanup; |
| 2956 | } |
| 2957 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2958 | dup = calloc(1, sizeof *dup); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2959 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2960 | |
| 2961 | dup->tokens = malloc(exp->used * sizeof *dup->tokens); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2962 | LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2963 | memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens); |
| 2964 | |
| 2965 | dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2966 | 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] | 2967 | memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos); |
| 2968 | |
| 2969 | dup->tok_len = malloc(exp->used * sizeof *dup->tok_len); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2970 | 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] | 2971 | memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len); |
| 2972 | |
| 2973 | dup->repeat = malloc(exp->used * sizeof *dup->repeat); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2974 | LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2975 | for (i = 0; i < exp->used; ++i) { |
| 2976 | if (!exp->repeat[i]) { |
| 2977 | dup->repeat[i] = NULL; |
| 2978 | } else { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2979 | for (j = 0; exp->repeat[i][j]; ++j) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2980 | /* the ending 0 as well */ |
| 2981 | ++j; |
| 2982 | |
Michal Vasko | 99c7164 | 2020-07-03 13:33:36 +0200 | [diff] [blame] | 2983 | dup->repeat[i] = malloc(j * sizeof **dup->repeat); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2984 | 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] | 2985 | memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat); |
| 2986 | dup->repeat[i][j - 1] = 0; |
| 2987 | } |
| 2988 | } |
| 2989 | |
| 2990 | dup->used = exp->used; |
| 2991 | dup->size = exp->used; |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2992 | 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] | 2993 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2994 | cleanup: |
| 2995 | if (ret) { |
| 2996 | lyxp_expr_free(ctx, dup); |
| 2997 | } else { |
| 2998 | *dup_p = dup; |
| 2999 | } |
| 3000 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3001 | } |
| 3002 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3003 | /* |
| 3004 | * warn functions |
| 3005 | * |
| 3006 | * Warn functions check specific reasonable conditions for schema XPath |
| 3007 | * and print a warning if they are not satisfied. |
| 3008 | */ |
| 3009 | |
| 3010 | /** |
| 3011 | * @brief Get the last-added schema node that is currently in the context. |
| 3012 | * |
| 3013 | * @param[in] set Set to search in. |
| 3014 | * @return Last-added schema context node, NULL if no node is in context. |
| 3015 | */ |
| 3016 | static struct lysc_node * |
| 3017 | warn_get_scnode_in_ctx(struct lyxp_set *set) |
| 3018 | { |
| 3019 | uint32_t i; |
| 3020 | |
| 3021 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
| 3022 | return NULL; |
| 3023 | } |
| 3024 | |
| 3025 | i = set->used; |
| 3026 | do { |
| 3027 | --i; |
| 3028 | if (set->val.scnodes[i].in_ctx == 1) { |
| 3029 | /* if there are more, simply return the first found (last added) */ |
| 3030 | return set->val.scnodes[i].scnode; |
| 3031 | } |
| 3032 | } while (i); |
| 3033 | |
| 3034 | return NULL; |
| 3035 | } |
| 3036 | |
| 3037 | /** |
| 3038 | * @brief Test whether a type is numeric - integer type or decimal64. |
| 3039 | * |
| 3040 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3041 | * @return Boolean value whether @p type is numeric type or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3042 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3043 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3044 | warn_is_numeric_type(struct lysc_type *type) |
| 3045 | { |
| 3046 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3047 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3048 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3049 | |
| 3050 | switch (type->basetype) { |
| 3051 | case LY_TYPE_DEC64: |
| 3052 | case LY_TYPE_INT8: |
| 3053 | case LY_TYPE_UINT8: |
| 3054 | case LY_TYPE_INT16: |
| 3055 | case LY_TYPE_UINT16: |
| 3056 | case LY_TYPE_INT32: |
| 3057 | case LY_TYPE_UINT32: |
| 3058 | case LY_TYPE_INT64: |
| 3059 | case LY_TYPE_UINT64: |
| 3060 | return 1; |
| 3061 | case LY_TYPE_UNION: |
| 3062 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3063 | LY_ARRAY_FOR(uni->types, u) { |
| 3064 | ret = warn_is_numeric_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3065 | if (ret) { |
| 3066 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3067 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3068 | } |
| 3069 | } |
| 3070 | /* did not find any suitable type */ |
| 3071 | return 0; |
| 3072 | case LY_TYPE_LEAFREF: |
| 3073 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
| 3074 | default: |
| 3075 | return 0; |
| 3076 | } |
| 3077 | } |
| 3078 | |
| 3079 | /** |
| 3080 | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
| 3081 | * |
| 3082 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3083 | * @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] | 3084 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3085 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3086 | warn_is_string_type(struct lysc_type *type) |
| 3087 | { |
| 3088 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3089 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3090 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3091 | |
| 3092 | switch (type->basetype) { |
| 3093 | case LY_TYPE_BITS: |
| 3094 | case LY_TYPE_ENUM: |
| 3095 | case LY_TYPE_IDENT: |
| 3096 | case LY_TYPE_INST: |
| 3097 | case LY_TYPE_STRING: |
| 3098 | return 1; |
| 3099 | case LY_TYPE_UNION: |
| 3100 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3101 | LY_ARRAY_FOR(uni->types, u) { |
| 3102 | ret = warn_is_string_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3103 | if (ret) { |
| 3104 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3105 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3106 | } |
| 3107 | } |
| 3108 | /* did not find any suitable type */ |
| 3109 | return 0; |
| 3110 | case LY_TYPE_LEAFREF: |
| 3111 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
| 3112 | default: |
| 3113 | return 0; |
| 3114 | } |
| 3115 | } |
| 3116 | |
| 3117 | /** |
| 3118 | * @brief Test whether a type is one specific type. |
| 3119 | * |
| 3120 | * @param[in] type Type to test. |
| 3121 | * @param[in] base Expected type. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3122 | * @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] | 3123 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3124 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3125 | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
| 3126 | { |
| 3127 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3128 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3129 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3130 | |
| 3131 | if (type->basetype == base) { |
| 3132 | return 1; |
| 3133 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3134 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3135 | LY_ARRAY_FOR(uni->types, u) { |
| 3136 | ret = warn_is_specific_type(uni->types[u], base); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3137 | if (ret) { |
| 3138 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3139 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3140 | } |
| 3141 | } |
| 3142 | /* did not find any suitable type */ |
| 3143 | return 0; |
| 3144 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
| 3145 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
| 3146 | } |
| 3147 | |
| 3148 | return 0; |
| 3149 | } |
| 3150 | |
| 3151 | /** |
| 3152 | * @brief Get next type of a (union) type. |
| 3153 | * |
| 3154 | * @param[in] type Base type. |
| 3155 | * @param[in] prev_type Previously returned type. |
| 3156 | * @return Next type or NULL. |
| 3157 | */ |
| 3158 | static struct lysc_type * |
| 3159 | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type) |
| 3160 | { |
| 3161 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3162 | ly_bool found = 0; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3163 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3164 | |
| 3165 | switch (type->basetype) { |
| 3166 | case LY_TYPE_UNION: |
| 3167 | uni = (struct lysc_type_union *)type; |
| 3168 | if (!prev_type) { |
| 3169 | return uni->types[0]; |
| 3170 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3171 | LY_ARRAY_FOR(uni->types, u) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3172 | if (found) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3173 | return uni->types[u]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3174 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3175 | if (prev_type == uni->types[u]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3176 | found = 1; |
| 3177 | } |
| 3178 | } |
| 3179 | return NULL; |
| 3180 | default: |
| 3181 | if (prev_type) { |
| 3182 | assert(type == prev_type); |
| 3183 | return NULL; |
| 3184 | } else { |
| 3185 | return type; |
| 3186 | } |
| 3187 | } |
| 3188 | } |
| 3189 | |
| 3190 | /** |
| 3191 | * @brief Test whether 2 types have a common type. |
| 3192 | * |
| 3193 | * @param[in] type1 First type. |
| 3194 | * @param[in] type2 Second type. |
| 3195 | * @return 1 if they do, 0 otherwise. |
| 3196 | */ |
| 3197 | static int |
| 3198 | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
| 3199 | { |
| 3200 | struct lysc_type *t1, *rt1, *t2, *rt2; |
| 3201 | |
| 3202 | t1 = NULL; |
| 3203 | while ((t1 = warn_is_equal_type_next_type(type1, t1))) { |
| 3204 | if (t1->basetype == LY_TYPE_LEAFREF) { |
| 3205 | rt1 = ((struct lysc_type_leafref *)t1)->realtype; |
| 3206 | } else { |
| 3207 | rt1 = t1; |
| 3208 | } |
| 3209 | |
| 3210 | t2 = NULL; |
| 3211 | while ((t2 = warn_is_equal_type_next_type(type2, t2))) { |
| 3212 | if (t2->basetype == LY_TYPE_LEAFREF) { |
| 3213 | rt2 = ((struct lysc_type_leafref *)t2)->realtype; |
| 3214 | } else { |
| 3215 | rt2 = t2; |
| 3216 | } |
| 3217 | |
| 3218 | if (rt2->basetype == rt1->basetype) { |
| 3219 | /* match found */ |
| 3220 | return 1; |
| 3221 | } |
| 3222 | } |
| 3223 | } |
| 3224 | |
| 3225 | return 0; |
| 3226 | } |
| 3227 | |
| 3228 | /** |
| 3229 | * @brief Check both operands of comparison operators. |
| 3230 | * |
| 3231 | * @param[in] ctx Context for errors. |
| 3232 | * @param[in] set1 First operand set. |
| 3233 | * @param[in] set2 Second operand set. |
| 3234 | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
| 3235 | * @param[in] expr Start of the expression to print with the warning. |
| 3236 | * @param[in] tok_pos Token position. |
| 3237 | */ |
| 3238 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3239 | 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] | 3240 | { |
| 3241 | struct lysc_node_leaf *node1, *node2; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3242 | ly_bool leaves = 1, warning = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3243 | |
| 3244 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
| 3245 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
| 3246 | |
| 3247 | if (!node1 && !node2) { |
| 3248 | /* no node-sets involved, nothing to do */ |
| 3249 | return; |
| 3250 | } |
| 3251 | |
| 3252 | if (node1) { |
| 3253 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3254 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
| 3255 | warning = 1; |
| 3256 | leaves = 0; |
| 3257 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
| 3258 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
| 3259 | warning = 1; |
| 3260 | } |
| 3261 | } |
| 3262 | |
| 3263 | if (node2) { |
| 3264 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3265 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
| 3266 | warning = 1; |
| 3267 | leaves = 0; |
| 3268 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
| 3269 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
| 3270 | warning = 1; |
| 3271 | } |
| 3272 | } |
| 3273 | |
| 3274 | if (node1 && node2 && leaves && !numbers_only) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3275 | if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) || |
| 3276 | (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) || |
| 3277 | (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) && |
| 3278 | !warn_is_equal_type(node1->type, node2->type))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3279 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
| 3280 | warning = 1; |
| 3281 | } |
| 3282 | } |
| 3283 | |
| 3284 | if (warning) { |
| 3285 | LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos); |
| 3286 | } |
| 3287 | } |
| 3288 | |
| 3289 | /** |
| 3290 | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
| 3291 | * |
| 3292 | * @param[in] exp Parsed XPath expression. |
| 3293 | * @param[in] set Set with the leaf/leaf-list. |
| 3294 | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
| 3295 | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
| 3296 | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
| 3297 | */ |
| 3298 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 3299 | warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, |
| 3300 | uint16_t last_equal_exp) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3301 | { |
| 3302 | struct lysc_node *scnode; |
| 3303 | struct lysc_type *type; |
| 3304 | char *value; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3305 | struct lyd_value storage; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3306 | LY_ERR rc; |
| 3307 | struct ly_err_item *err = NULL; |
| 3308 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3309 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && |
| 3310 | ((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] | 3311 | /* check that the node can have the specified value */ |
| 3312 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
| 3313 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
| 3314 | } else { |
| 3315 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
| 3316 | } |
| 3317 | if (!value) { |
| 3318 | LOGMEM(set->ctx); |
| 3319 | return; |
| 3320 | } |
| 3321 | |
| 3322 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
| 3323 | 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] | 3324 | " 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] | 3325 | 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] | 3326 | (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] | 3327 | exp->expr + exp->tok_pos[equal_exp]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3328 | } |
| 3329 | |
| 3330 | type = ((struct lysc_node_leaf *)scnode)->type; |
| 3331 | if (type->basetype != LY_TYPE_IDENT) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3332 | 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] | 3333 | LYD_HINT_DATA, scnode, &storage, &err); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3334 | |
| 3335 | if (err) { |
| 3336 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
| 3337 | ly_err_free(err); |
| 3338 | } else if (rc != LY_SUCCESS) { |
| 3339 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
| 3340 | } |
| 3341 | if (rc != LY_SUCCESS) { |
| 3342 | 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] | 3343 | (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] | 3344 | exp->expr + exp->tok_pos[equal_exp]); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3345 | } else { |
| 3346 | type->plugin->free(set->ctx, &storage); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3347 | } |
| 3348 | } |
| 3349 | free(value); |
| 3350 | } |
| 3351 | } |
| 3352 | |
| 3353 | /* |
| 3354 | * XPath functions |
| 3355 | */ |
| 3356 | |
| 3357 | /** |
| 3358 | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
| 3359 | * depending on whether the first node bit value from the second argument is set. |
| 3360 | * |
| 3361 | * @param[in] args Array of arguments. |
| 3362 | * @param[in] arg_count Count of elements in @p args. |
| 3363 | * @param[in,out] set Context and result set at the same time. |
| 3364 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3365 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3366 | */ |
| 3367 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3368 | 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] | 3369 | { |
| 3370 | struct lyd_node_term *leaf; |
| 3371 | struct lysc_node_leaf *sleaf; |
| 3372 | struct lysc_type_bits *bits; |
| 3373 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3374 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3375 | |
| 3376 | if (options & LYXP_SCNODE_ALL) { |
| 3377 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3378 | 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] | 3379 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3380 | 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] | 3381 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
| 3382 | 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] | 3383 | } |
| 3384 | |
| 3385 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3386 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3387 | 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] | 3388 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3389 | 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] | 3390 | } |
| 3391 | } |
| 3392 | set_scnode_clear_ctx(set); |
| 3393 | return rc; |
| 3394 | } |
| 3395 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3396 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3397 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 3398 | "bit-is-set(node-set, string)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3399 | return LY_EVALID; |
| 3400 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3401 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3402 | LY_CHECK_RET(rc); |
| 3403 | |
| 3404 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3405 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3406 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3407 | if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && |
| 3408 | (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3409 | bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3410 | LY_ARRAY_FOR(bits->bits, u) { |
| 3411 | if (!strcmp(bits->bits[u].name, args[1]->val.str)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3412 | set_fill_boolean(set, 1); |
| 3413 | break; |
| 3414 | } |
| 3415 | } |
| 3416 | } |
| 3417 | } |
| 3418 | |
| 3419 | return LY_SUCCESS; |
| 3420 | } |
| 3421 | |
| 3422 | /** |
| 3423 | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
| 3424 | * with the argument converted to boolean. |
| 3425 | * |
| 3426 | * @param[in] args Array of arguments. |
| 3427 | * @param[in] arg_count Count of elements in @p args. |
| 3428 | * @param[in,out] set Context and result set at the same time. |
| 3429 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3430 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3431 | */ |
| 3432 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3433 | 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] | 3434 | { |
| 3435 | LY_ERR rc; |
| 3436 | |
| 3437 | if (options & LYXP_SCNODE_ALL) { |
| 3438 | set_scnode_clear_ctx(set); |
| 3439 | return LY_SUCCESS; |
| 3440 | } |
| 3441 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3442 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3443 | LY_CHECK_RET(rc); |
| 3444 | set_fill_set(set, args[0]); |
| 3445 | |
| 3446 | return LY_SUCCESS; |
| 3447 | } |
| 3448 | |
| 3449 | /** |
| 3450 | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
| 3451 | * with the first argument rounded up to the nearest integer. |
| 3452 | * |
| 3453 | * @param[in] args Array of arguments. |
| 3454 | * @param[in] arg_count Count of elements in @p args. |
| 3455 | * @param[in,out] set Context and result set at the same time. |
| 3456 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3457 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3458 | */ |
| 3459 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3460 | 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] | 3461 | { |
| 3462 | struct lysc_node_leaf *sleaf; |
| 3463 | LY_ERR rc = LY_SUCCESS; |
| 3464 | |
| 3465 | if (options & LYXP_SCNODE_ALL) { |
| 3466 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3467 | 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] | 3468 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3469 | 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] | 3470 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 3471 | 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] | 3472 | } |
| 3473 | set_scnode_clear_ctx(set); |
| 3474 | return rc; |
| 3475 | } |
| 3476 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3477 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3478 | LY_CHECK_RET(rc); |
| 3479 | if ((long long)args[0]->val.num != args[0]->val.num) { |
| 3480 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
| 3481 | } else { |
| 3482 | set_fill_number(set, args[0]->val.num); |
| 3483 | } |
| 3484 | |
| 3485 | return LY_SUCCESS; |
| 3486 | } |
| 3487 | |
| 3488 | /** |
| 3489 | * @brief Execute the XPath concat(string, string, string*) function. |
| 3490 | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
| 3491 | * |
| 3492 | * @param[in] args Array of arguments. |
| 3493 | * @param[in] arg_count Count of elements in @p args. |
| 3494 | * @param[in,out] set Context and result set at the same time. |
| 3495 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3496 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3497 | */ |
| 3498 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3499 | 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] | 3500 | { |
| 3501 | uint16_t i; |
| 3502 | char *str = NULL; |
| 3503 | size_t used = 1; |
| 3504 | LY_ERR rc = LY_SUCCESS; |
| 3505 | struct lysc_node_leaf *sleaf; |
| 3506 | |
| 3507 | if (options & LYXP_SCNODE_ALL) { |
| 3508 | for (i = 0; i < arg_count; ++i) { |
| 3509 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
| 3510 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3511 | LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3512 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3513 | } else if (!warn_is_string_type(sleaf->type)) { |
Radek Krejci | 70124c8 | 2020-08-14 22:17:03 +0200 | [diff] [blame] | 3514 | 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] | 3515 | } |
| 3516 | } |
| 3517 | } |
| 3518 | set_scnode_clear_ctx(set); |
| 3519 | return rc; |
| 3520 | } |
| 3521 | |
| 3522 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3523 | rc = lyxp_set_cast(args[i], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3524 | if (rc != LY_SUCCESS) { |
| 3525 | free(str); |
| 3526 | return rc; |
| 3527 | } |
| 3528 | |
| 3529 | str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char)); |
| 3530 | LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM); |
| 3531 | strcpy(str + used - 1, args[i]->val.str); |
| 3532 | used += strlen(args[i]->val.str); |
| 3533 | } |
| 3534 | |
| 3535 | /* free, kind of */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3536 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3537 | set->type = LYXP_SET_STRING; |
| 3538 | set->val.str = str; |
| 3539 | |
| 3540 | return LY_SUCCESS; |
| 3541 | } |
| 3542 | |
| 3543 | /** |
| 3544 | * @brief Execute the XPath contains(string, string) function. |
| 3545 | * Returns LYXP_SET_BOOLEAN whether the second argument can |
| 3546 | * be found in the first or not. |
| 3547 | * |
| 3548 | * @param[in] args Array of arguments. |
| 3549 | * @param[in] arg_count Count of elements in @p args. |
| 3550 | * @param[in,out] set Context and result set at the same time. |
| 3551 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3552 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3553 | */ |
| 3554 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3555 | 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] | 3556 | { |
| 3557 | struct lysc_node_leaf *sleaf; |
| 3558 | LY_ERR rc = LY_SUCCESS; |
| 3559 | |
| 3560 | if (options & LYXP_SCNODE_ALL) { |
| 3561 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3562 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3563 | 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] | 3564 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3565 | 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] | 3566 | } |
| 3567 | } |
| 3568 | |
| 3569 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3570 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3571 | 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] | 3572 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3573 | 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] | 3574 | } |
| 3575 | } |
| 3576 | set_scnode_clear_ctx(set); |
| 3577 | return rc; |
| 3578 | } |
| 3579 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3580 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3581 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3582 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3583 | LY_CHECK_RET(rc); |
| 3584 | |
| 3585 | if (strstr(args[0]->val.str, args[1]->val.str)) { |
| 3586 | set_fill_boolean(set, 1); |
| 3587 | } else { |
| 3588 | set_fill_boolean(set, 0); |
| 3589 | } |
| 3590 | |
| 3591 | return LY_SUCCESS; |
| 3592 | } |
| 3593 | |
| 3594 | /** |
| 3595 | * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER |
| 3596 | * with the size of the node-set from the argument. |
| 3597 | * |
| 3598 | * @param[in] args Array of arguments. |
| 3599 | * @param[in] arg_count Count of elements in @p args. |
| 3600 | * @param[in,out] set Context and result set at the same time. |
| 3601 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3602 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3603 | */ |
| 3604 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3605 | 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] | 3606 | { |
| 3607 | struct lysc_node *scnode = NULL; |
| 3608 | LY_ERR rc = LY_SUCCESS; |
| 3609 | |
| 3610 | if (options & LYXP_SCNODE_ALL) { |
| 3611 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) { |
| 3612 | 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] | 3613 | } |
| 3614 | set_scnode_clear_ctx(set); |
| 3615 | return rc; |
| 3616 | } |
| 3617 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3618 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3619 | 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] | 3620 | return LY_EVALID; |
| 3621 | } |
| 3622 | |
| 3623 | set_fill_number(set, args[0]->used); |
| 3624 | return LY_SUCCESS; |
| 3625 | } |
| 3626 | |
| 3627 | /** |
| 3628 | * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET |
| 3629 | * with the context with the intial node. |
| 3630 | * |
| 3631 | * @param[in] args Array of arguments. |
| 3632 | * @param[in] arg_count Count of elements in @p args. |
| 3633 | * @param[in,out] set Context and result set at the same time. |
| 3634 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3635 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3636 | */ |
| 3637 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3638 | 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] | 3639 | { |
| 3640 | if (arg_count || args) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3641 | 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] | 3642 | return LY_EVALID; |
| 3643 | } |
| 3644 | |
| 3645 | if (options & LYXP_SCNODE_ALL) { |
| 3646 | set_scnode_clear_ctx(set); |
| 3647 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3648 | 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] | 3649 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3650 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3651 | |
| 3652 | /* position is filled later */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3653 | set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3654 | } |
| 3655 | |
| 3656 | return LY_SUCCESS; |
| 3657 | } |
| 3658 | |
| 3659 | /** |
| 3660 | * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either |
| 3661 | * leafref or instance-identifier target node(s). |
| 3662 | * |
| 3663 | * @param[in] args Array of arguments. |
| 3664 | * @param[in] arg_count Count of elements in @p args. |
| 3665 | * @param[in,out] set Context and result set at the same time. |
| 3666 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3667 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3668 | */ |
| 3669 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3670 | 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] | 3671 | { |
| 3672 | struct lyd_node_term *leaf; |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3673 | struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3674 | struct lysc_type_leafref *lref; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3675 | const struct lysc_node *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3676 | struct ly_path *p; |
| 3677 | struct lyd_node *node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3678 | char *errmsg = NULL; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3679 | uint8_t oper; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3680 | LY_ERR rc = LY_SUCCESS; |
| 3681 | |
| 3682 | if (options & LYXP_SCNODE_ALL) { |
| 3683 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3684 | 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] | 3685 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3686 | 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] | 3687 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
| 3688 | 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] | 3689 | __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3690 | } |
| 3691 | set_scnode_clear_ctx(set); |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3692 | if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3693 | lref = (struct lysc_type_leafref *)sleaf->type; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3694 | 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] | 3695 | |
| 3696 | /* it was already evaluated on schema, it must succeed */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3697 | rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, |
| 3698 | 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] | 3699 | assert(!rc); |
| 3700 | |
| 3701 | /* get the target node */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3702 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3703 | ly_path_free(set->ctx, p); |
| 3704 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 3705 | 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] | 3706 | } |
| 3707 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3708 | return rc; |
| 3709 | } |
| 3710 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3711 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3712 | 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] | 3713 | return LY_EVALID; |
| 3714 | } |
| 3715 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3716 | lyxp_set_free_content(set); |
| 3717 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3718 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3719 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3720 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 3721 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3722 | /* find leafref target */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3723 | 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] | 3724 | &leaf->value, set->tree, &node, &errmsg)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3725 | LOGERR(set->ctx, LY_EVALID, errmsg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3726 | free(errmsg); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3727 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3728 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3729 | } else { |
| 3730 | assert(sleaf->type->basetype == LY_TYPE_INST); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3731 | if (ly_path_eval(leaf->value.target, set->tree, &node)) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3732 | 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] | 3733 | LYD_CANON_VALUE(leaf)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3734 | return LY_EVALID; |
| 3735 | } |
| 3736 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3737 | |
| 3738 | /* insert it */ |
| 3739 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3740 | } |
| 3741 | } |
| 3742 | |
| 3743 | return LY_SUCCESS; |
| 3744 | } |
| 3745 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3746 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3747 | 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] | 3748 | { |
| 3749 | uint16_t i; |
| 3750 | LY_ARRAY_COUNT_TYPE u; |
| 3751 | struct lyd_node_term *leaf; |
| 3752 | struct lysc_node_leaf *sleaf; |
| 3753 | struct lyd_meta *meta; |
| 3754 | struct lyd_value data = {0}, *val; |
| 3755 | struct ly_err_item *err = NULL; |
| 3756 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3757 | ly_bool found; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3758 | |
| 3759 | if (options & LYXP_SCNODE_ALL) { |
| 3760 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3761 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func); |
| 3762 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3763 | 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] | 3764 | sleaf->name); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3765 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 3766 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name); |
| 3767 | } |
| 3768 | |
| 3769 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3770 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3771 | 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] | 3772 | sleaf->name); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3773 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3774 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name); |
| 3775 | } |
| 3776 | } |
| 3777 | set_scnode_clear_ctx(set); |
| 3778 | return rc; |
| 3779 | } |
| 3780 | |
| 3781 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3782 | 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] | 3783 | "derived-from(-or-self)(node-set, string)"); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3784 | return LY_EVALID; |
| 3785 | } |
| 3786 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
| 3787 | LY_CHECK_RET(rc); |
| 3788 | |
| 3789 | set_fill_boolean(set, 0); |
| 3790 | found = 0; |
| 3791 | for (i = 0; i < args[0]->used; ++i) { |
| 3792 | if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) { |
| 3793 | continue; |
| 3794 | } |
| 3795 | |
| 3796 | if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 3797 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 3798 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3799 | val = &leaf->value; |
| 3800 | if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) { |
| 3801 | /* uninteresting */ |
| 3802 | continue; |
| 3803 | } |
| 3804 | |
| 3805 | /* store args[1] as ident */ |
| 3806 | 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] | 3807 | 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] | 3808 | } else { |
| 3809 | meta = args[0]->val.meta[i].meta; |
| 3810 | val = &meta->value; |
| 3811 | if (val->realtype->basetype != LY_TYPE_IDENT) { |
| 3812 | /* uninteresting */ |
| 3813 | continue; |
| 3814 | } |
| 3815 | |
| 3816 | /* store args[1] as ident */ |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 3817 | 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] | 3818 | 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] | 3819 | } |
| 3820 | |
| 3821 | if (err) { |
| 3822 | ly_err_print(err); |
| 3823 | ly_err_free(err); |
| 3824 | } |
| 3825 | LY_CHECK_RET(rc); |
| 3826 | |
| 3827 | /* finally check the identity itself */ |
| 3828 | if (self_match && (data.ident == val->ident)) { |
| 3829 | set_fill_boolean(set, 1); |
| 3830 | found = 1; |
| 3831 | } |
| 3832 | if (!found) { |
| 3833 | LY_ARRAY_FOR(data.ident->derived, u) { |
| 3834 | if (data.ident->derived[u] == val->ident) { |
| 3835 | set_fill_boolean(set, 1); |
| 3836 | found = 1; |
| 3837 | break; |
| 3838 | } |
| 3839 | } |
| 3840 | } |
| 3841 | |
| 3842 | /* free temporary value */ |
| 3843 | val->realtype->plugin->free(set->ctx, &data); |
| 3844 | if (found) { |
| 3845 | break; |
| 3846 | } |
| 3847 | } |
| 3848 | |
| 3849 | return LY_SUCCESS; |
| 3850 | } |
| 3851 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3852 | /** |
| 3853 | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3854 | * on whether the first argument nodes contain a node of an identity derived from the second |
| 3855 | * argument identity. |
| 3856 | * |
| 3857 | * @param[in] args Array of arguments. |
| 3858 | * @param[in] arg_count Count of elements in @p args. |
| 3859 | * @param[in,out] set Context and result set at the same time. |
| 3860 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3861 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3862 | */ |
| 3863 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3864 | 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] | 3865 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3866 | return xpath_derived_(args, set, options, 0, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3867 | } |
| 3868 | |
| 3869 | /** |
| 3870 | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3871 | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
| 3872 | * the second argument identity. |
| 3873 | * |
| 3874 | * @param[in] args Array of arguments. |
| 3875 | * @param[in] arg_count Count of elements in @p args. |
| 3876 | * @param[in,out] set Context and result set at the same time. |
| 3877 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3878 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3879 | */ |
| 3880 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3881 | 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] | 3882 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3883 | return xpath_derived_(args, set, options, 1, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3884 | } |
| 3885 | |
| 3886 | /** |
| 3887 | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
| 3888 | * with the integer value of the first node's enum value, otherwise NaN. |
| 3889 | * |
| 3890 | * @param[in] args Array of arguments. |
| 3891 | * @param[in] arg_count Count of elements in @p args. |
| 3892 | * @param[in,out] set Context and result set at the same time. |
| 3893 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3894 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3895 | */ |
| 3896 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3897 | 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] | 3898 | { |
| 3899 | struct lyd_node_term *leaf; |
| 3900 | struct lysc_node_leaf *sleaf; |
| 3901 | LY_ERR rc = LY_SUCCESS; |
| 3902 | |
| 3903 | if (options & LYXP_SCNODE_ALL) { |
| 3904 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3905 | 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] | 3906 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3907 | 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] | 3908 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
| 3909 | 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] | 3910 | } |
| 3911 | set_scnode_clear_ctx(set); |
| 3912 | return rc; |
| 3913 | } |
| 3914 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3915 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3916 | 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] | 3917 | return LY_EVALID; |
| 3918 | } |
| 3919 | |
| 3920 | set_fill_number(set, NAN); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3921 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3922 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3923 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3924 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
| 3925 | set_fill_number(set, leaf->value.enum_item->value); |
| 3926 | } |
| 3927 | } |
| 3928 | |
| 3929 | return LY_SUCCESS; |
| 3930 | } |
| 3931 | |
| 3932 | /** |
| 3933 | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
| 3934 | * with false value. |
| 3935 | * |
| 3936 | * @param[in] args Array of arguments. |
| 3937 | * @param[in] arg_count Count of elements in @p args. |
| 3938 | * @param[in,out] set Context and result set at the same time. |
| 3939 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3940 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3941 | */ |
| 3942 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3943 | 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] | 3944 | { |
| 3945 | if (options & LYXP_SCNODE_ALL) { |
| 3946 | set_scnode_clear_ctx(set); |
| 3947 | return LY_SUCCESS; |
| 3948 | } |
| 3949 | |
| 3950 | set_fill_boolean(set, 0); |
| 3951 | return LY_SUCCESS; |
| 3952 | } |
| 3953 | |
| 3954 | /** |
| 3955 | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
| 3956 | * with the first argument floored (truncated). |
| 3957 | * |
| 3958 | * @param[in] args Array of arguments. |
| 3959 | * @param[in] arg_count Count of elements in @p args. |
| 3960 | * @param[in,out] set Context and result set at the same time. |
| 3961 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3962 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3963 | */ |
| 3964 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3965 | 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] | 3966 | { |
| 3967 | LY_ERR rc; |
| 3968 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3969 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3970 | LY_CHECK_RET(rc); |
| 3971 | if (isfinite(args[0]->val.num)) { |
| 3972 | set_fill_number(set, (long long)args[0]->val.num); |
| 3973 | } |
| 3974 | |
| 3975 | return LY_SUCCESS; |
| 3976 | } |
| 3977 | |
| 3978 | /** |
| 3979 | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
| 3980 | * whether the language of the text matches the one from the argument. |
| 3981 | * |
| 3982 | * @param[in] args Array of arguments. |
| 3983 | * @param[in] arg_count Count of elements in @p args. |
| 3984 | * @param[in,out] set Context and result set at the same time. |
| 3985 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3986 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3987 | */ |
| 3988 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3989 | 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] | 3990 | { |
| 3991 | const struct lyd_node *node; |
| 3992 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 3993 | struct lyd_meta *meta = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3994 | const char *val; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3995 | LY_ERR rc = LY_SUCCESS; |
| 3996 | |
| 3997 | if (options & LYXP_SCNODE_ALL) { |
| 3998 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3999 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4000 | 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] | 4001 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4002 | 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] | 4003 | } |
| 4004 | } |
| 4005 | set_scnode_clear_ctx(set); |
| 4006 | return rc; |
| 4007 | } |
| 4008 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4009 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4010 | LY_CHECK_RET(rc); |
| 4011 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4012 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4013 | 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] | 4014 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4015 | } else if (!set->used) { |
| 4016 | set_fill_boolean(set, 0); |
| 4017 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4018 | } |
| 4019 | |
| 4020 | switch (set->val.nodes[0].type) { |
| 4021 | case LYXP_NODE_ELEM: |
| 4022 | case LYXP_NODE_TEXT: |
| 4023 | node = set->val.nodes[0].node; |
| 4024 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4025 | case LYXP_NODE_META: |
| 4026 | node = set->val.meta[0].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4027 | break; |
| 4028 | default: |
| 4029 | /* nothing to do with roots */ |
| 4030 | set_fill_boolean(set, 0); |
| 4031 | return LY_SUCCESS; |
| 4032 | } |
| 4033 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4034 | /* find lang metadata */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 4035 | for ( ; node; node = (struct lyd_node *)node->parent) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4036 | for (meta = node->meta; meta; meta = meta->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4037 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4038 | 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] | 4039 | break; |
| 4040 | } |
| 4041 | } |
| 4042 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4043 | if (meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4044 | break; |
| 4045 | } |
| 4046 | } |
| 4047 | |
| 4048 | /* compare languages */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4049 | if (!meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4050 | set_fill_boolean(set, 0); |
| 4051 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4052 | uint64_t i; |
| 4053 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4054 | val = meta->value.canonical; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4055 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 4056 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
| 4057 | set_fill_boolean(set, 0); |
| 4058 | break; |
| 4059 | } |
| 4060 | } |
| 4061 | if (!args[0]->val.str[i]) { |
| 4062 | if (!val[i] || (val[i] == '-')) { |
| 4063 | set_fill_boolean(set, 1); |
| 4064 | } else { |
| 4065 | set_fill_boolean(set, 0); |
| 4066 | } |
| 4067 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4068 | } |
| 4069 | |
| 4070 | return LY_SUCCESS; |
| 4071 | } |
| 4072 | |
| 4073 | /** |
| 4074 | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
| 4075 | * with the context size. |
| 4076 | * |
| 4077 | * @param[in] args Array of arguments. |
| 4078 | * @param[in] arg_count Count of elements in @p args. |
| 4079 | * @param[in,out] set Context and result set at the same time. |
| 4080 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4081 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4082 | */ |
| 4083 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4084 | 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] | 4085 | { |
| 4086 | if (options & LYXP_SCNODE_ALL) { |
| 4087 | set_scnode_clear_ctx(set); |
| 4088 | return LY_SUCCESS; |
| 4089 | } |
| 4090 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4091 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4092 | 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] | 4093 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4094 | } else if (!set->used) { |
| 4095 | set_fill_number(set, 0); |
| 4096 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4097 | } |
| 4098 | |
| 4099 | set_fill_number(set, set->ctx_size); |
| 4100 | return LY_SUCCESS; |
| 4101 | } |
| 4102 | |
| 4103 | /** |
| 4104 | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
| 4105 | * with the node name without namespace from the argument or the context. |
| 4106 | * |
| 4107 | * @param[in] args Array of arguments. |
| 4108 | * @param[in] arg_count Count of elements in @p args. |
| 4109 | * @param[in,out] set Context and result set at the same time. |
| 4110 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4111 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4112 | */ |
| 4113 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4114 | 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] | 4115 | { |
| 4116 | struct lyxp_set_node *item; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4117 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4118 | /* suppress unused variable warning */ |
| 4119 | (void)options; |
| 4120 | |
| 4121 | if (options & LYXP_SCNODE_ALL) { |
| 4122 | set_scnode_clear_ctx(set); |
| 4123 | return LY_SUCCESS; |
| 4124 | } |
| 4125 | |
| 4126 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4127 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4128 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 4129 | "local-name(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4130 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4131 | } else if (!args[0]->used) { |
| 4132 | set_fill_string(set, "", 0); |
| 4133 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4134 | } |
| 4135 | |
| 4136 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4137 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4138 | |
| 4139 | item = &args[0]->val.nodes[0]; |
| 4140 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4141 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4142 | 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] | 4143 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4144 | } else if (!set->used) { |
| 4145 | set_fill_string(set, "", 0); |
| 4146 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4147 | } |
| 4148 | |
| 4149 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4150 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4151 | |
| 4152 | item = &set->val.nodes[0]; |
| 4153 | } |
| 4154 | |
| 4155 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4156 | case LYXP_NODE_NONE: |
| 4157 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4158 | case LYXP_NODE_ROOT: |
| 4159 | case LYXP_NODE_ROOT_CONFIG: |
| 4160 | case LYXP_NODE_TEXT: |
| 4161 | set_fill_string(set, "", 0); |
| 4162 | break; |
| 4163 | case LYXP_NODE_ELEM: |
| 4164 | set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name)); |
| 4165 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4166 | case LYXP_NODE_META: |
| 4167 | 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] | 4168 | break; |
| 4169 | } |
| 4170 | |
| 4171 | return LY_SUCCESS; |
| 4172 | } |
| 4173 | |
| 4174 | /** |
| 4175 | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
| 4176 | * with the node name fully qualified (with namespace) from the argument or the context. |
| 4177 | * |
| 4178 | * @param[in] args Array of arguments. |
| 4179 | * @param[in] arg_count Count of elements in @p args. |
| 4180 | * @param[in,out] set Context and result set at the same time. |
| 4181 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4182 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4183 | */ |
| 4184 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4185 | 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] | 4186 | { |
| 4187 | struct lyxp_set_node *item; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4188 | struct lys_module *mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4189 | char *str; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4190 | const char *name = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4191 | |
| 4192 | if (options & LYXP_SCNODE_ALL) { |
| 4193 | set_scnode_clear_ctx(set); |
| 4194 | return LY_SUCCESS; |
| 4195 | } |
| 4196 | |
| 4197 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4198 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4199 | 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] | 4200 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4201 | } else if (!args[0]->used) { |
| 4202 | set_fill_string(set, "", 0); |
| 4203 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4204 | } |
| 4205 | |
| 4206 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4207 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4208 | |
| 4209 | item = &args[0]->val.nodes[0]; |
| 4210 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4211 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4212 | 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] | 4213 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4214 | } else if (!set->used) { |
| 4215 | set_fill_string(set, "", 0); |
| 4216 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4217 | } |
| 4218 | |
| 4219 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4220 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4221 | |
| 4222 | item = &set->val.nodes[0]; |
| 4223 | } |
| 4224 | |
| 4225 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4226 | case LYXP_NODE_NONE: |
| 4227 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4228 | case LYXP_NODE_ROOT: |
| 4229 | case LYXP_NODE_ROOT_CONFIG: |
| 4230 | case LYXP_NODE_TEXT: |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4231 | /* keep NULL */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4232 | break; |
| 4233 | case LYXP_NODE_ELEM: |
| 4234 | mod = item->node->schema->module; |
| 4235 | name = item->node->schema->name; |
| 4236 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4237 | case LYXP_NODE_META: |
| 4238 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
| 4239 | name = ((struct lyd_meta *)item->node)->name; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4240 | break; |
| 4241 | } |
| 4242 | |
| 4243 | if (mod && name) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4244 | 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] | 4245 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
| 4246 | set_fill_string(set, str, strlen(str)); |
| 4247 | free(str); |
| 4248 | } else { |
| 4249 | set_fill_string(set, "", 0); |
| 4250 | } |
| 4251 | |
| 4252 | return LY_SUCCESS; |
| 4253 | } |
| 4254 | |
| 4255 | /** |
| 4256 | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
| 4257 | * with the namespace of the node from the argument or the context. |
| 4258 | * |
| 4259 | * @param[in] args Array of arguments. |
| 4260 | * @param[in] arg_count Count of elements in @p args. |
| 4261 | * @param[in,out] set Context and result set at the same time. |
| 4262 | * @param[in] options XPath options. |
| 4263 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4264 | */ |
| 4265 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4266 | 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] | 4267 | { |
| 4268 | struct lyxp_set_node *item; |
| 4269 | struct lys_module *mod; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4270 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4271 | /* suppress unused variable warning */ |
| 4272 | (void)options; |
| 4273 | |
| 4274 | if (options & LYXP_SCNODE_ALL) { |
| 4275 | set_scnode_clear_ctx(set); |
| 4276 | return LY_SUCCESS; |
| 4277 | } |
| 4278 | |
| 4279 | if (arg_count) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4280 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4281 | 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] | 4282 | "namespace-uri(node-set?)"); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4283 | return LY_EVALID; |
| 4284 | } else if (!args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4285 | set_fill_string(set, "", 0); |
| 4286 | return LY_SUCCESS; |
| 4287 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4288 | |
| 4289 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4290 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4291 | |
| 4292 | item = &args[0]->val.nodes[0]; |
| 4293 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4294 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4295 | 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] | 4296 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4297 | } else if (!set->used) { |
| 4298 | set_fill_string(set, "", 0); |
| 4299 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4300 | } |
| 4301 | |
| 4302 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4303 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4304 | |
| 4305 | item = &set->val.nodes[0]; |
| 4306 | } |
| 4307 | |
| 4308 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4309 | case LYXP_NODE_NONE: |
| 4310 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4311 | case LYXP_NODE_ROOT: |
| 4312 | case LYXP_NODE_ROOT_CONFIG: |
| 4313 | case LYXP_NODE_TEXT: |
| 4314 | set_fill_string(set, "", 0); |
| 4315 | break; |
| 4316 | case LYXP_NODE_ELEM: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4317 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4318 | if (item->type == LYXP_NODE_ELEM) { |
| 4319 | mod = item->node->schema->module; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4320 | } else { /* LYXP_NODE_META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4321 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4322 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4323 | } |
| 4324 | |
| 4325 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
| 4326 | break; |
| 4327 | } |
| 4328 | |
| 4329 | return LY_SUCCESS; |
| 4330 | } |
| 4331 | |
| 4332 | /** |
| 4333 | * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET |
| 4334 | * with only nodes from the context. In practice it either leaves the context |
| 4335 | * as it is or returns an empty node set. |
| 4336 | * |
| 4337 | * @param[in] args Array of arguments. |
| 4338 | * @param[in] arg_count Count of elements in @p args. |
| 4339 | * @param[in,out] set Context and result set at the same time. |
| 4340 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4341 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4342 | */ |
| 4343 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4344 | 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] | 4345 | { |
| 4346 | if (options & LYXP_SCNODE_ALL) { |
| 4347 | set_scnode_clear_ctx(set); |
| 4348 | return LY_SUCCESS; |
| 4349 | } |
| 4350 | |
| 4351 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4352 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4353 | } |
| 4354 | return LY_SUCCESS; |
| 4355 | } |
| 4356 | |
| 4357 | /** |
| 4358 | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
| 4359 | * with normalized value (no leading, trailing, double white spaces) of the node |
| 4360 | * from the argument or the context. |
| 4361 | * |
| 4362 | * @param[in] args Array of arguments. |
| 4363 | * @param[in] arg_count Count of elements in @p args. |
| 4364 | * @param[in,out] set Context and result set at the same time. |
| 4365 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4366 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4367 | */ |
| 4368 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4369 | 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] | 4370 | { |
| 4371 | uint16_t i, new_used; |
| 4372 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4373 | ly_bool have_spaces = 0, space_before = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4374 | struct lysc_node_leaf *sleaf; |
| 4375 | LY_ERR rc = LY_SUCCESS; |
| 4376 | |
| 4377 | if (options & LYXP_SCNODE_ALL) { |
| 4378 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4379 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4380 | 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] | 4381 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4382 | 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] | 4383 | } |
| 4384 | } |
| 4385 | set_scnode_clear_ctx(set); |
| 4386 | return rc; |
| 4387 | } |
| 4388 | |
| 4389 | if (arg_count) { |
| 4390 | set_fill_set(set, args[0]); |
| 4391 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4392 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4393 | LY_CHECK_RET(rc); |
| 4394 | |
| 4395 | /* is there any normalization necessary? */ |
| 4396 | for (i = 0; set->val.str[i]; ++i) { |
| 4397 | if (is_xmlws(set->val.str[i])) { |
| 4398 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
| 4399 | have_spaces = 1; |
| 4400 | break; |
| 4401 | } |
| 4402 | space_before = 1; |
| 4403 | } else { |
| 4404 | space_before = 0; |
| 4405 | } |
| 4406 | } |
| 4407 | |
| 4408 | /* yep, there is */ |
| 4409 | if (have_spaces) { |
| 4410 | /* it's enough, at least one character will go, makes space for ending '\0' */ |
| 4411 | new = malloc(strlen(set->val.str) * sizeof(char)); |
| 4412 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4413 | new_used = 0; |
| 4414 | |
| 4415 | space_before = 0; |
| 4416 | for (i = 0; set->val.str[i]; ++i) { |
| 4417 | if (is_xmlws(set->val.str[i])) { |
| 4418 | if ((i == 0) || space_before) { |
| 4419 | space_before = 1; |
| 4420 | continue; |
| 4421 | } else { |
| 4422 | space_before = 1; |
| 4423 | } |
| 4424 | } else { |
| 4425 | space_before = 0; |
| 4426 | } |
| 4427 | |
| 4428 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
| 4429 | ++new_used; |
| 4430 | } |
| 4431 | |
| 4432 | /* at worst there is one trailing space now */ |
| 4433 | if (new_used && is_xmlws(new[new_used - 1])) { |
| 4434 | --new_used; |
| 4435 | } |
| 4436 | |
| 4437 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 4438 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4439 | new[new_used] = '\0'; |
| 4440 | |
| 4441 | free(set->val.str); |
| 4442 | set->val.str = new; |
| 4443 | } |
| 4444 | |
| 4445 | return LY_SUCCESS; |
| 4446 | } |
| 4447 | |
| 4448 | /** |
| 4449 | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
| 4450 | * with the argument converted to boolean and logically inverted. |
| 4451 | * |
| 4452 | * @param[in] args Array of arguments. |
| 4453 | * @param[in] arg_count Count of elements in @p args. |
| 4454 | * @param[in,out] set Context and result set at the same time. |
| 4455 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4456 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4457 | */ |
| 4458 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4459 | 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] | 4460 | { |
| 4461 | if (options & LYXP_SCNODE_ALL) { |
| 4462 | set_scnode_clear_ctx(set); |
| 4463 | return LY_SUCCESS; |
| 4464 | } |
| 4465 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4466 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4467 | if (args[0]->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4468 | set_fill_boolean(set, 0); |
| 4469 | } else { |
| 4470 | set_fill_boolean(set, 1); |
| 4471 | } |
| 4472 | |
| 4473 | return LY_SUCCESS; |
| 4474 | } |
| 4475 | |
| 4476 | /** |
| 4477 | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
| 4478 | * with the number representation of either the argument or the context. |
| 4479 | * |
| 4480 | * @param[in] args Array of arguments. |
| 4481 | * @param[in] arg_count Count of elements in @p args. |
| 4482 | * @param[in,out] set Context and result set at the same time. |
| 4483 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4484 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4485 | */ |
| 4486 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4487 | 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] | 4488 | { |
| 4489 | LY_ERR rc; |
| 4490 | |
| 4491 | if (options & LYXP_SCNODE_ALL) { |
| 4492 | set_scnode_clear_ctx(set); |
| 4493 | return LY_SUCCESS; |
| 4494 | } |
| 4495 | |
| 4496 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4497 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4498 | LY_CHECK_RET(rc); |
| 4499 | set_fill_set(set, args[0]); |
| 4500 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4501 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4502 | LY_CHECK_RET(rc); |
| 4503 | } |
| 4504 | |
| 4505 | return LY_SUCCESS; |
| 4506 | } |
| 4507 | |
| 4508 | /** |
| 4509 | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
| 4510 | * with the context position. |
| 4511 | * |
| 4512 | * @param[in] args Array of arguments. |
| 4513 | * @param[in] arg_count Count of elements in @p args. |
| 4514 | * @param[in,out] set Context and result set at the same time. |
| 4515 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4516 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4517 | */ |
| 4518 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4519 | 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] | 4520 | { |
| 4521 | if (options & LYXP_SCNODE_ALL) { |
| 4522 | set_scnode_clear_ctx(set); |
| 4523 | return LY_SUCCESS; |
| 4524 | } |
| 4525 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4526 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4527 | 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] | 4528 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4529 | } else if (!set->used) { |
| 4530 | set_fill_number(set, 0); |
| 4531 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4532 | } |
| 4533 | |
| 4534 | set_fill_number(set, set->ctx_pos); |
| 4535 | |
| 4536 | /* UNUSED in 'Release' build type */ |
| 4537 | (void)options; |
| 4538 | return LY_SUCCESS; |
| 4539 | } |
| 4540 | |
| 4541 | /** |
| 4542 | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
| 4543 | * depending on whether the second argument regex matches the first argument string. For details refer to |
| 4544 | * YANG 1.1 RFC section 10.2.1. |
| 4545 | * |
| 4546 | * @param[in] args Array of arguments. |
| 4547 | * @param[in] arg_count Count of elements in @p args. |
| 4548 | * @param[in,out] set Context and result set at the same time. |
| 4549 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4550 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4551 | */ |
| 4552 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4553 | 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] | 4554 | { |
| 4555 | struct lysc_pattern **patterns = NULL, **pattern; |
| 4556 | struct lysc_node_leaf *sleaf; |
| 4557 | char *path; |
| 4558 | LY_ERR rc = LY_SUCCESS; |
| 4559 | struct ly_err_item *err; |
| 4560 | |
| 4561 | if (options & LYXP_SCNODE_ALL) { |
| 4562 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4563 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4564 | 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] | 4565 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4566 | 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] | 4567 | } |
| 4568 | } |
| 4569 | |
| 4570 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4571 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4572 | 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] | 4573 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4574 | 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] | 4575 | } |
| 4576 | } |
| 4577 | set_scnode_clear_ctx(set); |
| 4578 | return rc; |
| 4579 | } |
| 4580 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4581 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4582 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4583 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4584 | LY_CHECK_RET(rc); |
| 4585 | |
| 4586 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
| 4587 | *pattern = malloc(sizeof **pattern); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4588 | path = lyd_path(set->cur_node, LYD_PATH_LOG, NULL, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4589 | rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code); |
| 4590 | free(path); |
| 4591 | if (rc != LY_SUCCESS) { |
| 4592 | LY_ARRAY_FREE(patterns); |
| 4593 | return rc; |
| 4594 | } |
| 4595 | |
| 4596 | rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err); |
| 4597 | pcre2_code_free((*pattern)->code); |
| 4598 | free(*pattern); |
| 4599 | LY_ARRAY_FREE(patterns); |
| 4600 | if (rc && (rc != LY_EVALID)) { |
| 4601 | ly_err_print(err); |
| 4602 | ly_err_free(err); |
| 4603 | return rc; |
| 4604 | } |
| 4605 | |
| 4606 | if (rc == LY_EVALID) { |
| 4607 | ly_err_free(err); |
| 4608 | set_fill_boolean(set, 0); |
| 4609 | } else { |
| 4610 | set_fill_boolean(set, 1); |
| 4611 | } |
| 4612 | |
| 4613 | return LY_SUCCESS; |
| 4614 | } |
| 4615 | |
| 4616 | /** |
| 4617 | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
| 4618 | * with the rounded first argument. For details refer to |
| 4619 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
| 4620 | * |
| 4621 | * @param[in] args Array of arguments. |
| 4622 | * @param[in] arg_count Count of elements in @p args. |
| 4623 | * @param[in,out] set Context and result set at the same time. |
| 4624 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4625 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4626 | */ |
| 4627 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4628 | 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] | 4629 | { |
| 4630 | struct lysc_node_leaf *sleaf; |
| 4631 | LY_ERR rc = LY_SUCCESS; |
| 4632 | |
| 4633 | if (options & LYXP_SCNODE_ALL) { |
| 4634 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4635 | 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] | 4636 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4637 | 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] | 4638 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4639 | 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] | 4640 | } |
| 4641 | set_scnode_clear_ctx(set); |
| 4642 | return rc; |
| 4643 | } |
| 4644 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4645 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4646 | LY_CHECK_RET(rc); |
| 4647 | |
| 4648 | /* cover only the cases where floor can't be used */ |
| 4649 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
| 4650 | set_fill_number(set, -0.0f); |
| 4651 | } else { |
| 4652 | args[0]->val.num += 0.5; |
| 4653 | rc = xpath_floor(args, 1, args[0], options); |
| 4654 | LY_CHECK_RET(rc); |
| 4655 | set_fill_number(set, args[0]->val.num); |
| 4656 | } |
| 4657 | |
| 4658 | return LY_SUCCESS; |
| 4659 | } |
| 4660 | |
| 4661 | /** |
| 4662 | * @brief Execute the XPath starts-with(string, string) function. |
| 4663 | * Returns LYXP_SET_BOOLEAN whether the second argument is |
| 4664 | * the prefix of the first or not. |
| 4665 | * |
| 4666 | * @param[in] args Array of arguments. |
| 4667 | * @param[in] arg_count Count of elements in @p args. |
| 4668 | * @param[in,out] set Context and result set at the same time. |
| 4669 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4670 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4671 | */ |
| 4672 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4673 | 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] | 4674 | { |
| 4675 | struct lysc_node_leaf *sleaf; |
| 4676 | LY_ERR rc = LY_SUCCESS; |
| 4677 | |
| 4678 | if (options & LYXP_SCNODE_ALL) { |
| 4679 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4680 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4681 | 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] | 4682 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4683 | 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] | 4684 | } |
| 4685 | } |
| 4686 | |
| 4687 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4688 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4689 | 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] | 4690 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4691 | 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] | 4692 | } |
| 4693 | } |
| 4694 | set_scnode_clear_ctx(set); |
| 4695 | return rc; |
| 4696 | } |
| 4697 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4698 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4699 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4700 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4701 | LY_CHECK_RET(rc); |
| 4702 | |
| 4703 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
| 4704 | set_fill_boolean(set, 0); |
| 4705 | } else { |
| 4706 | set_fill_boolean(set, 1); |
| 4707 | } |
| 4708 | |
| 4709 | return LY_SUCCESS; |
| 4710 | } |
| 4711 | |
| 4712 | /** |
| 4713 | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
| 4714 | * with the string representation of either the argument or the context. |
| 4715 | * |
| 4716 | * @param[in] args Array of arguments. |
| 4717 | * @param[in] arg_count Count of elements in @p args. |
| 4718 | * @param[in,out] set Context and result set at the same time. |
| 4719 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4720 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4721 | */ |
| 4722 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4723 | 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] | 4724 | { |
| 4725 | LY_ERR rc; |
| 4726 | |
| 4727 | if (options & LYXP_SCNODE_ALL) { |
| 4728 | set_scnode_clear_ctx(set); |
| 4729 | return LY_SUCCESS; |
| 4730 | } |
| 4731 | |
| 4732 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4733 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4734 | LY_CHECK_RET(rc); |
| 4735 | set_fill_set(set, args[0]); |
| 4736 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4737 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4738 | LY_CHECK_RET(rc); |
| 4739 | } |
| 4740 | |
| 4741 | return LY_SUCCESS; |
| 4742 | } |
| 4743 | |
| 4744 | /** |
| 4745 | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
| 4746 | * with the length of the string in either the argument or the context. |
| 4747 | * |
| 4748 | * @param[in] args Array of arguments. |
| 4749 | * @param[in] arg_count Count of elements in @p args. |
| 4750 | * @param[in,out] set Context and result set at the same time. |
| 4751 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4752 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4753 | */ |
| 4754 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4755 | 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] | 4756 | { |
| 4757 | struct lysc_node_leaf *sleaf; |
| 4758 | LY_ERR rc = LY_SUCCESS; |
| 4759 | |
| 4760 | if (options & LYXP_SCNODE_ALL) { |
| 4761 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4762 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4763 | 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] | 4764 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4765 | 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] | 4766 | } |
| 4767 | } |
| 4768 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
| 4769 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4770 | 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] | 4771 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4772 | 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] | 4773 | } |
| 4774 | } |
| 4775 | set_scnode_clear_ctx(set); |
| 4776 | return rc; |
| 4777 | } |
| 4778 | |
| 4779 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4780 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4781 | LY_CHECK_RET(rc); |
| 4782 | set_fill_number(set, strlen(args[0]->val.str)); |
| 4783 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4784 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4785 | LY_CHECK_RET(rc); |
| 4786 | set_fill_number(set, strlen(set->val.str)); |
| 4787 | } |
| 4788 | |
| 4789 | return LY_SUCCESS; |
| 4790 | } |
| 4791 | |
| 4792 | /** |
| 4793 | * @brief Execute the XPath substring(string, number, number?) function. |
| 4794 | * Returns LYXP_SET_STRING substring of the first argument starting |
| 4795 | * on the second argument index ending on the third argument index, |
| 4796 | * indexed from 1. For exact definition refer to |
| 4797 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
| 4798 | * |
| 4799 | * @param[in] args Array of arguments. |
| 4800 | * @param[in] arg_count Count of elements in @p args. |
| 4801 | * @param[in,out] set Context and result set at the same time. |
| 4802 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4803 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4804 | */ |
| 4805 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4806 | 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] | 4807 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4808 | int32_t start, len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4809 | uint16_t str_start, str_len, pos; |
| 4810 | struct lysc_node_leaf *sleaf; |
| 4811 | LY_ERR rc = LY_SUCCESS; |
| 4812 | |
| 4813 | if (options & LYXP_SCNODE_ALL) { |
| 4814 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4815 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4816 | 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] | 4817 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4818 | 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] | 4819 | } |
| 4820 | } |
| 4821 | |
| 4822 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4823 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4824 | 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] | 4825 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4826 | 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] | 4827 | } |
| 4828 | } |
| 4829 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4830 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) && |
| 4831 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4832 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4833 | 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] | 4834 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4835 | 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] | 4836 | } |
| 4837 | } |
| 4838 | set_scnode_clear_ctx(set); |
| 4839 | return rc; |
| 4840 | } |
| 4841 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4842 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4843 | LY_CHECK_RET(rc); |
| 4844 | |
| 4845 | /* start */ |
| 4846 | if (xpath_round(&args[1], 1, args[1], options)) { |
| 4847 | return -1; |
| 4848 | } |
| 4849 | if (isfinite(args[1]->val.num)) { |
| 4850 | start = args[1]->val.num - 1; |
| 4851 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4852 | start = INT32_MIN; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4853 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4854 | start = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4855 | } |
| 4856 | |
| 4857 | /* len */ |
| 4858 | if (arg_count == 3) { |
| 4859 | rc = xpath_round(&args[2], 1, args[2], options); |
| 4860 | LY_CHECK_RET(rc); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4861 | if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4862 | len = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4863 | } else if (isfinite(args[2]->val.num)) { |
| 4864 | len = args[2]->val.num; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4865 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4866 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4867 | } |
| 4868 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4869 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4870 | } |
| 4871 | |
| 4872 | /* find matching character positions */ |
| 4873 | str_start = 0; |
| 4874 | str_len = 0; |
| 4875 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
| 4876 | if (pos < start) { |
| 4877 | ++str_start; |
| 4878 | } else if (pos < start + len) { |
| 4879 | ++str_len; |
| 4880 | } else { |
| 4881 | break; |
| 4882 | } |
| 4883 | } |
| 4884 | |
| 4885 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
| 4886 | return LY_SUCCESS; |
| 4887 | } |
| 4888 | |
| 4889 | /** |
| 4890 | * @brief Execute the XPath substring-after(string, string) function. |
| 4891 | * Returns LYXP_SET_STRING with the string succeeding the occurance |
| 4892 | * of the second argument in the first or an empty string. |
| 4893 | * |
| 4894 | * @param[in] args Array of arguments. |
| 4895 | * @param[in] arg_count Count of elements in @p args. |
| 4896 | * @param[in,out] set Context and result set at the same time. |
| 4897 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4898 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4899 | */ |
| 4900 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4901 | 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] | 4902 | { |
| 4903 | char *ptr; |
| 4904 | struct lysc_node_leaf *sleaf; |
| 4905 | LY_ERR rc = LY_SUCCESS; |
| 4906 | |
| 4907 | if (options & LYXP_SCNODE_ALL) { |
| 4908 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4909 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4910 | 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] | 4911 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4912 | 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] | 4913 | } |
| 4914 | } |
| 4915 | |
| 4916 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4917 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4918 | 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] | 4919 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4920 | 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] | 4921 | } |
| 4922 | } |
| 4923 | set_scnode_clear_ctx(set); |
| 4924 | return rc; |
| 4925 | } |
| 4926 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4927 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4928 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4929 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4930 | LY_CHECK_RET(rc); |
| 4931 | |
| 4932 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4933 | if (ptr) { |
| 4934 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
| 4935 | } else { |
| 4936 | set_fill_string(set, "", 0); |
| 4937 | } |
| 4938 | |
| 4939 | return LY_SUCCESS; |
| 4940 | } |
| 4941 | |
| 4942 | /** |
| 4943 | * @brief Execute the XPath substring-before(string, string) function. |
| 4944 | * Returns LYXP_SET_STRING with the string preceding the occurance |
| 4945 | * of the second argument in the first or an empty string. |
| 4946 | * |
| 4947 | * @param[in] args Array of arguments. |
| 4948 | * @param[in] arg_count Count of elements in @p args. |
| 4949 | * @param[in,out] set Context and result set at the same time. |
| 4950 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4951 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4952 | */ |
| 4953 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4954 | 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] | 4955 | { |
| 4956 | char *ptr; |
| 4957 | struct lysc_node_leaf *sleaf; |
| 4958 | LY_ERR rc = LY_SUCCESS; |
| 4959 | |
| 4960 | if (options & LYXP_SCNODE_ALL) { |
| 4961 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4962 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4963 | 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] | 4964 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4965 | 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] | 4966 | } |
| 4967 | } |
| 4968 | |
| 4969 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4970 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4971 | 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] | 4972 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4973 | 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] | 4974 | } |
| 4975 | } |
| 4976 | set_scnode_clear_ctx(set); |
| 4977 | return rc; |
| 4978 | } |
| 4979 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4980 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4981 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4982 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4983 | LY_CHECK_RET(rc); |
| 4984 | |
| 4985 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4986 | if (ptr) { |
| 4987 | set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str); |
| 4988 | } else { |
| 4989 | set_fill_string(set, "", 0); |
| 4990 | } |
| 4991 | |
| 4992 | return LY_SUCCESS; |
| 4993 | } |
| 4994 | |
| 4995 | /** |
| 4996 | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
| 4997 | * with the sum of all the nodes in the context. |
| 4998 | * |
| 4999 | * @param[in] args Array of arguments. |
| 5000 | * @param[in] arg_count Count of elements in @p args. |
| 5001 | * @param[in,out] set Context and result set at the same time. |
| 5002 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5003 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5004 | */ |
| 5005 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5006 | 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] | 5007 | { |
| 5008 | long double num; |
| 5009 | char *str; |
| 5010 | uint16_t i; |
| 5011 | struct lyxp_set set_item; |
| 5012 | struct lysc_node_leaf *sleaf; |
| 5013 | LY_ERR rc = LY_SUCCESS; |
| 5014 | |
| 5015 | if (options & LYXP_SCNODE_ALL) { |
| 5016 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
| 5017 | for (i = 0; i < args[0]->used; ++i) { |
| 5018 | if (args[0]->val.scnodes[i].in_ctx == 1) { |
| 5019 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
| 5020 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5021 | 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] | 5022 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5023 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5024 | 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] | 5025 | } |
| 5026 | } |
| 5027 | } |
| 5028 | } |
| 5029 | set_scnode_clear_ctx(set); |
| 5030 | return rc; |
| 5031 | } |
| 5032 | |
| 5033 | set_fill_number(set, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5034 | |
| 5035 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5036 | 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] | 5037 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5038 | } else if (!args[0]->used) { |
| 5039 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5040 | } |
| 5041 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5042 | set_init(&set_item, set); |
| 5043 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5044 | set_item.type = LYXP_SET_NODE_SET; |
| 5045 | set_item.val.nodes = malloc(sizeof *set_item.val.nodes); |
| 5046 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
| 5047 | |
| 5048 | set_item.used = 1; |
| 5049 | set_item.size = 1; |
| 5050 | |
| 5051 | for (i = 0; i < args[0]->used; ++i) { |
| 5052 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
| 5053 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5054 | rc = cast_node_set_to_string(&set_item, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5055 | LY_CHECK_RET(rc); |
| 5056 | num = cast_string_to_number(str); |
| 5057 | free(str); |
| 5058 | set->val.num += num; |
| 5059 | } |
| 5060 | |
| 5061 | free(set_item.val.nodes); |
| 5062 | |
| 5063 | return LY_SUCCESS; |
| 5064 | } |
| 5065 | |
| 5066 | /** |
| 5067 | * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET |
| 5068 | * with the text content of the nodes in the context. |
| 5069 | * |
| 5070 | * @param[in] args Array of arguments. |
| 5071 | * @param[in] arg_count Count of elements in @p args. |
| 5072 | * @param[in,out] set Context and result set at the same time. |
| 5073 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5074 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5075 | */ |
| 5076 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5077 | 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] | 5078 | { |
| 5079 | uint32_t i; |
| 5080 | |
| 5081 | if (options & LYXP_SCNODE_ALL) { |
| 5082 | set_scnode_clear_ctx(set); |
| 5083 | return LY_SUCCESS; |
| 5084 | } |
| 5085 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5086 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5087 | 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] | 5088 | return LY_EVALID; |
| 5089 | } |
| 5090 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 5091 | for (i = 0; i < set->used; ) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5092 | switch (set->val.nodes[i].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 5093 | case LYXP_NODE_NONE: |
| 5094 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5095 | case LYXP_NODE_ELEM: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5096 | if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 5097 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
| 5098 | ++i; |
| 5099 | break; |
| 5100 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5101 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5102 | case LYXP_NODE_ROOT: |
| 5103 | case LYXP_NODE_ROOT_CONFIG: |
| 5104 | case LYXP_NODE_TEXT: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5105 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5106 | set_remove_node(set, i); |
| 5107 | break; |
| 5108 | } |
| 5109 | } |
| 5110 | |
| 5111 | return LY_SUCCESS; |
| 5112 | } |
| 5113 | |
| 5114 | /** |
| 5115 | * @brief Execute the XPath translate(string, string, string) function. |
| 5116 | * Returns LYXP_SET_STRING with the first argument with the characters |
| 5117 | * from the second argument replaced by those on the corresponding |
| 5118 | * positions in the third argument. |
| 5119 | * |
| 5120 | * @param[in] args Array of arguments. |
| 5121 | * @param[in] arg_count Count of elements in @p args. |
| 5122 | * @param[in,out] set Context and result set at the same time. |
| 5123 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5124 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5125 | */ |
| 5126 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5127 | 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] | 5128 | { |
| 5129 | uint16_t i, j, new_used; |
| 5130 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5131 | ly_bool have_removed; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5132 | struct lysc_node_leaf *sleaf; |
| 5133 | LY_ERR rc = LY_SUCCESS; |
| 5134 | |
| 5135 | if (options & LYXP_SCNODE_ALL) { |
| 5136 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5137 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5138 | 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] | 5139 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5140 | 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] | 5141 | } |
| 5142 | } |
| 5143 | |
| 5144 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5145 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5146 | 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] | 5147 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5148 | 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] | 5149 | } |
| 5150 | } |
| 5151 | |
| 5152 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 5153 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5154 | 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] | 5155 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5156 | 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] | 5157 | } |
| 5158 | } |
| 5159 | set_scnode_clear_ctx(set); |
| 5160 | return rc; |
| 5161 | } |
| 5162 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5163 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5164 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5165 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5166 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5167 | rc = lyxp_set_cast(args[2], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5168 | LY_CHECK_RET(rc); |
| 5169 | |
| 5170 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
| 5171 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5172 | new_used = 0; |
| 5173 | |
| 5174 | have_removed = 0; |
| 5175 | for (i = 0; args[0]->val.str[i]; ++i) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5176 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5177 | |
| 5178 | for (j = 0; args[1]->val.str[j]; ++j) { |
| 5179 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
| 5180 | /* removing this char */ |
| 5181 | if (j >= strlen(args[2]->val.str)) { |
| 5182 | have_removed = 1; |
| 5183 | found = 1; |
| 5184 | break; |
| 5185 | } |
| 5186 | /* replacing this char */ |
| 5187 | new[new_used] = args[2]->val.str[j]; |
| 5188 | ++new_used; |
| 5189 | found = 1; |
| 5190 | break; |
| 5191 | } |
| 5192 | } |
| 5193 | |
| 5194 | /* copying this char */ |
| 5195 | if (!found) { |
| 5196 | new[new_used] = args[0]->val.str[i]; |
| 5197 | ++new_used; |
| 5198 | } |
| 5199 | } |
| 5200 | |
| 5201 | if (have_removed) { |
| 5202 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 5203 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5204 | } |
| 5205 | new[new_used] = '\0'; |
| 5206 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5207 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5208 | set->type = LYXP_SET_STRING; |
| 5209 | set->val.str = new; |
| 5210 | |
| 5211 | return LY_SUCCESS; |
| 5212 | } |
| 5213 | |
| 5214 | /** |
| 5215 | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
| 5216 | * with true value. |
| 5217 | * |
| 5218 | * @param[in] args Array of arguments. |
| 5219 | * @param[in] arg_count Count of elements in @p args. |
| 5220 | * @param[in,out] set Context and result set at the same time. |
| 5221 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5222 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5223 | */ |
| 5224 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5225 | 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] | 5226 | { |
| 5227 | if (options & LYXP_SCNODE_ALL) { |
| 5228 | set_scnode_clear_ctx(set); |
| 5229 | return LY_SUCCESS; |
| 5230 | } |
| 5231 | |
| 5232 | set_fill_boolean(set, 1); |
| 5233 | return LY_SUCCESS; |
| 5234 | } |
| 5235 | |
| 5236 | /* |
| 5237 | * moveto functions |
| 5238 | * |
| 5239 | * They and only they actually change the context (set). |
| 5240 | */ |
| 5241 | |
| 5242 | /** |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5243 | * @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] | 5244 | * |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5245 | * XPath @p set is expected to be a (sc)node set! |
| 5246 | * |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5247 | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
| 5248 | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5249 | * @param[in] set Set with general XPath context. |
| 5250 | * @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] | 5251 | * @param[out] moveto_mod Expected module of a matching node. |
| 5252 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5253 | */ |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5254 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5255 | moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set, |
| 5256 | const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5257 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 5258 | const struct lys_module *mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5259 | const char *ptr; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5260 | size_t pref_len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5261 | |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5262 | assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET)); |
| 5263 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5264 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
| 5265 | /* specific module */ |
| 5266 | pref_len = ptr - *qname; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5267 | 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] | 5268 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5269 | /* check for errors and non-implemented modules, as they are not valid */ |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5270 | if (!mod || !mod->implemented) { |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5271 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5272 | 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] | 5273 | } else { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5274 | 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] | 5275 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5276 | return LY_EVALID; |
| 5277 | } |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5278 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5279 | *qname += pref_len + 1; |
| 5280 | *qname_len -= pref_len + 1; |
| 5281 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
| 5282 | /* all modules - special case */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5283 | mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5284 | } else { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5285 | switch (set->format) { |
| 5286 | case LY_PREF_SCHEMA: |
| 5287 | case LY_PREF_SCHEMA_RESOLVED: |
| 5288 | /* current module */ |
| 5289 | mod = set->cur_mod; |
| 5290 | break; |
| 5291 | case LY_PREF_JSON: |
| 5292 | /* inherit parent (context node) module */ |
| 5293 | if (ctx_scnode) { |
| 5294 | mod = ctx_scnode->module; |
| 5295 | } else { |
| 5296 | mod = NULL; |
| 5297 | } |
| 5298 | break; |
| 5299 | case LY_PREF_XML: |
| 5300 | /* not defined */ |
| 5301 | LOGINT_RET(set->ctx); |
| 5302 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5303 | } |
| 5304 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5305 | *moveto_mod = mod; |
| 5306 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5307 | } |
| 5308 | |
| 5309 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5310 | * @brief Move context @p set to the root. Handles absolute path. |
| 5311 | * Result is LYXP_SET_NODE_SET. |
| 5312 | * |
| 5313 | * @param[in,out] set Set to use. |
| 5314 | * @param[in] options Xpath options. |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5315 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5316 | */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5317 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5318 | moveto_root(struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5319 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5320 | if (!set) { |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5321 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5322 | } |
| 5323 | |
| 5324 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5325 | set_scnode_clear_ctx(set); |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5326 | 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] | 5327 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5328 | set->type = LYXP_SET_NODE_SET; |
| 5329 | set->used = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5330 | set_insert_node(set, NULL, 0, set->root_type, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5331 | } |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5332 | |
| 5333 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5334 | } |
| 5335 | |
| 5336 | /** |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5337 | * @brief Check whether a node has some unresolved "when". |
| 5338 | * |
| 5339 | * @param[in] node Node to check. |
| 5340 | * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when") |
| 5341 | */ |
| 5342 | static LY_ERR |
| 5343 | moveto_when_check(const struct lyd_node *node) |
| 5344 | { |
| 5345 | const struct lysc_node *schema; |
| 5346 | |
| 5347 | if (!node) { |
| 5348 | return LY_SUCCESS; |
| 5349 | } |
| 5350 | |
| 5351 | schema = node->schema; |
| 5352 | do { |
| 5353 | if (schema->when && !(node->flags & LYD_WHEN_TRUE)) { |
| 5354 | return LY_EINCOMPLETE; |
| 5355 | } |
| 5356 | schema = schema->parent; |
| 5357 | } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 5358 | |
| 5359 | return LY_SUCCESS; |
| 5360 | } |
| 5361 | |
| 5362 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5363 | * @brief Check @p node as a part of NameTest processing. |
| 5364 | * |
| 5365 | * @param[in] node Node to check. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5366 | * @param[in] ctx_node Context node. |
| 5367 | * @param[in] set Set to read general context from. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5368 | * @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] | 5369 | * @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] | 5370 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when, |
| 5371 | * LY_EINVAL if netither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5372 | */ |
| 5373 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5374 | 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] | 5375 | const char *node_name, const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5376 | { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5377 | if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) { |
| 5378 | switch (set->format) { |
| 5379 | case LY_PREF_SCHEMA: |
| 5380 | case LY_PREF_SCHEMA_RESOLVED: |
| 5381 | /* use current module */ |
| 5382 | moveto_mod = set->cur_mod; |
| 5383 | break; |
| 5384 | case LY_PREF_JSON: |
| 5385 | /* inherit module of the context node, if any */ |
| 5386 | if (ctx_node) { |
| 5387 | moveto_mod = ctx_node->schema->module; |
| 5388 | } |
| 5389 | break; |
| 5390 | case LY_PREF_XML: |
| 5391 | /* not defined */ |
| 5392 | LOGINT(set->ctx); |
| 5393 | return LY_EINVAL; |
| 5394 | } |
| 5395 | } |
| 5396 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5397 | /* module check */ |
| 5398 | if (moveto_mod && (node->schema->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5399 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5400 | } |
| 5401 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5402 | /* context check */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5403 | 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] | 5404 | return LY_EINVAL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5405 | } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 5406 | (node->schema != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5407 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5408 | } |
| 5409 | |
| 5410 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5411 | if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5412 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5413 | } |
| 5414 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5415 | /* when check */ |
| 5416 | if (moveto_when_check(node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5417 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5418 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5419 | |
| 5420 | /* match */ |
| 5421 | return LY_SUCCESS; |
| 5422 | } |
| 5423 | |
| 5424 | /** |
| 5425 | * @brief Check @p node as a part of schema NameTest processing. |
| 5426 | * |
| 5427 | * @param[in] node Schema node to check. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5428 | * @param[in] ctx_scnode Context node. |
| 5429 | * @param[in] set Set to read general context from. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5430 | * @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] | 5431 | * @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] | 5432 | * @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] | 5433 | */ |
| 5434 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5435 | 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] | 5436 | const char *node_name, const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5437 | { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5438 | if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) { |
| 5439 | switch (set->format) { |
| 5440 | case LY_PREF_SCHEMA: |
| 5441 | case LY_PREF_SCHEMA_RESOLVED: |
| 5442 | /* use current module */ |
| 5443 | moveto_mod = set->cur_mod; |
| 5444 | break; |
| 5445 | case LY_PREF_JSON: |
| 5446 | /* inherit module of the context node, if any */ |
| 5447 | if (ctx_scnode) { |
| 5448 | moveto_mod = ctx_scnode->module; |
| 5449 | } |
| 5450 | break; |
| 5451 | case LY_PREF_XML: |
| 5452 | /* not defined */ |
| 5453 | LOGINT(set->ctx); |
| 5454 | return LY_EINVAL; |
| 5455 | } |
| 5456 | } |
| 5457 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5458 | /* module check */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5459 | if (moveto_mod && (node->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5460 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5461 | } |
| 5462 | |
| 5463 | /* context check */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5464 | 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] | 5465 | return LY_EINVAL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5466 | } 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] | 5467 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5468 | } |
| 5469 | |
| 5470 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5471 | if (node_name && strcmp(node_name, "*") && (node->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5472 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5473 | } |
| 5474 | |
| 5475 | /* match */ |
| 5476 | return LY_SUCCESS; |
| 5477 | } |
| 5478 | |
| 5479 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5480 | * @brief Move context @p set to a node. 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] | 5481 | * |
| 5482 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5483 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5484 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5485 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5486 | */ |
| 5487 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5488 | 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] | 5489 | { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 5490 | uint32_t i; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5491 | const struct lyd_node *siblings, *sub, *ctx_node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5492 | LY_ERR rc; |
| 5493 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5494 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5495 | return LY_SUCCESS; |
| 5496 | } |
| 5497 | |
| 5498 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5499 | 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] | 5500 | return LY_EVALID; |
| 5501 | } |
| 5502 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5503 | for (i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5504 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5505 | |
| 5506 | 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] | 5507 | assert(!set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5508 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5509 | /* search in all the trees */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5510 | ctx_node = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5511 | siblings = set->tree; |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5512 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5513 | /* search in children */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5514 | ctx_node = set->val.nodes[i].node; |
| 5515 | siblings = lyd_child(ctx_node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5516 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5517 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5518 | for (sub = siblings; sub; sub = sub->next) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5519 | rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5520 | if (rc == LY_SUCCESS) { |
| 5521 | if (!replaced) { |
| 5522 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
| 5523 | replaced = 1; |
| 5524 | } else { |
| 5525 | set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5526 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5527 | ++i; |
| 5528 | } else if (rc == LY_EINCOMPLETE) { |
| 5529 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5530 | } |
| 5531 | } |
| 5532 | |
| 5533 | if (!replaced) { |
| 5534 | /* no match */ |
| 5535 | set_remove_node(set, i); |
| 5536 | } |
| 5537 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5538 | |
| 5539 | return LY_SUCCESS; |
| 5540 | } |
| 5541 | |
| 5542 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5543 | * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). |
| 5544 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5545 | * |
| 5546 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5547 | * @param[in] scnode Matching node schema. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5548 | * @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] | 5549 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5550 | */ |
| 5551 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5552 | 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] | 5553 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5554 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5555 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5556 | const struct lyd_node *siblings; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5557 | struct lyd_node *sub, *inst = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5558 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5559 | assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5560 | |
| 5561 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5562 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5563 | } |
| 5564 | |
| 5565 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5566 | 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] | 5567 | ret = LY_EVALID; |
| 5568 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5569 | } |
| 5570 | |
| 5571 | /* context check for all the nodes since we have the schema node */ |
| 5572 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 5573 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5574 | goto cleanup; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 5575 | } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 5576 | (scnode != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5577 | lyxp_set_free_content(set); |
| 5578 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5579 | } |
| 5580 | |
| 5581 | /* create specific data instance if needed */ |
| 5582 | if (scnode->nodetype == LYS_LIST) { |
| 5583 | LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup); |
| 5584 | } else if (scnode->nodetype == LYS_LEAFLIST) { |
| 5585 | 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] | 5586 | } |
| 5587 | |
| 5588 | for (i = 0; i < set->used; ) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5589 | siblings = NULL; |
| 5590 | |
| 5591 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
| 5592 | assert(!set->val.nodes[i].node); |
| 5593 | |
| 5594 | /* search in all the trees */ |
| 5595 | siblings = set->tree; |
| 5596 | } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 5597 | /* search in children */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 5598 | siblings = lyd_child(set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5599 | } |
| 5600 | |
| 5601 | /* find the node using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5602 | if (inst) { |
| 5603 | lyd_find_sibling_first(siblings, inst, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5604 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5605 | lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5606 | } |
| 5607 | |
| 5608 | /* when check */ |
| 5609 | if (sub && moveto_when_check(sub)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5610 | ret = LY_EINCOMPLETE; |
| 5611 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5612 | } |
| 5613 | |
| 5614 | if (sub) { |
| 5615 | /* pos filled later */ |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5616 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5617 | ++i; |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5618 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5619 | /* no match */ |
| 5620 | set_remove_node(set, i); |
| 5621 | } |
| 5622 | } |
| 5623 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5624 | cleanup: |
| 5625 | lyd_free_tree(inst); |
| 5626 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5627 | } |
| 5628 | |
| 5629 | /** |
| 5630 | * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
| 5631 | * |
| 5632 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5633 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5634 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5635 | * @param[in] options XPath options. |
| 5636 | * @return LY_ERR |
| 5637 | */ |
| 5638 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5639 | 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] | 5640 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5641 | ly_bool temp_ctx = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5642 | uint32_t getnext_opts; |
| 5643 | uint32_t orig_used, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5644 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5645 | const struct lysc_node *iter, *start_parent; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5646 | const struct lys_module *mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5647 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5648 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5649 | return LY_SUCCESS; |
| 5650 | } |
| 5651 | |
| 5652 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5653 | 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] | 5654 | return LY_EVALID; |
| 5655 | } |
| 5656 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5657 | /* getnext opts */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 5658 | getnext_opts = 0; |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5659 | if (options & LYXP_SCNODE_OUTPUT) { |
| 5660 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 5661 | } |
| 5662 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5663 | orig_used = set->used; |
| 5664 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5665 | uint32_t idx; |
| 5666 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5667 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5668 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5669 | continue; |
| 5670 | } |
| 5671 | |
| 5672 | /* remember context node */ |
| 5673 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5674 | } else { |
| 5675 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5676 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5677 | |
| 5678 | start_parent = set->val.scnodes[i].scnode; |
| 5679 | |
| 5680 | 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] | 5681 | /* 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] | 5682 | * 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] | 5683 | mod_idx = 0; |
Michal Vasko | 9e9f26d | 2020-10-12 16:31:33 +0200 | [diff] [blame] | 5684 | 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] | 5685 | iter = NULL; |
Michal Vasko | 509de4d | 2019-12-10 14:51:30 +0100 | [diff] [blame] | 5686 | /* module may not be implemented */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5687 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5688 | if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5689 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx)); |
| 5690 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5691 | /* we need to prevent these nodes from being considered in this moveto */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5692 | if ((idx < orig_used) && (idx > i)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5693 | set->val.scnodes[idx].in_ctx = 2; |
| 5694 | temp_ctx = 1; |
| 5695 | } |
| 5696 | } |
| 5697 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5698 | } |
| 5699 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5700 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 5701 | iter = NULL; |
| 5702 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5703 | if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5704 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx)); |
| 5705 | |
| 5706 | if ((idx < orig_used) && (idx > i)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5707 | set->val.scnodes[idx].in_ctx = 2; |
| 5708 | temp_ctx = 1; |
| 5709 | } |
| 5710 | } |
| 5711 | } |
| 5712 | } |
| 5713 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5714 | |
| 5715 | /* correct temporary in_ctx values */ |
| 5716 | if (temp_ctx) { |
| 5717 | for (i = 0; i < orig_used; ++i) { |
| 5718 | if (set->val.scnodes[i].in_ctx == 2) { |
| 5719 | set->val.scnodes[i].in_ctx = 1; |
| 5720 | } |
| 5721 | } |
| 5722 | } |
| 5723 | |
| 5724 | return LY_SUCCESS; |
| 5725 | } |
| 5726 | |
| 5727 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5728 | * @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] | 5729 | * Context position aware. |
| 5730 | * |
| 5731 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5732 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5733 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5734 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5735 | */ |
| 5736 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5737 | 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] | 5738 | { |
| 5739 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5740 | const struct lyd_node *next, *elem, *start; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5741 | struct lyxp_set ret_set; |
| 5742 | LY_ERR rc; |
| 5743 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5744 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5745 | return LY_SUCCESS; |
| 5746 | } |
| 5747 | |
| 5748 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5749 | 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] | 5750 | return LY_EVALID; |
| 5751 | } |
| 5752 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5753 | /* 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] | 5754 | rc = moveto_node(set, NULL, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5755 | LY_CHECK_RET(rc); |
| 5756 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5757 | /* 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] | 5758 | set_init(&ret_set, set); |
| 5759 | for (i = 0; i < set->used; ++i) { |
| 5760 | |
| 5761 | /* TREE DFS */ |
| 5762 | start = set->val.nodes[i].node; |
| 5763 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5764 | rc = moveto_node_check(elem, start, set, ncname, moveto_mod); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5765 | if (!rc) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5766 | /* add matching node into result set */ |
| 5767 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
| 5768 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
| 5769 | /* the node is a duplicate, we'll process it later in the set */ |
| 5770 | goto skip_children; |
| 5771 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5772 | } else if (rc == LY_EINCOMPLETE) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5773 | return rc; |
| 5774 | } else if (rc == LY_EINVAL) { |
| 5775 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5776 | } |
| 5777 | |
| 5778 | /* TREE DFS NEXT ELEM */ |
| 5779 | /* select element for the next run - children first */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 5780 | next = lyd_child(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5781 | if (!next) { |
| 5782 | skip_children: |
| 5783 | /* no children, so try siblings, but only if it's not the start, |
| 5784 | * that is considered to be the root and it's siblings are not traversed */ |
| 5785 | if (elem != start) { |
| 5786 | next = elem->next; |
| 5787 | } else { |
| 5788 | break; |
| 5789 | } |
| 5790 | } |
| 5791 | while (!next) { |
| 5792 | /* no siblings, go back through the parents */ |
| 5793 | if ((struct lyd_node *)elem->parent == start) { |
| 5794 | /* we are done, no next element to process */ |
| 5795 | break; |
| 5796 | } |
| 5797 | /* parent is already processed, go to its sibling */ |
| 5798 | elem = (struct lyd_node *)elem->parent; |
| 5799 | next = elem->next; |
| 5800 | } |
| 5801 | } |
| 5802 | } |
| 5803 | |
| 5804 | /* make the temporary set the current one */ |
| 5805 | ret_set.ctx_pos = set->ctx_pos; |
| 5806 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5807 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5808 | memcpy(set, &ret_set, sizeof *set); |
| 5809 | |
| 5810 | return LY_SUCCESS; |
| 5811 | } |
| 5812 | |
| 5813 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5814 | * @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] | 5815 | * |
| 5816 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5817 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5818 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5819 | * @param[in] options XPath options. |
| 5820 | * @return LY_ERR |
| 5821 | */ |
| 5822 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5823 | 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] | 5824 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5825 | uint32_t i, orig_used; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5826 | const struct lysc_node *next, *elem, *start; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5827 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5828 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5829 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5830 | return LY_SUCCESS; |
| 5831 | } |
| 5832 | |
| 5833 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5834 | 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] | 5835 | return LY_EVALID; |
| 5836 | } |
| 5837 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5838 | orig_used = set->used; |
| 5839 | for (i = 0; i < orig_used; ++i) { |
| 5840 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5841 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5842 | continue; |
| 5843 | } |
| 5844 | |
| 5845 | /* remember context node */ |
| 5846 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5847 | } else { |
| 5848 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5849 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5850 | |
| 5851 | /* TREE DFS */ |
| 5852 | start = set->val.scnodes[i].scnode; |
| 5853 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5854 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 5855 | /* schema-only nodes, skip root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5856 | goto next_iter; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5857 | } |
| 5858 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5859 | rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5860 | if (!rc) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5861 | uint32_t idx; |
| 5862 | |
| 5863 | if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5864 | set->val.scnodes[idx].in_ctx = 1; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5865 | if ((uint32_t)idx > i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5866 | /* we will process it later in the set */ |
| 5867 | goto skip_children; |
| 5868 | } |
| 5869 | } else { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5870 | 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] | 5871 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5872 | } else if (rc == LY_EINVAL) { |
| 5873 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5874 | } |
| 5875 | |
| 5876 | next_iter: |
| 5877 | /* TREE DFS NEXT ELEM */ |
| 5878 | /* select element for the next run - children first */ |
| 5879 | 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] | 5880 | if (!next) { |
| 5881 | skip_children: |
| 5882 | /* no children, so try siblings, but only if it's not the start, |
| 5883 | * that is considered to be the root and it's siblings are not traversed */ |
| 5884 | if (elem != start) { |
| 5885 | next = elem->next; |
| 5886 | } else { |
| 5887 | break; |
| 5888 | } |
| 5889 | } |
| 5890 | while (!next) { |
| 5891 | /* no siblings, go back through the parents */ |
| 5892 | if (elem->parent == start) { |
| 5893 | /* we are done, no next element to process */ |
| 5894 | break; |
| 5895 | } |
| 5896 | /* parent is already processed, go to its sibling */ |
| 5897 | elem = elem->parent; |
| 5898 | next = elem->next; |
| 5899 | } |
| 5900 | } |
| 5901 | } |
| 5902 | |
| 5903 | return LY_SUCCESS; |
| 5904 | } |
| 5905 | |
| 5906 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5907 | * @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] | 5908 | * Indirectly context position aware. |
| 5909 | * |
| 5910 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5911 | * @param[in] mod Matching metadata module, NULL for any. |
| 5912 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5913 | * @return LY_ERR |
| 5914 | */ |
| 5915 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5916 | 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] | 5917 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5918 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5919 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5920 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5921 | return LY_SUCCESS; |
| 5922 | } |
| 5923 | |
| 5924 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5925 | 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] | 5926 | return LY_EVALID; |
| 5927 | } |
| 5928 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5929 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5930 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5931 | |
| 5932 | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
| 5933 | * our attributes are always qualified */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5934 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5935 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5936 | |
| 5937 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5938 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5939 | continue; |
| 5940 | } |
| 5941 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5942 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5943 | /* match */ |
| 5944 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5945 | set->val.meta[i].meta = sub; |
| 5946 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5947 | /* pos does not change */ |
| 5948 | replaced = 1; |
| 5949 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5950 | 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] | 5951 | } |
| 5952 | ++i; |
| 5953 | } |
| 5954 | } |
| 5955 | } |
| 5956 | |
| 5957 | if (!replaced) { |
| 5958 | /* no match */ |
| 5959 | set_remove_node(set, i); |
| 5960 | } |
| 5961 | } |
| 5962 | |
| 5963 | return LY_SUCCESS; |
| 5964 | } |
| 5965 | |
| 5966 | /** |
| 5967 | * @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] | 5968 | * Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5969 | * |
| 5970 | * @param[in,out] set1 Set to use for the result. |
| 5971 | * @param[in] set2 Set that is copied to @p set1. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5972 | * @return LY_ERR |
| 5973 | */ |
| 5974 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5975 | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5976 | { |
| 5977 | LY_ERR rc; |
| 5978 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5979 | 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] | 5980 | 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] | 5981 | return LY_EVALID; |
| 5982 | } |
| 5983 | |
| 5984 | /* set2 is empty or both set1 and set2 */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5985 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5986 | return LY_SUCCESS; |
| 5987 | } |
| 5988 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5989 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5990 | memcpy(set1, set2, sizeof *set1); |
| 5991 | /* dynamic memory belongs to set1 now, do not free */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5992 | memset(set2, 0, sizeof *set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5993 | return LY_SUCCESS; |
| 5994 | } |
| 5995 | |
| 5996 | /* we assume sets are sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5997 | assert(!set_sort(set1) && !set_sort(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5998 | |
| 5999 | /* sort, remove duplicates */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6000 | rc = set_sorted_merge(set1, set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6001 | LY_CHECK_RET(rc); |
| 6002 | |
| 6003 | /* final set must be sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6004 | assert(!set_sort(set1)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6005 | |
| 6006 | return LY_SUCCESS; |
| 6007 | } |
| 6008 | |
| 6009 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6010 | * @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] | 6011 | * Context position aware. |
| 6012 | * |
| 6013 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6014 | * @param[in] mod Matching metadata module, NULL for any. |
| 6015 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6016 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6017 | */ |
| 6018 | static int |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6019 | 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] | 6020 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6021 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6022 | struct lyxp_set *set_all_desc = NULL; |
| 6023 | LY_ERR rc; |
| 6024 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6025 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6026 | return LY_SUCCESS; |
| 6027 | } |
| 6028 | |
| 6029 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6030 | 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] | 6031 | return LY_EVALID; |
| 6032 | } |
| 6033 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6034 | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
| 6035 | * but it likely won't be used much, so it's a waste of time */ |
| 6036 | /* copy the context */ |
| 6037 | set_all_desc = set_copy(set); |
| 6038 | /* get all descendant nodes (the original context nodes are removed) */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6039 | rc = moveto_node_alldesc(set_all_desc, NULL, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6040 | if (rc != LY_SUCCESS) { |
| 6041 | lyxp_set_free(set_all_desc); |
| 6042 | return rc; |
| 6043 | } |
| 6044 | /* prepend the original context nodes */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6045 | rc = moveto_union(set, set_all_desc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6046 | if (rc != LY_SUCCESS) { |
| 6047 | lyxp_set_free(set_all_desc); |
| 6048 | return rc; |
| 6049 | } |
| 6050 | lyxp_set_free(set_all_desc); |
| 6051 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6052 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6053 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6054 | |
| 6055 | /* only attributes of an elem can be in the result, skip all the rest, |
| 6056 | * we have all attributes qualified in lyd tree */ |
| 6057 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6058 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6059 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6060 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6061 | continue; |
| 6062 | } |
| 6063 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6064 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6065 | /* match */ |
| 6066 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6067 | set->val.meta[i].meta = sub; |
| 6068 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6069 | /* pos does not change */ |
| 6070 | replaced = 1; |
| 6071 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6072 | 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] | 6073 | } |
| 6074 | ++i; |
| 6075 | } |
| 6076 | } |
| 6077 | } |
| 6078 | |
| 6079 | if (!replaced) { |
| 6080 | /* no match */ |
| 6081 | set_remove_node(set, i); |
| 6082 | } |
| 6083 | } |
| 6084 | |
| 6085 | return LY_SUCCESS; |
| 6086 | } |
| 6087 | |
| 6088 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6089 | * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET. |
| 6090 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6091 | * |
| 6092 | * @param[in] parent Current parent. |
| 6093 | * @param[in] parent_pos Position of @p parent. |
| 6094 | * @param[in] parent_type Node type of @p parent. |
| 6095 | * @param[in,out] to_set Set to use. |
| 6096 | * @param[in] dup_check_set Set for checking duplicities. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6097 | * @param[in] options XPath options. |
| 6098 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6099 | */ |
| 6100 | static LY_ERR |
| 6101 | 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] | 6102 | 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] | 6103 | { |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6104 | const struct lyd_node *iter, *first; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6105 | LY_ERR rc; |
| 6106 | |
| 6107 | switch (parent_type) { |
| 6108 | case LYXP_NODE_ROOT: |
| 6109 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6110 | assert(!parent); |
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 | /* add all top-level nodes as elements */ |
| 6113 | first = to_set->tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6114 | break; |
| 6115 | case LYXP_NODE_ELEM: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6116 | /* add just the text node of this term element node */ |
| 6117 | if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6118 | if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) { |
| 6119 | set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used); |
| 6120 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6121 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6122 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6123 | |
| 6124 | /* add all the children of this node */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 6125 | first = lyd_child(parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6126 | break; |
| 6127 | default: |
| 6128 | LOGINT_RET(parent->schema->module->ctx); |
| 6129 | } |
| 6130 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6131 | /* add all top-level nodes as elements */ |
| 6132 | LY_LIST_FOR(first, iter) { |
| 6133 | /* context check */ |
| 6134 | if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) { |
| 6135 | continue; |
| 6136 | } |
| 6137 | |
| 6138 | /* when check */ |
| 6139 | if (moveto_when_check(iter)) { |
| 6140 | return LY_EINCOMPLETE; |
| 6141 | } |
| 6142 | |
| 6143 | if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) { |
| 6144 | set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used); |
| 6145 | |
| 6146 | /* also add all the children of this node, recursively */ |
| 6147 | rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options); |
| 6148 | LY_CHECK_RET(rc); |
| 6149 | } |
| 6150 | } |
| 6151 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6152 | return LY_SUCCESS; |
| 6153 | } |
| 6154 | |
| 6155 | /** |
| 6156 | * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET |
| 6157 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6158 | * |
| 6159 | * @param[in,out] set Set to use. |
| 6160 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6161 | * @param[in] options XPath options. |
| 6162 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6163 | */ |
| 6164 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6165 | 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] | 6166 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6167 | struct lyxp_set ret_set; |
| 6168 | LY_ERR rc; |
| 6169 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6170 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6171 | return LY_SUCCESS; |
| 6172 | } |
| 6173 | |
| 6174 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6175 | 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] | 6176 | return LY_EVALID; |
| 6177 | } |
| 6178 | |
| 6179 | /* nothing to do */ |
| 6180 | if (!all_desc) { |
| 6181 | return LY_SUCCESS; |
| 6182 | } |
| 6183 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6184 | /* add all the children, they get added recursively */ |
| 6185 | set_init(&ret_set, set); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6186 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6187 | /* copy the current node to tmp */ |
| 6188 | set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used); |
| 6189 | |
| 6190 | /* do not touch attributes and text nodes */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6191 | 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] | 6192 | continue; |
| 6193 | } |
| 6194 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6195 | /* add all the children */ |
| 6196 | 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] | 6197 | set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6198 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6199 | lyxp_set_free_content(&ret_set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6200 | return rc; |
| 6201 | } |
| 6202 | } |
| 6203 | |
| 6204 | /* use the temporary set as the current one */ |
| 6205 | ret_set.ctx_pos = set->ctx_pos; |
| 6206 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6207 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6208 | memcpy(set, &ret_set, sizeof *set); |
| 6209 | |
| 6210 | return LY_SUCCESS; |
| 6211 | } |
| 6212 | |
| 6213 | /** |
| 6214 | * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET |
| 6215 | * (or LYXP_SET_EMPTY). |
| 6216 | * |
| 6217 | * @param[in,out] set Set to use. |
| 6218 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6219 | * @param[in] options XPath options. |
| 6220 | * @return LY_ERR |
| 6221 | */ |
| 6222 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6223 | 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] | 6224 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6225 | uint32_t getnext_opts; |
| 6226 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6227 | const struct lysc_node *iter, *start_parent; |
| 6228 | const struct lys_module *mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6229 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6230 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6231 | return LY_SUCCESS; |
| 6232 | } |
| 6233 | |
| 6234 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6235 | 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] | 6236 | return LY_EVALID; |
| 6237 | } |
| 6238 | |
| 6239 | /* nothing to do */ |
| 6240 | if (!all_desc) { |
| 6241 | return LY_SUCCESS; |
| 6242 | } |
| 6243 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6244 | /* getnext opts */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 6245 | getnext_opts = 0; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6246 | if (options & LYXP_SCNODE_OUTPUT) { |
| 6247 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 6248 | } |
| 6249 | |
| 6250 | /* 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] | 6251 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6252 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6253 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6254 | continue; |
| 6255 | } |
| 6256 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6257 | /* remember context node */ |
| 6258 | set->val.scnodes[i].in_ctx = -1; |
| 6259 | } else { |
| 6260 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6261 | } |
| 6262 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6263 | start_parent = set->val.scnodes[i].scnode; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6264 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6265 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
| 6266 | /* it can actually be in any module, it's all <running> */ |
| 6267 | mod_idx = 0; |
| 6268 | while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
| 6269 | iter = NULL; |
| 6270 | /* module may not be implemented */ |
| 6271 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
| 6272 | /* context check */ |
| 6273 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) { |
| 6274 | continue; |
| 6275 | } |
| 6276 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6277 | 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] | 6278 | /* throw away the insert index, we want to consider that node again, recursively */ |
| 6279 | } |
| 6280 | } |
| 6281 | |
| 6282 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 6283 | iter = NULL; |
| 6284 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6285 | /* context check */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6286 | 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] | 6287 | continue; |
| 6288 | } |
| 6289 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6290 | 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] | 6291 | } |
| 6292 | } |
| 6293 | } |
| 6294 | |
| 6295 | return LY_SUCCESS; |
| 6296 | } |
| 6297 | |
| 6298 | /** |
| 6299 | * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET |
| 6300 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6301 | * |
| 6302 | * @param[in] set Set to use. |
| 6303 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6304 | * @param[in] options XPath options. |
| 6305 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6306 | */ |
| 6307 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6308 | 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] | 6309 | { |
| 6310 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6311 | struct lyd_node *node, *new_node; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6312 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6313 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6314 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6315 | return LY_SUCCESS; |
| 6316 | } |
| 6317 | |
| 6318 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6319 | 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] | 6320 | return LY_EVALID; |
| 6321 | } |
| 6322 | |
| 6323 | if (all_desc) { |
| 6324 | /* <path>//.. == <path>//./.. */ |
| 6325 | rc = moveto_self(set, 1, options); |
| 6326 | LY_CHECK_RET(rc); |
| 6327 | } |
| 6328 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6329 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6330 | node = set->val.nodes[i].node; |
| 6331 | |
| 6332 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 6333 | new_node = (struct lyd_node *)node->parent; |
| 6334 | } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) { |
| 6335 | new_node = node; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6336 | } else if (set->val.nodes[i].type == LYXP_NODE_META) { |
| 6337 | new_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6338 | if (!new_node) { |
| 6339 | LOGINT_RET(set->ctx); |
| 6340 | } |
| 6341 | } else { |
| 6342 | /* root does not have a parent */ |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6343 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6344 | continue; |
| 6345 | } |
| 6346 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6347 | /* when check */ |
| 6348 | if (moveto_when_check(new_node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6349 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6350 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6351 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6352 | if (!new_node) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6353 | /* node already there can also be the root */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6354 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6355 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6356 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6357 | /* 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] | 6358 | new_type = LYXP_NODE_ELEM; |
| 6359 | } |
| 6360 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6361 | if (set_dup_node_check(set, new_node, new_type, -1)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6362 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6363 | } else { |
| 6364 | set_replace_node(set, new_node, 0, new_type, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6365 | } |
| 6366 | } |
| 6367 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6368 | set_remove_nodes_none(set); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6369 | assert(!set_sort(set) && !set_sorted_dup_node_clean(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6370 | |
| 6371 | return LY_SUCCESS; |
| 6372 | } |
| 6373 | |
| 6374 | /** |
| 6375 | * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET |
| 6376 | * (or LYXP_SET_EMPTY). |
| 6377 | * |
| 6378 | * @param[in] set Set to use. |
| 6379 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6380 | * @param[in] options XPath options. |
| 6381 | * @return LY_ERR |
| 6382 | */ |
| 6383 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6384 | 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] | 6385 | { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6386 | uint32_t i, orig_used, idx; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6387 | ly_bool temp_ctx = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6388 | const struct lysc_node *node, *new_node; |
| 6389 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6390 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6391 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6392 | return LY_SUCCESS; |
| 6393 | } |
| 6394 | |
| 6395 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6396 | 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] | 6397 | return LY_EVALID; |
| 6398 | } |
| 6399 | |
| 6400 | if (all_desc) { |
| 6401 | /* <path>//.. == <path>//./.. */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6402 | LY_CHECK_RET(moveto_scnode_self(set, 1, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6403 | } |
| 6404 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6405 | orig_used = set->used; |
| 6406 | for (i = 0; i < orig_used; ++i) { |
| 6407 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6408 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6409 | continue; |
| 6410 | } |
| 6411 | |
| 6412 | /* remember context node */ |
| 6413 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 6414 | } else { |
| 6415 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6416 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6417 | |
| 6418 | node = set->val.scnodes[i].scnode; |
| 6419 | |
| 6420 | if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6421 | new_node = lysc_data_parent(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6422 | } else { |
| 6423 | /* root does not have a parent */ |
| 6424 | continue; |
| 6425 | } |
| 6426 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6427 | if (!new_node) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6428 | /* node has no parent */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6429 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6430 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6431 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6432 | /* 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] | 6433 | new_type = LYXP_NODE_ELEM; |
| 6434 | } |
| 6435 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6436 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx)); |
| 6437 | if ((idx < orig_used) && (idx > i)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6438 | set->val.scnodes[idx].in_ctx = 2; |
| 6439 | temp_ctx = 1; |
| 6440 | } |
| 6441 | } |
| 6442 | |
| 6443 | if (temp_ctx) { |
| 6444 | for (i = 0; i < orig_used; ++i) { |
| 6445 | if (set->val.scnodes[i].in_ctx == 2) { |
| 6446 | set->val.scnodes[i].in_ctx = 1; |
| 6447 | } |
| 6448 | } |
| 6449 | } |
| 6450 | |
| 6451 | return LY_SUCCESS; |
| 6452 | } |
| 6453 | |
| 6454 | /** |
| 6455 | * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'. |
| 6456 | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
| 6457 | * |
| 6458 | * @param[in,out] set1 Set to use for the result. |
| 6459 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6460 | * @param[in] op Comparison operator to process. |
| 6461 | * @param[in] options XPath options. |
| 6462 | * @return LY_ERR |
| 6463 | */ |
| 6464 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6465 | 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] | 6466 | { |
| 6467 | /* |
| 6468 | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
| 6469 | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
| 6470 | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6471 | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6472 | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
| 6473 | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6474 | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6475 | * |
| 6476 | * '=' or '!=' |
| 6477 | * BOOLEAN + BOOLEAN |
| 6478 | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6479 | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6480 | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6481 | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6482 | * NUMBER + NUMBER |
| 6483 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6484 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6485 | * STRING + STRING |
| 6486 | * |
| 6487 | * '<=', '<', '>=', '>' |
| 6488 | * NUMBER + NUMBER |
| 6489 | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6490 | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6491 | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6492 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6493 | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6494 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6495 | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6496 | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6497 | */ |
| 6498 | struct lyxp_set iter1, iter2; |
| 6499 | int result; |
| 6500 | int64_t i; |
| 6501 | LY_ERR rc; |
| 6502 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6503 | memset(&iter1, 0, sizeof iter1); |
| 6504 | memset(&iter2, 0, sizeof iter2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6505 | |
| 6506 | /* iterative evaluation with node-sets */ |
| 6507 | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
| 6508 | if (set1->type == LYXP_SET_NODE_SET) { |
| 6509 | for (i = 0; i < set1->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6510 | /* cast set1 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6511 | switch (set2->type) { |
| 6512 | case LYXP_SET_NUMBER: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6513 | rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6514 | break; |
| 6515 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6516 | rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6517 | break; |
| 6518 | default: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6519 | rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6520 | break; |
| 6521 | } |
| 6522 | LY_CHECK_RET(rc); |
| 6523 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6524 | /* canonize set2 */ |
| 6525 | LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc); |
| 6526 | |
| 6527 | /* compare recursively */ |
| 6528 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
| 6529 | lyxp_set_free_content(&iter2); |
| 6530 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6531 | |
| 6532 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6533 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6534 | set_fill_boolean(set1, 1); |
| 6535 | return LY_SUCCESS; |
| 6536 | } |
| 6537 | } |
| 6538 | } else { |
| 6539 | for (i = 0; i < set2->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6540 | /* set set2 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6541 | switch (set1->type) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6542 | case LYXP_SET_NUMBER: |
| 6543 | rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i); |
| 6544 | break; |
| 6545 | case LYXP_SET_BOOLEAN: |
| 6546 | rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i); |
| 6547 | break; |
| 6548 | default: |
| 6549 | rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i); |
| 6550 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6551 | } |
| 6552 | LY_CHECK_RET(rc); |
| 6553 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6554 | /* canonize set1 */ |
| 6555 | 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] | 6556 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6557 | /* compare recursively */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6558 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6559 | lyxp_set_free_content(&iter2); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6560 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6561 | |
| 6562 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6563 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6564 | set_fill_boolean(set1, 1); |
| 6565 | return LY_SUCCESS; |
| 6566 | } |
| 6567 | } |
| 6568 | } |
| 6569 | |
| 6570 | /* false for all nodes */ |
| 6571 | set_fill_boolean(set1, 0); |
| 6572 | return LY_SUCCESS; |
| 6573 | } |
| 6574 | |
| 6575 | /* first convert properly */ |
| 6576 | if ((op[0] == '=') || (op[0] == '!')) { |
| 6577 | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6578 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
| 6579 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6580 | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6581 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6582 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6583 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6584 | LY_CHECK_RET(rc); |
| 6585 | } /* else we have 2 strings */ |
| 6586 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6587 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6588 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6589 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6590 | LY_CHECK_RET(rc); |
| 6591 | } |
| 6592 | |
| 6593 | assert(set1->type == set2->type); |
| 6594 | |
| 6595 | /* compute result */ |
| 6596 | if (op[0] == '=') { |
| 6597 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6598 | result = (set1->val.bln == set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6599 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6600 | result = (set1->val.num == set2->val.num); |
| 6601 | } else { |
| 6602 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6603 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6604 | } |
| 6605 | } else if (op[0] == '!') { |
| 6606 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6607 | result = (set1->val.bln != set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6608 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6609 | result = (set1->val.num != set2->val.num); |
| 6610 | } else { |
| 6611 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6612 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6613 | } |
| 6614 | } else { |
| 6615 | assert(set1->type == LYXP_SET_NUMBER); |
| 6616 | if (op[0] == '<') { |
| 6617 | if (op[1] == '=') { |
| 6618 | result = (set1->val.num <= set2->val.num); |
| 6619 | } else { |
| 6620 | result = (set1->val.num < set2->val.num); |
| 6621 | } |
| 6622 | } else { |
| 6623 | if (op[1] == '=') { |
| 6624 | result = (set1->val.num >= set2->val.num); |
| 6625 | } else { |
| 6626 | result = (set1->val.num > set2->val.num); |
| 6627 | } |
| 6628 | } |
| 6629 | } |
| 6630 | |
| 6631 | /* assign result */ |
| 6632 | if (result) { |
| 6633 | set_fill_boolean(set1, 1); |
| 6634 | } else { |
| 6635 | set_fill_boolean(set1, 0); |
| 6636 | } |
| 6637 | |
| 6638 | return LY_SUCCESS; |
| 6639 | } |
| 6640 | |
| 6641 | /** |
| 6642 | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
| 6643 | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
| 6644 | * |
| 6645 | * @param[in,out] set1 Set to use for the result. |
| 6646 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6647 | * @param[in] op Operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6648 | * @return LY_ERR |
| 6649 | */ |
| 6650 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6651 | 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] | 6652 | { |
| 6653 | LY_ERR rc; |
| 6654 | |
| 6655 | /* unary '-' */ |
| 6656 | if (!set2 && (op[0] == '-')) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6657 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6658 | LY_CHECK_RET(rc); |
| 6659 | set1->val.num *= -1; |
| 6660 | lyxp_set_free(set2); |
| 6661 | return LY_SUCCESS; |
| 6662 | } |
| 6663 | |
| 6664 | assert(set1 && set2); |
| 6665 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6666 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6667 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6668 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6669 | LY_CHECK_RET(rc); |
| 6670 | |
| 6671 | switch (op[0]) { |
| 6672 | /* '+' */ |
| 6673 | case '+': |
| 6674 | set1->val.num += set2->val.num; |
| 6675 | break; |
| 6676 | |
| 6677 | /* '-' */ |
| 6678 | case '-': |
| 6679 | set1->val.num -= set2->val.num; |
| 6680 | break; |
| 6681 | |
| 6682 | /* '*' */ |
| 6683 | case '*': |
| 6684 | set1->val.num *= set2->val.num; |
| 6685 | break; |
| 6686 | |
| 6687 | /* 'div' */ |
| 6688 | case 'd': |
| 6689 | set1->val.num /= set2->val.num; |
| 6690 | break; |
| 6691 | |
| 6692 | /* 'mod' */ |
| 6693 | case 'm': |
| 6694 | set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num); |
| 6695 | break; |
| 6696 | |
| 6697 | default: |
| 6698 | LOGINT_RET(set1->ctx); |
| 6699 | } |
| 6700 | |
| 6701 | return LY_SUCCESS; |
| 6702 | } |
| 6703 | |
| 6704 | /* |
| 6705 | * eval functions |
| 6706 | * |
| 6707 | * They execute a parsed XPath expression on some data subtree. |
| 6708 | */ |
| 6709 | |
| 6710 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6711 | * @brief Evaluate Predicate. Logs directly on error. |
| 6712 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6713 | * [9] Predicate ::= '[' Expr ']' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6714 | * |
| 6715 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6716 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6717 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6718 | * @param[in] options XPath options. |
| 6719 | * @param[in] parent_pos_pred Whether parent predicate was a positional one. |
| 6720 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6721 | */ |
| 6722 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 6723 | 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] | 6724 | { |
| 6725 | LY_ERR rc; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 6726 | uint16_t i, orig_exp; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6727 | uint32_t orig_pos, orig_size; |
| 6728 | int32_t pred_in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6729 | struct lyxp_set set2; |
| 6730 | struct lyd_node *orig_parent; |
| 6731 | |
| 6732 | /* '[' */ |
| 6733 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6734 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6735 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6736 | |
| 6737 | if (!set) { |
| 6738 | only_parse: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6739 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6740 | LY_CHECK_RET(rc); |
| 6741 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 6742 | /* 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] | 6743 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6744 | |
| 6745 | /* empty set, nothing to evaluate */ |
| 6746 | if (!set->used) { |
| 6747 | goto only_parse; |
| 6748 | } |
| 6749 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6750 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6751 | orig_pos = 0; |
| 6752 | orig_size = set->used; |
| 6753 | orig_parent = NULL; |
Michal Vasko | 39dbf35 | 2020-05-21 10:08:59 +0200 | [diff] [blame] | 6754 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6755 | set_init(&set2, set); |
| 6756 | set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0); |
| 6757 | /* remember the node context position for position() and context size for last(), |
| 6758 | * predicates should always be evaluated with respect to the child axis (since we do |
| 6759 | * not support explicit axes) so we assign positions based on their parents */ |
| 6760 | if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) { |
| 6761 | orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent; |
| 6762 | orig_pos = 1; |
| 6763 | } else { |
| 6764 | ++orig_pos; |
| 6765 | } |
| 6766 | |
| 6767 | set2.ctx_pos = orig_pos; |
| 6768 | set2.ctx_size = orig_size; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6769 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6770 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6771 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6772 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6773 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6774 | return rc; |
| 6775 | } |
| 6776 | |
| 6777 | /* number is a position */ |
| 6778 | if (set2.type == LYXP_SET_NUMBER) { |
| 6779 | if ((long long)set2.val.num == orig_pos) { |
| 6780 | set2.val.num = 1; |
| 6781 | } else { |
| 6782 | set2.val.num = 0; |
| 6783 | } |
| 6784 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6785 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6786 | |
| 6787 | /* predicate satisfied or not? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6788 | if (!set2.val.bln) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6789 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6790 | } |
| 6791 | } |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6792 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6793 | |
| 6794 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 6795 | for (i = 0; i < set->used; ++i) { |
| 6796 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6797 | /* there is a currently-valid node */ |
| 6798 | break; |
| 6799 | } |
| 6800 | } |
| 6801 | /* empty set, nothing to evaluate */ |
| 6802 | if (i == set->used) { |
| 6803 | goto only_parse; |
| 6804 | } |
| 6805 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6806 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6807 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6808 | /* set special in_ctx to all the valid snodes */ |
| 6809 | pred_in_ctx = set_scnode_new_in_ctx(set); |
| 6810 | |
| 6811 | /* use the valid snodes one-by-one */ |
| 6812 | for (i = 0; i < set->used; ++i) { |
| 6813 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
| 6814 | continue; |
| 6815 | } |
| 6816 | set->val.scnodes[i].in_ctx = 1; |
| 6817 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6818 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6819 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6820 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6821 | LY_CHECK_RET(rc); |
| 6822 | |
| 6823 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
| 6824 | } |
| 6825 | |
| 6826 | /* restore the state as it was before the predicate */ |
| 6827 | for (i = 0; i < set->used; ++i) { |
| 6828 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6829 | set->val.scnodes[i].in_ctx = 0; |
| 6830 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
| 6831 | set->val.scnodes[i].in_ctx = 1; |
| 6832 | } |
| 6833 | } |
| 6834 | |
| 6835 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6836 | set2.type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6837 | set_fill_set(&set2, set); |
| 6838 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6839 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6840 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6841 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6842 | return rc; |
| 6843 | } |
| 6844 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6845 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6846 | if (!set2.val.bln) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6847 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6848 | } |
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 | } |
| 6851 | |
| 6852 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6853 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6854 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6855 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6856 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6857 | |
| 6858 | return LY_SUCCESS; |
| 6859 | } |
| 6860 | |
| 6861 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6862 | * @brief Evaluate Literal. Logs directly on error. |
| 6863 | * |
| 6864 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6865 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6866 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6867 | */ |
| 6868 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 6869 | 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] | 6870 | { |
| 6871 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6872 | if (exp->tok_len[*tok_idx] == 2) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6873 | set_fill_string(set, "", 0); |
| 6874 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6875 | 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] | 6876 | } |
| 6877 | } |
| 6878 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6879 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6880 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6881 | } |
| 6882 | |
| 6883 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6884 | * @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] | 6885 | * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6886 | * @param[in] exp Full parsed XPath expression. |
| 6887 | * @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] | 6888 | * @param[in] ctx_node Found schema node as the context for the predicate. |
| 6889 | * @param[in] cur_mod Current module for the expression. |
| 6890 | * @param[in] cur_node Current (original context) node. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6891 | * @param[in] format Format of any prefixes in key names/values. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6892 | * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix). |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6893 | * @param[out] predicates Parsed predicates. |
| 6894 | * @param[out] pred_type Type of @p predicates. |
| 6895 | * @return LY_SUCCESS on success, |
| 6896 | * @return LY_ERR on any error. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6897 | */ |
| 6898 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 6899 | 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] | 6900 | const struct lys_module *cur_mod, const struct lysc_node *cur_node, LY_PREFIX_FORMAT format, void *prefix_data, |
| 6901 | struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6902 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6903 | LY_ERR ret = LY_SUCCESS; |
| 6904 | uint16_t key_count, e_idx, pred_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6905 | const struct lysc_node *key; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6906 | size_t pred_len; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6907 | uint32_t prev_lo; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6908 | struct lyxp_expr *exp2 = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6909 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6910 | assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6911 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6912 | if (ctx_node->nodetype == LYS_LIST) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6913 | /* get key count */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6914 | if (ctx_node->flags & LYS_KEYLESS) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6915 | return LY_EINVAL; |
| 6916 | } |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6917 | 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] | 6918 | assert(key_count); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6919 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6920 | /* learn where the predicates end */ |
| 6921 | e_idx = *tok_idx; |
| 6922 | while (key_count) { |
| 6923 | /* '[' */ |
| 6924 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6925 | return LY_EINVAL; |
| 6926 | } |
| 6927 | ++e_idx; |
| 6928 | |
| 6929 | /* ']' */ |
| 6930 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6931 | ++e_idx; |
| 6932 | } |
| 6933 | ++e_idx; |
| 6934 | |
| 6935 | /* another presumably key predicate parsed */ |
| 6936 | --key_count; |
| 6937 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6938 | } else { |
| 6939 | /* learn just where this single predicate ends */ |
| 6940 | e_idx = *tok_idx; |
| 6941 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6942 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6943 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6944 | return LY_EINVAL; |
| 6945 | } |
| 6946 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6947 | |
| 6948 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6949 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6950 | ++e_idx; |
| 6951 | } |
| 6952 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6953 | } |
| 6954 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6955 | /* get the joined length of all the keys predicates/of the single leaf-list predicate */ |
| 6956 | pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx]; |
| 6957 | |
| 6958 | /* turn logging off */ |
| 6959 | prev_lo = ly_log_options(0); |
| 6960 | |
| 6961 | /* parse the predicate(s) */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6962 | LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx], |
| 6963 | pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6964 | |
| 6965 | /* compile */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6966 | ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format, |
| 6967 | prefix_data, predicates, pred_type); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6968 | LY_CHECK_GOTO(ret, cleanup); |
| 6969 | |
| 6970 | /* success, the predicate must include all the needed information for hash-based search */ |
| 6971 | *tok_idx = e_idx; |
| 6972 | |
| 6973 | cleanup: |
| 6974 | ly_log_options(prev_lo); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6975 | lyxp_expr_free(ctx_node->module->ctx, exp2); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6976 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6977 | } |
| 6978 | |
| 6979 | /** |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6980 | * @brief Search for/check the next schema node that could be the only matching schema node meaning the |
| 6981 | * data node(s) could be found using a single hash-based search. |
| 6982 | * |
| 6983 | * @param[in] node Next context node to check. |
| 6984 | * @param[in] name Expected node name. |
| 6985 | * @param[in] name_len Length of @p name. |
| 6986 | * @param[in] moveto_mod Expected node module. |
| 6987 | * @param[in] root_type XPath root type. |
| 6988 | * @param[in] no_prefix Whether the node name was unprefixed. |
| 6989 | * @param[in] format Prefix format. |
| 6990 | * @param[in,out] found Previously found node, is updated. |
| 6991 | * @return LY_SUCCESS on success, |
| 6992 | * @return LY_ENOT if the whole check failed and hashes cannot be used. |
| 6993 | */ |
| 6994 | static LY_ERR |
| 6995 | eval_name_test_with_predicate_get_scnode(const struct lyd_node *node, const char *name, uint16_t name_len, |
| 6996 | const struct lys_module *moveto_mod, enum lyxp_node_type root_type, ly_bool no_prefix, LY_PREFIX_FORMAT format, |
| 6997 | const struct lysc_node **found) |
| 6998 | { |
| 6999 | const struct lysc_node *scnode; |
| 7000 | const struct lys_module *mod; |
| 7001 | uint32_t idx = 0; |
| 7002 | |
| 7003 | continue_search: |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7004 | scnode = NULL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7005 | if (!node) { |
| 7006 | if (no_prefix && (format == LY_PREF_JSON)) { |
| 7007 | /* search all modules for a single match */ |
| 7008 | while ((mod = ly_ctx_get_module_iter(moveto_mod->ctx, &idx))) { |
| 7009 | scnode = lys_find_child(NULL, mod, name, name_len, 0, 0); |
| 7010 | if (scnode) { |
| 7011 | /* we have found a match */ |
| 7012 | break; |
| 7013 | } |
| 7014 | } |
| 7015 | |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7016 | if (!scnode) { |
| 7017 | /* all modules searched */ |
| 7018 | idx = 0; |
| 7019 | } |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7020 | } else { |
| 7021 | /* search in top-level */ |
| 7022 | scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0); |
| 7023 | } |
| 7024 | } else if (!*found || (lysc_data_parent(*found) != node->schema)) { |
| 7025 | if (no_prefix && (format == LY_PREF_JSON)) { |
| 7026 | /* we must adjust the module to inherit the one from the context node */ |
| 7027 | moveto_mod = node->schema->module; |
| 7028 | } |
| 7029 | |
| 7030 | /* search in children, do not repeat the same search */ |
| 7031 | 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] | 7032 | } /* else skip redundant search */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7033 | |
| 7034 | /* additional context check */ |
| 7035 | if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 7036 | scnode = NULL; |
| 7037 | } |
| 7038 | |
| 7039 | if (scnode) { |
| 7040 | if (*found) { |
| 7041 | /* we found a schema node with the same name but at different level, give up, too complicated |
| 7042 | * (more hash-based searches would be required, not supported) */ |
| 7043 | return LY_ENOT; |
| 7044 | } else { |
| 7045 | /* remember the found schema node and continue to make sure it can be used */ |
| 7046 | *found = scnode; |
| 7047 | } |
| 7048 | } |
| 7049 | |
| 7050 | if (idx) { |
| 7051 | /* continue searching all the following models */ |
| 7052 | goto continue_search; |
| 7053 | } |
| 7054 | |
| 7055 | return LY_SUCCESS; |
| 7056 | } |
| 7057 | |
| 7058 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7059 | * @brief Evaluate NameTest and any following Predicates. Logs directly on error. |
| 7060 | * |
| 7061 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7062 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7063 | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
| 7064 | * |
| 7065 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7066 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7067 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 7068 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7069 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7070 | * @param[in] options XPath options. |
| 7071 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7072 | */ |
| 7073 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7074 | 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] | 7075 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7076 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7077 | char *path; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7078 | const char *ncname, *ncname_dict = NULL; |
| 7079 | uint16_t ncname_len; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7080 | const struct lys_module *moveto_mod = NULL; |
| 7081 | const struct lysc_node *scnode = NULL, *ctx_scnode; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7082 | struct ly_path_predicate *predicates = NULL; |
| 7083 | enum ly_path_pred_type pred_type = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7084 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7085 | ly_bool no_prefix; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7086 | |
| 7087 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7088 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7089 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7090 | |
| 7091 | if (!set) { |
| 7092 | goto moveto; |
| 7093 | } |
| 7094 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7095 | ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]]; |
| 7096 | ncname_len = exp->tok_len[*tok_idx - 1]; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7097 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7098 | /* get the first schema context node */ |
| 7099 | if (set->used) { |
| 7100 | if (set->type == LYXP_SET_NODE_SET) { |
| 7101 | ctx_scnode = set->val.nodes[0].node ? set->val.nodes[0].node->schema : NULL; |
| 7102 | } else { |
| 7103 | ctx_scnode = set->val.scnodes[0].scnode; |
| 7104 | } |
| 7105 | } else { |
| 7106 | ctx_scnode = NULL; |
| 7107 | } |
| 7108 | |
| 7109 | /* parse (and skip) module name, use any context node (if available) just so we get some moveto_mod if there |
| 7110 | * is no prefix for ::LY_PREF_JSON */ |
| 7111 | rc = moveto_resolve_model(&ncname, &ncname_len, set, ctx_scnode, &moveto_mod); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7112 | LY_CHECK_GOTO(rc, cleanup); |
| 7113 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7114 | if (ncname_len == exp->tok_len[*tok_idx - 1]) { |
| 7115 | /* remember that there is no prefix */ |
| 7116 | no_prefix = 1; |
| 7117 | } else { |
| 7118 | no_prefix = 0; |
| 7119 | } |
| 7120 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7121 | if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) { |
| 7122 | /* find the matching schema node in some parent in the context */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7123 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7124 | if (eval_name_test_with_predicate_get_scnode(set->val.nodes[i].node, ncname, ncname_len, moveto_mod, |
| 7125 | set->root_type, no_prefix, set->format, &scnode)) { |
| 7126 | /* check failed */ |
| 7127 | scnode = NULL; |
| 7128 | break; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7129 | } |
| 7130 | } |
| 7131 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7132 | if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 7133 | /* try to create the predicates */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7134 | if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ? |
| 7135 | 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] | 7136 | /* hashes cannot be used */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7137 | scnode = NULL; |
| 7138 | } |
| 7139 | } |
| 7140 | } |
| 7141 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7142 | if (!scnode && moveto_mod) { |
| 7143 | /* we are not able to match based on a schema node and not all the modules match, |
| 7144 | * use dictionary for efficient comparison */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 7145 | 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] | 7146 | } |
| 7147 | |
| 7148 | moveto: |
| 7149 | /* move to the attribute(s), data node(s), or schema node(s) */ |
| 7150 | if (attr_axis) { |
| 7151 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7152 | set_scnode_clear_ctx(set); |
| 7153 | } else { |
| 7154 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7155 | rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7156 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7157 | rc = moveto_attr(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7158 | } |
| 7159 | LY_CHECK_GOTO(rc, cleanup); |
| 7160 | } |
| 7161 | } else { |
| 7162 | if (set && (options & LYXP_SCNODE_ALL)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7163 | int64_t i; |
| 7164 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7165 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7166 | rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7167 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7168 | rc = moveto_scnode(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7169 | } |
| 7170 | LY_CHECK_GOTO(rc, cleanup); |
| 7171 | |
| 7172 | for (i = set->used - 1; i > -1; --i) { |
| 7173 | if (set->val.scnodes[i].in_ctx > 0) { |
| 7174 | break; |
| 7175 | } |
| 7176 | } |
| 7177 | if (i == -1) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7178 | path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7179 | 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] | 7180 | ncname_len, ncname, ncname - exp->expr, exp->expr, path); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7181 | free(path); |
| 7182 | } |
| 7183 | } else { |
| 7184 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7185 | rc = moveto_node_alldesc(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7186 | } else { |
| 7187 | if (scnode) { |
| 7188 | /* we can find the nodes using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7189 | rc = moveto_node_hash(set, scnode, predicates); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7190 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7191 | rc = moveto_node(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7192 | } |
| 7193 | } |
| 7194 | LY_CHECK_GOTO(rc, cleanup); |
| 7195 | } |
| 7196 | } |
| 7197 | |
| 7198 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7199 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7200 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7201 | LY_CHECK_RET(rc); |
| 7202 | } |
| 7203 | |
| 7204 | cleanup: |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7205 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7206 | lydict_remove(set->ctx, ncname_dict); |
Michal Vasko | f7e16e2 | 2020-10-21 09:24:39 +0200 | [diff] [blame] | 7207 | ly_path_predicates_free(set->ctx, pred_type, predicates); |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7208 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7209 | return rc; |
| 7210 | } |
| 7211 | |
| 7212 | /** |
| 7213 | * @brief Evaluate NodeType and any following Predicates. Logs directly on error. |
| 7214 | * |
| 7215 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7216 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7217 | * [8] NodeType ::= 'text' | 'node' |
| 7218 | * |
| 7219 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7220 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7221 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 7222 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7223 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7224 | * @param[in] options XPath options. |
| 7225 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7226 | */ |
| 7227 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7228 | 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] | 7229 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7230 | { |
| 7231 | LY_ERR rc; |
| 7232 | |
| 7233 | /* TODO */ |
| 7234 | (void)attr_axis; |
| 7235 | (void)all_desc; |
| 7236 | |
| 7237 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7238 | assert(exp->tok_len[*tok_idx] == 4); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7239 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 7240 | set_scnode_clear_ctx(set); |
| 7241 | /* just for the debug message below */ |
| 7242 | set = NULL; |
| 7243 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7244 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7245 | rc = xpath_node(NULL, 0, set, options); |
| 7246 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7247 | assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7248 | rc = xpath_text(NULL, 0, set, options); |
| 7249 | } |
| 7250 | LY_CHECK_RET(rc); |
| 7251 | } |
| 7252 | } |
| 7253 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7254 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7255 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7256 | |
| 7257 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7258 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7259 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7260 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7261 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7262 | |
| 7263 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7264 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7265 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7266 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7267 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7268 | |
| 7269 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7270 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7271 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7272 | LY_CHECK_RET(rc); |
| 7273 | } |
| 7274 | |
| 7275 | return LY_SUCCESS; |
| 7276 | } |
| 7277 | |
| 7278 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7279 | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
| 7280 | * |
| 7281 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 7282 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7283 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7284 | * |
| 7285 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7286 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7287 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7288 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7289 | * @param[in] options XPath options. |
| 7290 | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
| 7291 | */ |
| 7292 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7293 | eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set, |
| 7294 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7295 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7296 | ly_bool attr_axis; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7297 | LY_ERR rc; |
| 7298 | |
| 7299 | goto step; |
| 7300 | do { |
| 7301 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7302 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7303 | all_desc = 0; |
| 7304 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7305 | assert(exp->tok_len[*tok_idx] == 2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7306 | all_desc = 1; |
| 7307 | } |
| 7308 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7309 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7310 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7311 | |
| 7312 | step: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7313 | /* evaluate abbreviated axis '@'? if any */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7314 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7315 | attr_axis = 1; |
| 7316 | |
| 7317 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7318 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7319 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7320 | } else { |
| 7321 | attr_axis = 0; |
| 7322 | } |
| 7323 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7324 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7325 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7326 | case LYXP_TOKEN_DOT: |
| 7327 | /* evaluate '.' */ |
| 7328 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7329 | rc = moveto_scnode_self(set, all_desc, options); |
| 7330 | } else { |
| 7331 | rc = moveto_self(set, all_desc, options); |
| 7332 | } |
| 7333 | LY_CHECK_RET(rc); |
| 7334 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7335 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7336 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7337 | break; |
| 7338 | |
| 7339 | case LYXP_TOKEN_DDOT: |
| 7340 | /* evaluate '..' */ |
| 7341 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7342 | rc = moveto_scnode_parent(set, all_desc, options); |
| 7343 | } else { |
| 7344 | rc = moveto_parent(set, all_desc, options); |
| 7345 | } |
| 7346 | LY_CHECK_RET(rc); |
| 7347 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7348 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7349 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7350 | break; |
| 7351 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7352 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7353 | /* evaluate NameTest Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7354 | 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] | 7355 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7356 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7357 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7358 | case LYXP_TOKEN_NODETYPE: |
| 7359 | /* evaluate NodeType Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7360 | 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] | 7361 | LY_CHECK_RET(rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7362 | break; |
| 7363 | |
| 7364 | default: |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 7365 | LOGINT_RET(set ? set->ctx : NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7366 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7367 | } 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] | 7368 | |
| 7369 | return LY_SUCCESS; |
| 7370 | } |
| 7371 | |
| 7372 | /** |
| 7373 | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
| 7374 | * |
| 7375 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 7376 | * |
| 7377 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7378 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7379 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7380 | * @param[in] options XPath options. |
| 7381 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7382 | */ |
| 7383 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7384 | 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] | 7385 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7386 | ly_bool all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7387 | |
| 7388 | if (set) { |
| 7389 | /* no matter what tokens follow, we need to be at the root */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7390 | LY_CHECK_RET(moveto_root(set, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7391 | } |
| 7392 | |
| 7393 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7394 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7395 | /* evaluate '/' - deferred */ |
| 7396 | all_desc = 0; |
| 7397 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7398 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7399 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7400 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7401 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7402 | return LY_SUCCESS; |
| 7403 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7404 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7405 | case LYXP_TOKEN_DOT: |
| 7406 | case LYXP_TOKEN_DDOT: |
| 7407 | case LYXP_TOKEN_AT: |
| 7408 | case LYXP_TOKEN_NAMETEST: |
| 7409 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7410 | 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] | 7411 | break; |
| 7412 | default: |
| 7413 | break; |
| 7414 | } |
| 7415 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7416 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 7417 | /* '//' RelativeLocationPath */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7418 | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
| 7419 | all_desc = 1; |
| 7420 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7421 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7422 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7423 | |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7424 | 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] | 7425 | } |
| 7426 | |
| 7427 | return LY_SUCCESS; |
| 7428 | } |
| 7429 | |
| 7430 | /** |
| 7431 | * @brief Evaluate FunctionCall. Logs directly on error. |
| 7432 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7433 | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7434 | * |
| 7435 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7436 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7437 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7438 | * @param[in] options XPath options. |
| 7439 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7440 | */ |
| 7441 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7442 | 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] | 7443 | { |
| 7444 | LY_ERR rc; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7445 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7446 | 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] | 7447 | uint16_t arg_count = 0, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7448 | struct lyxp_set **args = NULL, **args_aux; |
| 7449 | |
| 7450 | if (set) { |
| 7451 | /* FunctionName */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7452 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7453 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7454 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7455 | xpath_func = &xpath_not; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7456 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7457 | xpath_func = &xpath_sum; |
| 7458 | } |
| 7459 | break; |
| 7460 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7461 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7462 | xpath_func = &xpath_lang; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7463 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7464 | xpath_func = &xpath_last; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7465 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7466 | xpath_func = &xpath_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7467 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7468 | xpath_func = &xpath_true; |
| 7469 | } |
| 7470 | break; |
| 7471 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7472 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7473 | xpath_func = &xpath_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7474 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7475 | xpath_func = &xpath_false; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7476 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7477 | xpath_func = &xpath_floor; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7478 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7479 | xpath_func = &xpath_round; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7480 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7481 | xpath_func = &xpath_deref; |
| 7482 | } |
| 7483 | break; |
| 7484 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7485 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7486 | xpath_func = &xpath_concat; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7487 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7488 | xpath_func = &xpath_number; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7489 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7490 | xpath_func = &xpath_string; |
| 7491 | } |
| 7492 | break; |
| 7493 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7494 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7495 | xpath_func = &xpath_boolean; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7496 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7497 | xpath_func = &xpath_ceiling; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7498 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7499 | xpath_func = &xpath_current; |
| 7500 | } |
| 7501 | break; |
| 7502 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7503 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7504 | xpath_func = &xpath_contains; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7505 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7506 | xpath_func = &xpath_position; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7507 | } 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] | 7508 | xpath_func = &xpath_re_match; |
| 7509 | } |
| 7510 | break; |
| 7511 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7512 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7513 | xpath_func = &xpath_substring; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7514 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7515 | xpath_func = &xpath_translate; |
| 7516 | } |
| 7517 | break; |
| 7518 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7519 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7520 | xpath_func = &xpath_local_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7521 | } 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] | 7522 | xpath_func = &xpath_enum_value; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7523 | } 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] | 7524 | xpath_func = &xpath_bit_is_set; |
| 7525 | } |
| 7526 | break; |
| 7527 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7528 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7529 | xpath_func = &xpath_starts_with; |
| 7530 | } |
| 7531 | break; |
| 7532 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7533 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7534 | xpath_func = &xpath_derived_from; |
| 7535 | } |
| 7536 | break; |
| 7537 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7538 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7539 | xpath_func = &xpath_namespace_uri; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7540 | } 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] | 7541 | xpath_func = &xpath_string_length; |
| 7542 | } |
| 7543 | break; |
| 7544 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7545 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7546 | xpath_func = &xpath_normalize_space; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7547 | } 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] | 7548 | xpath_func = &xpath_substring_after; |
| 7549 | } |
| 7550 | break; |
| 7551 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7552 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7553 | xpath_func = &xpath_substring_before; |
| 7554 | } |
| 7555 | break; |
| 7556 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7557 | 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] | 7558 | xpath_func = &xpath_derived_from_or_self; |
| 7559 | } |
| 7560 | break; |
| 7561 | } |
| 7562 | |
| 7563 | if (!xpath_func) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7564 | 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] | 7565 | return LY_EVALID; |
| 7566 | } |
| 7567 | } |
| 7568 | |
| 7569 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7570 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7571 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7572 | |
| 7573 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7574 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7575 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7576 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7577 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7578 | |
| 7579 | /* ( Expr ( ',' Expr )* )? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7580 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7581 | if (set) { |
| 7582 | args = malloc(sizeof *args); |
| 7583 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7584 | arg_count = 1; |
| 7585 | args[0] = set_copy(set); |
| 7586 | if (!args[0]) { |
| 7587 | rc = LY_EMEM; |
| 7588 | goto cleanup; |
| 7589 | } |
| 7590 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7591 | rc = eval_expr_select(exp, tok_idx, 0, args[0], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7592 | LY_CHECK_GOTO(rc, cleanup); |
| 7593 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7594 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7595 | LY_CHECK_GOTO(rc, cleanup); |
| 7596 | } |
| 7597 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7598 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7599 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7600 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7601 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7602 | |
| 7603 | if (set) { |
| 7604 | ++arg_count; |
| 7605 | args_aux = realloc(args, arg_count * sizeof *args); |
| 7606 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7607 | args = args_aux; |
| 7608 | args[arg_count - 1] = set_copy(set); |
| 7609 | if (!args[arg_count - 1]) { |
| 7610 | rc = LY_EMEM; |
| 7611 | goto cleanup; |
| 7612 | } |
| 7613 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7614 | 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] | 7615 | LY_CHECK_GOTO(rc, cleanup); |
| 7616 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7617 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7618 | LY_CHECK_GOTO(rc, cleanup); |
| 7619 | } |
| 7620 | } |
| 7621 | |
| 7622 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7623 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7624 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7625 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7626 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7627 | |
| 7628 | if (set) { |
| 7629 | /* evaluate function */ |
| 7630 | rc = xpath_func(args, arg_count, set, options); |
| 7631 | |
| 7632 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7633 | /* merge all nodes from arg evaluations */ |
| 7634 | for (i = 0; i < arg_count; ++i) { |
| 7635 | set_scnode_clear_ctx(args[i]); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7636 | lyxp_set_scnode_merge(set, args[i]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7637 | } |
| 7638 | } |
| 7639 | } else { |
| 7640 | rc = LY_SUCCESS; |
| 7641 | } |
| 7642 | |
| 7643 | cleanup: |
| 7644 | for (i = 0; i < arg_count; ++i) { |
| 7645 | lyxp_set_free(args[i]); |
| 7646 | } |
| 7647 | free(args); |
| 7648 | |
| 7649 | return rc; |
| 7650 | } |
| 7651 | |
| 7652 | /** |
| 7653 | * @brief Evaluate Number. Logs directly on error. |
| 7654 | * |
| 7655 | * @param[in] ctx Context for errors. |
| 7656 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7657 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7658 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7659 | * @return LY_ERR |
| 7660 | */ |
| 7661 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7662 | 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] | 7663 | { |
| 7664 | long double num; |
| 7665 | char *endptr; |
| 7666 | |
| 7667 | if (set) { |
| 7668 | errno = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7669 | num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7670 | if (errno) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7671 | LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 7672 | 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] | 7673 | 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] | 7674 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7675 | } 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] | 7676 | LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 7677 | LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7678 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7679 | return LY_EVALID; |
| 7680 | } |
| 7681 | |
| 7682 | set_fill_number(set, num); |
| 7683 | } |
| 7684 | |
| 7685 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7686 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7687 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7688 | return LY_SUCCESS; |
| 7689 | } |
| 7690 | |
| 7691 | /** |
| 7692 | * @brief Evaluate PathExpr. Logs directly on error. |
| 7693 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7694 | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7695 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 7696 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 7697 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7698 | * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7699 | * |
| 7700 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7701 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7702 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7703 | * @param[in] options XPath options. |
| 7704 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7705 | */ |
| 7706 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7707 | 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] | 7708 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7709 | ly_bool all_desc, parent_pos_pred; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7710 | LY_ERR rc; |
| 7711 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7712 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7713 | case LYXP_TOKEN_PAR1: |
| 7714 | /* '(' Expr ')' */ |
| 7715 | |
| 7716 | /* '(' */ |
| 7717 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7718 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7719 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7720 | |
| 7721 | /* Expr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7722 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7723 | LY_CHECK_RET(rc); |
| 7724 | |
| 7725 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7726 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7727 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7728 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7729 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7730 | |
| 7731 | parent_pos_pred = 0; |
| 7732 | goto predicate; |
| 7733 | |
| 7734 | case LYXP_TOKEN_DOT: |
| 7735 | case LYXP_TOKEN_DDOT: |
| 7736 | case LYXP_TOKEN_AT: |
| 7737 | case LYXP_TOKEN_NAMETEST: |
| 7738 | case LYXP_TOKEN_NODETYPE: |
| 7739 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7740 | rc = eval_relative_location_path(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7741 | LY_CHECK_RET(rc); |
| 7742 | break; |
| 7743 | |
| 7744 | case LYXP_TOKEN_FUNCNAME: |
| 7745 | /* FunctionCall */ |
| 7746 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7747 | rc = eval_function_call(exp, tok_idx, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7748 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7749 | rc = eval_function_call(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7750 | } |
| 7751 | LY_CHECK_RET(rc); |
| 7752 | |
| 7753 | parent_pos_pred = 1; |
| 7754 | goto predicate; |
| 7755 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 7756 | case LYXP_TOKEN_OPER_PATH: |
| 7757 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7758 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7759 | rc = eval_absolute_location_path(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7760 | LY_CHECK_RET(rc); |
| 7761 | break; |
| 7762 | |
| 7763 | case LYXP_TOKEN_LITERAL: |
| 7764 | /* Literal */ |
| 7765 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7766 | if (set) { |
| 7767 | set_scnode_clear_ctx(set); |
| 7768 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7769 | eval_literal(exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7770 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7771 | eval_literal(exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7772 | } |
| 7773 | |
| 7774 | parent_pos_pred = 1; |
| 7775 | goto predicate; |
| 7776 | |
| 7777 | case LYXP_TOKEN_NUMBER: |
| 7778 | /* Number */ |
| 7779 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7780 | if (set) { |
| 7781 | set_scnode_clear_ctx(set); |
| 7782 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7783 | rc = eval_number(NULL, exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7784 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7785 | rc = eval_number(set->ctx, exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7786 | } |
| 7787 | LY_CHECK_RET(rc); |
| 7788 | |
| 7789 | parent_pos_pred = 1; |
| 7790 | goto predicate; |
| 7791 | |
| 7792 | default: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7793 | 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] | 7794 | &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7795 | return LY_EVALID; |
| 7796 | } |
| 7797 | |
| 7798 | return LY_SUCCESS; |
| 7799 | |
| 7800 | predicate: |
| 7801 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7802 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7803 | rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7804 | LY_CHECK_RET(rc); |
| 7805 | } |
| 7806 | |
| 7807 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7808 | 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] | 7809 | |
| 7810 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7811 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7812 | all_desc = 0; |
| 7813 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7814 | all_desc = 1; |
| 7815 | } |
| 7816 | |
| 7817 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7818 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7819 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7820 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7821 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7822 | LY_CHECK_RET(rc); |
| 7823 | } |
| 7824 | |
| 7825 | return LY_SUCCESS; |
| 7826 | } |
| 7827 | |
| 7828 | /** |
| 7829 | * @brief Evaluate UnionExpr. Logs directly on error. |
| 7830 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7831 | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7832 | * |
| 7833 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7834 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7835 | * @param[in] repeat How many times this expression is repeated. |
| 7836 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7837 | * @param[in] options XPath options. |
| 7838 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7839 | */ |
| 7840 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7841 | 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] | 7842 | { |
| 7843 | LY_ERR rc = LY_SUCCESS; |
| 7844 | struct lyxp_set orig_set, set2; |
| 7845 | uint16_t i; |
| 7846 | |
| 7847 | assert(repeat); |
| 7848 | |
| 7849 | set_init(&orig_set, set); |
| 7850 | set_init(&set2, set); |
| 7851 | |
| 7852 | set_fill_set(&orig_set, set); |
| 7853 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7854 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7855 | LY_CHECK_GOTO(rc, cleanup); |
| 7856 | |
| 7857 | /* ('|' PathExpr)* */ |
| 7858 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7859 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7860 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7861 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7862 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7863 | |
| 7864 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7865 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7866 | LY_CHECK_GOTO(rc, cleanup); |
| 7867 | continue; |
| 7868 | } |
| 7869 | |
| 7870 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7871 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7872 | LY_CHECK_GOTO(rc, cleanup); |
| 7873 | |
| 7874 | /* eval */ |
| 7875 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7876 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7877 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7878 | rc = moveto_union(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7879 | LY_CHECK_GOTO(rc, cleanup); |
| 7880 | } |
| 7881 | } |
| 7882 | |
| 7883 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7884 | lyxp_set_free_content(&orig_set); |
| 7885 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7886 | return rc; |
| 7887 | } |
| 7888 | |
| 7889 | /** |
| 7890 | * @brief Evaluate UnaryExpr. Logs directly on error. |
| 7891 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7892 | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7893 | * |
| 7894 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7895 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7896 | * @param[in] repeat How many times this expression is repeated. |
| 7897 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7898 | * @param[in] options XPath options. |
| 7899 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7900 | */ |
| 7901 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7902 | 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] | 7903 | { |
| 7904 | LY_ERR rc; |
| 7905 | uint16_t this_op, i; |
| 7906 | |
| 7907 | assert(repeat); |
| 7908 | |
| 7909 | /* ('-')+ */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7910 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7911 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7912 | 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] | 7913 | |
| 7914 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7915 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7916 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7917 | } |
| 7918 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7919 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7920 | LY_CHECK_RET(rc); |
| 7921 | |
| 7922 | if (set && (repeat % 2)) { |
| 7923 | if (options & LYXP_SCNODE_ALL) { |
| 7924 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
| 7925 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7926 | 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] | 7927 | LY_CHECK_RET(rc); |
| 7928 | } |
| 7929 | } |
| 7930 | |
| 7931 | return LY_SUCCESS; |
| 7932 | } |
| 7933 | |
| 7934 | /** |
| 7935 | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
| 7936 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7937 | * [18] MultiplicativeExpr ::= UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7938 | * | MultiplicativeExpr '*' UnaryExpr |
| 7939 | * | MultiplicativeExpr 'div' UnaryExpr |
| 7940 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 7941 | * |
| 7942 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7943 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7944 | * @param[in] repeat How many times this expression is repeated. |
| 7945 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7946 | * @param[in] options XPath options. |
| 7947 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7948 | */ |
| 7949 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7950 | eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, |
| 7951 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7952 | { |
| 7953 | LY_ERR rc; |
| 7954 | uint16_t this_op; |
| 7955 | struct lyxp_set orig_set, set2; |
| 7956 | uint16_t i; |
| 7957 | |
| 7958 | assert(repeat); |
| 7959 | |
| 7960 | set_init(&orig_set, set); |
| 7961 | set_init(&set2, set); |
| 7962 | |
| 7963 | set_fill_set(&orig_set, set); |
| 7964 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7965 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7966 | LY_CHECK_GOTO(rc, cleanup); |
| 7967 | |
| 7968 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 7969 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7970 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7971 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7972 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7973 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7974 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7975 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7976 | |
| 7977 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7978 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7979 | LY_CHECK_GOTO(rc, cleanup); |
| 7980 | continue; |
| 7981 | } |
| 7982 | |
| 7983 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7984 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7985 | LY_CHECK_GOTO(rc, cleanup); |
| 7986 | |
| 7987 | /* eval */ |
| 7988 | if (options & LYXP_SCNODE_ALL) { |
| 7989 | 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] | 7990 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7991 | set_scnode_clear_ctx(set); |
| 7992 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7993 | 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] | 7994 | LY_CHECK_GOTO(rc, cleanup); |
| 7995 | } |
| 7996 | } |
| 7997 | |
| 7998 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7999 | lyxp_set_free_content(&orig_set); |
| 8000 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8001 | return rc; |
| 8002 | } |
| 8003 | |
| 8004 | /** |
| 8005 | * @brief Evaluate AdditiveExpr. Logs directly on error. |
| 8006 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8007 | * [17] AdditiveExpr ::= MultiplicativeExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8008 | * | AdditiveExpr '+' MultiplicativeExpr |
| 8009 | * | AdditiveExpr '-' MultiplicativeExpr |
| 8010 | * |
| 8011 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8012 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8013 | * @param[in] repeat How many times this expression is repeated. |
| 8014 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8015 | * @param[in] options XPath options. |
| 8016 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8017 | */ |
| 8018 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8019 | 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] | 8020 | { |
| 8021 | LY_ERR rc; |
| 8022 | uint16_t this_op; |
| 8023 | struct lyxp_set orig_set, set2; |
| 8024 | uint16_t i; |
| 8025 | |
| 8026 | assert(repeat); |
| 8027 | |
| 8028 | set_init(&orig_set, set); |
| 8029 | set_init(&set2, set); |
| 8030 | |
| 8031 | set_fill_set(&orig_set, set); |
| 8032 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8033 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8034 | LY_CHECK_GOTO(rc, cleanup); |
| 8035 | |
| 8036 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 8037 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8038 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8039 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8040 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8041 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8042 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8043 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8044 | |
| 8045 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8046 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8047 | LY_CHECK_GOTO(rc, cleanup); |
| 8048 | continue; |
| 8049 | } |
| 8050 | |
| 8051 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8052 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8053 | LY_CHECK_GOTO(rc, cleanup); |
| 8054 | |
| 8055 | /* eval */ |
| 8056 | if (options & LYXP_SCNODE_ALL) { |
| 8057 | 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] | 8058 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8059 | set_scnode_clear_ctx(set); |
| 8060 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8061 | 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] | 8062 | LY_CHECK_GOTO(rc, cleanup); |
| 8063 | } |
| 8064 | } |
| 8065 | |
| 8066 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8067 | lyxp_set_free_content(&orig_set); |
| 8068 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8069 | return rc; |
| 8070 | } |
| 8071 | |
| 8072 | /** |
| 8073 | * @brief Evaluate RelationalExpr. Logs directly on error. |
| 8074 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8075 | * [16] RelationalExpr ::= AdditiveExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8076 | * | RelationalExpr '<' AdditiveExpr |
| 8077 | * | RelationalExpr '>' AdditiveExpr |
| 8078 | * | RelationalExpr '<=' AdditiveExpr |
| 8079 | * | RelationalExpr '>=' AdditiveExpr |
| 8080 | * |
| 8081 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8082 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8083 | * @param[in] repeat How many times this expression is repeated. |
| 8084 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8085 | * @param[in] options XPath options. |
| 8086 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8087 | */ |
| 8088 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8089 | 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] | 8090 | { |
| 8091 | LY_ERR rc; |
| 8092 | uint16_t this_op; |
| 8093 | struct lyxp_set orig_set, set2; |
| 8094 | uint16_t i; |
| 8095 | |
| 8096 | assert(repeat); |
| 8097 | |
| 8098 | set_init(&orig_set, set); |
| 8099 | set_init(&set2, set); |
| 8100 | |
| 8101 | set_fill_set(&orig_set, set); |
| 8102 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8103 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8104 | LY_CHECK_GOTO(rc, cleanup); |
| 8105 | |
| 8106 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 8107 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8108 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8109 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8110 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8111 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8112 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8113 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8114 | |
| 8115 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8116 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8117 | LY_CHECK_GOTO(rc, cleanup); |
| 8118 | continue; |
| 8119 | } |
| 8120 | |
| 8121 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8122 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8123 | LY_CHECK_GOTO(rc, cleanup); |
| 8124 | |
| 8125 | /* eval */ |
| 8126 | if (options & LYXP_SCNODE_ALL) { |
| 8127 | 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] | 8128 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8129 | set_scnode_clear_ctx(set); |
| 8130 | } else { |
| 8131 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8132 | LY_CHECK_GOTO(rc, cleanup); |
| 8133 | } |
| 8134 | } |
| 8135 | |
| 8136 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8137 | lyxp_set_free_content(&orig_set); |
| 8138 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8139 | return rc; |
| 8140 | } |
| 8141 | |
| 8142 | /** |
| 8143 | * @brief Evaluate EqualityExpr. Logs directly on error. |
| 8144 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8145 | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8146 | * | EqualityExpr '!=' RelationalExpr |
| 8147 | * |
| 8148 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8149 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8150 | * @param[in] repeat How many times this expression is repeated. |
| 8151 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8152 | * @param[in] options XPath options. |
| 8153 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8154 | */ |
| 8155 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8156 | 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] | 8157 | { |
| 8158 | LY_ERR rc; |
| 8159 | uint16_t this_op; |
| 8160 | struct lyxp_set orig_set, set2; |
| 8161 | uint16_t i; |
| 8162 | |
| 8163 | assert(repeat); |
| 8164 | |
| 8165 | set_init(&orig_set, set); |
| 8166 | set_init(&set2, set); |
| 8167 | |
| 8168 | set_fill_set(&orig_set, set); |
| 8169 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8170 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8171 | LY_CHECK_GOTO(rc, cleanup); |
| 8172 | |
| 8173 | /* ('=' / '!=' RelationalExpr)* */ |
| 8174 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8175 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8176 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8177 | 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] | 8178 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8179 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8180 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8181 | |
| 8182 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8183 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8184 | LY_CHECK_GOTO(rc, cleanup); |
| 8185 | continue; |
| 8186 | } |
| 8187 | |
| 8188 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8189 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8190 | LY_CHECK_GOTO(rc, cleanup); |
| 8191 | |
| 8192 | /* eval */ |
| 8193 | if (options & LYXP_SCNODE_ALL) { |
| 8194 | 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] | 8195 | warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1); |
| 8196 | 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] | 8197 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8198 | set_scnode_clear_ctx(set); |
| 8199 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8200 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8201 | LY_CHECK_GOTO(rc, cleanup); |
| 8202 | } |
| 8203 | } |
| 8204 | |
| 8205 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8206 | lyxp_set_free_content(&orig_set); |
| 8207 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8208 | return rc; |
| 8209 | } |
| 8210 | |
| 8211 | /** |
| 8212 | * @brief Evaluate AndExpr. Logs directly on error. |
| 8213 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8214 | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8215 | * |
| 8216 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8217 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8218 | * @param[in] repeat How many times this expression is repeated. |
| 8219 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8220 | * @param[in] options XPath options. |
| 8221 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8222 | */ |
| 8223 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8224 | 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] | 8225 | { |
| 8226 | LY_ERR rc; |
| 8227 | struct lyxp_set orig_set, set2; |
| 8228 | uint16_t i; |
| 8229 | |
| 8230 | assert(repeat); |
| 8231 | |
| 8232 | set_init(&orig_set, set); |
| 8233 | set_init(&set2, set); |
| 8234 | |
| 8235 | set_fill_set(&orig_set, set); |
| 8236 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8237 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8238 | LY_CHECK_GOTO(rc, cleanup); |
| 8239 | |
| 8240 | /* cast to boolean, we know that will be the final result */ |
| 8241 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8242 | set_scnode_clear_ctx(set); |
| 8243 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8244 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8245 | } |
| 8246 | |
| 8247 | /* ('and' EqualityExpr)* */ |
| 8248 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8249 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8250 | 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] | 8251 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8252 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8253 | |
| 8254 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8255 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) { |
| 8256 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8257 | LY_CHECK_GOTO(rc, cleanup); |
| 8258 | continue; |
| 8259 | } |
| 8260 | |
| 8261 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8262 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8263 | LY_CHECK_GOTO(rc, cleanup); |
| 8264 | |
| 8265 | /* eval - just get boolean value actually */ |
| 8266 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8267 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8268 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8269 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8270 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8271 | set_fill_set(set, &set2); |
| 8272 | } |
| 8273 | } |
| 8274 | |
| 8275 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8276 | lyxp_set_free_content(&orig_set); |
| 8277 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8278 | return rc; |
| 8279 | } |
| 8280 | |
| 8281 | /** |
| 8282 | * @brief Evaluate OrExpr. Logs directly on error. |
| 8283 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8284 | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8285 | * |
| 8286 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8287 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8288 | * @param[in] repeat How many times this expression is repeated. |
| 8289 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8290 | * @param[in] options XPath options. |
| 8291 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8292 | */ |
| 8293 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8294 | 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] | 8295 | { |
| 8296 | LY_ERR rc; |
| 8297 | struct lyxp_set orig_set, set2; |
| 8298 | uint16_t i; |
| 8299 | |
| 8300 | assert(repeat); |
| 8301 | |
| 8302 | set_init(&orig_set, set); |
| 8303 | set_init(&set2, set); |
| 8304 | |
| 8305 | set_fill_set(&orig_set, set); |
| 8306 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8307 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8308 | LY_CHECK_GOTO(rc, cleanup); |
| 8309 | |
| 8310 | /* cast to boolean, we know that will be the final result */ |
| 8311 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8312 | set_scnode_clear_ctx(set); |
| 8313 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8314 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8315 | } |
| 8316 | |
| 8317 | /* ('or' AndExpr)* */ |
| 8318 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8319 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8320 | 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] | 8321 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8322 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8323 | |
| 8324 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8325 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) { |
| 8326 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8327 | LY_CHECK_GOTO(rc, cleanup); |
| 8328 | continue; |
| 8329 | } |
| 8330 | |
| 8331 | set_fill_set(&set2, &orig_set); |
| 8332 | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
| 8333 | * but it does not matter */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8334 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8335 | LY_CHECK_GOTO(rc, cleanup); |
| 8336 | |
| 8337 | /* eval - just get boolean value actually */ |
| 8338 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8339 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8340 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8341 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8342 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8343 | set_fill_set(set, &set2); |
| 8344 | } |
| 8345 | } |
| 8346 | |
| 8347 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8348 | lyxp_set_free_content(&orig_set); |
| 8349 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8350 | return rc; |
| 8351 | } |
| 8352 | |
| 8353 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8354 | * @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] | 8355 | * |
| 8356 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8357 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8358 | * @param[in] etype Expression type to evaluate. |
| 8359 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8360 | * @param[in] options XPath options. |
| 8361 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8362 | */ |
| 8363 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8364 | eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, |
| 8365 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8366 | { |
| 8367 | uint16_t i, count; |
| 8368 | enum lyxp_expr_type next_etype; |
| 8369 | LY_ERR rc; |
| 8370 | |
| 8371 | /* process operator repeats */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8372 | if (!exp->repeat[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8373 | next_etype = LYXP_EXPR_NONE; |
| 8374 | } else { |
| 8375 | /* find etype repeat */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8376 | for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8377 | |
| 8378 | /* select one-priority lower because etype expression called us */ |
| 8379 | if (i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8380 | next_etype = exp->repeat[*tok_idx][i - 1]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8381 | /* count repeats for that expression */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8382 | 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] | 8383 | } else { |
| 8384 | next_etype = LYXP_EXPR_NONE; |
| 8385 | } |
| 8386 | } |
| 8387 | |
| 8388 | /* decide what expression are we parsing based on the repeat */ |
| 8389 | switch (next_etype) { |
| 8390 | case LYXP_EXPR_OR: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8391 | rc = eval_or_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8392 | break; |
| 8393 | case LYXP_EXPR_AND: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8394 | rc = eval_and_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8395 | break; |
| 8396 | case LYXP_EXPR_EQUALITY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8397 | rc = eval_equality_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8398 | break; |
| 8399 | case LYXP_EXPR_RELATIONAL: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8400 | rc = eval_relational_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8401 | break; |
| 8402 | case LYXP_EXPR_ADDITIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8403 | rc = eval_additive_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8404 | break; |
| 8405 | case LYXP_EXPR_MULTIPLICATIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8406 | rc = eval_multiplicative_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8407 | break; |
| 8408 | case LYXP_EXPR_UNARY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8409 | rc = eval_unary_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8410 | break; |
| 8411 | case LYXP_EXPR_UNION: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8412 | rc = eval_union_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8413 | break; |
| 8414 | case LYXP_EXPR_NONE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8415 | rc = eval_path_expr(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8416 | break; |
| 8417 | default: |
| 8418 | LOGINT_RET(set->ctx); |
| 8419 | } |
| 8420 | |
| 8421 | return rc; |
| 8422 | } |
| 8423 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8424 | /** |
| 8425 | * @brief Get root type. |
| 8426 | * |
| 8427 | * @param[in] ctx_node Context node. |
| 8428 | * @param[in] ctx_scnode Schema context node. |
| 8429 | * @param[in] options XPath options. |
| 8430 | * @return Root type. |
| 8431 | */ |
| 8432 | static enum lyxp_node_type |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8433 | 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] | 8434 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8435 | const struct lysc_node *op; |
| 8436 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8437 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8438 | /* schema */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8439 | 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] | 8440 | |
| 8441 | if (op || (options & LYXP_SCNODE)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8442 | /* general root that can access everything */ |
| 8443 | return LYXP_NODE_ROOT; |
| 8444 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
| 8445 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8446 | return LYXP_NODE_ROOT_CONFIG; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8447 | } |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8448 | return LYXP_NODE_ROOT; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8449 | } |
| 8450 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8451 | /* data */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8452 | op = ctx_node ? ctx_node->schema : NULL; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 8453 | 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] | 8454 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8455 | if (op || !(options & LYXP_SCHEMA)) { |
| 8456 | /* general root that can access everything */ |
| 8457 | return LYXP_NODE_ROOT; |
| 8458 | } else if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8459 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8460 | return LYXP_NODE_ROOT_CONFIG; |
| 8461 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8462 | return LYXP_NODE_ROOT; |
| 8463 | } |
| 8464 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8465 | LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8466 | 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] | 8467 | 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] | 8468 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8469 | uint16_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8470 | LY_ERR rc; |
| 8471 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8472 | LY_CHECK_ARG_RET(NULL, exp, set, LY_EINVAL); |
| 8473 | if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) { |
| 8474 | LOGARG(NULL, "Current module must be set if schema format is used."); |
| 8475 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8476 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8477 | |
| 8478 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8479 | memset(set, 0, sizeof *set); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8480 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8481 | set->root_type = lyxp_get_root_type(ctx_node, NULL, options); |
| 8482 | set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0); |
| 8483 | |
| 8484 | set->ctx = (struct ly_ctx *)(cur_mod ? cur_mod->ctx : (ctx_node ? LYD_CTX(ctx_node) : (tree ? LYD_CTX(tree) : NULL))); |
| 8485 | set->cur_node = ctx_node; |
| 8486 | for (set->context_op = ctx_node ? ctx_node->schema : NULL; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 8487 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8488 | set->context_op = set->context_op->parent) {} |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 8489 | set->tree = tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8490 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8491 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8492 | set->prefix_data = prefix_data; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8493 | |
| 8494 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8495 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8496 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8497 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8498 | } |
| 8499 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8500 | return rc; |
| 8501 | } |
| 8502 | |
| 8503 | #if 0 |
| 8504 | |
| 8505 | /* full xml printing of set elements, not used currently */ |
| 8506 | |
| 8507 | void |
| 8508 | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
| 8509 | { |
| 8510 | uint32_t i; |
| 8511 | char *str_num; |
| 8512 | struct lyout out; |
| 8513 | |
| 8514 | memset(&out, 0, sizeof out); |
| 8515 | |
| 8516 | out.type = LYOUT_STREAM; |
| 8517 | out.method.f = f; |
| 8518 | |
| 8519 | switch (set->type) { |
| 8520 | case LYXP_SET_EMPTY: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8521 | ly_print_(&out, "Empty XPath set\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8522 | break; |
| 8523 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8524 | ly_print_(&out, "Boolean XPath set:\n"); |
| 8525 | ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8526 | break; |
| 8527 | case LYXP_SET_STRING: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8528 | ly_print_(&out, "String XPath set:\n"); |
| 8529 | ly_print_(&out, "\"%s\"\n\n", set->value.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8530 | break; |
| 8531 | case LYXP_SET_NUMBER: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8532 | ly_print_(&out, "Number XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8533 | |
| 8534 | if (isnan(set->value.num)) { |
| 8535 | str_num = strdup("NaN"); |
| 8536 | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
| 8537 | str_num = strdup("0"); |
| 8538 | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
| 8539 | str_num = strdup("Infinity"); |
| 8540 | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
| 8541 | str_num = strdup("-Infinity"); |
| 8542 | } else if ((long long)set->value.num == set->value.num) { |
| 8543 | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
| 8544 | str_num = NULL; |
| 8545 | } |
| 8546 | } else { |
| 8547 | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
| 8548 | str_num = NULL; |
| 8549 | } |
| 8550 | } |
| 8551 | if (!str_num) { |
| 8552 | LOGMEM; |
| 8553 | return; |
| 8554 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8555 | ly_print_(&out, "%s\n\n", str_num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8556 | free(str_num); |
| 8557 | break; |
| 8558 | case LYXP_SET_NODE_SET: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8559 | ly_print_(&out, "Node XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8560 | |
| 8561 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8562 | ly_print_(&out, "%d. ", i + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8563 | switch (set->node_type[i]) { |
| 8564 | case LYXP_NODE_ROOT_ALL: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8565 | ly_print_(&out, "ROOT all\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8566 | break; |
| 8567 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8568 | ly_print_(&out, "ROOT config\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8569 | break; |
| 8570 | case LYXP_NODE_ROOT_STATE: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8571 | ly_print_(&out, "ROOT state\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8572 | break; |
| 8573 | case LYXP_NODE_ROOT_NOTIF: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8574 | 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] | 8575 | break; |
| 8576 | case LYXP_NODE_ROOT_RPC: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8577 | 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] | 8578 | break; |
| 8579 | case LYXP_NODE_ROOT_OUTPUT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8580 | 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] | 8581 | break; |
| 8582 | case LYXP_NODE_ELEM: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8583 | ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8584 | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8585 | ly_print_(&out, "\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8586 | break; |
| 8587 | case LYXP_NODE_TEXT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8588 | 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] | 8589 | break; |
| 8590 | case LYXP_NODE_ATTR: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8591 | 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] | 8592 | break; |
| 8593 | } |
| 8594 | } |
| 8595 | break; |
| 8596 | } |
| 8597 | } |
| 8598 | |
| 8599 | #endif |
| 8600 | |
| 8601 | LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8602 | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8603 | { |
| 8604 | long double num; |
| 8605 | char *str; |
| 8606 | LY_ERR rc; |
| 8607 | |
| 8608 | if (!set || (set->type == target)) { |
| 8609 | return LY_SUCCESS; |
| 8610 | } |
| 8611 | |
| 8612 | /* it's not possible to convert anything into a node set */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8613 | assert(target != LYXP_SET_NODE_SET); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8614 | |
| 8615 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8616 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8617 | return LY_EINVAL; |
| 8618 | } |
| 8619 | |
| 8620 | /* to STRING */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8621 | 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] | 8622 | switch (set->type) { |
| 8623 | case LYXP_SET_NUMBER: |
| 8624 | if (isnan(set->val.num)) { |
| 8625 | set->val.str = strdup("NaN"); |
| 8626 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8627 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 8628 | set->val.str = strdup("0"); |
| 8629 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8630 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 8631 | set->val.str = strdup("Infinity"); |
| 8632 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8633 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 8634 | set->val.str = strdup("-Infinity"); |
| 8635 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8636 | } else if ((long long)set->val.num == set->val.num) { |
| 8637 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 8638 | LOGMEM_RET(set->ctx); |
| 8639 | } |
| 8640 | set->val.str = str; |
| 8641 | } else { |
| 8642 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 8643 | LOGMEM_RET(set->ctx); |
| 8644 | } |
| 8645 | set->val.str = str; |
| 8646 | } |
| 8647 | break; |
| 8648 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8649 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8650 | set->val.str = strdup("true"); |
| 8651 | } else { |
| 8652 | set->val.str = strdup("false"); |
| 8653 | } |
| 8654 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 8655 | break; |
| 8656 | case LYXP_SET_NODE_SET: |
| 8657 | assert(set->used); |
| 8658 | |
| 8659 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8660 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8661 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8662 | rc = cast_node_set_to_string(set, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8663 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8664 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8665 | set->val.str = str; |
| 8666 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8667 | default: |
| 8668 | LOGINT_RET(set->ctx); |
| 8669 | } |
| 8670 | set->type = LYXP_SET_STRING; |
| 8671 | } |
| 8672 | |
| 8673 | /* to NUMBER */ |
| 8674 | if (target == LYXP_SET_NUMBER) { |
| 8675 | switch (set->type) { |
| 8676 | case LYXP_SET_STRING: |
| 8677 | num = cast_string_to_number(set->val.str); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8678 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8679 | set->val.num = num; |
| 8680 | break; |
| 8681 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8682 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8683 | set->val.num = 1; |
| 8684 | } else { |
| 8685 | set->val.num = 0; |
| 8686 | } |
| 8687 | break; |
| 8688 | default: |
| 8689 | LOGINT_RET(set->ctx); |
| 8690 | } |
| 8691 | set->type = LYXP_SET_NUMBER; |
| 8692 | } |
| 8693 | |
| 8694 | /* to BOOLEAN */ |
| 8695 | if (target == LYXP_SET_BOOLEAN) { |
| 8696 | switch (set->type) { |
| 8697 | case LYXP_SET_NUMBER: |
| 8698 | 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] | 8699 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8700 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8701 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8702 | } |
| 8703 | break; |
| 8704 | case LYXP_SET_STRING: |
| 8705 | if (set->val.str[0]) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8706 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8707 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8708 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8709 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8710 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8711 | } |
| 8712 | break; |
| 8713 | case LYXP_SET_NODE_SET: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8714 | if (set->used) { |
| 8715 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8716 | set->val.bln = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8717 | } else { |
| 8718 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8719 | set->val.bln = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8720 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8721 | break; |
| 8722 | default: |
| 8723 | LOGINT_RET(set->ctx); |
| 8724 | } |
| 8725 | set->type = LYXP_SET_BOOLEAN; |
| 8726 | } |
| 8727 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8728 | return LY_SUCCESS; |
| 8729 | } |
| 8730 | |
| 8731 | LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8732 | 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] | 8733 | 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] | 8734 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8735 | uint16_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8736 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8737 | LY_CHECK_ARG_RET(NULL, exp, set, LY_EINVAL); |
| 8738 | if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) { |
| 8739 | LOGARG(NULL, "Current module must be set if schema format is used."); |
| 8740 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8741 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8742 | |
| 8743 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8744 | memset(set, 0, sizeof *set); |
| 8745 | set->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8746 | set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options); |
| 8747 | 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] | 8748 | set->val.scnodes[0].in_ctx = -2; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8749 | |
| 8750 | set->ctx = cur_mod ? cur_mod->ctx : (ctx_scnode ? ctx_scnode->module->ctx : NULL); |
| 8751 | set->cur_scnode = ctx_scnode; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8752 | for (set->context_op = ctx_scnode; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 8753 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8754 | set->context_op = set->context_op->parent) {} |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8755 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8756 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8757 | set->prefix_data = prefix_data; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8758 | |
| 8759 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8760 | return eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8761 | } |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 8762 | |
| 8763 | API const char * |
| 8764 | lyxp_get_expr(const struct lyxp_expr *path) |
| 8765 | { |
| 8766 | if (!path) { |
| 8767 | return NULL; |
| 8768 | } |
| 8769 | |
| 8770 | return path->expr; |
| 8771 | } |