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; |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1290 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1291 | |
| 1292 | assert(prev && prev_pos && !root->prev->next); |
| 1293 | |
| 1294 | if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 1295 | return 0; |
| 1296 | } |
| 1297 | |
| 1298 | if (*prev) { |
| 1299 | /* start from the previous element instead from the root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1300 | pos = *prev_pos; |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1301 | for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1302 | goto dfs_search; |
| 1303 | } |
| 1304 | |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1305 | LY_LIST_FOR(root, top_sibling) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1306 | LYD_TREE_DFS_BEGIN(top_sibling, elem) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1307 | dfs_search: |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1308 | if (*prev && !elem) { |
| 1309 | /* resume previous DFS */ |
| 1310 | elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev; |
| 1311 | LYD_TREE_DFS_continue = 0; |
| 1312 | } |
| 1313 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1314 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1315 | /* skip */ |
| 1316 | LYD_TREE_DFS_continue = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1317 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1318 | if (elem == node) { |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1319 | found = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1320 | break; |
| 1321 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1322 | ++pos; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1323 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1324 | |
| 1325 | LYD_TREE_DFS_END(top_sibling, elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | /* node found */ |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1329 | if (found) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1330 | break; |
| 1331 | } |
| 1332 | } |
| 1333 | |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1334 | if (!found) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1335 | if (!(*prev)) { |
| 1336 | /* we went from root and failed to find it, cannot be */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1337 | LOGINT(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1338 | return 0; |
| 1339 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1340 | /* start the search again from the beginning */ |
| 1341 | *prev = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1342 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1343 | top_sibling = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1344 | pos = 1; |
| 1345 | goto dfs_search; |
| 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | /* remember the last found node for next time */ |
| 1350 | *prev = node; |
| 1351 | *prev_pos = pos; |
| 1352 | |
| 1353 | return pos; |
| 1354 | } |
| 1355 | |
| 1356 | /** |
| 1357 | * @brief Assign (fill) missing node positions. |
| 1358 | * |
| 1359 | * @param[in] set Set to fill positions in. |
| 1360 | * @param[in] root Context root node. |
| 1361 | * @param[in] root_type Context root type. |
| 1362 | * @return LY_ERR |
| 1363 | */ |
| 1364 | static LY_ERR |
| 1365 | set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type) |
| 1366 | { |
| 1367 | const struct lyd_node *prev = NULL, *tmp_node; |
| 1368 | uint32_t i, tmp_pos = 0; |
| 1369 | |
| 1370 | for (i = 0; i < set->used; ++i) { |
| 1371 | if (!set->val.nodes[i].pos) { |
| 1372 | tmp_node = NULL; |
| 1373 | switch (set->val.nodes[i].type) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1374 | case LYXP_NODE_META: |
| 1375 | tmp_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1376 | if (!tmp_node) { |
| 1377 | LOGINT_RET(root->schema->module->ctx); |
| 1378 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1379 | /* fallthrough */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1380 | case LYXP_NODE_ELEM: |
| 1381 | case LYXP_NODE_TEXT: |
| 1382 | if (!tmp_node) { |
| 1383 | tmp_node = set->val.nodes[i].node; |
| 1384 | } |
| 1385 | set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos); |
| 1386 | break; |
| 1387 | default: |
| 1388 | /* all roots have position 0 */ |
| 1389 | break; |
| 1390 | } |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | return LY_SUCCESS; |
| 1395 | } |
| 1396 | |
| 1397 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1398 | * @brief Get unique @p meta position in the parent metadata. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1399 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1400 | * @param[in] meta Metadata to use. |
| 1401 | * @return Metadata position. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1402 | */ |
| 1403 | static uint16_t |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1404 | get_meta_pos(struct lyd_meta *meta) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1405 | { |
| 1406 | uint16_t pos = 0; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1407 | struct lyd_meta *meta2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1408 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1409 | for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1410 | ++pos; |
| 1411 | } |
| 1412 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1413 | assert(meta2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1414 | return pos; |
| 1415 | } |
| 1416 | |
| 1417 | /** |
| 1418 | * @brief Compare 2 nodes in respect to XPath document order. |
| 1419 | * |
| 1420 | * @param[in] item1 1st node. |
| 1421 | * @param[in] item2 2nd node. |
| 1422 | * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1. |
| 1423 | */ |
| 1424 | static int |
| 1425 | set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2) |
| 1426 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1427 | uint32_t meta_pos1 = 0, meta_pos2 = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1428 | |
| 1429 | if (item1->pos < item2->pos) { |
| 1430 | return -1; |
| 1431 | } |
| 1432 | |
| 1433 | if (item1->pos > item2->pos) { |
| 1434 | return 1; |
| 1435 | } |
| 1436 | |
| 1437 | /* node positions are equal, the fun case */ |
| 1438 | |
| 1439 | /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */ |
| 1440 | /* special case since text nodes are actually saved as their parents */ |
| 1441 | if ((item1->node == item2->node) && (item1->type != item2->type)) { |
| 1442 | if (item1->type == LYXP_NODE_ELEM) { |
| 1443 | assert(item2->type == LYXP_NODE_TEXT); |
| 1444 | return -1; |
| 1445 | } else { |
| 1446 | assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM)); |
| 1447 | return 1; |
| 1448 | } |
| 1449 | } |
| 1450 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1451 | /* we need meta positions now */ |
| 1452 | if (item1->type == LYXP_NODE_META) { |
| 1453 | meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1454 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1455 | if (item2->type == LYXP_NODE_META) { |
| 1456 | meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1457 | } |
| 1458 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1459 | /* 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] | 1460 | /* check for duplicates */ |
| 1461 | if (item1->node == item2->node) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1462 | 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] | 1463 | return 0; |
| 1464 | } |
| 1465 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1466 | /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1467 | /* elem is always first, 2nd node is after it */ |
| 1468 | if (item1->type == LYXP_NODE_ELEM) { |
| 1469 | assert(item2->type != LYXP_NODE_ELEM); |
| 1470 | return -1; |
| 1471 | } |
| 1472 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1473 | /* 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] | 1474 | /* 2nd is before 1st */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1475 | if (((item1->type == LYXP_NODE_TEXT) && |
| 1476 | ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) || |
| 1477 | ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) || |
| 1478 | (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) && |
| 1479 | (meta_pos1 > meta_pos2))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1480 | return 1; |
| 1481 | } |
| 1482 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1483 | /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1484 | /* 2nd is after 1st */ |
| 1485 | return -1; |
| 1486 | } |
| 1487 | |
| 1488 | /** |
| 1489 | * @brief Set cast for comparisons. |
| 1490 | * |
| 1491 | * @param[in] trg Target set to cast source into. |
| 1492 | * @param[in] src Source set. |
| 1493 | * @param[in] type Target set type. |
| 1494 | * @param[in] src_idx Source set node index. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1495 | * @return LY_ERR |
| 1496 | */ |
| 1497 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1498 | 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] | 1499 | { |
| 1500 | assert(src->type == LYXP_SET_NODE_SET); |
| 1501 | |
| 1502 | set_init(trg, src); |
| 1503 | |
| 1504 | /* insert node into target set */ |
| 1505 | set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0); |
| 1506 | |
| 1507 | /* cast target set appropriately */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1508 | return lyxp_set_cast(trg, type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1509 | } |
| 1510 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1511 | /** |
| 1512 | * @brief Set content canonization for comparisons. |
| 1513 | * |
| 1514 | * @param[in] trg Target set to put the canononized source into. |
| 1515 | * @param[in] src Source set. |
| 1516 | * @param[in] xp_node Source XPath node/meta to use for canonization. |
| 1517 | * @return LY_ERR |
| 1518 | */ |
| 1519 | static LY_ERR |
| 1520 | set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node) |
| 1521 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1522 | const struct lysc_type *type = NULL; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1523 | struct lyd_value val; |
| 1524 | struct ly_err_item *err = NULL; |
| 1525 | char *str, *ptr; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1526 | LY_ERR rc; |
| 1527 | |
| 1528 | /* is there anything to canonize even? */ |
| 1529 | if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) { |
| 1530 | /* do we have a type to use for canonization? */ |
| 1531 | if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) { |
| 1532 | type = ((struct lyd_node_term *)xp_node->node)->value.realtype; |
| 1533 | } else if (xp_node->type == LYXP_NODE_META) { |
| 1534 | type = ((struct lyd_meta *)xp_node->node)->value.realtype; |
| 1535 | } |
| 1536 | } |
| 1537 | if (!type) { |
| 1538 | goto fill; |
| 1539 | } |
| 1540 | |
| 1541 | if (src->type == LYXP_SET_NUMBER) { |
| 1542 | /* canonize number */ |
| 1543 | if (asprintf(&str, "%Lf", src->val.num) == -1) { |
| 1544 | LOGMEM(src->ctx); |
| 1545 | return LY_EMEM; |
| 1546 | } |
| 1547 | } else { |
| 1548 | /* canonize string */ |
| 1549 | str = strdup(src->val.str); |
| 1550 | } |
| 1551 | |
| 1552 | /* ignore errors, the value may not satisfy schema constraints */ |
Michal Vasko | 0fdb0d9 | 2020-11-06 17:25:50 +0100 | [diff] [blame] | 1553 | rc = type->plugin->store(src->ctx, type, str, strlen(str), LY_TYPE_STORE_DYNAMIC, src->format, src->prefix_data, |
| 1554 | LYD_HINT_DATA, xp_node->node->schema, &val, &err); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1555 | ly_err_free(err); |
| 1556 | if (rc) { |
| 1557 | /* invalid value */ |
| 1558 | free(str); |
| 1559 | goto fill; |
| 1560 | } |
| 1561 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1562 | /* use the canonized value */ |
| 1563 | set_init(trg, src); |
| 1564 | trg->type = src->type; |
| 1565 | if (src->type == LYXP_SET_NUMBER) { |
Michal Vasko | 0fdb0d9 | 2020-11-06 17:25:50 +0100 | [diff] [blame] | 1566 | trg->val.num = strtold(val.canonical, &ptr); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1567 | LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT); |
| 1568 | } else { |
Michal Vasko | 0fdb0d9 | 2020-11-06 17:25:50 +0100 | [diff] [blame] | 1569 | trg->val.str = strdup(val.canonical); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1570 | } |
| 1571 | type->plugin->free(src->ctx, &val); |
| 1572 | return LY_SUCCESS; |
| 1573 | |
| 1574 | fill: |
| 1575 | /* no canonization needed/possible */ |
| 1576 | set_fill_set(trg, src); |
| 1577 | return LY_SUCCESS; |
| 1578 | } |
| 1579 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1580 | #ifndef NDEBUG |
| 1581 | |
| 1582 | /** |
| 1583 | * @brief Bubble sort @p set into XPath document order. |
| 1584 | * Context position aware. Unused in the 'Release' build target. |
| 1585 | * |
| 1586 | * @param[in] set Set to sort. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1587 | * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0). |
| 1588 | */ |
| 1589 | static int |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1590 | set_sort(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1591 | { |
| 1592 | uint32_t i, j; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1593 | int ret = 0, cmp; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1594 | ly_bool inverted, change; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1595 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1596 | struct lyxp_set_node item; |
| 1597 | struct lyxp_set_hash_node hnode; |
| 1598 | uint64_t hash; |
| 1599 | |
Michal Vasko | 3cf8fbf | 2020-05-27 15:21:21 +0200 | [diff] [blame] | 1600 | if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1601 | return 0; |
| 1602 | } |
| 1603 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1604 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1605 | 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] | 1606 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1607 | |
| 1608 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1609 | if (set_assign_pos(set, root, set->root_type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1610 | return -1; |
| 1611 | } |
| 1612 | |
| 1613 | LOGDBG(LY_LDGXPATH, "SORT BEGIN"); |
| 1614 | print_set_debug(set); |
| 1615 | |
| 1616 | for (i = 0; i < set->used; ++i) { |
| 1617 | inverted = 0; |
| 1618 | change = 0; |
| 1619 | |
| 1620 | for (j = 1; j < set->used - i; ++j) { |
| 1621 | /* compare node positions */ |
| 1622 | if (inverted) { |
| 1623 | cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]); |
| 1624 | } else { |
| 1625 | cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]); |
| 1626 | } |
| 1627 | |
| 1628 | /* swap if needed */ |
| 1629 | if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) { |
| 1630 | change = 1; |
| 1631 | |
| 1632 | item = set->val.nodes[j - 1]; |
| 1633 | set->val.nodes[j - 1] = set->val.nodes[j]; |
| 1634 | set->val.nodes[j] = item; |
| 1635 | } else { |
| 1636 | /* whether node_pos1 should be smaller than node_pos2 or the other way around */ |
| 1637 | inverted = !inverted; |
| 1638 | } |
| 1639 | } |
| 1640 | |
| 1641 | ++ret; |
| 1642 | |
| 1643 | if (!change) { |
| 1644 | break; |
| 1645 | } |
| 1646 | } |
| 1647 | |
| 1648 | LOGDBG(LY_LDGXPATH, "SORT END %d", ret); |
| 1649 | print_set_debug(set); |
| 1650 | |
| 1651 | /* check node hashes */ |
| 1652 | if (set->used >= LYD_HT_MIN_ITEMS) { |
| 1653 | assert(set->ht); |
| 1654 | for (i = 0; i < set->used; ++i) { |
| 1655 | hnode.node = set->val.nodes[i].node; |
| 1656 | hnode.type = set->val.nodes[i].type; |
| 1657 | |
| 1658 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 1659 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 1660 | hash = dict_hash_multi(hash, NULL, 0); |
| 1661 | |
| 1662 | assert(!lyht_find(set->ht, &hnode, hash, NULL)); |
| 1663 | } |
| 1664 | } |
| 1665 | |
| 1666 | return ret - 1; |
| 1667 | } |
| 1668 | |
| 1669 | /** |
| 1670 | * @brief Remove duplicate entries in a sorted node set. |
| 1671 | * |
| 1672 | * @param[in] set Sorted set to check. |
| 1673 | * @return LY_ERR (LY_EEXIST if some duplicates are found) |
| 1674 | */ |
| 1675 | static LY_ERR |
| 1676 | set_sorted_dup_node_clean(struct lyxp_set *set) |
| 1677 | { |
| 1678 | uint32_t i = 0; |
| 1679 | LY_ERR ret = LY_SUCCESS; |
| 1680 | |
| 1681 | if (set->used > 1) { |
| 1682 | while (i < set->used - 1) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1683 | if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) && |
| 1684 | (set->val.nodes[i].type == set->val.nodes[i + 1].type)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1685 | set_remove_node_none(set, i + 1); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1686 | ret = LY_EEXIST; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1687 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1688 | ++i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1689 | } |
| 1690 | } |
| 1691 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1692 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1693 | return ret; |
| 1694 | } |
| 1695 | |
| 1696 | #endif |
| 1697 | |
| 1698 | /** |
| 1699 | * @brief Merge 2 sorted sets into one. |
| 1700 | * |
| 1701 | * @param[in,out] trg Set to merge into. Duplicates are removed. |
| 1702 | * @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] | 1703 | * @return LY_ERR |
| 1704 | */ |
| 1705 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1706 | set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1707 | { |
| 1708 | uint32_t i, j, k, count, dup_count; |
| 1709 | int cmp; |
| 1710 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1711 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1712 | 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] | 1713 | return LY_EINVAL; |
| 1714 | } |
| 1715 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1716 | if (!src->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1717 | return LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1718 | } else if (!trg->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1719 | set_fill_set(trg, src); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1720 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1721 | return LY_SUCCESS; |
| 1722 | } |
| 1723 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1724 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1725 | 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] | 1726 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1727 | |
| 1728 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1729 | 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] | 1730 | return LY_EINT; |
| 1731 | } |
| 1732 | |
| 1733 | #ifndef NDEBUG |
| 1734 | LOGDBG(LY_LDGXPATH, "MERGE target"); |
| 1735 | print_set_debug(trg); |
| 1736 | LOGDBG(LY_LDGXPATH, "MERGE source"); |
| 1737 | print_set_debug(src); |
| 1738 | #endif |
| 1739 | |
| 1740 | /* make memory for the merge (duplicates are not detected yet, so space |
| 1741 | * will likely be wasted on them, too bad) */ |
| 1742 | if (trg->size - trg->used < src->used) { |
| 1743 | trg->size = trg->used + src->used; |
| 1744 | |
| 1745 | trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes); |
| 1746 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM); |
| 1747 | } |
| 1748 | |
| 1749 | i = 0; |
| 1750 | j = 0; |
| 1751 | count = 0; |
| 1752 | dup_count = 0; |
| 1753 | do { |
| 1754 | cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]); |
| 1755 | if (!cmp) { |
| 1756 | if (!count) { |
| 1757 | /* duplicate, just skip it */ |
| 1758 | ++i; |
| 1759 | ++j; |
| 1760 | } else { |
| 1761 | /* we are copying something already, so let's copy the duplicate too, |
| 1762 | * we are hoping that afterwards there are some more nodes to |
| 1763 | * copy and this way we can copy them all together */ |
| 1764 | ++count; |
| 1765 | ++dup_count; |
| 1766 | ++i; |
| 1767 | ++j; |
| 1768 | } |
| 1769 | } else if (cmp < 0) { |
| 1770 | /* inserting src node into trg, just remember it for now */ |
| 1771 | ++count; |
| 1772 | ++i; |
| 1773 | |
| 1774 | /* insert the hash now */ |
| 1775 | set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type); |
| 1776 | } else if (count) { |
| 1777 | copy_nodes: |
| 1778 | /* time to actually copy the nodes, we have found the largest block of nodes */ |
| 1779 | memmove(&trg->val.nodes[j + (count - dup_count)], |
| 1780 | &trg->val.nodes[j], |
| 1781 | (trg->used - j) * sizeof *trg->val.nodes); |
| 1782 | memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes); |
| 1783 | |
| 1784 | trg->used += count - dup_count; |
| 1785 | /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */ |
| 1786 | j += count - dup_count; |
| 1787 | |
| 1788 | count = 0; |
| 1789 | dup_count = 0; |
| 1790 | } else { |
| 1791 | ++j; |
| 1792 | } |
| 1793 | } while ((i < src->used) && (j < trg->used)); |
| 1794 | |
| 1795 | if ((i < src->used) || count) { |
| 1796 | /* insert all the hashes first */ |
| 1797 | for (k = i; k < src->used; ++k) { |
| 1798 | set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type); |
| 1799 | } |
| 1800 | |
| 1801 | /* loop ended, but we need to copy something at trg end */ |
| 1802 | count += src->used - i; |
| 1803 | i = src->used; |
| 1804 | goto copy_nodes; |
| 1805 | } |
| 1806 | |
| 1807 | /* we are inserting hashes before the actual node insert, which causes |
| 1808 | * situations when there were initially not enough items for a hash table, |
| 1809 | * but even after some were inserted, hash table was not created (during |
| 1810 | * insertion the number of items is not updated yet) */ |
| 1811 | if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) { |
| 1812 | set_insert_node_hash(trg, NULL, 0); |
| 1813 | } |
| 1814 | |
| 1815 | #ifndef NDEBUG |
| 1816 | LOGDBG(LY_LDGXPATH, "MERGE result"); |
| 1817 | print_set_debug(trg); |
| 1818 | #endif |
| 1819 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1820 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1821 | return LY_SUCCESS; |
| 1822 | } |
| 1823 | |
| 1824 | /* |
| 1825 | * (re)parse functions |
| 1826 | * |
| 1827 | * Parse functions parse the expression into |
| 1828 | * tokens (syntactic analysis). |
| 1829 | * |
| 1830 | * Reparse functions perform semantic analysis |
| 1831 | * (do not save the result, just a check) of |
| 1832 | * the expression and fill repeat indices. |
| 1833 | */ |
| 1834 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1835 | LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1836 | 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] | 1837 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1838 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1839 | if (ctx) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1840 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1841 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1842 | return LY_EINCOMPLETE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1843 | } |
| 1844 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1845 | if (want_tok && (exp->tokens[tok_idx] != want_tok)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1846 | if (ctx) { |
Michal Vasko | 0b468e6 | 2020-10-19 09:33:04 +0200 | [diff] [blame] | 1847 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]), |
| 1848 | &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1849 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1850 | return LY_ENOT; |
| 1851 | } |
| 1852 | |
| 1853 | return LY_SUCCESS; |
| 1854 | } |
| 1855 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1856 | LY_ERR |
| 1857 | lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok) |
| 1858 | { |
| 1859 | LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok)); |
| 1860 | |
| 1861 | /* skip the token */ |
| 1862 | ++(*tok_idx); |
| 1863 | |
| 1864 | return LY_SUCCESS; |
| 1865 | } |
| 1866 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1867 | /* just like lyxp_check_token() but tests for 2 tokens */ |
| 1868 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 1869 | 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] | 1870 | enum lyxp_token want_tok2) |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1871 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1872 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1873 | if (ctx) { |
| 1874 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1875 | } |
| 1876 | return LY_EINCOMPLETE; |
| 1877 | } |
| 1878 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1879 | 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] | 1880 | if (ctx) { |
Michal Vasko | 0b468e6 | 2020-10-19 09:33:04 +0200 | [diff] [blame] | 1881 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]), |
| 1882 | &exp->expr[exp->tok_pos[tok_idx]]); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1883 | } |
| 1884 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1885 | } |
| 1886 | |
| 1887 | return LY_SUCCESS; |
| 1888 | } |
| 1889 | |
| 1890 | /** |
| 1891 | * @brief Stack operation push on the repeat array. |
| 1892 | * |
| 1893 | * @param[in] exp Expression to use. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1894 | * @param[in] tok_idx Position in the expresion \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1895 | * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed. |
| 1896 | */ |
| 1897 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1898 | 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] | 1899 | { |
| 1900 | uint16_t i; |
| 1901 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1902 | if (exp->repeat[tok_idx]) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1903 | for (i = 0; exp->repeat[tok_idx][i]; ++i) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1904 | exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]); |
| 1905 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1906 | exp->repeat[tok_idx][i] = repeat_op_idx; |
| 1907 | exp->repeat[tok_idx][i + 1] = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1908 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1909 | exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]); |
| 1910 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1911 | exp->repeat[tok_idx][0] = repeat_op_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1912 | } |
| 1913 | } |
| 1914 | |
| 1915 | /** |
| 1916 | * @brief Reparse Predicate. Logs directly on error. |
| 1917 | * |
| 1918 | * [7] Predicate ::= '[' Expr ']' |
| 1919 | * |
| 1920 | * @param[in] ctx Context for logging. |
| 1921 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1922 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1923 | * @return LY_ERR |
| 1924 | */ |
| 1925 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1926 | 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] | 1927 | { |
| 1928 | LY_ERR rc; |
| 1929 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1930 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1931 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1932 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1933 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1934 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1935 | LY_CHECK_RET(rc); |
| 1936 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1937 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1938 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1939 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1940 | |
| 1941 | return LY_SUCCESS; |
| 1942 | } |
| 1943 | |
| 1944 | /** |
| 1945 | * @brief Reparse RelativeLocationPath. Logs directly on error. |
| 1946 | * |
| 1947 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 1948 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 1949 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 1950 | * |
| 1951 | * @param[in] ctx Context for logging. |
| 1952 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1953 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1954 | * @return LY_ERR (LY_EINCOMPLETE on forward reference) |
| 1955 | */ |
| 1956 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1957 | 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] | 1958 | { |
| 1959 | LY_ERR rc; |
| 1960 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1961 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1962 | LY_CHECK_RET(rc); |
| 1963 | |
| 1964 | goto step; |
| 1965 | do { |
| 1966 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1967 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1968 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1969 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1970 | LY_CHECK_RET(rc); |
| 1971 | step: |
| 1972 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1973 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1974 | case LYXP_TOKEN_DOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1975 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1976 | break; |
| 1977 | |
| 1978 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1979 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1980 | break; |
| 1981 | |
| 1982 | case LYXP_TOKEN_AT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1983 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1984 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1985 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1986 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1987 | 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] | 1988 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1989 | 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] | 1990 | return LY_EVALID; |
| 1991 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1992 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1993 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1994 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1995 | goto reparse_predicate; |
| 1996 | break; |
| 1997 | |
| 1998 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1999 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2000 | |
| 2001 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2002 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2003 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2004 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2005 | |
| 2006 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2007 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2008 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2009 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2010 | |
| 2011 | reparse_predicate: |
| 2012 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2013 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 2014 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2015 | LY_CHECK_RET(rc); |
| 2016 | } |
| 2017 | break; |
| 2018 | default: |
| 2019 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2020 | 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] | 2021 | return LY_EVALID; |
| 2022 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2023 | } 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] | 2024 | |
| 2025 | return LY_SUCCESS; |
| 2026 | } |
| 2027 | |
| 2028 | /** |
| 2029 | * @brief Reparse AbsoluteLocationPath. Logs directly on error. |
| 2030 | * |
| 2031 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 2032 | * |
| 2033 | * @param[in] ctx Context for logging. |
| 2034 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2035 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2036 | * @return LY_ERR |
| 2037 | */ |
| 2038 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2039 | 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] | 2040 | { |
| 2041 | LY_ERR rc; |
| 2042 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2043 | 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] | 2044 | |
| 2045 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2046 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
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 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2049 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2050 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2051 | return LY_SUCCESS; |
| 2052 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2053 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2054 | case LYXP_TOKEN_DOT: |
| 2055 | case LYXP_TOKEN_DDOT: |
| 2056 | case LYXP_TOKEN_AT: |
| 2057 | case LYXP_TOKEN_NAMETEST: |
| 2058 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2059 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2060 | LY_CHECK_RET(rc); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2061 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2062 | default: |
| 2063 | break; |
| 2064 | } |
| 2065 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2066 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 2067 | /* '//' RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2068 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2069 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2070 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2071 | LY_CHECK_RET(rc); |
| 2072 | } |
| 2073 | |
| 2074 | return LY_SUCCESS; |
| 2075 | } |
| 2076 | |
| 2077 | /** |
| 2078 | * @brief Reparse FunctionCall. Logs directly on error. |
| 2079 | * |
| 2080 | * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
| 2081 | * |
| 2082 | * @param[in] ctx Context for logging. |
| 2083 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2084 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2085 | * @return LY_ERR |
| 2086 | */ |
| 2087 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2088 | 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] | 2089 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2090 | int8_t min_arg_count = -1; |
| 2091 | uint32_t arg_count, max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2092 | uint16_t func_tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2093 | LY_ERR rc; |
| 2094 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2095 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2096 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2097 | func_tok_idx = *tok_idx; |
| 2098 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2099 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2100 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2101 | min_arg_count = 1; |
| 2102 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2103 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2104 | min_arg_count = 1; |
| 2105 | max_arg_count = 1; |
| 2106 | } |
| 2107 | break; |
| 2108 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2109 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2110 | min_arg_count = 1; |
| 2111 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2112 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2113 | min_arg_count = 0; |
| 2114 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2115 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2116 | min_arg_count = 0; |
| 2117 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2118 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2119 | min_arg_count = 0; |
| 2120 | max_arg_count = 0; |
| 2121 | } |
| 2122 | break; |
| 2123 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2124 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2125 | min_arg_count = 1; |
| 2126 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2127 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2128 | min_arg_count = 0; |
| 2129 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2130 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2131 | min_arg_count = 1; |
| 2132 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2133 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2134 | min_arg_count = 1; |
| 2135 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2136 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2137 | min_arg_count = 1; |
| 2138 | max_arg_count = 1; |
| 2139 | } |
| 2140 | break; |
| 2141 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2142 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2143 | min_arg_count = 2; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2144 | max_arg_count = UINT32_MAX; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2145 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2146 | min_arg_count = 0; |
| 2147 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2148 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2149 | min_arg_count = 0; |
| 2150 | max_arg_count = 1; |
| 2151 | } |
| 2152 | break; |
| 2153 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2154 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2155 | min_arg_count = 1; |
| 2156 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2157 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2158 | min_arg_count = 1; |
| 2159 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2160 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2161 | min_arg_count = 0; |
| 2162 | max_arg_count = 0; |
| 2163 | } |
| 2164 | break; |
| 2165 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2166 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2167 | min_arg_count = 2; |
| 2168 | max_arg_count = 2; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2169 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2170 | min_arg_count = 0; |
| 2171 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2172 | } 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] | 2173 | min_arg_count = 2; |
| 2174 | max_arg_count = 2; |
| 2175 | } |
| 2176 | break; |
| 2177 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2178 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2179 | min_arg_count = 2; |
| 2180 | max_arg_count = 3; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2181 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2182 | min_arg_count = 3; |
| 2183 | max_arg_count = 3; |
| 2184 | } |
| 2185 | break; |
| 2186 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2187 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2188 | min_arg_count = 0; |
| 2189 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2190 | } 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] | 2191 | min_arg_count = 1; |
| 2192 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2193 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2194 | min_arg_count = 2; |
| 2195 | max_arg_count = 2; |
| 2196 | } |
| 2197 | break; |
| 2198 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2199 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2200 | min_arg_count = 2; |
| 2201 | max_arg_count = 2; |
| 2202 | } |
| 2203 | break; |
| 2204 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2205 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2206 | min_arg_count = 2; |
| 2207 | max_arg_count = 2; |
| 2208 | } |
| 2209 | break; |
| 2210 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2211 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2212 | min_arg_count = 0; |
| 2213 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2214 | } 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] | 2215 | min_arg_count = 0; |
| 2216 | max_arg_count = 1; |
| 2217 | } |
| 2218 | break; |
| 2219 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2220 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2221 | min_arg_count = 0; |
| 2222 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2223 | } 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] | 2224 | min_arg_count = 2; |
| 2225 | max_arg_count = 2; |
| 2226 | } |
| 2227 | break; |
| 2228 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2229 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2230 | min_arg_count = 2; |
| 2231 | max_arg_count = 2; |
| 2232 | } |
| 2233 | break; |
| 2234 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2235 | 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] | 2236 | min_arg_count = 2; |
| 2237 | max_arg_count = 2; |
| 2238 | } |
| 2239 | break; |
| 2240 | } |
| 2241 | if (min_arg_count == -1) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2242 | 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] | 2243 | return LY_EINVAL; |
| 2244 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2245 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2246 | |
| 2247 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2248 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2249 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2250 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2251 | |
| 2252 | /* ( Expr ( ',' Expr )* )? */ |
| 2253 | arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2254 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2255 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2256 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2257 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2258 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2259 | LY_CHECK_RET(rc); |
| 2260 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2261 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
| 2262 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2263 | |
| 2264 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2265 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2266 | LY_CHECK_RET(rc); |
| 2267 | } |
| 2268 | |
| 2269 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2270 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2271 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2272 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2273 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2274 | 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] | 2275 | 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] | 2276 | &exp->expr[exp->tok_pos[func_tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2277 | return LY_EVALID; |
| 2278 | } |
| 2279 | |
| 2280 | return LY_SUCCESS; |
| 2281 | } |
| 2282 | |
| 2283 | /** |
| 2284 | * @brief Reparse PathExpr. Logs directly on error. |
| 2285 | * |
| 2286 | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
| 2287 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 2288 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 2289 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
| 2290 | * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
| 2291 | * |
| 2292 | * @param[in] ctx Context for logging. |
| 2293 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2294 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2295 | * @return LY_ERR |
| 2296 | */ |
| 2297 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2298 | 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] | 2299 | { |
| 2300 | LY_ERR rc; |
| 2301 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2302 | if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2303 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2304 | } |
| 2305 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2306 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2307 | case LYXP_TOKEN_PAR1: |
| 2308 | /* '(' Expr ')' Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2309 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2310 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2311 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2312 | LY_CHECK_RET(rc); |
| 2313 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2314 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2315 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2316 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2317 | goto predicate; |
| 2318 | break; |
| 2319 | case LYXP_TOKEN_DOT: |
| 2320 | case LYXP_TOKEN_DDOT: |
| 2321 | case LYXP_TOKEN_AT: |
| 2322 | case LYXP_TOKEN_NAMETEST: |
| 2323 | case LYXP_TOKEN_NODETYPE: |
| 2324 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2325 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2326 | LY_CHECK_RET(rc); |
| 2327 | break; |
| 2328 | case LYXP_TOKEN_FUNCNAME: |
| 2329 | /* FunctionCall */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2330 | rc = reparse_function_call(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2331 | LY_CHECK_RET(rc); |
| 2332 | goto predicate; |
| 2333 | break; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2334 | case LYXP_TOKEN_OPER_PATH: |
| 2335 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2336 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2337 | rc = reparse_absolute_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2338 | LY_CHECK_RET(rc); |
| 2339 | break; |
| 2340 | case LYXP_TOKEN_LITERAL: |
| 2341 | /* Literal */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2342 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2343 | goto predicate; |
| 2344 | break; |
| 2345 | case LYXP_TOKEN_NUMBER: |
| 2346 | /* Number */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2347 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2348 | goto predicate; |
| 2349 | break; |
| 2350 | default: |
| 2351 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2352 | 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] | 2353 | return LY_EVALID; |
| 2354 | } |
| 2355 | |
| 2356 | return LY_SUCCESS; |
| 2357 | |
| 2358 | predicate: |
| 2359 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2360 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 2361 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2362 | LY_CHECK_RET(rc); |
| 2363 | } |
| 2364 | |
| 2365 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2366 | 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] | 2367 | |
| 2368 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2369 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2370 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2371 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2372 | LY_CHECK_RET(rc); |
| 2373 | } |
| 2374 | |
| 2375 | return LY_SUCCESS; |
| 2376 | } |
| 2377 | |
| 2378 | /** |
| 2379 | * @brief Reparse UnaryExpr. Logs directly on error. |
| 2380 | * |
| 2381 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 2382 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 2383 | * |
| 2384 | * @param[in] ctx Context for logging. |
| 2385 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2386 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2387 | * @return LY_ERR |
| 2388 | */ |
| 2389 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2390 | 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] | 2391 | { |
| 2392 | uint16_t prev_exp; |
| 2393 | LY_ERR rc; |
| 2394 | |
| 2395 | /* ('-')* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2396 | prev_exp = *tok_idx; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2397 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2398 | (exp->expr[exp->tok_pos[*tok_idx]] == '-')) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2399 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2400 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2401 | } |
| 2402 | |
| 2403 | /* PathExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2404 | prev_exp = *tok_idx; |
| 2405 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2406 | LY_CHECK_RET(rc); |
| 2407 | |
| 2408 | /* ('|' PathExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2409 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2410 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2411 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2412 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2413 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2414 | LY_CHECK_RET(rc); |
| 2415 | } |
| 2416 | |
| 2417 | return LY_SUCCESS; |
| 2418 | } |
| 2419 | |
| 2420 | /** |
| 2421 | * @brief Reparse AdditiveExpr. Logs directly on error. |
| 2422 | * |
| 2423 | * [15] AdditiveExpr ::= MultiplicativeExpr |
| 2424 | * | AdditiveExpr '+' MultiplicativeExpr |
| 2425 | * | AdditiveExpr '-' MultiplicativeExpr |
| 2426 | * [16] MultiplicativeExpr ::= UnaryExpr |
| 2427 | * | MultiplicativeExpr '*' UnaryExpr |
| 2428 | * | MultiplicativeExpr 'div' UnaryExpr |
| 2429 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 2430 | * |
| 2431 | * @param[in] ctx Context for logging. |
| 2432 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2433 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2434 | * @return LY_ERR |
| 2435 | */ |
| 2436 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2437 | 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] | 2438 | { |
| 2439 | uint16_t prev_add_exp, prev_mul_exp; |
| 2440 | LY_ERR rc; |
| 2441 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2442 | prev_add_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2443 | goto reparse_multiplicative_expr; |
| 2444 | |
| 2445 | /* ('+' / '-' MultiplicativeExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2446 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2447 | ((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] | 2448 | exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2449 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2450 | |
| 2451 | reparse_multiplicative_expr: |
| 2452 | /* UnaryExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2453 | prev_mul_exp = *tok_idx; |
| 2454 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2455 | LY_CHECK_RET(rc); |
| 2456 | |
| 2457 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2458 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2459 | ((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] | 2460 | exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2461 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2462 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2463 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2464 | LY_CHECK_RET(rc); |
| 2465 | } |
| 2466 | } |
| 2467 | |
| 2468 | return LY_SUCCESS; |
| 2469 | } |
| 2470 | |
| 2471 | /** |
| 2472 | * @brief Reparse EqualityExpr. Logs directly on error. |
| 2473 | * |
| 2474 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
| 2475 | * | EqualityExpr '!=' RelationalExpr |
| 2476 | * [14] RelationalExpr ::= AdditiveExpr |
| 2477 | * | RelationalExpr '<' AdditiveExpr |
| 2478 | * | RelationalExpr '>' AdditiveExpr |
| 2479 | * | RelationalExpr '<=' AdditiveExpr |
| 2480 | * | RelationalExpr '>=' AdditiveExpr |
| 2481 | * |
| 2482 | * @param[in] ctx Context for logging. |
| 2483 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2484 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2485 | * @return LY_ERR |
| 2486 | */ |
| 2487 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2488 | 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] | 2489 | { |
| 2490 | uint16_t prev_eq_exp, prev_rel_exp; |
| 2491 | LY_ERR rc; |
| 2492 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2493 | prev_eq_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2494 | goto reparse_additive_expr; |
| 2495 | |
| 2496 | /* ('=' / '!=' RelationalExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2497 | 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] | 2498 | exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2499 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2500 | |
| 2501 | reparse_additive_expr: |
| 2502 | /* AdditiveExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2503 | prev_rel_exp = *tok_idx; |
| 2504 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2505 | LY_CHECK_RET(rc); |
| 2506 | |
| 2507 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2508 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2509 | exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2510 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2511 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2512 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2513 | LY_CHECK_RET(rc); |
| 2514 | } |
| 2515 | } |
| 2516 | |
| 2517 | return LY_SUCCESS; |
| 2518 | } |
| 2519 | |
| 2520 | /** |
| 2521 | * @brief Reparse OrExpr. Logs directly on error. |
| 2522 | * |
| 2523 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 2524 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 2525 | * |
| 2526 | * @param[in] ctx Context for logging. |
| 2527 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2528 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2529 | * @return LY_ERR |
| 2530 | */ |
| 2531 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2532 | 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] | 2533 | { |
| 2534 | uint16_t prev_or_exp, prev_and_exp; |
| 2535 | LY_ERR rc; |
| 2536 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2537 | prev_or_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2538 | goto reparse_equality_expr; |
| 2539 | |
| 2540 | /* ('or' AndExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2541 | 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] | 2542 | exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2543 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2544 | |
| 2545 | reparse_equality_expr: |
| 2546 | /* EqualityExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2547 | prev_and_exp = *tok_idx; |
| 2548 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2549 | LY_CHECK_RET(rc); |
| 2550 | |
| 2551 | /* ('and' EqualityExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2552 | 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] | 2553 | exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2554 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2555 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2556 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2557 | LY_CHECK_RET(rc); |
| 2558 | } |
| 2559 | } |
| 2560 | |
| 2561 | return LY_SUCCESS; |
| 2562 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2563 | |
| 2564 | /** |
| 2565 | * @brief Parse NCName. |
| 2566 | * |
| 2567 | * @param[in] ncname Name to parse. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2568 | * @return Length of @p ncname valid bytes. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2569 | */ |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2570 | static long int |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2571 | parse_ncname(const char *ncname) |
| 2572 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2573 | uint32_t uc; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2574 | size_t size; |
| 2575 | long int len = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2576 | |
| 2577 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0); |
| 2578 | if (!is_xmlqnamestartchar(uc) || (uc == ':')) { |
| 2579 | return len; |
| 2580 | } |
| 2581 | |
| 2582 | do { |
| 2583 | len += size; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2584 | if (!*ncname) { |
| 2585 | break; |
| 2586 | } |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2587 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2588 | } while (is_xmlqnamechar(uc) && (uc != ':')); |
| 2589 | |
| 2590 | return len; |
| 2591 | } |
| 2592 | |
| 2593 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2594 | * @brief Add @p token into the expression @p exp. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2595 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2596 | * @param[in] ctx Context for logging. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2597 | * @param[in] exp Expression to use. |
| 2598 | * @param[in] token Token to add. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2599 | * @param[in] tok_pos Token position in the XPath expression. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2600 | * @param[in] tok_len Token length in the XPath expression. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2601 | * @return LY_ERR |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2602 | */ |
| 2603 | static LY_ERR |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2604 | 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] | 2605 | { |
| 2606 | uint32_t prev; |
| 2607 | |
| 2608 | if (exp->used == exp->size) { |
| 2609 | prev = exp->size; |
| 2610 | exp->size += LYXP_EXPR_SIZE_STEP; |
| 2611 | if (prev > exp->size) { |
| 2612 | LOGINT(ctx); |
| 2613 | return LY_EINT; |
| 2614 | } |
| 2615 | |
| 2616 | exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens); |
| 2617 | LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM); |
| 2618 | exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos); |
| 2619 | LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM); |
| 2620 | exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len); |
| 2621 | LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM); |
| 2622 | } |
| 2623 | |
| 2624 | exp->tokens[exp->used] = token; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2625 | exp->tok_pos[exp->used] = tok_pos; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2626 | exp->tok_len[exp->used] = tok_len; |
| 2627 | ++exp->used; |
| 2628 | return LY_SUCCESS; |
| 2629 | } |
| 2630 | |
| 2631 | void |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2632 | lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2633 | { |
| 2634 | uint16_t i; |
| 2635 | |
| 2636 | if (!expr) { |
| 2637 | return; |
| 2638 | } |
| 2639 | |
| 2640 | lydict_remove(ctx, expr->expr); |
| 2641 | free(expr->tokens); |
| 2642 | free(expr->tok_pos); |
| 2643 | free(expr->tok_len); |
| 2644 | if (expr->repeat) { |
| 2645 | for (i = 0; i < expr->used; ++i) { |
| 2646 | free(expr->repeat[i]); |
| 2647 | } |
| 2648 | } |
| 2649 | free(expr->repeat); |
| 2650 | free(expr); |
| 2651 | } |
| 2652 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2653 | LY_ERR |
| 2654 | 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] | 2655 | { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2656 | LY_ERR ret = LY_SUCCESS; |
| 2657 | struct lyxp_expr *expr; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2658 | size_t parsed = 0, tok_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2659 | enum lyxp_token tok_type; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2660 | ly_bool prev_function_check = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2661 | uint16_t tok_idx = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2662 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2663 | assert(expr_p); |
| 2664 | |
| 2665 | if (!expr_str[0]) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2666 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2667 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2668 | } |
| 2669 | |
| 2670 | if (!expr_len) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2671 | expr_len = strlen(expr_str); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2672 | } |
| 2673 | if (expr_len > UINT16_MAX) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2674 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "XPath expression cannot be longer than %ud characters.", UINT16_MAX); |
| 2675 | return LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2676 | } |
| 2677 | |
| 2678 | /* init lyxp_expr structure */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2679 | expr = calloc(1, sizeof *expr); |
| 2680 | LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error); |
| 2681 | LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error); |
| 2682 | expr->used = 0; |
| 2683 | expr->size = LYXP_EXPR_SIZE_START; |
| 2684 | expr->tokens = malloc(expr->size * sizeof *expr->tokens); |
| 2685 | LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2686 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2687 | expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos); |
| 2688 | 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] | 2689 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2690 | expr->tok_len = malloc(expr->size * sizeof *expr->tok_len); |
| 2691 | 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] | 2692 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2693 | /* make expr 0-terminated */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2694 | expr_str = expr->expr; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2695 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2696 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2697 | ++parsed; |
| 2698 | } |
| 2699 | |
| 2700 | do { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2701 | if (expr_str[parsed] == '(') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2702 | |
| 2703 | /* '(' */ |
| 2704 | tok_len = 1; |
| 2705 | tok_type = LYXP_TOKEN_PAR1; |
| 2706 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2707 | 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] | 2708 | /* it is a NodeType/FunctionName after all */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2709 | if (((expr->tok_len[expr->used - 1] == 4) && |
| 2710 | (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) || |
| 2711 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) || |
| 2712 | ((expr->tok_len[expr->used - 1] == 7) && |
| 2713 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2714 | expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2715 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2716 | expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2717 | } |
| 2718 | prev_function_check = 0; |
| 2719 | } |
| 2720 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2721 | } else if (expr_str[parsed] == ')') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2722 | |
| 2723 | /* ')' */ |
| 2724 | tok_len = 1; |
| 2725 | tok_type = LYXP_TOKEN_PAR2; |
| 2726 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2727 | } else if (expr_str[parsed] == '[') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2728 | |
| 2729 | /* '[' */ |
| 2730 | tok_len = 1; |
| 2731 | tok_type = LYXP_TOKEN_BRACK1; |
| 2732 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2733 | } else if (expr_str[parsed] == ']') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2734 | |
| 2735 | /* ']' */ |
| 2736 | tok_len = 1; |
| 2737 | tok_type = LYXP_TOKEN_BRACK2; |
| 2738 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2739 | } else if (!strncmp(&expr_str[parsed], "..", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2740 | |
| 2741 | /* '..' */ |
| 2742 | tok_len = 2; |
| 2743 | tok_type = LYXP_TOKEN_DDOT; |
| 2744 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2745 | } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2746 | |
| 2747 | /* '.' */ |
| 2748 | tok_len = 1; |
| 2749 | tok_type = LYXP_TOKEN_DOT; |
| 2750 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2751 | } else if (expr_str[parsed] == '@') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2752 | |
| 2753 | /* '@' */ |
| 2754 | tok_len = 1; |
| 2755 | tok_type = LYXP_TOKEN_AT; |
| 2756 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2757 | } else if (expr_str[parsed] == ',') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2758 | |
| 2759 | /* ',' */ |
| 2760 | tok_len = 1; |
| 2761 | tok_type = LYXP_TOKEN_COMMA; |
| 2762 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2763 | } else if (expr_str[parsed] == '\'') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2764 | |
| 2765 | /* Literal with ' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2766 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {} |
| 2767 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2768 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID, |
| 2769 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2770 | ++tok_len; |
| 2771 | tok_type = LYXP_TOKEN_LITERAL; |
| 2772 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2773 | } else if (expr_str[parsed] == '\"') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2774 | |
| 2775 | /* Literal with " */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2776 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {} |
| 2777 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2778 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID, |
| 2779 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2780 | ++tok_len; |
| 2781 | tok_type = LYXP_TOKEN_LITERAL; |
| 2782 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2783 | } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2784 | |
| 2785 | /* Number */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2786 | for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
| 2787 | if (expr_str[parsed + tok_len] == '.') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2788 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2789 | for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2790 | } |
| 2791 | tok_type = LYXP_TOKEN_NUMBER; |
| 2792 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2793 | } else if (expr_str[parsed] == '/') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2794 | |
| 2795 | /* Operator '/', '//' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2796 | if (!strncmp(&expr_str[parsed], "//", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2797 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2798 | tok_type = LYXP_TOKEN_OPER_RPATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2799 | } else { |
| 2800 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2801 | tok_type = LYXP_TOKEN_OPER_PATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2802 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2803 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2804 | } else if (!strncmp(&expr_str[parsed], "!=", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2805 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2806 | /* Operator '!=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2807 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2808 | tok_type = LYXP_TOKEN_OPER_NEQUAL; |
| 2809 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2810 | } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2811 | |
| 2812 | /* Operator '<=', '>=' */ |
| 2813 | tok_len = 2; |
| 2814 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2815 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2816 | } else if (expr_str[parsed] == '|') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2817 | |
| 2818 | /* Operator '|' */ |
| 2819 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2820 | tok_type = LYXP_TOKEN_OPER_UNI; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2821 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2822 | } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2823 | |
| 2824 | /* Operator '+', '-' */ |
| 2825 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2826 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2827 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2828 | } else if (expr_str[parsed] == '=') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2829 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2830 | /* Operator '=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2831 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2832 | tok_type = LYXP_TOKEN_OPER_EQUAL; |
| 2833 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2834 | } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2835 | |
| 2836 | /* Operator '<', '>' */ |
| 2837 | tok_len = 1; |
| 2838 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2839 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2840 | } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) && |
| 2841 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) && |
| 2842 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) && |
| 2843 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) && |
| 2844 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) && |
| 2845 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) && |
| 2846 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) && |
| 2847 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) && |
| 2848 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) && |
| 2849 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) && |
| 2850 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) && |
| 2851 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2852 | |
| 2853 | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2854 | if (expr_str[parsed] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2855 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2856 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2857 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2858 | } else if (!strncmp(&expr_str[parsed], "or", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2859 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2860 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2861 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2862 | } else if (!strncmp(&expr_str[parsed], "and", 3)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2863 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2864 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2865 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2866 | } 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] | 2867 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2868 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2869 | |
| 2870 | } else if (prev_function_check) { |
Michal Vasko | 5307857 | 2019-05-24 08:50:15 +0200 | [diff] [blame] | 2871 | 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] | 2872 | 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] | 2873 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2874 | goto error; |
| 2875 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2876 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed + 1, expr_str); |
| 2877 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2878 | goto error; |
| 2879 | } |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2880 | } else if (expr_str[parsed] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2881 | |
| 2882 | /* NameTest '*' */ |
| 2883 | tok_len = 1; |
| 2884 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2885 | |
| 2886 | } else { |
| 2887 | |
| 2888 | /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2889 | long int ncname_len = parse_ncname(&expr_str[parsed]); |
| 2890 | LY_CHECK_ERR_GOTO(ncname_len < 0, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2891 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID, |
| 2892 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2893 | tok_len = ncname_len; |
| 2894 | |
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; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2897 | if (expr_str[parsed + tok_len] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2898 | ++tok_len; |
| 2899 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2900 | ncname_len = parse_ncname(&expr_str[parsed + tok_len]); |
| 2901 | LY_CHECK_ERR_GOTO(ncname_len < 0, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2902 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID, |
| 2903 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2904 | tok_len += ncname_len; |
| 2905 | } |
| 2906 | /* remove old flag to prevent ambiguities */ |
| 2907 | prev_function_check = 0; |
| 2908 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2909 | } else { |
| 2910 | /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */ |
| 2911 | prev_function_check = 1; |
| 2912 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2913 | } |
| 2914 | } |
| 2915 | |
| 2916 | /* store the token, move on to the next one */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2917 | 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] | 2918 | parsed += tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2919 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2920 | ++parsed; |
| 2921 | } |
| 2922 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2923 | } while (expr_str[parsed]); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2924 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2925 | if (reparse) { |
| 2926 | /* prealloc repeat */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2927 | expr->repeat = calloc(expr->size, sizeof *expr->repeat); |
| 2928 | LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2929 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2930 | /* fill repeat */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2931 | LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx), ret = LY_EVALID, error); |
| 2932 | if (expr->used > tok_idx) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2933 | 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] | 2934 | &expr->expr[expr->tok_pos[tok_idx]]); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2935 | ret = LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2936 | goto error; |
| 2937 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2938 | } |
| 2939 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2940 | print_expr_struct_debug(expr); |
| 2941 | *expr_p = expr; |
| 2942 | return LY_SUCCESS; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2943 | |
| 2944 | error: |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2945 | lyxp_expr_free(ctx, expr); |
| 2946 | return ret; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2947 | } |
| 2948 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2949 | LY_ERR |
| 2950 | 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] | 2951 | { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2952 | LY_ERR ret = LY_SUCCESS; |
| 2953 | struct lyxp_expr *dup = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2954 | uint32_t i, j; |
| 2955 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2956 | if (!exp) { |
| 2957 | goto cleanup; |
| 2958 | } |
| 2959 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2960 | dup = calloc(1, sizeof *dup); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2961 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2962 | |
| 2963 | dup->tokens = malloc(exp->used * sizeof *dup->tokens); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2964 | LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2965 | memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens); |
| 2966 | |
| 2967 | dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2968 | 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] | 2969 | memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos); |
| 2970 | |
| 2971 | dup->tok_len = malloc(exp->used * sizeof *dup->tok_len); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2972 | 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] | 2973 | memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len); |
| 2974 | |
| 2975 | dup->repeat = malloc(exp->used * sizeof *dup->repeat); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2976 | LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2977 | for (i = 0; i < exp->used; ++i) { |
| 2978 | if (!exp->repeat[i]) { |
| 2979 | dup->repeat[i] = NULL; |
| 2980 | } else { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2981 | for (j = 0; exp->repeat[i][j]; ++j) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2982 | /* the ending 0 as well */ |
| 2983 | ++j; |
| 2984 | |
Michal Vasko | 99c7164 | 2020-07-03 13:33:36 +0200 | [diff] [blame] | 2985 | dup->repeat[i] = malloc(j * sizeof **dup->repeat); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2986 | 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] | 2987 | memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat); |
| 2988 | dup->repeat[i][j - 1] = 0; |
| 2989 | } |
| 2990 | } |
| 2991 | |
| 2992 | dup->used = exp->used; |
| 2993 | dup->size = exp->used; |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2994 | 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] | 2995 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2996 | cleanup: |
| 2997 | if (ret) { |
| 2998 | lyxp_expr_free(ctx, dup); |
| 2999 | } else { |
| 3000 | *dup_p = dup; |
| 3001 | } |
| 3002 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3003 | } |
| 3004 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3005 | /* |
| 3006 | * warn functions |
| 3007 | * |
| 3008 | * Warn functions check specific reasonable conditions for schema XPath |
| 3009 | * and print a warning if they are not satisfied. |
| 3010 | */ |
| 3011 | |
| 3012 | /** |
| 3013 | * @brief Get the last-added schema node that is currently in the context. |
| 3014 | * |
| 3015 | * @param[in] set Set to search in. |
| 3016 | * @return Last-added schema context node, NULL if no node is in context. |
| 3017 | */ |
| 3018 | static struct lysc_node * |
| 3019 | warn_get_scnode_in_ctx(struct lyxp_set *set) |
| 3020 | { |
| 3021 | uint32_t i; |
| 3022 | |
| 3023 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
| 3024 | return NULL; |
| 3025 | } |
| 3026 | |
| 3027 | i = set->used; |
| 3028 | do { |
| 3029 | --i; |
| 3030 | if (set->val.scnodes[i].in_ctx == 1) { |
| 3031 | /* if there are more, simply return the first found (last added) */ |
| 3032 | return set->val.scnodes[i].scnode; |
| 3033 | } |
| 3034 | } while (i); |
| 3035 | |
| 3036 | return NULL; |
| 3037 | } |
| 3038 | |
| 3039 | /** |
| 3040 | * @brief Test whether a type is numeric - integer type or decimal64. |
| 3041 | * |
| 3042 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3043 | * @return Boolean value whether @p type is numeric type or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3044 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3045 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3046 | warn_is_numeric_type(struct lysc_type *type) |
| 3047 | { |
| 3048 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3049 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3050 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3051 | |
| 3052 | switch (type->basetype) { |
| 3053 | case LY_TYPE_DEC64: |
| 3054 | case LY_TYPE_INT8: |
| 3055 | case LY_TYPE_UINT8: |
| 3056 | case LY_TYPE_INT16: |
| 3057 | case LY_TYPE_UINT16: |
| 3058 | case LY_TYPE_INT32: |
| 3059 | case LY_TYPE_UINT32: |
| 3060 | case LY_TYPE_INT64: |
| 3061 | case LY_TYPE_UINT64: |
| 3062 | return 1; |
| 3063 | case LY_TYPE_UNION: |
| 3064 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3065 | LY_ARRAY_FOR(uni->types, u) { |
| 3066 | ret = warn_is_numeric_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3067 | if (ret) { |
| 3068 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3069 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3070 | } |
| 3071 | } |
| 3072 | /* did not find any suitable type */ |
| 3073 | return 0; |
| 3074 | case LY_TYPE_LEAFREF: |
| 3075 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
| 3076 | default: |
| 3077 | return 0; |
| 3078 | } |
| 3079 | } |
| 3080 | |
| 3081 | /** |
| 3082 | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
| 3083 | * |
| 3084 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3085 | * @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] | 3086 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3087 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3088 | warn_is_string_type(struct lysc_type *type) |
| 3089 | { |
| 3090 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3091 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3092 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3093 | |
| 3094 | switch (type->basetype) { |
| 3095 | case LY_TYPE_BITS: |
| 3096 | case LY_TYPE_ENUM: |
| 3097 | case LY_TYPE_IDENT: |
| 3098 | case LY_TYPE_INST: |
| 3099 | case LY_TYPE_STRING: |
| 3100 | return 1; |
| 3101 | case LY_TYPE_UNION: |
| 3102 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3103 | LY_ARRAY_FOR(uni->types, u) { |
| 3104 | ret = warn_is_string_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3105 | if (ret) { |
| 3106 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3107 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3108 | } |
| 3109 | } |
| 3110 | /* did not find any suitable type */ |
| 3111 | return 0; |
| 3112 | case LY_TYPE_LEAFREF: |
| 3113 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
| 3114 | default: |
| 3115 | return 0; |
| 3116 | } |
| 3117 | } |
| 3118 | |
| 3119 | /** |
| 3120 | * @brief Test whether a type is one specific type. |
| 3121 | * |
| 3122 | * @param[in] type Type to test. |
| 3123 | * @param[in] base Expected type. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3124 | * @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] | 3125 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3126 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3127 | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
| 3128 | { |
| 3129 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3130 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3131 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3132 | |
| 3133 | if (type->basetype == base) { |
| 3134 | return 1; |
| 3135 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3136 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3137 | LY_ARRAY_FOR(uni->types, u) { |
| 3138 | ret = warn_is_specific_type(uni->types[u], base); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3139 | if (ret) { |
| 3140 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3141 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3142 | } |
| 3143 | } |
| 3144 | /* did not find any suitable type */ |
| 3145 | return 0; |
| 3146 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
| 3147 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
| 3148 | } |
| 3149 | |
| 3150 | return 0; |
| 3151 | } |
| 3152 | |
| 3153 | /** |
| 3154 | * @brief Get next type of a (union) type. |
| 3155 | * |
| 3156 | * @param[in] type Base type. |
| 3157 | * @param[in] prev_type Previously returned type. |
| 3158 | * @return Next type or NULL. |
| 3159 | */ |
| 3160 | static struct lysc_type * |
| 3161 | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type) |
| 3162 | { |
| 3163 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3164 | ly_bool found = 0; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3165 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3166 | |
| 3167 | switch (type->basetype) { |
| 3168 | case LY_TYPE_UNION: |
| 3169 | uni = (struct lysc_type_union *)type; |
| 3170 | if (!prev_type) { |
| 3171 | return uni->types[0]; |
| 3172 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3173 | LY_ARRAY_FOR(uni->types, u) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3174 | if (found) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3175 | return uni->types[u]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3176 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3177 | if (prev_type == uni->types[u]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3178 | found = 1; |
| 3179 | } |
| 3180 | } |
| 3181 | return NULL; |
| 3182 | default: |
| 3183 | if (prev_type) { |
| 3184 | assert(type == prev_type); |
| 3185 | return NULL; |
| 3186 | } else { |
| 3187 | return type; |
| 3188 | } |
| 3189 | } |
| 3190 | } |
| 3191 | |
| 3192 | /** |
| 3193 | * @brief Test whether 2 types have a common type. |
| 3194 | * |
| 3195 | * @param[in] type1 First type. |
| 3196 | * @param[in] type2 Second type. |
| 3197 | * @return 1 if they do, 0 otherwise. |
| 3198 | */ |
| 3199 | static int |
| 3200 | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
| 3201 | { |
| 3202 | struct lysc_type *t1, *rt1, *t2, *rt2; |
| 3203 | |
| 3204 | t1 = NULL; |
| 3205 | while ((t1 = warn_is_equal_type_next_type(type1, t1))) { |
| 3206 | if (t1->basetype == LY_TYPE_LEAFREF) { |
| 3207 | rt1 = ((struct lysc_type_leafref *)t1)->realtype; |
| 3208 | } else { |
| 3209 | rt1 = t1; |
| 3210 | } |
| 3211 | |
| 3212 | t2 = NULL; |
| 3213 | while ((t2 = warn_is_equal_type_next_type(type2, t2))) { |
| 3214 | if (t2->basetype == LY_TYPE_LEAFREF) { |
| 3215 | rt2 = ((struct lysc_type_leafref *)t2)->realtype; |
| 3216 | } else { |
| 3217 | rt2 = t2; |
| 3218 | } |
| 3219 | |
| 3220 | if (rt2->basetype == rt1->basetype) { |
| 3221 | /* match found */ |
| 3222 | return 1; |
| 3223 | } |
| 3224 | } |
| 3225 | } |
| 3226 | |
| 3227 | return 0; |
| 3228 | } |
| 3229 | |
| 3230 | /** |
| 3231 | * @brief Check both operands of comparison operators. |
| 3232 | * |
| 3233 | * @param[in] ctx Context for errors. |
| 3234 | * @param[in] set1 First operand set. |
| 3235 | * @param[in] set2 Second operand set. |
| 3236 | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
| 3237 | * @param[in] expr Start of the expression to print with the warning. |
| 3238 | * @param[in] tok_pos Token position. |
| 3239 | */ |
| 3240 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3241 | 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] | 3242 | { |
| 3243 | struct lysc_node_leaf *node1, *node2; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3244 | ly_bool leaves = 1, warning = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3245 | |
| 3246 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
| 3247 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
| 3248 | |
| 3249 | if (!node1 && !node2) { |
| 3250 | /* no node-sets involved, nothing to do */ |
| 3251 | return; |
| 3252 | } |
| 3253 | |
| 3254 | if (node1) { |
| 3255 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3256 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
| 3257 | warning = 1; |
| 3258 | leaves = 0; |
| 3259 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
| 3260 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
| 3261 | warning = 1; |
| 3262 | } |
| 3263 | } |
| 3264 | |
| 3265 | if (node2) { |
| 3266 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3267 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
| 3268 | warning = 1; |
| 3269 | leaves = 0; |
| 3270 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
| 3271 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
| 3272 | warning = 1; |
| 3273 | } |
| 3274 | } |
| 3275 | |
| 3276 | if (node1 && node2 && leaves && !numbers_only) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3277 | if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) || |
| 3278 | (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) || |
| 3279 | (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) && |
| 3280 | !warn_is_equal_type(node1->type, node2->type))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3281 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
| 3282 | warning = 1; |
| 3283 | } |
| 3284 | } |
| 3285 | |
| 3286 | if (warning) { |
| 3287 | LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos); |
| 3288 | } |
| 3289 | } |
| 3290 | |
| 3291 | /** |
| 3292 | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
| 3293 | * |
| 3294 | * @param[in] exp Parsed XPath expression. |
| 3295 | * @param[in] set Set with the leaf/leaf-list. |
| 3296 | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
| 3297 | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
| 3298 | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
| 3299 | */ |
| 3300 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 3301 | warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, |
| 3302 | uint16_t last_equal_exp) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3303 | { |
| 3304 | struct lysc_node *scnode; |
| 3305 | struct lysc_type *type; |
| 3306 | char *value; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3307 | struct lyd_value storage; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3308 | LY_ERR rc; |
| 3309 | struct ly_err_item *err = NULL; |
| 3310 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3311 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && |
| 3312 | ((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] | 3313 | /* check that the node can have the specified value */ |
| 3314 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
| 3315 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
| 3316 | } else { |
| 3317 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
| 3318 | } |
| 3319 | if (!value) { |
| 3320 | LOGMEM(set->ctx); |
| 3321 | return; |
| 3322 | } |
| 3323 | |
| 3324 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
| 3325 | 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] | 3326 | " 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] | 3327 | 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] | 3328 | (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] | 3329 | exp->expr + exp->tok_pos[equal_exp]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3330 | } |
| 3331 | |
| 3332 | type = ((struct lysc_node_leaf *)scnode)->type; |
| 3333 | if (type->basetype != LY_TYPE_IDENT) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3334 | 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] | 3335 | LYD_HINT_DATA, scnode, &storage, &err); |
Michal Vasko | bf42e83 | 2020-11-23 16:59:42 +0100 | [diff] [blame^] | 3336 | if (rc == LY_EINCOMPLETE) { |
| 3337 | rc = LY_SUCCESS; |
| 3338 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3339 | |
| 3340 | if (err) { |
| 3341 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
| 3342 | ly_err_free(err); |
| 3343 | } else if (rc != LY_SUCCESS) { |
| 3344 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
| 3345 | } |
| 3346 | if (rc != LY_SUCCESS) { |
| 3347 | 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] | 3348 | (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] | 3349 | exp->expr + exp->tok_pos[equal_exp]); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3350 | } else { |
| 3351 | type->plugin->free(set->ctx, &storage); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3352 | } |
| 3353 | } |
| 3354 | free(value); |
| 3355 | } |
| 3356 | } |
| 3357 | |
| 3358 | /* |
| 3359 | * XPath functions |
| 3360 | */ |
| 3361 | |
| 3362 | /** |
| 3363 | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
| 3364 | * depending on whether the first node bit value from the second argument is set. |
| 3365 | * |
| 3366 | * @param[in] args Array of arguments. |
| 3367 | * @param[in] arg_count Count of elements in @p args. |
| 3368 | * @param[in,out] set Context and result set at the same time. |
| 3369 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3370 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3371 | */ |
| 3372 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3373 | 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] | 3374 | { |
| 3375 | struct lyd_node_term *leaf; |
| 3376 | struct lysc_node_leaf *sleaf; |
| 3377 | struct lysc_type_bits *bits; |
| 3378 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3379 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3380 | |
| 3381 | if (options & LYXP_SCNODE_ALL) { |
| 3382 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3383 | 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] | 3384 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3385 | 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] | 3386 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
| 3387 | 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] | 3388 | } |
| 3389 | |
| 3390 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3391 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3392 | 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] | 3393 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3394 | 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] | 3395 | } |
| 3396 | } |
| 3397 | set_scnode_clear_ctx(set); |
| 3398 | return rc; |
| 3399 | } |
| 3400 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3401 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3402 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 3403 | "bit-is-set(node-set, string)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3404 | return LY_EVALID; |
| 3405 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3406 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3407 | LY_CHECK_RET(rc); |
| 3408 | |
| 3409 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3410 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3411 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3412 | if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && |
| 3413 | (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3414 | bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3415 | LY_ARRAY_FOR(bits->bits, u) { |
| 3416 | if (!strcmp(bits->bits[u].name, args[1]->val.str)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3417 | set_fill_boolean(set, 1); |
| 3418 | break; |
| 3419 | } |
| 3420 | } |
| 3421 | } |
| 3422 | } |
| 3423 | |
| 3424 | return LY_SUCCESS; |
| 3425 | } |
| 3426 | |
| 3427 | /** |
| 3428 | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
| 3429 | * with the argument converted to boolean. |
| 3430 | * |
| 3431 | * @param[in] args Array of arguments. |
| 3432 | * @param[in] arg_count Count of elements in @p args. |
| 3433 | * @param[in,out] set Context and result set at the same time. |
| 3434 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3435 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3436 | */ |
| 3437 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3438 | 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] | 3439 | { |
| 3440 | LY_ERR rc; |
| 3441 | |
| 3442 | if (options & LYXP_SCNODE_ALL) { |
| 3443 | set_scnode_clear_ctx(set); |
| 3444 | return LY_SUCCESS; |
| 3445 | } |
| 3446 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3447 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3448 | LY_CHECK_RET(rc); |
| 3449 | set_fill_set(set, args[0]); |
| 3450 | |
| 3451 | return LY_SUCCESS; |
| 3452 | } |
| 3453 | |
| 3454 | /** |
| 3455 | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
| 3456 | * with the first argument rounded up to the nearest integer. |
| 3457 | * |
| 3458 | * @param[in] args Array of arguments. |
| 3459 | * @param[in] arg_count Count of elements in @p args. |
| 3460 | * @param[in,out] set Context and result set at the same time. |
| 3461 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3462 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3463 | */ |
| 3464 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3465 | 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] | 3466 | { |
| 3467 | struct lysc_node_leaf *sleaf; |
| 3468 | LY_ERR rc = LY_SUCCESS; |
| 3469 | |
| 3470 | if (options & LYXP_SCNODE_ALL) { |
| 3471 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3472 | 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] | 3473 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3474 | 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] | 3475 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 3476 | 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] | 3477 | } |
| 3478 | set_scnode_clear_ctx(set); |
| 3479 | return rc; |
| 3480 | } |
| 3481 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3482 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3483 | LY_CHECK_RET(rc); |
| 3484 | if ((long long)args[0]->val.num != args[0]->val.num) { |
| 3485 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
| 3486 | } else { |
| 3487 | set_fill_number(set, args[0]->val.num); |
| 3488 | } |
| 3489 | |
| 3490 | return LY_SUCCESS; |
| 3491 | } |
| 3492 | |
| 3493 | /** |
| 3494 | * @brief Execute the XPath concat(string, string, string*) function. |
| 3495 | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
| 3496 | * |
| 3497 | * @param[in] args Array of arguments. |
| 3498 | * @param[in] arg_count Count of elements in @p args. |
| 3499 | * @param[in,out] set Context and result set at the same time. |
| 3500 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3501 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3502 | */ |
| 3503 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3504 | 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] | 3505 | { |
| 3506 | uint16_t i; |
| 3507 | char *str = NULL; |
| 3508 | size_t used = 1; |
| 3509 | LY_ERR rc = LY_SUCCESS; |
| 3510 | struct lysc_node_leaf *sleaf; |
| 3511 | |
| 3512 | if (options & LYXP_SCNODE_ALL) { |
| 3513 | for (i = 0; i < arg_count; ++i) { |
| 3514 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
| 3515 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3516 | LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3517 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3518 | } else if (!warn_is_string_type(sleaf->type)) { |
Radek Krejci | 70124c8 | 2020-08-14 22:17:03 +0200 | [diff] [blame] | 3519 | 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] | 3520 | } |
| 3521 | } |
| 3522 | } |
| 3523 | set_scnode_clear_ctx(set); |
| 3524 | return rc; |
| 3525 | } |
| 3526 | |
| 3527 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3528 | rc = lyxp_set_cast(args[i], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3529 | if (rc != LY_SUCCESS) { |
| 3530 | free(str); |
| 3531 | return rc; |
| 3532 | } |
| 3533 | |
| 3534 | str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char)); |
| 3535 | LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM); |
| 3536 | strcpy(str + used - 1, args[i]->val.str); |
| 3537 | used += strlen(args[i]->val.str); |
| 3538 | } |
| 3539 | |
| 3540 | /* free, kind of */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3541 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3542 | set->type = LYXP_SET_STRING; |
| 3543 | set->val.str = str; |
| 3544 | |
| 3545 | return LY_SUCCESS; |
| 3546 | } |
| 3547 | |
| 3548 | /** |
| 3549 | * @brief Execute the XPath contains(string, string) function. |
| 3550 | * Returns LYXP_SET_BOOLEAN whether the second argument can |
| 3551 | * be found in the first or not. |
| 3552 | * |
| 3553 | * @param[in] args Array of arguments. |
| 3554 | * @param[in] arg_count Count of elements in @p args. |
| 3555 | * @param[in,out] set Context and result set at the same time. |
| 3556 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3557 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3558 | */ |
| 3559 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3560 | 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] | 3561 | { |
| 3562 | struct lysc_node_leaf *sleaf; |
| 3563 | LY_ERR rc = LY_SUCCESS; |
| 3564 | |
| 3565 | if (options & LYXP_SCNODE_ALL) { |
| 3566 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3567 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3568 | 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] | 3569 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3570 | 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] | 3571 | } |
| 3572 | } |
| 3573 | |
| 3574 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3575 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3576 | 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] | 3577 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3578 | 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] | 3579 | } |
| 3580 | } |
| 3581 | set_scnode_clear_ctx(set); |
| 3582 | return rc; |
| 3583 | } |
| 3584 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3585 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3586 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3587 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3588 | LY_CHECK_RET(rc); |
| 3589 | |
| 3590 | if (strstr(args[0]->val.str, args[1]->val.str)) { |
| 3591 | set_fill_boolean(set, 1); |
| 3592 | } else { |
| 3593 | set_fill_boolean(set, 0); |
| 3594 | } |
| 3595 | |
| 3596 | return LY_SUCCESS; |
| 3597 | } |
| 3598 | |
| 3599 | /** |
| 3600 | * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER |
| 3601 | * with the size of the node-set from the argument. |
| 3602 | * |
| 3603 | * @param[in] args Array of arguments. |
| 3604 | * @param[in] arg_count Count of elements in @p args. |
| 3605 | * @param[in,out] set Context and result set at the same time. |
| 3606 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3607 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3608 | */ |
| 3609 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3610 | 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] | 3611 | { |
| 3612 | struct lysc_node *scnode = NULL; |
| 3613 | LY_ERR rc = LY_SUCCESS; |
| 3614 | |
| 3615 | if (options & LYXP_SCNODE_ALL) { |
| 3616 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) { |
| 3617 | 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] | 3618 | } |
| 3619 | set_scnode_clear_ctx(set); |
| 3620 | return rc; |
| 3621 | } |
| 3622 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3623 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3624 | 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] | 3625 | return LY_EVALID; |
| 3626 | } |
| 3627 | |
| 3628 | set_fill_number(set, args[0]->used); |
| 3629 | return LY_SUCCESS; |
| 3630 | } |
| 3631 | |
| 3632 | /** |
| 3633 | * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET |
| 3634 | * with the context with the intial node. |
| 3635 | * |
| 3636 | * @param[in] args Array of arguments. |
| 3637 | * @param[in] arg_count Count of elements in @p args. |
| 3638 | * @param[in,out] set Context and result set at the same time. |
| 3639 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3640 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3641 | */ |
| 3642 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3643 | 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] | 3644 | { |
| 3645 | if (arg_count || args) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3646 | 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] | 3647 | return LY_EVALID; |
| 3648 | } |
| 3649 | |
| 3650 | if (options & LYXP_SCNODE_ALL) { |
| 3651 | set_scnode_clear_ctx(set); |
| 3652 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3653 | 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] | 3654 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3655 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3656 | |
| 3657 | /* position is filled later */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3658 | set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3659 | } |
| 3660 | |
| 3661 | return LY_SUCCESS; |
| 3662 | } |
| 3663 | |
| 3664 | /** |
| 3665 | * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either |
| 3666 | * leafref or instance-identifier target node(s). |
| 3667 | * |
| 3668 | * @param[in] args Array of arguments. |
| 3669 | * @param[in] arg_count Count of elements in @p args. |
| 3670 | * @param[in,out] set Context and result set at the same time. |
| 3671 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3672 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3673 | */ |
| 3674 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3675 | 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] | 3676 | { |
| 3677 | struct lyd_node_term *leaf; |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3678 | struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3679 | struct lysc_type_leafref *lref; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3680 | const struct lysc_node *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3681 | struct ly_path *p; |
| 3682 | struct lyd_node *node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3683 | char *errmsg = NULL; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3684 | uint8_t oper; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3685 | LY_ERR rc = LY_SUCCESS; |
| 3686 | |
| 3687 | if (options & LYXP_SCNODE_ALL) { |
| 3688 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3689 | 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] | 3690 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3691 | 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] | 3692 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
| 3693 | 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] | 3694 | __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3695 | } |
| 3696 | set_scnode_clear_ctx(set); |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3697 | if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3698 | lref = (struct lysc_type_leafref *)sleaf->type; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3699 | 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] | 3700 | |
| 3701 | /* it was already evaluated on schema, it must succeed */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3702 | rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, |
| 3703 | 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] | 3704 | assert(!rc); |
| 3705 | |
| 3706 | /* get the target node */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3707 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3708 | ly_path_free(set->ctx, p); |
| 3709 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 3710 | 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] | 3711 | } |
| 3712 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3713 | return rc; |
| 3714 | } |
| 3715 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3716 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3717 | 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] | 3718 | return LY_EVALID; |
| 3719 | } |
| 3720 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3721 | lyxp_set_free_content(set); |
| 3722 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3723 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3724 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3725 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 3726 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3727 | /* find leafref target */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3728 | 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] | 3729 | &leaf->value, set->tree, &node, &errmsg)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3730 | LOGERR(set->ctx, LY_EVALID, errmsg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3731 | free(errmsg); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3732 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3733 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3734 | } else { |
| 3735 | assert(sleaf->type->basetype == LY_TYPE_INST); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3736 | if (ly_path_eval(leaf->value.target, set->tree, &node)) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3737 | 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] | 3738 | LYD_CANON_VALUE(leaf)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3739 | return LY_EVALID; |
| 3740 | } |
| 3741 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3742 | |
| 3743 | /* insert it */ |
| 3744 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3745 | } |
| 3746 | } |
| 3747 | |
| 3748 | return LY_SUCCESS; |
| 3749 | } |
| 3750 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3751 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3752 | 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] | 3753 | { |
| 3754 | uint16_t i; |
| 3755 | LY_ARRAY_COUNT_TYPE u; |
| 3756 | struct lyd_node_term *leaf; |
| 3757 | struct lysc_node_leaf *sleaf; |
| 3758 | struct lyd_meta *meta; |
| 3759 | struct lyd_value data = {0}, *val; |
| 3760 | struct ly_err_item *err = NULL; |
| 3761 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3762 | ly_bool found; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3763 | |
| 3764 | if (options & LYXP_SCNODE_ALL) { |
| 3765 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3766 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func); |
| 3767 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3768 | 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] | 3769 | sleaf->name); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3770 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 3771 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name); |
| 3772 | } |
| 3773 | |
| 3774 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3775 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3776 | 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] | 3777 | sleaf->name); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3778 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3779 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name); |
| 3780 | } |
| 3781 | } |
| 3782 | set_scnode_clear_ctx(set); |
| 3783 | return rc; |
| 3784 | } |
| 3785 | |
| 3786 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3787 | 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] | 3788 | "derived-from(-or-self)(node-set, string)"); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3789 | return LY_EVALID; |
| 3790 | } |
| 3791 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
| 3792 | LY_CHECK_RET(rc); |
| 3793 | |
| 3794 | set_fill_boolean(set, 0); |
| 3795 | found = 0; |
| 3796 | for (i = 0; i < args[0]->used; ++i) { |
| 3797 | if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) { |
| 3798 | continue; |
| 3799 | } |
| 3800 | |
| 3801 | if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 3802 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 3803 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3804 | val = &leaf->value; |
| 3805 | if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) { |
| 3806 | /* uninteresting */ |
| 3807 | continue; |
| 3808 | } |
| 3809 | |
| 3810 | /* store args[1] as ident */ |
| 3811 | 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] | 3812 | 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] | 3813 | } else { |
| 3814 | meta = args[0]->val.meta[i].meta; |
| 3815 | val = &meta->value; |
| 3816 | if (val->realtype->basetype != LY_TYPE_IDENT) { |
| 3817 | /* uninteresting */ |
| 3818 | continue; |
| 3819 | } |
| 3820 | |
| 3821 | /* store args[1] as ident */ |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 3822 | 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] | 3823 | 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] | 3824 | } |
| 3825 | |
| 3826 | if (err) { |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 3827 | ly_err_print(set->ctx, err); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3828 | ly_err_free(err); |
| 3829 | } |
| 3830 | LY_CHECK_RET(rc); |
| 3831 | |
| 3832 | /* finally check the identity itself */ |
| 3833 | if (self_match && (data.ident == val->ident)) { |
| 3834 | set_fill_boolean(set, 1); |
| 3835 | found = 1; |
| 3836 | } |
| 3837 | if (!found) { |
| 3838 | LY_ARRAY_FOR(data.ident->derived, u) { |
| 3839 | if (data.ident->derived[u] == val->ident) { |
| 3840 | set_fill_boolean(set, 1); |
| 3841 | found = 1; |
| 3842 | break; |
| 3843 | } |
| 3844 | } |
| 3845 | } |
| 3846 | |
| 3847 | /* free temporary value */ |
| 3848 | val->realtype->plugin->free(set->ctx, &data); |
| 3849 | if (found) { |
| 3850 | break; |
| 3851 | } |
| 3852 | } |
| 3853 | |
| 3854 | return LY_SUCCESS; |
| 3855 | } |
| 3856 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3857 | /** |
| 3858 | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3859 | * on whether the first argument nodes contain a node of an identity derived from the second |
| 3860 | * argument identity. |
| 3861 | * |
| 3862 | * @param[in] args Array of arguments. |
| 3863 | * @param[in] arg_count Count of elements in @p args. |
| 3864 | * @param[in,out] set Context and result set at the same time. |
| 3865 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3866 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3867 | */ |
| 3868 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3869 | 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] | 3870 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3871 | return xpath_derived_(args, set, options, 0, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3872 | } |
| 3873 | |
| 3874 | /** |
| 3875 | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3876 | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
| 3877 | * the second argument identity. |
| 3878 | * |
| 3879 | * @param[in] args Array of arguments. |
| 3880 | * @param[in] arg_count Count of elements in @p args. |
| 3881 | * @param[in,out] set Context and result set at the same time. |
| 3882 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3883 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3884 | */ |
| 3885 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3886 | 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] | 3887 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3888 | return xpath_derived_(args, set, options, 1, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3889 | } |
| 3890 | |
| 3891 | /** |
| 3892 | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
| 3893 | * with the integer value of the first node's enum value, otherwise NaN. |
| 3894 | * |
| 3895 | * @param[in] args Array of arguments. |
| 3896 | * @param[in] arg_count Count of elements in @p args. |
| 3897 | * @param[in,out] set Context and result set at the same time. |
| 3898 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3899 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3900 | */ |
| 3901 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3902 | 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] | 3903 | { |
| 3904 | struct lyd_node_term *leaf; |
| 3905 | struct lysc_node_leaf *sleaf; |
| 3906 | LY_ERR rc = LY_SUCCESS; |
| 3907 | |
| 3908 | if (options & LYXP_SCNODE_ALL) { |
| 3909 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3910 | 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] | 3911 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3912 | 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] | 3913 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
| 3914 | 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] | 3915 | } |
| 3916 | set_scnode_clear_ctx(set); |
| 3917 | return rc; |
| 3918 | } |
| 3919 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3920 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3921 | 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] | 3922 | return LY_EVALID; |
| 3923 | } |
| 3924 | |
| 3925 | set_fill_number(set, NAN); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3926 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3927 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3928 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3929 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
| 3930 | set_fill_number(set, leaf->value.enum_item->value); |
| 3931 | } |
| 3932 | } |
| 3933 | |
| 3934 | return LY_SUCCESS; |
| 3935 | } |
| 3936 | |
| 3937 | /** |
| 3938 | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
| 3939 | * with false value. |
| 3940 | * |
| 3941 | * @param[in] args Array of arguments. |
| 3942 | * @param[in] arg_count Count of elements in @p args. |
| 3943 | * @param[in,out] set Context and result set at the same time. |
| 3944 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3945 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3946 | */ |
| 3947 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3948 | 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] | 3949 | { |
| 3950 | if (options & LYXP_SCNODE_ALL) { |
| 3951 | set_scnode_clear_ctx(set); |
| 3952 | return LY_SUCCESS; |
| 3953 | } |
| 3954 | |
| 3955 | set_fill_boolean(set, 0); |
| 3956 | return LY_SUCCESS; |
| 3957 | } |
| 3958 | |
| 3959 | /** |
| 3960 | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
| 3961 | * with the first argument floored (truncated). |
| 3962 | * |
| 3963 | * @param[in] args Array of arguments. |
| 3964 | * @param[in] arg_count Count of elements in @p args. |
| 3965 | * @param[in,out] set Context and result set at the same time. |
| 3966 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3967 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3968 | */ |
| 3969 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3970 | 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] | 3971 | { |
| 3972 | LY_ERR rc; |
| 3973 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3974 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3975 | LY_CHECK_RET(rc); |
| 3976 | if (isfinite(args[0]->val.num)) { |
| 3977 | set_fill_number(set, (long long)args[0]->val.num); |
| 3978 | } |
| 3979 | |
| 3980 | return LY_SUCCESS; |
| 3981 | } |
| 3982 | |
| 3983 | /** |
| 3984 | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
| 3985 | * whether the language of the text matches the one from the argument. |
| 3986 | * |
| 3987 | * @param[in] args Array of arguments. |
| 3988 | * @param[in] arg_count Count of elements in @p args. |
| 3989 | * @param[in,out] set Context and result set at the same time. |
| 3990 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3991 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3992 | */ |
| 3993 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3994 | 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] | 3995 | { |
| 3996 | const struct lyd_node *node; |
| 3997 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 3998 | struct lyd_meta *meta = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3999 | const char *val; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4000 | LY_ERR rc = LY_SUCCESS; |
| 4001 | |
| 4002 | if (options & LYXP_SCNODE_ALL) { |
| 4003 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4004 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4005 | 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] | 4006 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4007 | 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] | 4008 | } |
| 4009 | } |
| 4010 | set_scnode_clear_ctx(set); |
| 4011 | return rc; |
| 4012 | } |
| 4013 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4014 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4015 | LY_CHECK_RET(rc); |
| 4016 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4017 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4018 | 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] | 4019 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4020 | } else if (!set->used) { |
| 4021 | set_fill_boolean(set, 0); |
| 4022 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4023 | } |
| 4024 | |
| 4025 | switch (set->val.nodes[0].type) { |
| 4026 | case LYXP_NODE_ELEM: |
| 4027 | case LYXP_NODE_TEXT: |
| 4028 | node = set->val.nodes[0].node; |
| 4029 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4030 | case LYXP_NODE_META: |
| 4031 | node = set->val.meta[0].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4032 | break; |
| 4033 | default: |
| 4034 | /* nothing to do with roots */ |
| 4035 | set_fill_boolean(set, 0); |
| 4036 | return LY_SUCCESS; |
| 4037 | } |
| 4038 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4039 | /* find lang metadata */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 4040 | for ( ; node; node = (struct lyd_node *)node->parent) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4041 | for (meta = node->meta; meta; meta = meta->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4042 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4043 | 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] | 4044 | break; |
| 4045 | } |
| 4046 | } |
| 4047 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4048 | if (meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4049 | break; |
| 4050 | } |
| 4051 | } |
| 4052 | |
| 4053 | /* compare languages */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4054 | if (!meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4055 | set_fill_boolean(set, 0); |
| 4056 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4057 | uint64_t i; |
| 4058 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4059 | val = meta->value.canonical; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4060 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 4061 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
| 4062 | set_fill_boolean(set, 0); |
| 4063 | break; |
| 4064 | } |
| 4065 | } |
| 4066 | if (!args[0]->val.str[i]) { |
| 4067 | if (!val[i] || (val[i] == '-')) { |
| 4068 | set_fill_boolean(set, 1); |
| 4069 | } else { |
| 4070 | set_fill_boolean(set, 0); |
| 4071 | } |
| 4072 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4073 | } |
| 4074 | |
| 4075 | return LY_SUCCESS; |
| 4076 | } |
| 4077 | |
| 4078 | /** |
| 4079 | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
| 4080 | * with the context size. |
| 4081 | * |
| 4082 | * @param[in] args Array of arguments. |
| 4083 | * @param[in] arg_count Count of elements in @p args. |
| 4084 | * @param[in,out] set Context and result set at the same time. |
| 4085 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4086 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4087 | */ |
| 4088 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4089 | 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] | 4090 | { |
| 4091 | if (options & LYXP_SCNODE_ALL) { |
| 4092 | set_scnode_clear_ctx(set); |
| 4093 | return LY_SUCCESS; |
| 4094 | } |
| 4095 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4096 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4097 | 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] | 4098 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4099 | } else if (!set->used) { |
| 4100 | set_fill_number(set, 0); |
| 4101 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4102 | } |
| 4103 | |
| 4104 | set_fill_number(set, set->ctx_size); |
| 4105 | return LY_SUCCESS; |
| 4106 | } |
| 4107 | |
| 4108 | /** |
| 4109 | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
| 4110 | * with the node name without namespace from the argument or the context. |
| 4111 | * |
| 4112 | * @param[in] args Array of arguments. |
| 4113 | * @param[in] arg_count Count of elements in @p args. |
| 4114 | * @param[in,out] set Context and result set at the same time. |
| 4115 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4116 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4117 | */ |
| 4118 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4119 | 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] | 4120 | { |
| 4121 | struct lyxp_set_node *item; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4122 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4123 | /* suppress unused variable warning */ |
| 4124 | (void)options; |
| 4125 | |
| 4126 | if (options & LYXP_SCNODE_ALL) { |
| 4127 | set_scnode_clear_ctx(set); |
| 4128 | return LY_SUCCESS; |
| 4129 | } |
| 4130 | |
| 4131 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4132 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4133 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 4134 | "local-name(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4135 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4136 | } else if (!args[0]->used) { |
| 4137 | set_fill_string(set, "", 0); |
| 4138 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4139 | } |
| 4140 | |
| 4141 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4142 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4143 | |
| 4144 | item = &args[0]->val.nodes[0]; |
| 4145 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4146 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4147 | 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] | 4148 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4149 | } else if (!set->used) { |
| 4150 | set_fill_string(set, "", 0); |
| 4151 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4152 | } |
| 4153 | |
| 4154 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4155 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4156 | |
| 4157 | item = &set->val.nodes[0]; |
| 4158 | } |
| 4159 | |
| 4160 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4161 | case LYXP_NODE_NONE: |
| 4162 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4163 | case LYXP_NODE_ROOT: |
| 4164 | case LYXP_NODE_ROOT_CONFIG: |
| 4165 | case LYXP_NODE_TEXT: |
| 4166 | set_fill_string(set, "", 0); |
| 4167 | break; |
| 4168 | case LYXP_NODE_ELEM: |
| 4169 | set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name)); |
| 4170 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4171 | case LYXP_NODE_META: |
| 4172 | 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] | 4173 | break; |
| 4174 | } |
| 4175 | |
| 4176 | return LY_SUCCESS; |
| 4177 | } |
| 4178 | |
| 4179 | /** |
| 4180 | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
| 4181 | * with the node name fully qualified (with namespace) from the argument or the context. |
| 4182 | * |
| 4183 | * @param[in] args Array of arguments. |
| 4184 | * @param[in] arg_count Count of elements in @p args. |
| 4185 | * @param[in,out] set Context and result set at the same time. |
| 4186 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4187 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4188 | */ |
| 4189 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4190 | 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] | 4191 | { |
| 4192 | struct lyxp_set_node *item; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4193 | struct lys_module *mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4194 | char *str; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4195 | const char *name = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4196 | |
| 4197 | if (options & LYXP_SCNODE_ALL) { |
| 4198 | set_scnode_clear_ctx(set); |
| 4199 | return LY_SUCCESS; |
| 4200 | } |
| 4201 | |
| 4202 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4203 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4204 | 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] | 4205 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4206 | } else if (!args[0]->used) { |
| 4207 | set_fill_string(set, "", 0); |
| 4208 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4209 | } |
| 4210 | |
| 4211 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4212 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4213 | |
| 4214 | item = &args[0]->val.nodes[0]; |
| 4215 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4216 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4217 | 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] | 4218 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4219 | } else if (!set->used) { |
| 4220 | set_fill_string(set, "", 0); |
| 4221 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4222 | } |
| 4223 | |
| 4224 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4225 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4226 | |
| 4227 | item = &set->val.nodes[0]; |
| 4228 | } |
| 4229 | |
| 4230 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4231 | case LYXP_NODE_NONE: |
| 4232 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4233 | case LYXP_NODE_ROOT: |
| 4234 | case LYXP_NODE_ROOT_CONFIG: |
| 4235 | case LYXP_NODE_TEXT: |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4236 | /* keep NULL */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4237 | break; |
| 4238 | case LYXP_NODE_ELEM: |
| 4239 | mod = item->node->schema->module; |
| 4240 | name = item->node->schema->name; |
| 4241 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4242 | case LYXP_NODE_META: |
| 4243 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
| 4244 | name = ((struct lyd_meta *)item->node)->name; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4245 | break; |
| 4246 | } |
| 4247 | |
| 4248 | if (mod && name) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4249 | 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] | 4250 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
| 4251 | set_fill_string(set, str, strlen(str)); |
| 4252 | free(str); |
| 4253 | } else { |
| 4254 | set_fill_string(set, "", 0); |
| 4255 | } |
| 4256 | |
| 4257 | return LY_SUCCESS; |
| 4258 | } |
| 4259 | |
| 4260 | /** |
| 4261 | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
| 4262 | * with the namespace of the node from the argument or the context. |
| 4263 | * |
| 4264 | * @param[in] args Array of arguments. |
| 4265 | * @param[in] arg_count Count of elements in @p args. |
| 4266 | * @param[in,out] set Context and result set at the same time. |
| 4267 | * @param[in] options XPath options. |
| 4268 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4269 | */ |
| 4270 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4271 | 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] | 4272 | { |
| 4273 | struct lyxp_set_node *item; |
| 4274 | struct lys_module *mod; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4275 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4276 | /* suppress unused variable warning */ |
| 4277 | (void)options; |
| 4278 | |
| 4279 | if (options & LYXP_SCNODE_ALL) { |
| 4280 | set_scnode_clear_ctx(set); |
| 4281 | return LY_SUCCESS; |
| 4282 | } |
| 4283 | |
| 4284 | if (arg_count) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4285 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4286 | 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] | 4287 | "namespace-uri(node-set?)"); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4288 | return LY_EVALID; |
| 4289 | } else if (!args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4290 | set_fill_string(set, "", 0); |
| 4291 | return LY_SUCCESS; |
| 4292 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4293 | |
| 4294 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4295 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4296 | |
| 4297 | item = &args[0]->val.nodes[0]; |
| 4298 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4299 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4300 | 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] | 4301 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4302 | } else if (!set->used) { |
| 4303 | set_fill_string(set, "", 0); |
| 4304 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4305 | } |
| 4306 | |
| 4307 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4308 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4309 | |
| 4310 | item = &set->val.nodes[0]; |
| 4311 | } |
| 4312 | |
| 4313 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4314 | case LYXP_NODE_NONE: |
| 4315 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4316 | case LYXP_NODE_ROOT: |
| 4317 | case LYXP_NODE_ROOT_CONFIG: |
| 4318 | case LYXP_NODE_TEXT: |
| 4319 | set_fill_string(set, "", 0); |
| 4320 | break; |
| 4321 | case LYXP_NODE_ELEM: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4322 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4323 | if (item->type == LYXP_NODE_ELEM) { |
| 4324 | mod = item->node->schema->module; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4325 | } else { /* LYXP_NODE_META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4326 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4327 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4328 | } |
| 4329 | |
| 4330 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
| 4331 | break; |
| 4332 | } |
| 4333 | |
| 4334 | return LY_SUCCESS; |
| 4335 | } |
| 4336 | |
| 4337 | /** |
| 4338 | * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET |
| 4339 | * with only nodes from the context. In practice it either leaves the context |
| 4340 | * as it is or returns an empty node set. |
| 4341 | * |
| 4342 | * @param[in] args Array of arguments. |
| 4343 | * @param[in] arg_count Count of elements in @p args. |
| 4344 | * @param[in,out] set Context and result set at the same time. |
| 4345 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4346 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4347 | */ |
| 4348 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4349 | 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] | 4350 | { |
| 4351 | if (options & LYXP_SCNODE_ALL) { |
| 4352 | set_scnode_clear_ctx(set); |
| 4353 | return LY_SUCCESS; |
| 4354 | } |
| 4355 | |
| 4356 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4357 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4358 | } |
| 4359 | return LY_SUCCESS; |
| 4360 | } |
| 4361 | |
| 4362 | /** |
| 4363 | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
| 4364 | * with normalized value (no leading, trailing, double white spaces) of the node |
| 4365 | * from the argument or the context. |
| 4366 | * |
| 4367 | * @param[in] args Array of arguments. |
| 4368 | * @param[in] arg_count Count of elements in @p args. |
| 4369 | * @param[in,out] set Context and result set at the same time. |
| 4370 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4371 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4372 | */ |
| 4373 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4374 | 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] | 4375 | { |
| 4376 | uint16_t i, new_used; |
| 4377 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4378 | ly_bool have_spaces = 0, space_before = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4379 | struct lysc_node_leaf *sleaf; |
| 4380 | LY_ERR rc = LY_SUCCESS; |
| 4381 | |
| 4382 | if (options & LYXP_SCNODE_ALL) { |
| 4383 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4384 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4385 | 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] | 4386 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4387 | 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] | 4388 | } |
| 4389 | } |
| 4390 | set_scnode_clear_ctx(set); |
| 4391 | return rc; |
| 4392 | } |
| 4393 | |
| 4394 | if (arg_count) { |
| 4395 | set_fill_set(set, args[0]); |
| 4396 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4397 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4398 | LY_CHECK_RET(rc); |
| 4399 | |
| 4400 | /* is there any normalization necessary? */ |
| 4401 | for (i = 0; set->val.str[i]; ++i) { |
| 4402 | if (is_xmlws(set->val.str[i])) { |
| 4403 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
| 4404 | have_spaces = 1; |
| 4405 | break; |
| 4406 | } |
| 4407 | space_before = 1; |
| 4408 | } else { |
| 4409 | space_before = 0; |
| 4410 | } |
| 4411 | } |
| 4412 | |
| 4413 | /* yep, there is */ |
| 4414 | if (have_spaces) { |
| 4415 | /* it's enough, at least one character will go, makes space for ending '\0' */ |
| 4416 | new = malloc(strlen(set->val.str) * sizeof(char)); |
| 4417 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4418 | new_used = 0; |
| 4419 | |
| 4420 | space_before = 0; |
| 4421 | for (i = 0; set->val.str[i]; ++i) { |
| 4422 | if (is_xmlws(set->val.str[i])) { |
| 4423 | if ((i == 0) || space_before) { |
| 4424 | space_before = 1; |
| 4425 | continue; |
| 4426 | } else { |
| 4427 | space_before = 1; |
| 4428 | } |
| 4429 | } else { |
| 4430 | space_before = 0; |
| 4431 | } |
| 4432 | |
| 4433 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
| 4434 | ++new_used; |
| 4435 | } |
| 4436 | |
| 4437 | /* at worst there is one trailing space now */ |
| 4438 | if (new_used && is_xmlws(new[new_used - 1])) { |
| 4439 | --new_used; |
| 4440 | } |
| 4441 | |
| 4442 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 4443 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4444 | new[new_used] = '\0'; |
| 4445 | |
| 4446 | free(set->val.str); |
| 4447 | set->val.str = new; |
| 4448 | } |
| 4449 | |
| 4450 | return LY_SUCCESS; |
| 4451 | } |
| 4452 | |
| 4453 | /** |
| 4454 | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
| 4455 | * with the argument converted to boolean and logically inverted. |
| 4456 | * |
| 4457 | * @param[in] args Array of arguments. |
| 4458 | * @param[in] arg_count Count of elements in @p args. |
| 4459 | * @param[in,out] set Context and result set at the same time. |
| 4460 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4461 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4462 | */ |
| 4463 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4464 | 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] | 4465 | { |
| 4466 | if (options & LYXP_SCNODE_ALL) { |
| 4467 | set_scnode_clear_ctx(set); |
| 4468 | return LY_SUCCESS; |
| 4469 | } |
| 4470 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4471 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4472 | if (args[0]->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4473 | set_fill_boolean(set, 0); |
| 4474 | } else { |
| 4475 | set_fill_boolean(set, 1); |
| 4476 | } |
| 4477 | |
| 4478 | return LY_SUCCESS; |
| 4479 | } |
| 4480 | |
| 4481 | /** |
| 4482 | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
| 4483 | * with the number representation of either the argument or the context. |
| 4484 | * |
| 4485 | * @param[in] args Array of arguments. |
| 4486 | * @param[in] arg_count Count of elements in @p args. |
| 4487 | * @param[in,out] set Context and result set at the same time. |
| 4488 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4489 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4490 | */ |
| 4491 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4492 | 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] | 4493 | { |
| 4494 | LY_ERR rc; |
| 4495 | |
| 4496 | if (options & LYXP_SCNODE_ALL) { |
| 4497 | set_scnode_clear_ctx(set); |
| 4498 | return LY_SUCCESS; |
| 4499 | } |
| 4500 | |
| 4501 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4502 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4503 | LY_CHECK_RET(rc); |
| 4504 | set_fill_set(set, args[0]); |
| 4505 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4506 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4507 | LY_CHECK_RET(rc); |
| 4508 | } |
| 4509 | |
| 4510 | return LY_SUCCESS; |
| 4511 | } |
| 4512 | |
| 4513 | /** |
| 4514 | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
| 4515 | * with the context position. |
| 4516 | * |
| 4517 | * @param[in] args Array of arguments. |
| 4518 | * @param[in] arg_count Count of elements in @p args. |
| 4519 | * @param[in,out] set Context and result set at the same time. |
| 4520 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4521 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4522 | */ |
| 4523 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4524 | 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] | 4525 | { |
| 4526 | if (options & LYXP_SCNODE_ALL) { |
| 4527 | set_scnode_clear_ctx(set); |
| 4528 | return LY_SUCCESS; |
| 4529 | } |
| 4530 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4531 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4532 | 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] | 4533 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4534 | } else if (!set->used) { |
| 4535 | set_fill_number(set, 0); |
| 4536 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4537 | } |
| 4538 | |
| 4539 | set_fill_number(set, set->ctx_pos); |
| 4540 | |
| 4541 | /* UNUSED in 'Release' build type */ |
| 4542 | (void)options; |
| 4543 | return LY_SUCCESS; |
| 4544 | } |
| 4545 | |
| 4546 | /** |
| 4547 | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
| 4548 | * depending on whether the second argument regex matches the first argument string. For details refer to |
| 4549 | * YANG 1.1 RFC section 10.2.1. |
| 4550 | * |
| 4551 | * @param[in] args Array of arguments. |
| 4552 | * @param[in] arg_count Count of elements in @p args. |
| 4553 | * @param[in,out] set Context and result set at the same time. |
| 4554 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4555 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4556 | */ |
| 4557 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4558 | 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] | 4559 | { |
| 4560 | struct lysc_pattern **patterns = NULL, **pattern; |
| 4561 | struct lysc_node_leaf *sleaf; |
| 4562 | char *path; |
| 4563 | LY_ERR rc = LY_SUCCESS; |
| 4564 | struct ly_err_item *err; |
| 4565 | |
| 4566 | if (options & LYXP_SCNODE_ALL) { |
| 4567 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4568 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4569 | 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] | 4570 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4571 | 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] | 4572 | } |
| 4573 | } |
| 4574 | |
| 4575 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4576 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4577 | 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] | 4578 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4579 | 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] | 4580 | } |
| 4581 | } |
| 4582 | set_scnode_clear_ctx(set); |
| 4583 | return rc; |
| 4584 | } |
| 4585 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4586 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4587 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4588 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4589 | LY_CHECK_RET(rc); |
| 4590 | |
| 4591 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
| 4592 | *pattern = malloc(sizeof **pattern); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4593 | path = lyd_path(set->cur_node, LYD_PATH_LOG, NULL, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4594 | rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code); |
| 4595 | free(path); |
| 4596 | if (rc != LY_SUCCESS) { |
| 4597 | LY_ARRAY_FREE(patterns); |
| 4598 | return rc; |
| 4599 | } |
| 4600 | |
| 4601 | rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err); |
| 4602 | pcre2_code_free((*pattern)->code); |
| 4603 | free(*pattern); |
| 4604 | LY_ARRAY_FREE(patterns); |
| 4605 | if (rc && (rc != LY_EVALID)) { |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 4606 | ly_err_print(set->ctx, err); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4607 | ly_err_free(err); |
| 4608 | return rc; |
| 4609 | } |
| 4610 | |
| 4611 | if (rc == LY_EVALID) { |
| 4612 | ly_err_free(err); |
| 4613 | set_fill_boolean(set, 0); |
| 4614 | } else { |
| 4615 | set_fill_boolean(set, 1); |
| 4616 | } |
| 4617 | |
| 4618 | return LY_SUCCESS; |
| 4619 | } |
| 4620 | |
| 4621 | /** |
| 4622 | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
| 4623 | * with the rounded first argument. For details refer to |
| 4624 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
| 4625 | * |
| 4626 | * @param[in] args Array of arguments. |
| 4627 | * @param[in] arg_count Count of elements in @p args. |
| 4628 | * @param[in,out] set Context and result set at the same time. |
| 4629 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4630 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4631 | */ |
| 4632 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4633 | 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] | 4634 | { |
| 4635 | struct lysc_node_leaf *sleaf; |
| 4636 | LY_ERR rc = LY_SUCCESS; |
| 4637 | |
| 4638 | if (options & LYXP_SCNODE_ALL) { |
| 4639 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4640 | 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] | 4641 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4642 | 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] | 4643 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4644 | 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] | 4645 | } |
| 4646 | set_scnode_clear_ctx(set); |
| 4647 | return rc; |
| 4648 | } |
| 4649 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4650 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4651 | LY_CHECK_RET(rc); |
| 4652 | |
| 4653 | /* cover only the cases where floor can't be used */ |
| 4654 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
| 4655 | set_fill_number(set, -0.0f); |
| 4656 | } else { |
| 4657 | args[0]->val.num += 0.5; |
| 4658 | rc = xpath_floor(args, 1, args[0], options); |
| 4659 | LY_CHECK_RET(rc); |
| 4660 | set_fill_number(set, args[0]->val.num); |
| 4661 | } |
| 4662 | |
| 4663 | return LY_SUCCESS; |
| 4664 | } |
| 4665 | |
| 4666 | /** |
| 4667 | * @brief Execute the XPath starts-with(string, string) function. |
| 4668 | * Returns LYXP_SET_BOOLEAN whether the second argument is |
| 4669 | * the prefix of the first or not. |
| 4670 | * |
| 4671 | * @param[in] args Array of arguments. |
| 4672 | * @param[in] arg_count Count of elements in @p args. |
| 4673 | * @param[in,out] set Context and result set at the same time. |
| 4674 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4675 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4676 | */ |
| 4677 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4678 | 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] | 4679 | { |
| 4680 | struct lysc_node_leaf *sleaf; |
| 4681 | LY_ERR rc = LY_SUCCESS; |
| 4682 | |
| 4683 | if (options & LYXP_SCNODE_ALL) { |
| 4684 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4685 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4686 | 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] | 4687 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4688 | 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] | 4689 | } |
| 4690 | } |
| 4691 | |
| 4692 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4693 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4694 | 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] | 4695 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4696 | 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] | 4697 | } |
| 4698 | } |
| 4699 | set_scnode_clear_ctx(set); |
| 4700 | return rc; |
| 4701 | } |
| 4702 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4703 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4704 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4705 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4706 | LY_CHECK_RET(rc); |
| 4707 | |
| 4708 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
| 4709 | set_fill_boolean(set, 0); |
| 4710 | } else { |
| 4711 | set_fill_boolean(set, 1); |
| 4712 | } |
| 4713 | |
| 4714 | return LY_SUCCESS; |
| 4715 | } |
| 4716 | |
| 4717 | /** |
| 4718 | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
| 4719 | * with the string representation of either the argument or the context. |
| 4720 | * |
| 4721 | * @param[in] args Array of arguments. |
| 4722 | * @param[in] arg_count Count of elements in @p args. |
| 4723 | * @param[in,out] set Context and result set at the same time. |
| 4724 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4725 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4726 | */ |
| 4727 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4728 | 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] | 4729 | { |
| 4730 | LY_ERR rc; |
| 4731 | |
| 4732 | if (options & LYXP_SCNODE_ALL) { |
| 4733 | set_scnode_clear_ctx(set); |
| 4734 | return LY_SUCCESS; |
| 4735 | } |
| 4736 | |
| 4737 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4738 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4739 | LY_CHECK_RET(rc); |
| 4740 | set_fill_set(set, args[0]); |
| 4741 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4742 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4743 | LY_CHECK_RET(rc); |
| 4744 | } |
| 4745 | |
| 4746 | return LY_SUCCESS; |
| 4747 | } |
| 4748 | |
| 4749 | /** |
| 4750 | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
| 4751 | * with the length of the string in either the argument or the context. |
| 4752 | * |
| 4753 | * @param[in] args Array of arguments. |
| 4754 | * @param[in] arg_count Count of elements in @p args. |
| 4755 | * @param[in,out] set Context and result set at the same time. |
| 4756 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4757 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4758 | */ |
| 4759 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4760 | 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] | 4761 | { |
| 4762 | struct lysc_node_leaf *sleaf; |
| 4763 | LY_ERR rc = LY_SUCCESS; |
| 4764 | |
| 4765 | if (options & LYXP_SCNODE_ALL) { |
| 4766 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4767 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4768 | 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] | 4769 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4770 | 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] | 4771 | } |
| 4772 | } |
| 4773 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
| 4774 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4775 | 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] | 4776 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4777 | 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] | 4778 | } |
| 4779 | } |
| 4780 | set_scnode_clear_ctx(set); |
| 4781 | return rc; |
| 4782 | } |
| 4783 | |
| 4784 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4785 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4786 | LY_CHECK_RET(rc); |
| 4787 | set_fill_number(set, strlen(args[0]->val.str)); |
| 4788 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4789 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4790 | LY_CHECK_RET(rc); |
| 4791 | set_fill_number(set, strlen(set->val.str)); |
| 4792 | } |
| 4793 | |
| 4794 | return LY_SUCCESS; |
| 4795 | } |
| 4796 | |
| 4797 | /** |
| 4798 | * @brief Execute the XPath substring(string, number, number?) function. |
| 4799 | * Returns LYXP_SET_STRING substring of the first argument starting |
| 4800 | * on the second argument index ending on the third argument index, |
| 4801 | * indexed from 1. For exact definition refer to |
| 4802 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
| 4803 | * |
| 4804 | * @param[in] args Array of arguments. |
| 4805 | * @param[in] arg_count Count of elements in @p args. |
| 4806 | * @param[in,out] set Context and result set at the same time. |
| 4807 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4808 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4809 | */ |
| 4810 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4811 | 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] | 4812 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4813 | int32_t start, len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4814 | uint16_t str_start, str_len, pos; |
| 4815 | struct lysc_node_leaf *sleaf; |
| 4816 | LY_ERR rc = LY_SUCCESS; |
| 4817 | |
| 4818 | if (options & LYXP_SCNODE_ALL) { |
| 4819 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4820 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4821 | 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] | 4822 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4823 | 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] | 4824 | } |
| 4825 | } |
| 4826 | |
| 4827 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4828 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4829 | 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] | 4830 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4831 | 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] | 4832 | } |
| 4833 | } |
| 4834 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4835 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) && |
| 4836 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4837 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4838 | 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] | 4839 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4840 | 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] | 4841 | } |
| 4842 | } |
| 4843 | set_scnode_clear_ctx(set); |
| 4844 | return rc; |
| 4845 | } |
| 4846 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4847 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4848 | LY_CHECK_RET(rc); |
| 4849 | |
| 4850 | /* start */ |
| 4851 | if (xpath_round(&args[1], 1, args[1], options)) { |
| 4852 | return -1; |
| 4853 | } |
| 4854 | if (isfinite(args[1]->val.num)) { |
| 4855 | start = args[1]->val.num - 1; |
| 4856 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4857 | start = INT32_MIN; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4858 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4859 | start = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4860 | } |
| 4861 | |
| 4862 | /* len */ |
| 4863 | if (arg_count == 3) { |
| 4864 | rc = xpath_round(&args[2], 1, args[2], options); |
| 4865 | LY_CHECK_RET(rc); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4866 | if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4867 | len = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4868 | } else if (isfinite(args[2]->val.num)) { |
| 4869 | len = args[2]->val.num; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4870 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4871 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4872 | } |
| 4873 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4874 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4875 | } |
| 4876 | |
| 4877 | /* find matching character positions */ |
| 4878 | str_start = 0; |
| 4879 | str_len = 0; |
| 4880 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
| 4881 | if (pos < start) { |
| 4882 | ++str_start; |
| 4883 | } else if (pos < start + len) { |
| 4884 | ++str_len; |
| 4885 | } else { |
| 4886 | break; |
| 4887 | } |
| 4888 | } |
| 4889 | |
| 4890 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
| 4891 | return LY_SUCCESS; |
| 4892 | } |
| 4893 | |
| 4894 | /** |
| 4895 | * @brief Execute the XPath substring-after(string, string) function. |
| 4896 | * Returns LYXP_SET_STRING with the string succeeding the occurance |
| 4897 | * of the second argument in the first or an empty string. |
| 4898 | * |
| 4899 | * @param[in] args Array of arguments. |
| 4900 | * @param[in] arg_count Count of elements in @p args. |
| 4901 | * @param[in,out] set Context and result set at the same time. |
| 4902 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4903 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4904 | */ |
| 4905 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4906 | 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] | 4907 | { |
| 4908 | char *ptr; |
| 4909 | struct lysc_node_leaf *sleaf; |
| 4910 | LY_ERR rc = LY_SUCCESS; |
| 4911 | |
| 4912 | if (options & LYXP_SCNODE_ALL) { |
| 4913 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4914 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4915 | 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] | 4916 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4917 | 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] | 4918 | } |
| 4919 | } |
| 4920 | |
| 4921 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4922 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4923 | 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] | 4924 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4925 | 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] | 4926 | } |
| 4927 | } |
| 4928 | set_scnode_clear_ctx(set); |
| 4929 | return rc; |
| 4930 | } |
| 4931 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4932 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4933 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4934 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4935 | LY_CHECK_RET(rc); |
| 4936 | |
| 4937 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4938 | if (ptr) { |
| 4939 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
| 4940 | } else { |
| 4941 | set_fill_string(set, "", 0); |
| 4942 | } |
| 4943 | |
| 4944 | return LY_SUCCESS; |
| 4945 | } |
| 4946 | |
| 4947 | /** |
| 4948 | * @brief Execute the XPath substring-before(string, string) function. |
| 4949 | * Returns LYXP_SET_STRING with the string preceding the occurance |
| 4950 | * of the second argument in the first or an empty string. |
| 4951 | * |
| 4952 | * @param[in] args Array of arguments. |
| 4953 | * @param[in] arg_count Count of elements in @p args. |
| 4954 | * @param[in,out] set Context and result set at the same time. |
| 4955 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4956 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4957 | */ |
| 4958 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4959 | 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] | 4960 | { |
| 4961 | char *ptr; |
| 4962 | struct lysc_node_leaf *sleaf; |
| 4963 | LY_ERR rc = LY_SUCCESS; |
| 4964 | |
| 4965 | if (options & LYXP_SCNODE_ALL) { |
| 4966 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4967 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4968 | 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] | 4969 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4970 | 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] | 4971 | } |
| 4972 | } |
| 4973 | |
| 4974 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4975 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4976 | 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] | 4977 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4978 | 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] | 4979 | } |
| 4980 | } |
| 4981 | set_scnode_clear_ctx(set); |
| 4982 | return rc; |
| 4983 | } |
| 4984 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4985 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4986 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4987 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4988 | LY_CHECK_RET(rc); |
| 4989 | |
| 4990 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4991 | if (ptr) { |
| 4992 | set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str); |
| 4993 | } else { |
| 4994 | set_fill_string(set, "", 0); |
| 4995 | } |
| 4996 | |
| 4997 | return LY_SUCCESS; |
| 4998 | } |
| 4999 | |
| 5000 | /** |
| 5001 | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
| 5002 | * with the sum of all the nodes in the context. |
| 5003 | * |
| 5004 | * @param[in] args Array of arguments. |
| 5005 | * @param[in] arg_count Count of elements in @p args. |
| 5006 | * @param[in,out] set Context and result set at the same time. |
| 5007 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5008 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5009 | */ |
| 5010 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5011 | 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] | 5012 | { |
| 5013 | long double num; |
| 5014 | char *str; |
| 5015 | uint16_t i; |
| 5016 | struct lyxp_set set_item; |
| 5017 | struct lysc_node_leaf *sleaf; |
| 5018 | LY_ERR rc = LY_SUCCESS; |
| 5019 | |
| 5020 | if (options & LYXP_SCNODE_ALL) { |
| 5021 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
| 5022 | for (i = 0; i < args[0]->used; ++i) { |
| 5023 | if (args[0]->val.scnodes[i].in_ctx == 1) { |
| 5024 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
| 5025 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5026 | 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] | 5027 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5028 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5029 | 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] | 5030 | } |
| 5031 | } |
| 5032 | } |
| 5033 | } |
| 5034 | set_scnode_clear_ctx(set); |
| 5035 | return rc; |
| 5036 | } |
| 5037 | |
| 5038 | set_fill_number(set, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5039 | |
| 5040 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5041 | 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] | 5042 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5043 | } else if (!args[0]->used) { |
| 5044 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5045 | } |
| 5046 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5047 | set_init(&set_item, set); |
| 5048 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5049 | set_item.type = LYXP_SET_NODE_SET; |
| 5050 | set_item.val.nodes = malloc(sizeof *set_item.val.nodes); |
| 5051 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
| 5052 | |
| 5053 | set_item.used = 1; |
| 5054 | set_item.size = 1; |
| 5055 | |
| 5056 | for (i = 0; i < args[0]->used; ++i) { |
| 5057 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
| 5058 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5059 | rc = cast_node_set_to_string(&set_item, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5060 | LY_CHECK_RET(rc); |
| 5061 | num = cast_string_to_number(str); |
| 5062 | free(str); |
| 5063 | set->val.num += num; |
| 5064 | } |
| 5065 | |
| 5066 | free(set_item.val.nodes); |
| 5067 | |
| 5068 | return LY_SUCCESS; |
| 5069 | } |
| 5070 | |
| 5071 | /** |
| 5072 | * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET |
| 5073 | * with the text content of the nodes in the context. |
| 5074 | * |
| 5075 | * @param[in] args Array of arguments. |
| 5076 | * @param[in] arg_count Count of elements in @p args. |
| 5077 | * @param[in,out] set Context and result set at the same time. |
| 5078 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5079 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5080 | */ |
| 5081 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5082 | 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] | 5083 | { |
| 5084 | uint32_t i; |
| 5085 | |
| 5086 | if (options & LYXP_SCNODE_ALL) { |
| 5087 | set_scnode_clear_ctx(set); |
| 5088 | return LY_SUCCESS; |
| 5089 | } |
| 5090 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5091 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5092 | 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] | 5093 | return LY_EVALID; |
| 5094 | } |
| 5095 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 5096 | for (i = 0; i < set->used; ) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5097 | switch (set->val.nodes[i].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 5098 | case LYXP_NODE_NONE: |
| 5099 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5100 | case LYXP_NODE_ELEM: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5101 | if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 5102 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
| 5103 | ++i; |
| 5104 | break; |
| 5105 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5106 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5107 | case LYXP_NODE_ROOT: |
| 5108 | case LYXP_NODE_ROOT_CONFIG: |
| 5109 | case LYXP_NODE_TEXT: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5110 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5111 | set_remove_node(set, i); |
| 5112 | break; |
| 5113 | } |
| 5114 | } |
| 5115 | |
| 5116 | return LY_SUCCESS; |
| 5117 | } |
| 5118 | |
| 5119 | /** |
| 5120 | * @brief Execute the XPath translate(string, string, string) function. |
| 5121 | * Returns LYXP_SET_STRING with the first argument with the characters |
| 5122 | * from the second argument replaced by those on the corresponding |
| 5123 | * positions in the third argument. |
| 5124 | * |
| 5125 | * @param[in] args Array of arguments. |
| 5126 | * @param[in] arg_count Count of elements in @p args. |
| 5127 | * @param[in,out] set Context and result set at the same time. |
| 5128 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5129 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5130 | */ |
| 5131 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5132 | 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] | 5133 | { |
| 5134 | uint16_t i, j, new_used; |
| 5135 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5136 | ly_bool have_removed; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5137 | struct lysc_node_leaf *sleaf; |
| 5138 | LY_ERR rc = LY_SUCCESS; |
| 5139 | |
| 5140 | if (options & LYXP_SCNODE_ALL) { |
| 5141 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5142 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5143 | 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] | 5144 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5145 | 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] | 5146 | } |
| 5147 | } |
| 5148 | |
| 5149 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5150 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5151 | 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] | 5152 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5153 | 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] | 5154 | } |
| 5155 | } |
| 5156 | |
| 5157 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 5158 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5159 | 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] | 5160 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5161 | 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] | 5162 | } |
| 5163 | } |
| 5164 | set_scnode_clear_ctx(set); |
| 5165 | return rc; |
| 5166 | } |
| 5167 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5168 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5169 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5170 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5171 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5172 | rc = lyxp_set_cast(args[2], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5173 | LY_CHECK_RET(rc); |
| 5174 | |
| 5175 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
| 5176 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5177 | new_used = 0; |
| 5178 | |
| 5179 | have_removed = 0; |
| 5180 | for (i = 0; args[0]->val.str[i]; ++i) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5181 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5182 | |
| 5183 | for (j = 0; args[1]->val.str[j]; ++j) { |
| 5184 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
| 5185 | /* removing this char */ |
| 5186 | if (j >= strlen(args[2]->val.str)) { |
| 5187 | have_removed = 1; |
| 5188 | found = 1; |
| 5189 | break; |
| 5190 | } |
| 5191 | /* replacing this char */ |
| 5192 | new[new_used] = args[2]->val.str[j]; |
| 5193 | ++new_used; |
| 5194 | found = 1; |
| 5195 | break; |
| 5196 | } |
| 5197 | } |
| 5198 | |
| 5199 | /* copying this char */ |
| 5200 | if (!found) { |
| 5201 | new[new_used] = args[0]->val.str[i]; |
| 5202 | ++new_used; |
| 5203 | } |
| 5204 | } |
| 5205 | |
| 5206 | if (have_removed) { |
| 5207 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 5208 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5209 | } |
| 5210 | new[new_used] = '\0'; |
| 5211 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5212 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5213 | set->type = LYXP_SET_STRING; |
| 5214 | set->val.str = new; |
| 5215 | |
| 5216 | return LY_SUCCESS; |
| 5217 | } |
| 5218 | |
| 5219 | /** |
| 5220 | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
| 5221 | * with true value. |
| 5222 | * |
| 5223 | * @param[in] args Array of arguments. |
| 5224 | * @param[in] arg_count Count of elements in @p args. |
| 5225 | * @param[in,out] set Context and result set at the same time. |
| 5226 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5227 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5228 | */ |
| 5229 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5230 | 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] | 5231 | { |
| 5232 | if (options & LYXP_SCNODE_ALL) { |
| 5233 | set_scnode_clear_ctx(set); |
| 5234 | return LY_SUCCESS; |
| 5235 | } |
| 5236 | |
| 5237 | set_fill_boolean(set, 1); |
| 5238 | return LY_SUCCESS; |
| 5239 | } |
| 5240 | |
| 5241 | /* |
| 5242 | * moveto functions |
| 5243 | * |
| 5244 | * They and only they actually change the context (set). |
| 5245 | */ |
| 5246 | |
| 5247 | /** |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5248 | * @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] | 5249 | * |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5250 | * XPath @p set is expected to be a (sc)node set! |
| 5251 | * |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5252 | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
| 5253 | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5254 | * @param[in] set Set with general XPath context. |
| 5255 | * @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] | 5256 | * @param[out] moveto_mod Expected module of a matching node. |
| 5257 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5258 | */ |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5259 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5260 | moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set, |
| 5261 | const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5262 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 5263 | const struct lys_module *mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5264 | const char *ptr; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5265 | size_t pref_len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5266 | |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5267 | assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET)); |
| 5268 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5269 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
| 5270 | /* specific module */ |
| 5271 | pref_len = ptr - *qname; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5272 | 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] | 5273 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5274 | /* check for errors and non-implemented modules, as they are not valid */ |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5275 | if (!mod || !mod->implemented) { |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5276 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5277 | 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] | 5278 | } else { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5279 | 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] | 5280 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5281 | return LY_EVALID; |
| 5282 | } |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5283 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5284 | *qname += pref_len + 1; |
| 5285 | *qname_len -= pref_len + 1; |
| 5286 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
| 5287 | /* all modules - special case */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5288 | mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5289 | } else { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5290 | switch (set->format) { |
| 5291 | case LY_PREF_SCHEMA: |
| 5292 | case LY_PREF_SCHEMA_RESOLVED: |
| 5293 | /* current module */ |
| 5294 | mod = set->cur_mod; |
| 5295 | break; |
| 5296 | case LY_PREF_JSON: |
| 5297 | /* inherit parent (context node) module */ |
| 5298 | if (ctx_scnode) { |
| 5299 | mod = ctx_scnode->module; |
| 5300 | } else { |
| 5301 | mod = NULL; |
| 5302 | } |
| 5303 | break; |
| 5304 | case LY_PREF_XML: |
| 5305 | /* not defined */ |
| 5306 | LOGINT_RET(set->ctx); |
| 5307 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5308 | } |
| 5309 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5310 | *moveto_mod = mod; |
| 5311 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5312 | } |
| 5313 | |
| 5314 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5315 | * @brief Move context @p set to the root. Handles absolute path. |
| 5316 | * Result is LYXP_SET_NODE_SET. |
| 5317 | * |
| 5318 | * @param[in,out] set Set to use. |
| 5319 | * @param[in] options Xpath options. |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5320 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5321 | */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5322 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5323 | moveto_root(struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5324 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5325 | if (!set) { |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5326 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5327 | } |
| 5328 | |
| 5329 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5330 | set_scnode_clear_ctx(set); |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5331 | 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] | 5332 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5333 | set->type = LYXP_SET_NODE_SET; |
| 5334 | set->used = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5335 | set_insert_node(set, NULL, 0, set->root_type, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5336 | } |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5337 | |
| 5338 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5339 | } |
| 5340 | |
| 5341 | /** |
| 5342 | * @brief Check @p node as a part of NameTest processing. |
| 5343 | * |
| 5344 | * @param[in] node Node to check. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5345 | * @param[in] ctx_node Context node. |
| 5346 | * @param[in] set Set to read general context from. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5347 | * @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] | 5348 | * @param[in] moveto_mod Expected module of the node, NULL for no prefix. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5349 | * @param[in] options XPath options. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5350 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when, |
| 5351 | * LY_EINVAL if netither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5352 | */ |
| 5353 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5354 | moveto_node_check(const struct lyd_node *node, const struct lyd_node *ctx_node, const struct lyxp_set *set, |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5355 | const char *node_name, const struct lys_module *moveto_mod, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5356 | { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5357 | if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) { |
| 5358 | switch (set->format) { |
| 5359 | case LY_PREF_SCHEMA: |
| 5360 | case LY_PREF_SCHEMA_RESOLVED: |
| 5361 | /* use current module */ |
| 5362 | moveto_mod = set->cur_mod; |
| 5363 | break; |
| 5364 | case LY_PREF_JSON: |
| 5365 | /* inherit module of the context node, if any */ |
| 5366 | if (ctx_node) { |
| 5367 | moveto_mod = ctx_node->schema->module; |
| 5368 | } |
| 5369 | break; |
| 5370 | case LY_PREF_XML: |
| 5371 | /* not defined */ |
| 5372 | LOGINT(set->ctx); |
| 5373 | return LY_EINVAL; |
| 5374 | } |
| 5375 | } |
| 5376 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5377 | /* module check */ |
| 5378 | if (moveto_mod && (node->schema->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5379 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5380 | } |
| 5381 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5382 | /* context check */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5383 | 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] | 5384 | return LY_EINVAL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5385 | } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 5386 | (node->schema != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5387 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5388 | } |
| 5389 | |
| 5390 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5391 | if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5392 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5393 | } |
| 5394 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5395 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 5396 | if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5397 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5398 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5399 | |
| 5400 | /* match */ |
| 5401 | return LY_SUCCESS; |
| 5402 | } |
| 5403 | |
| 5404 | /** |
| 5405 | * @brief Check @p node as a part of schema NameTest processing. |
| 5406 | * |
| 5407 | * @param[in] node Schema node to check. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5408 | * @param[in] ctx_scnode Context node. |
| 5409 | * @param[in] set Set to read general context from. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5410 | * @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] | 5411 | * @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] | 5412 | * @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] | 5413 | */ |
| 5414 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5415 | 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] | 5416 | const char *node_name, const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5417 | { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5418 | if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) { |
| 5419 | switch (set->format) { |
| 5420 | case LY_PREF_SCHEMA: |
| 5421 | case LY_PREF_SCHEMA_RESOLVED: |
| 5422 | /* use current module */ |
| 5423 | moveto_mod = set->cur_mod; |
| 5424 | break; |
| 5425 | case LY_PREF_JSON: |
| 5426 | /* inherit module of the context node, if any */ |
| 5427 | if (ctx_scnode) { |
| 5428 | moveto_mod = ctx_scnode->module; |
| 5429 | } |
| 5430 | break; |
| 5431 | case LY_PREF_XML: |
| 5432 | /* not defined */ |
| 5433 | LOGINT(set->ctx); |
| 5434 | return LY_EINVAL; |
| 5435 | } |
| 5436 | } |
| 5437 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5438 | /* module check */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5439 | if (moveto_mod && (node->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5440 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5441 | } |
| 5442 | |
| 5443 | /* context check */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5444 | 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] | 5445 | return LY_EINVAL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5446 | } 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] | 5447 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5448 | } |
| 5449 | |
| 5450 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5451 | if (node_name && strcmp(node_name, "*") && (node->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5452 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5453 | } |
| 5454 | |
| 5455 | /* match */ |
| 5456 | return LY_SUCCESS; |
| 5457 | } |
| 5458 | |
| 5459 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5460 | * @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] | 5461 | * |
| 5462 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5463 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5464 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5465 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5466 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5467 | */ |
| 5468 | static LY_ERR |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5469 | moveto_node(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] | 5470 | { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 5471 | uint32_t i; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5472 | const struct lyd_node *siblings, *sub, *ctx_node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5473 | LY_ERR rc; |
| 5474 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5475 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5476 | return LY_SUCCESS; |
| 5477 | } |
| 5478 | |
| 5479 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5480 | 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] | 5481 | return LY_EVALID; |
| 5482 | } |
| 5483 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5484 | for (i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5485 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5486 | |
| 5487 | 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] | 5488 | assert(!set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5489 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5490 | /* search in all the trees */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5491 | ctx_node = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5492 | siblings = set->tree; |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5493 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5494 | /* search in children */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5495 | ctx_node = set->val.nodes[i].node; |
| 5496 | siblings = lyd_child(ctx_node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5497 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5498 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5499 | for (sub = siblings; sub; sub = sub->next) { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5500 | rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5501 | if (rc == LY_SUCCESS) { |
| 5502 | if (!replaced) { |
| 5503 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
| 5504 | replaced = 1; |
| 5505 | } else { |
| 5506 | set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5507 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5508 | ++i; |
| 5509 | } else if (rc == LY_EINCOMPLETE) { |
| 5510 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5511 | } |
| 5512 | } |
| 5513 | |
| 5514 | if (!replaced) { |
| 5515 | /* no match */ |
| 5516 | set_remove_node(set, i); |
| 5517 | } |
| 5518 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5519 | |
| 5520 | return LY_SUCCESS; |
| 5521 | } |
| 5522 | |
| 5523 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5524 | * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). |
| 5525 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5526 | * |
| 5527 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5528 | * @param[in] scnode Matching node schema. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5529 | * @param[in] predicates If @p scnode is ::LYS_LIST or ::LYS_LEAFLIST, the predicates specifying a single instance. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5530 | * @param[in] options XPath options. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5531 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5532 | */ |
| 5533 | static LY_ERR |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5534 | moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates, |
| 5535 | uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5536 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5537 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5538 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5539 | const struct lyd_node *siblings; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5540 | struct lyd_node *sub, *inst = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5541 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5542 | assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5543 | |
| 5544 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5545 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5546 | } |
| 5547 | |
| 5548 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5549 | 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] | 5550 | ret = LY_EVALID; |
| 5551 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5552 | } |
| 5553 | |
| 5554 | /* context check for all the nodes since we have the schema node */ |
| 5555 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 5556 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5557 | goto cleanup; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 5558 | } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 5559 | (scnode != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5560 | lyxp_set_free_content(set); |
| 5561 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5562 | } |
| 5563 | |
| 5564 | /* create specific data instance if needed */ |
| 5565 | if (scnode->nodetype == LYS_LIST) { |
| 5566 | LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup); |
| 5567 | } else if (scnode->nodetype == LYS_LEAFLIST) { |
| 5568 | 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] | 5569 | } |
| 5570 | |
| 5571 | for (i = 0; i < set->used; ) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5572 | siblings = NULL; |
| 5573 | |
| 5574 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
| 5575 | assert(!set->val.nodes[i].node); |
| 5576 | |
| 5577 | /* search in all the trees */ |
| 5578 | siblings = set->tree; |
| 5579 | } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 5580 | /* search in children */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 5581 | siblings = lyd_child(set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5582 | } |
| 5583 | |
| 5584 | /* find the node using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5585 | if (inst) { |
| 5586 | lyd_find_sibling_first(siblings, inst, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5587 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5588 | lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5589 | } |
| 5590 | |
| 5591 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 5592 | if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5593 | ret = LY_EINCOMPLETE; |
| 5594 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5595 | } |
| 5596 | |
| 5597 | if (sub) { |
| 5598 | /* pos filled later */ |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5599 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5600 | ++i; |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5601 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5602 | /* no match */ |
| 5603 | set_remove_node(set, i); |
| 5604 | } |
| 5605 | } |
| 5606 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5607 | cleanup: |
| 5608 | lyd_free_tree(inst); |
| 5609 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5610 | } |
| 5611 | |
| 5612 | /** |
| 5613 | * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
| 5614 | * |
| 5615 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5616 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5617 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5618 | * @param[in] options XPath options. |
| 5619 | * @return LY_ERR |
| 5620 | */ |
| 5621 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5622 | 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] | 5623 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5624 | ly_bool temp_ctx = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5625 | uint32_t getnext_opts; |
| 5626 | uint32_t orig_used, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5627 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5628 | const struct lysc_node *iter, *start_parent; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5629 | const struct lys_module *mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5630 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5631 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5632 | return LY_SUCCESS; |
| 5633 | } |
| 5634 | |
| 5635 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5636 | 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] | 5637 | return LY_EVALID; |
| 5638 | } |
| 5639 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5640 | /* getnext opts */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 5641 | getnext_opts = 0; |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5642 | if (options & LYXP_SCNODE_OUTPUT) { |
| 5643 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 5644 | } |
| 5645 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5646 | orig_used = set->used; |
| 5647 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5648 | uint32_t idx; |
| 5649 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5650 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5651 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5652 | continue; |
| 5653 | } |
| 5654 | |
| 5655 | /* remember context node */ |
| 5656 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5657 | } else { |
| 5658 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5659 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5660 | |
| 5661 | start_parent = set->val.scnodes[i].scnode; |
| 5662 | |
| 5663 | 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] | 5664 | /* 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] | 5665 | * 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] | 5666 | mod_idx = 0; |
Michal Vasko | 9e9f26d | 2020-10-12 16:31:33 +0200 | [diff] [blame] | 5667 | 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] | 5668 | iter = NULL; |
Michal Vasko | 509de4d | 2019-12-10 14:51:30 +0100 | [diff] [blame] | 5669 | /* module may not be implemented */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5670 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5671 | if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5672 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx)); |
| 5673 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5674 | /* we need to prevent these nodes from being considered in this moveto */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5675 | if ((idx < orig_used) && (idx > i)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5676 | set->val.scnodes[idx].in_ctx = 2; |
| 5677 | temp_ctx = 1; |
| 5678 | } |
| 5679 | } |
| 5680 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5681 | } |
| 5682 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5683 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 5684 | iter = NULL; |
| 5685 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5686 | if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5687 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx)); |
| 5688 | |
| 5689 | if ((idx < orig_used) && (idx > i)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5690 | set->val.scnodes[idx].in_ctx = 2; |
| 5691 | temp_ctx = 1; |
| 5692 | } |
| 5693 | } |
| 5694 | } |
| 5695 | } |
| 5696 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5697 | |
| 5698 | /* correct temporary in_ctx values */ |
| 5699 | if (temp_ctx) { |
| 5700 | for (i = 0; i < orig_used; ++i) { |
| 5701 | if (set->val.scnodes[i].in_ctx == 2) { |
| 5702 | set->val.scnodes[i].in_ctx = 1; |
| 5703 | } |
| 5704 | } |
| 5705 | } |
| 5706 | |
| 5707 | return LY_SUCCESS; |
| 5708 | } |
| 5709 | |
| 5710 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5711 | * @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] | 5712 | * Context position aware. |
| 5713 | * |
| 5714 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5715 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5716 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5717 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5718 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5719 | */ |
| 5720 | static LY_ERR |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5721 | moveto_node_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] | 5722 | { |
| 5723 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5724 | const struct lyd_node *next, *elem, *start; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5725 | struct lyxp_set ret_set; |
| 5726 | LY_ERR rc; |
| 5727 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5728 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5729 | return LY_SUCCESS; |
| 5730 | } |
| 5731 | |
| 5732 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5733 | 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] | 5734 | return LY_EVALID; |
| 5735 | } |
| 5736 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5737 | /* replace the original nodes (and throws away all text and meta nodes, root is replaced by a child) */ |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5738 | rc = moveto_node(set, NULL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5739 | LY_CHECK_RET(rc); |
| 5740 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5741 | /* 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] | 5742 | set_init(&ret_set, set); |
| 5743 | for (i = 0; i < set->used; ++i) { |
| 5744 | |
| 5745 | /* TREE DFS */ |
| 5746 | start = set->val.nodes[i].node; |
| 5747 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5748 | rc = moveto_node_check(elem, start, set, ncname, moveto_mod, options); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5749 | if (!rc) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5750 | /* add matching node into result set */ |
| 5751 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
| 5752 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
| 5753 | /* the node is a duplicate, we'll process it later in the set */ |
| 5754 | goto skip_children; |
| 5755 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5756 | } else if (rc == LY_EINCOMPLETE) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5757 | return rc; |
| 5758 | } else if (rc == LY_EINVAL) { |
| 5759 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5760 | } |
| 5761 | |
| 5762 | /* TREE DFS NEXT ELEM */ |
| 5763 | /* select element for the next run - children first */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 5764 | next = lyd_child(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5765 | if (!next) { |
| 5766 | skip_children: |
| 5767 | /* no children, so try siblings, but only if it's not the start, |
| 5768 | * that is considered to be the root and it's siblings are not traversed */ |
| 5769 | if (elem != start) { |
| 5770 | next = elem->next; |
| 5771 | } else { |
| 5772 | break; |
| 5773 | } |
| 5774 | } |
| 5775 | while (!next) { |
| 5776 | /* no siblings, go back through the parents */ |
| 5777 | if ((struct lyd_node *)elem->parent == start) { |
| 5778 | /* we are done, no next element to process */ |
| 5779 | break; |
| 5780 | } |
| 5781 | /* parent is already processed, go to its sibling */ |
| 5782 | elem = (struct lyd_node *)elem->parent; |
| 5783 | next = elem->next; |
| 5784 | } |
| 5785 | } |
| 5786 | } |
| 5787 | |
| 5788 | /* make the temporary set the current one */ |
| 5789 | ret_set.ctx_pos = set->ctx_pos; |
| 5790 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5791 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5792 | memcpy(set, &ret_set, sizeof *set); |
| 5793 | |
| 5794 | return LY_SUCCESS; |
| 5795 | } |
| 5796 | |
| 5797 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5798 | * @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] | 5799 | * |
| 5800 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5801 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5802 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5803 | * @param[in] options XPath options. |
| 5804 | * @return LY_ERR |
| 5805 | */ |
| 5806 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5807 | 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] | 5808 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5809 | uint32_t i, orig_used; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5810 | const struct lysc_node *next, *elem, *start; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5811 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5812 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5813 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5814 | return LY_SUCCESS; |
| 5815 | } |
| 5816 | |
| 5817 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5818 | 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] | 5819 | return LY_EVALID; |
| 5820 | } |
| 5821 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5822 | orig_used = set->used; |
| 5823 | for (i = 0; i < orig_used; ++i) { |
| 5824 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5825 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5826 | continue; |
| 5827 | } |
| 5828 | |
| 5829 | /* remember context node */ |
| 5830 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5831 | } else { |
| 5832 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5833 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5834 | |
| 5835 | /* TREE DFS */ |
| 5836 | start = set->val.scnodes[i].scnode; |
| 5837 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5838 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 5839 | /* schema-only nodes, skip root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5840 | goto next_iter; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5841 | } |
| 5842 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5843 | rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5844 | if (!rc) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5845 | uint32_t idx; |
| 5846 | |
| 5847 | if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5848 | set->val.scnodes[idx].in_ctx = 1; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5849 | if ((uint32_t)idx > i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5850 | /* we will process it later in the set */ |
| 5851 | goto skip_children; |
| 5852 | } |
| 5853 | } else { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5854 | 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] | 5855 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5856 | } else if (rc == LY_EINVAL) { |
| 5857 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5858 | } |
| 5859 | |
| 5860 | next_iter: |
| 5861 | /* TREE DFS NEXT ELEM */ |
| 5862 | /* select element for the next run - children first */ |
| 5863 | 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] | 5864 | if (!next) { |
| 5865 | skip_children: |
| 5866 | /* no children, so try siblings, but only if it's not the start, |
| 5867 | * that is considered to be the root and it's siblings are not traversed */ |
| 5868 | if (elem != start) { |
| 5869 | next = elem->next; |
| 5870 | } else { |
| 5871 | break; |
| 5872 | } |
| 5873 | } |
| 5874 | while (!next) { |
| 5875 | /* no siblings, go back through the parents */ |
| 5876 | if (elem->parent == start) { |
| 5877 | /* we are done, no next element to process */ |
| 5878 | break; |
| 5879 | } |
| 5880 | /* parent is already processed, go to its sibling */ |
| 5881 | elem = elem->parent; |
| 5882 | next = elem->next; |
| 5883 | } |
| 5884 | } |
| 5885 | } |
| 5886 | |
| 5887 | return LY_SUCCESS; |
| 5888 | } |
| 5889 | |
| 5890 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5891 | * @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] | 5892 | * Indirectly context position aware. |
| 5893 | * |
| 5894 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5895 | * @param[in] mod Matching metadata module, NULL for any. |
| 5896 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5897 | * @return LY_ERR |
| 5898 | */ |
| 5899 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5900 | 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] | 5901 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5902 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5903 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5904 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5905 | return LY_SUCCESS; |
| 5906 | } |
| 5907 | |
| 5908 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5909 | 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] | 5910 | return LY_EVALID; |
| 5911 | } |
| 5912 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5913 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5914 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5915 | |
| 5916 | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
| 5917 | * our attributes are always qualified */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5918 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5919 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5920 | |
| 5921 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5922 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5923 | continue; |
| 5924 | } |
| 5925 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5926 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5927 | /* match */ |
| 5928 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5929 | set->val.meta[i].meta = sub; |
| 5930 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5931 | /* pos does not change */ |
| 5932 | replaced = 1; |
| 5933 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5934 | 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] | 5935 | } |
| 5936 | ++i; |
| 5937 | } |
| 5938 | } |
| 5939 | } |
| 5940 | |
| 5941 | if (!replaced) { |
| 5942 | /* no match */ |
| 5943 | set_remove_node(set, i); |
| 5944 | } |
| 5945 | } |
| 5946 | |
| 5947 | return LY_SUCCESS; |
| 5948 | } |
| 5949 | |
| 5950 | /** |
| 5951 | * @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] | 5952 | * Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5953 | * |
| 5954 | * @param[in,out] set1 Set to use for the result. |
| 5955 | * @param[in] set2 Set that is copied to @p set1. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5956 | * @return LY_ERR |
| 5957 | */ |
| 5958 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5959 | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5960 | { |
| 5961 | LY_ERR rc; |
| 5962 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5963 | 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] | 5964 | 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] | 5965 | return LY_EVALID; |
| 5966 | } |
| 5967 | |
| 5968 | /* set2 is empty or both set1 and set2 */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5969 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5970 | return LY_SUCCESS; |
| 5971 | } |
| 5972 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5973 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5974 | memcpy(set1, set2, sizeof *set1); |
| 5975 | /* dynamic memory belongs to set1 now, do not free */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5976 | memset(set2, 0, sizeof *set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5977 | return LY_SUCCESS; |
| 5978 | } |
| 5979 | |
| 5980 | /* we assume sets are sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5981 | assert(!set_sort(set1) && !set_sort(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5982 | |
| 5983 | /* sort, remove duplicates */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5984 | rc = set_sorted_merge(set1, set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5985 | LY_CHECK_RET(rc); |
| 5986 | |
| 5987 | /* final set must be sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5988 | assert(!set_sort(set1)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5989 | |
| 5990 | return LY_SUCCESS; |
| 5991 | } |
| 5992 | |
| 5993 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5994 | * @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] | 5995 | * Context position aware. |
| 5996 | * |
| 5997 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5998 | * @param[in] mod Matching metadata module, NULL for any. |
| 5999 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6000 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6001 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6002 | */ |
| 6003 | static int |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6004 | moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6005 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6006 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6007 | struct lyxp_set *set_all_desc = NULL; |
| 6008 | LY_ERR rc; |
| 6009 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6010 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6011 | return LY_SUCCESS; |
| 6012 | } |
| 6013 | |
| 6014 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6015 | 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] | 6016 | return LY_EVALID; |
| 6017 | } |
| 6018 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6019 | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
| 6020 | * but it likely won't be used much, so it's a waste of time */ |
| 6021 | /* copy the context */ |
| 6022 | set_all_desc = set_copy(set); |
| 6023 | /* get all descendant nodes (the original context nodes are removed) */ |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6024 | rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6025 | if (rc != LY_SUCCESS) { |
| 6026 | lyxp_set_free(set_all_desc); |
| 6027 | return rc; |
| 6028 | } |
| 6029 | /* prepend the original context nodes */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6030 | rc = moveto_union(set, set_all_desc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6031 | if (rc != LY_SUCCESS) { |
| 6032 | lyxp_set_free(set_all_desc); |
| 6033 | return rc; |
| 6034 | } |
| 6035 | lyxp_set_free(set_all_desc); |
| 6036 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6037 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6038 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6039 | |
| 6040 | /* only attributes of an elem can be in the result, skip all the rest, |
| 6041 | * we have all attributes qualified in lyd tree */ |
| 6042 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6043 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6044 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6045 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6046 | continue; |
| 6047 | } |
| 6048 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6049 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6050 | /* match */ |
| 6051 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6052 | set->val.meta[i].meta = sub; |
| 6053 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6054 | /* pos does not change */ |
| 6055 | replaced = 1; |
| 6056 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6057 | 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] | 6058 | } |
| 6059 | ++i; |
| 6060 | } |
| 6061 | } |
| 6062 | } |
| 6063 | |
| 6064 | if (!replaced) { |
| 6065 | /* no match */ |
| 6066 | set_remove_node(set, i); |
| 6067 | } |
| 6068 | } |
| 6069 | |
| 6070 | return LY_SUCCESS; |
| 6071 | } |
| 6072 | |
| 6073 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6074 | * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET. |
| 6075 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6076 | * |
| 6077 | * @param[in] parent Current parent. |
| 6078 | * @param[in] parent_pos Position of @p parent. |
| 6079 | * @param[in] parent_type Node type of @p parent. |
| 6080 | * @param[in,out] to_set Set to use. |
| 6081 | * @param[in] dup_check_set Set for checking duplicities. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6082 | * @param[in] options XPath options. |
| 6083 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6084 | */ |
| 6085 | static LY_ERR |
| 6086 | 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] | 6087 | 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] | 6088 | { |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6089 | const struct lyd_node *iter, *first; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6090 | LY_ERR rc; |
| 6091 | |
| 6092 | switch (parent_type) { |
| 6093 | case LYXP_NODE_ROOT: |
| 6094 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6095 | assert(!parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6096 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6097 | /* add all top-level nodes as elements */ |
| 6098 | first = to_set->tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6099 | break; |
| 6100 | case LYXP_NODE_ELEM: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6101 | /* add just the text node of this term element node */ |
| 6102 | if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6103 | if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) { |
| 6104 | set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used); |
| 6105 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6106 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6107 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6108 | |
| 6109 | /* add all the children of this node */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 6110 | first = lyd_child(parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6111 | break; |
| 6112 | default: |
| 6113 | LOGINT_RET(parent->schema->module->ctx); |
| 6114 | } |
| 6115 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6116 | /* add all top-level nodes as elements */ |
| 6117 | LY_LIST_FOR(first, iter) { |
| 6118 | /* context check */ |
| 6119 | if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) { |
| 6120 | continue; |
| 6121 | } |
| 6122 | |
| 6123 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 6124 | if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(iter->schema) && !(iter->flags & LYD_WHEN_TRUE)) { |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6125 | return LY_EINCOMPLETE; |
| 6126 | } |
| 6127 | |
| 6128 | if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) { |
| 6129 | set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used); |
| 6130 | |
| 6131 | /* also add all the children of this node, recursively */ |
| 6132 | rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options); |
| 6133 | LY_CHECK_RET(rc); |
| 6134 | } |
| 6135 | } |
| 6136 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6137 | return LY_SUCCESS; |
| 6138 | } |
| 6139 | |
| 6140 | /** |
| 6141 | * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET |
| 6142 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6143 | * |
| 6144 | * @param[in,out] set Set to use. |
| 6145 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6146 | * @param[in] options XPath options. |
| 6147 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6148 | */ |
| 6149 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6150 | 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] | 6151 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6152 | struct lyxp_set ret_set; |
| 6153 | LY_ERR rc; |
| 6154 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6155 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6156 | return LY_SUCCESS; |
| 6157 | } |
| 6158 | |
| 6159 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6160 | 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] | 6161 | return LY_EVALID; |
| 6162 | } |
| 6163 | |
| 6164 | /* nothing to do */ |
| 6165 | if (!all_desc) { |
| 6166 | return LY_SUCCESS; |
| 6167 | } |
| 6168 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6169 | /* add all the children, they get added recursively */ |
| 6170 | set_init(&ret_set, set); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6171 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6172 | /* copy the current node to tmp */ |
| 6173 | set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used); |
| 6174 | |
| 6175 | /* do not touch attributes and text nodes */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6176 | 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] | 6177 | continue; |
| 6178 | } |
| 6179 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6180 | /* add all the children */ |
| 6181 | 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] | 6182 | set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6183 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6184 | lyxp_set_free_content(&ret_set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6185 | return rc; |
| 6186 | } |
| 6187 | } |
| 6188 | |
| 6189 | /* use the temporary set as the current one */ |
| 6190 | ret_set.ctx_pos = set->ctx_pos; |
| 6191 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6192 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6193 | memcpy(set, &ret_set, sizeof *set); |
| 6194 | |
| 6195 | return LY_SUCCESS; |
| 6196 | } |
| 6197 | |
| 6198 | /** |
| 6199 | * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET |
| 6200 | * (or LYXP_SET_EMPTY). |
| 6201 | * |
| 6202 | * @param[in,out] set Set to use. |
| 6203 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6204 | * @param[in] options XPath options. |
| 6205 | * @return LY_ERR |
| 6206 | */ |
| 6207 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6208 | 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] | 6209 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6210 | uint32_t getnext_opts; |
| 6211 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6212 | const struct lysc_node *iter, *start_parent; |
| 6213 | const struct lys_module *mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6214 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6215 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6216 | return LY_SUCCESS; |
| 6217 | } |
| 6218 | |
| 6219 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6220 | 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] | 6221 | return LY_EVALID; |
| 6222 | } |
| 6223 | |
| 6224 | /* nothing to do */ |
| 6225 | if (!all_desc) { |
| 6226 | return LY_SUCCESS; |
| 6227 | } |
| 6228 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6229 | /* getnext opts */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 6230 | getnext_opts = 0; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6231 | if (options & LYXP_SCNODE_OUTPUT) { |
| 6232 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 6233 | } |
| 6234 | |
| 6235 | /* 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] | 6236 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6237 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6238 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6239 | continue; |
| 6240 | } |
| 6241 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6242 | /* remember context node */ |
| 6243 | set->val.scnodes[i].in_ctx = -1; |
| 6244 | } else { |
| 6245 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6246 | } |
| 6247 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6248 | start_parent = set->val.scnodes[i].scnode; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6249 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6250 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
| 6251 | /* it can actually be in any module, it's all <running> */ |
| 6252 | mod_idx = 0; |
| 6253 | while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
| 6254 | iter = NULL; |
| 6255 | /* module may not be implemented */ |
| 6256 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
| 6257 | /* context check */ |
| 6258 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) { |
| 6259 | continue; |
| 6260 | } |
| 6261 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6262 | 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] | 6263 | /* throw away the insert index, we want to consider that node again, recursively */ |
| 6264 | } |
| 6265 | } |
| 6266 | |
| 6267 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 6268 | iter = NULL; |
| 6269 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6270 | /* context check */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6271 | 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] | 6272 | continue; |
| 6273 | } |
| 6274 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6275 | 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] | 6276 | } |
| 6277 | } |
| 6278 | } |
| 6279 | |
| 6280 | return LY_SUCCESS; |
| 6281 | } |
| 6282 | |
| 6283 | /** |
| 6284 | * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET |
| 6285 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6286 | * |
| 6287 | * @param[in] set Set to use. |
| 6288 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6289 | * @param[in] options XPath options. |
| 6290 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6291 | */ |
| 6292 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6293 | 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] | 6294 | { |
| 6295 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6296 | struct lyd_node *node, *new_node; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6297 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6298 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6299 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6300 | return LY_SUCCESS; |
| 6301 | } |
| 6302 | |
| 6303 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6304 | 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] | 6305 | return LY_EVALID; |
| 6306 | } |
| 6307 | |
| 6308 | if (all_desc) { |
| 6309 | /* <path>//.. == <path>//./.. */ |
| 6310 | rc = moveto_self(set, 1, options); |
| 6311 | LY_CHECK_RET(rc); |
| 6312 | } |
| 6313 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6314 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6315 | node = set->val.nodes[i].node; |
| 6316 | |
| 6317 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 6318 | new_node = (struct lyd_node *)node->parent; |
| 6319 | } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) { |
| 6320 | new_node = node; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6321 | } else if (set->val.nodes[i].type == LYXP_NODE_META) { |
| 6322 | new_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6323 | if (!new_node) { |
| 6324 | LOGINT_RET(set->ctx); |
| 6325 | } |
| 6326 | } else { |
| 6327 | /* root does not have a parent */ |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6328 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6329 | continue; |
| 6330 | } |
| 6331 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6332 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 6333 | if (!(options & LYXP_IGNORE_WHEN) && new_node && lysc_has_when(new_node->schema) && !(new_node->flags & LYD_WHEN_TRUE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6334 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6335 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6336 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6337 | if (!new_node) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6338 | /* node already there can also be the root */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6339 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6340 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6341 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6342 | /* 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] | 6343 | new_type = LYXP_NODE_ELEM; |
| 6344 | } |
| 6345 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6346 | if (set_dup_node_check(set, new_node, new_type, -1)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6347 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6348 | } else { |
| 6349 | set_replace_node(set, new_node, 0, new_type, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6350 | } |
| 6351 | } |
| 6352 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6353 | set_remove_nodes_none(set); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6354 | assert(!set_sort(set) && !set_sorted_dup_node_clean(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6355 | |
| 6356 | return LY_SUCCESS; |
| 6357 | } |
| 6358 | |
| 6359 | /** |
| 6360 | * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET |
| 6361 | * (or LYXP_SET_EMPTY). |
| 6362 | * |
| 6363 | * @param[in] set Set to use. |
| 6364 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6365 | * @param[in] options XPath options. |
| 6366 | * @return LY_ERR |
| 6367 | */ |
| 6368 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6369 | 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] | 6370 | { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6371 | uint32_t i, orig_used, idx; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6372 | ly_bool temp_ctx = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6373 | const struct lysc_node *node, *new_node; |
| 6374 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6375 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6376 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6377 | return LY_SUCCESS; |
| 6378 | } |
| 6379 | |
| 6380 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6381 | 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] | 6382 | return LY_EVALID; |
| 6383 | } |
| 6384 | |
| 6385 | if (all_desc) { |
| 6386 | /* <path>//.. == <path>//./.. */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6387 | LY_CHECK_RET(moveto_scnode_self(set, 1, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6388 | } |
| 6389 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6390 | orig_used = set->used; |
| 6391 | for (i = 0; i < orig_used; ++i) { |
| 6392 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6393 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6394 | continue; |
| 6395 | } |
| 6396 | |
| 6397 | /* remember context node */ |
| 6398 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 6399 | } else { |
| 6400 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6401 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6402 | |
| 6403 | node = set->val.scnodes[i].scnode; |
| 6404 | |
| 6405 | if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6406 | new_node = lysc_data_parent(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6407 | } else { |
| 6408 | /* root does not have a parent */ |
| 6409 | continue; |
| 6410 | } |
| 6411 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6412 | if (!new_node) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6413 | /* node has no parent */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6414 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6415 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6416 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6417 | /* 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] | 6418 | new_type = LYXP_NODE_ELEM; |
| 6419 | } |
| 6420 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6421 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx)); |
| 6422 | if ((idx < orig_used) && (idx > i)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6423 | set->val.scnodes[idx].in_ctx = 2; |
| 6424 | temp_ctx = 1; |
| 6425 | } |
| 6426 | } |
| 6427 | |
| 6428 | if (temp_ctx) { |
| 6429 | for (i = 0; i < orig_used; ++i) { |
| 6430 | if (set->val.scnodes[i].in_ctx == 2) { |
| 6431 | set->val.scnodes[i].in_ctx = 1; |
| 6432 | } |
| 6433 | } |
| 6434 | } |
| 6435 | |
| 6436 | return LY_SUCCESS; |
| 6437 | } |
| 6438 | |
| 6439 | /** |
| 6440 | * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'. |
| 6441 | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
| 6442 | * |
| 6443 | * @param[in,out] set1 Set to use for the result. |
| 6444 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6445 | * @param[in] op Comparison operator to process. |
| 6446 | * @param[in] options XPath options. |
| 6447 | * @return LY_ERR |
| 6448 | */ |
| 6449 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6450 | 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] | 6451 | { |
| 6452 | /* |
| 6453 | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
| 6454 | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
| 6455 | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6456 | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6457 | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
| 6458 | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6459 | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6460 | * |
| 6461 | * '=' or '!=' |
| 6462 | * BOOLEAN + BOOLEAN |
| 6463 | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6464 | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6465 | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6466 | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6467 | * NUMBER + NUMBER |
| 6468 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6469 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6470 | * STRING + STRING |
| 6471 | * |
| 6472 | * '<=', '<', '>=', '>' |
| 6473 | * NUMBER + NUMBER |
| 6474 | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6475 | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6476 | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6477 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6478 | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6479 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6480 | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6481 | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6482 | */ |
| 6483 | struct lyxp_set iter1, iter2; |
| 6484 | int result; |
| 6485 | int64_t i; |
| 6486 | LY_ERR rc; |
| 6487 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6488 | memset(&iter1, 0, sizeof iter1); |
| 6489 | memset(&iter2, 0, sizeof iter2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6490 | |
| 6491 | /* iterative evaluation with node-sets */ |
| 6492 | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
| 6493 | if (set1->type == LYXP_SET_NODE_SET) { |
| 6494 | for (i = 0; i < set1->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6495 | /* cast set1 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6496 | switch (set2->type) { |
| 6497 | case LYXP_SET_NUMBER: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6498 | rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6499 | break; |
| 6500 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6501 | rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6502 | break; |
| 6503 | default: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6504 | rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6505 | break; |
| 6506 | } |
| 6507 | LY_CHECK_RET(rc); |
| 6508 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6509 | /* canonize set2 */ |
| 6510 | LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc); |
| 6511 | |
| 6512 | /* compare recursively */ |
| 6513 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
| 6514 | lyxp_set_free_content(&iter2); |
| 6515 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6516 | |
| 6517 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6518 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6519 | set_fill_boolean(set1, 1); |
| 6520 | return LY_SUCCESS; |
| 6521 | } |
| 6522 | } |
| 6523 | } else { |
| 6524 | for (i = 0; i < set2->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6525 | /* set set2 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6526 | switch (set1->type) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6527 | case LYXP_SET_NUMBER: |
| 6528 | rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i); |
| 6529 | break; |
| 6530 | case LYXP_SET_BOOLEAN: |
| 6531 | rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i); |
| 6532 | break; |
| 6533 | default: |
| 6534 | rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i); |
| 6535 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6536 | } |
| 6537 | LY_CHECK_RET(rc); |
| 6538 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6539 | /* canonize set1 */ |
| 6540 | 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] | 6541 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6542 | /* compare recursively */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6543 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6544 | lyxp_set_free_content(&iter2); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6545 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6546 | |
| 6547 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6548 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6549 | set_fill_boolean(set1, 1); |
| 6550 | return LY_SUCCESS; |
| 6551 | } |
| 6552 | } |
| 6553 | } |
| 6554 | |
| 6555 | /* false for all nodes */ |
| 6556 | set_fill_boolean(set1, 0); |
| 6557 | return LY_SUCCESS; |
| 6558 | } |
| 6559 | |
| 6560 | /* first convert properly */ |
| 6561 | if ((op[0] == '=') || (op[0] == '!')) { |
| 6562 | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6563 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
| 6564 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6565 | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6566 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6567 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6568 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6569 | LY_CHECK_RET(rc); |
| 6570 | } /* else we have 2 strings */ |
| 6571 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6572 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6573 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6574 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6575 | LY_CHECK_RET(rc); |
| 6576 | } |
| 6577 | |
| 6578 | assert(set1->type == set2->type); |
| 6579 | |
| 6580 | /* compute result */ |
| 6581 | if (op[0] == '=') { |
| 6582 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6583 | result = (set1->val.bln == set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6584 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6585 | result = (set1->val.num == set2->val.num); |
| 6586 | } else { |
| 6587 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6588 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6589 | } |
| 6590 | } else if (op[0] == '!') { |
| 6591 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6592 | result = (set1->val.bln != set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6593 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6594 | result = (set1->val.num != set2->val.num); |
| 6595 | } else { |
| 6596 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | c205843 | 2020-11-06 17:26:21 +0100 | [diff] [blame] | 6597 | result = strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6598 | } |
| 6599 | } else { |
| 6600 | assert(set1->type == LYXP_SET_NUMBER); |
| 6601 | if (op[0] == '<') { |
| 6602 | if (op[1] == '=') { |
| 6603 | result = (set1->val.num <= set2->val.num); |
| 6604 | } else { |
| 6605 | result = (set1->val.num < set2->val.num); |
| 6606 | } |
| 6607 | } else { |
| 6608 | if (op[1] == '=') { |
| 6609 | result = (set1->val.num >= set2->val.num); |
| 6610 | } else { |
| 6611 | result = (set1->val.num > set2->val.num); |
| 6612 | } |
| 6613 | } |
| 6614 | } |
| 6615 | |
| 6616 | /* assign result */ |
| 6617 | if (result) { |
| 6618 | set_fill_boolean(set1, 1); |
| 6619 | } else { |
| 6620 | set_fill_boolean(set1, 0); |
| 6621 | } |
| 6622 | |
| 6623 | return LY_SUCCESS; |
| 6624 | } |
| 6625 | |
| 6626 | /** |
| 6627 | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
| 6628 | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
| 6629 | * |
| 6630 | * @param[in,out] set1 Set to use for the result. |
| 6631 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6632 | * @param[in] op Operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6633 | * @return LY_ERR |
| 6634 | */ |
| 6635 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6636 | 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] | 6637 | { |
| 6638 | LY_ERR rc; |
| 6639 | |
| 6640 | /* unary '-' */ |
| 6641 | if (!set2 && (op[0] == '-')) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6642 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6643 | LY_CHECK_RET(rc); |
| 6644 | set1->val.num *= -1; |
| 6645 | lyxp_set_free(set2); |
| 6646 | return LY_SUCCESS; |
| 6647 | } |
| 6648 | |
| 6649 | assert(set1 && set2); |
| 6650 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6651 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6652 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6653 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6654 | LY_CHECK_RET(rc); |
| 6655 | |
| 6656 | switch (op[0]) { |
| 6657 | /* '+' */ |
| 6658 | case '+': |
| 6659 | set1->val.num += set2->val.num; |
| 6660 | break; |
| 6661 | |
| 6662 | /* '-' */ |
| 6663 | case '-': |
| 6664 | set1->val.num -= set2->val.num; |
| 6665 | break; |
| 6666 | |
| 6667 | /* '*' */ |
| 6668 | case '*': |
| 6669 | set1->val.num *= set2->val.num; |
| 6670 | break; |
| 6671 | |
| 6672 | /* 'div' */ |
| 6673 | case 'd': |
| 6674 | set1->val.num /= set2->val.num; |
| 6675 | break; |
| 6676 | |
| 6677 | /* 'mod' */ |
| 6678 | case 'm': |
| 6679 | set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num); |
| 6680 | break; |
| 6681 | |
| 6682 | default: |
| 6683 | LOGINT_RET(set1->ctx); |
| 6684 | } |
| 6685 | |
| 6686 | return LY_SUCCESS; |
| 6687 | } |
| 6688 | |
| 6689 | /* |
| 6690 | * eval functions |
| 6691 | * |
| 6692 | * They execute a parsed XPath expression on some data subtree. |
| 6693 | */ |
| 6694 | |
| 6695 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6696 | * @brief Evaluate Predicate. Logs directly on error. |
| 6697 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6698 | * [9] Predicate ::= '[' Expr ']' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6699 | * |
| 6700 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6701 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6702 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6703 | * @param[in] options XPath options. |
| 6704 | * @param[in] parent_pos_pred Whether parent predicate was a positional one. |
| 6705 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6706 | */ |
| 6707 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 6708 | 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] | 6709 | { |
| 6710 | LY_ERR rc; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 6711 | uint16_t i, orig_exp; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6712 | uint32_t orig_pos, orig_size; |
| 6713 | int32_t pred_in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6714 | struct lyxp_set set2; |
| 6715 | struct lyd_node *orig_parent; |
| 6716 | |
| 6717 | /* '[' */ |
| 6718 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6719 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6720 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6721 | |
| 6722 | if (!set) { |
| 6723 | only_parse: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6724 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6725 | LY_CHECK_RET(rc); |
| 6726 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 6727 | /* 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] | 6728 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6729 | |
| 6730 | /* empty set, nothing to evaluate */ |
| 6731 | if (!set->used) { |
| 6732 | goto only_parse; |
| 6733 | } |
| 6734 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6735 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6736 | orig_pos = 0; |
| 6737 | orig_size = set->used; |
| 6738 | orig_parent = NULL; |
Michal Vasko | 39dbf35 | 2020-05-21 10:08:59 +0200 | [diff] [blame] | 6739 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6740 | set_init(&set2, set); |
| 6741 | set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0); |
| 6742 | /* remember the node context position for position() and context size for last(), |
| 6743 | * predicates should always be evaluated with respect to the child axis (since we do |
| 6744 | * not support explicit axes) so we assign positions based on their parents */ |
| 6745 | if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) { |
| 6746 | orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent; |
| 6747 | orig_pos = 1; |
| 6748 | } else { |
| 6749 | ++orig_pos; |
| 6750 | } |
| 6751 | |
| 6752 | set2.ctx_pos = orig_pos; |
| 6753 | set2.ctx_size = orig_size; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6754 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6755 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6756 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6757 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6758 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6759 | return rc; |
| 6760 | } |
| 6761 | |
| 6762 | /* number is a position */ |
| 6763 | if (set2.type == LYXP_SET_NUMBER) { |
| 6764 | if ((long long)set2.val.num == orig_pos) { |
| 6765 | set2.val.num = 1; |
| 6766 | } else { |
| 6767 | set2.val.num = 0; |
| 6768 | } |
| 6769 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6770 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6771 | |
| 6772 | /* predicate satisfied or not? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6773 | if (!set2.val.bln) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6774 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6775 | } |
| 6776 | } |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6777 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6778 | |
| 6779 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 6780 | for (i = 0; i < set->used; ++i) { |
| 6781 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6782 | /* there is a currently-valid node */ |
| 6783 | break; |
| 6784 | } |
| 6785 | } |
| 6786 | /* empty set, nothing to evaluate */ |
| 6787 | if (i == set->used) { |
| 6788 | goto only_parse; |
| 6789 | } |
| 6790 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6791 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6792 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6793 | /* set special in_ctx to all the valid snodes */ |
| 6794 | pred_in_ctx = set_scnode_new_in_ctx(set); |
| 6795 | |
| 6796 | /* use the valid snodes one-by-one */ |
| 6797 | for (i = 0; i < set->used; ++i) { |
| 6798 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
| 6799 | continue; |
| 6800 | } |
| 6801 | set->val.scnodes[i].in_ctx = 1; |
| 6802 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6803 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6804 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6805 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6806 | LY_CHECK_RET(rc); |
| 6807 | |
| 6808 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
| 6809 | } |
| 6810 | |
| 6811 | /* restore the state as it was before the predicate */ |
| 6812 | for (i = 0; i < set->used; ++i) { |
| 6813 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6814 | set->val.scnodes[i].in_ctx = 0; |
| 6815 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
| 6816 | set->val.scnodes[i].in_ctx = 1; |
| 6817 | } |
| 6818 | } |
| 6819 | |
| 6820 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6821 | set2.type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6822 | set_fill_set(&set2, set); |
| 6823 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6824 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6825 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6826 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6827 | return rc; |
| 6828 | } |
| 6829 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6830 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6831 | if (!set2.val.bln) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6832 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6833 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6834 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6835 | } |
| 6836 | |
| 6837 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6838 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6839 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6840 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6841 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6842 | |
| 6843 | return LY_SUCCESS; |
| 6844 | } |
| 6845 | |
| 6846 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6847 | * @brief Evaluate Literal. Logs directly on error. |
| 6848 | * |
| 6849 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6850 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6851 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6852 | */ |
| 6853 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 6854 | 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] | 6855 | { |
| 6856 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6857 | if (exp->tok_len[*tok_idx] == 2) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6858 | set_fill_string(set, "", 0); |
| 6859 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6860 | 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] | 6861 | } |
| 6862 | } |
| 6863 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6864 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6865 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6866 | } |
| 6867 | |
| 6868 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6869 | * @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] | 6870 | * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6871 | * @param[in] exp Full parsed XPath expression. |
| 6872 | * @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] | 6873 | * @param[in] ctx_node Found schema node as the context for the predicate. |
| 6874 | * @param[in] cur_mod Current module for the expression. |
| 6875 | * @param[in] cur_node Current (original context) node. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6876 | * @param[in] format Format of any prefixes in key names/values. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6877 | * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix). |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6878 | * @param[out] predicates Parsed predicates. |
| 6879 | * @param[out] pred_type Type of @p predicates. |
| 6880 | * @return LY_SUCCESS on success, |
| 6881 | * @return LY_ERR on any error. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6882 | */ |
| 6883 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 6884 | 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] | 6885 | const struct lys_module *cur_mod, const struct lysc_node *cur_node, LY_PREFIX_FORMAT format, void *prefix_data, |
| 6886 | struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6887 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6888 | LY_ERR ret = LY_SUCCESS; |
| 6889 | uint16_t key_count, e_idx, pred_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6890 | const struct lysc_node *key; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6891 | size_t pred_len; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6892 | uint32_t prev_lo; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6893 | struct lyxp_expr *exp2 = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6894 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6895 | assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6896 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6897 | if (ctx_node->nodetype == LYS_LIST) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6898 | /* get key count */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6899 | if (ctx_node->flags & LYS_KEYLESS) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6900 | return LY_EINVAL; |
| 6901 | } |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6902 | 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] | 6903 | assert(key_count); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6904 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6905 | /* learn where the predicates end */ |
| 6906 | e_idx = *tok_idx; |
| 6907 | while (key_count) { |
| 6908 | /* '[' */ |
| 6909 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6910 | return LY_EINVAL; |
| 6911 | } |
| 6912 | ++e_idx; |
| 6913 | |
| 6914 | /* ']' */ |
| 6915 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6916 | ++e_idx; |
| 6917 | } |
| 6918 | ++e_idx; |
| 6919 | |
| 6920 | /* another presumably key predicate parsed */ |
| 6921 | --key_count; |
| 6922 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6923 | } else { |
| 6924 | /* learn just where this single predicate ends */ |
| 6925 | e_idx = *tok_idx; |
| 6926 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6927 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6928 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6929 | return LY_EINVAL; |
| 6930 | } |
| 6931 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6932 | |
| 6933 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6934 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6935 | ++e_idx; |
| 6936 | } |
| 6937 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6938 | } |
| 6939 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6940 | /* get the joined length of all the keys predicates/of the single leaf-list predicate */ |
| 6941 | pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx]; |
| 6942 | |
| 6943 | /* turn logging off */ |
| 6944 | prev_lo = ly_log_options(0); |
| 6945 | |
| 6946 | /* parse the predicate(s) */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6947 | LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx], |
| 6948 | pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6949 | |
| 6950 | /* compile */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6951 | ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format, |
| 6952 | prefix_data, predicates, pred_type); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6953 | LY_CHECK_GOTO(ret, cleanup); |
| 6954 | |
| 6955 | /* success, the predicate must include all the needed information for hash-based search */ |
| 6956 | *tok_idx = e_idx; |
| 6957 | |
| 6958 | cleanup: |
| 6959 | ly_log_options(prev_lo); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6960 | lyxp_expr_free(ctx_node->module->ctx, exp2); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6961 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6962 | } |
| 6963 | |
| 6964 | /** |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6965 | * @brief Search for/check the next schema node that could be the only matching schema node meaning the |
| 6966 | * data node(s) could be found using a single hash-based search. |
| 6967 | * |
| 6968 | * @param[in] node Next context node to check. |
| 6969 | * @param[in] name Expected node name. |
| 6970 | * @param[in] name_len Length of @p name. |
| 6971 | * @param[in] moveto_mod Expected node module. |
| 6972 | * @param[in] root_type XPath root type. |
| 6973 | * @param[in] no_prefix Whether the node name was unprefixed. |
| 6974 | * @param[in] format Prefix format. |
| 6975 | * @param[in,out] found Previously found node, is updated. |
| 6976 | * @return LY_SUCCESS on success, |
| 6977 | * @return LY_ENOT if the whole check failed and hashes cannot be used. |
| 6978 | */ |
| 6979 | static LY_ERR |
| 6980 | eval_name_test_with_predicate_get_scnode(const struct lyd_node *node, const char *name, uint16_t name_len, |
| 6981 | const struct lys_module *moveto_mod, enum lyxp_node_type root_type, ly_bool no_prefix, LY_PREFIX_FORMAT format, |
| 6982 | const struct lysc_node **found) |
| 6983 | { |
| 6984 | const struct lysc_node *scnode; |
| 6985 | const struct lys_module *mod; |
| 6986 | uint32_t idx = 0; |
| 6987 | |
| 6988 | continue_search: |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 6989 | scnode = NULL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6990 | if (!node) { |
| 6991 | if (no_prefix && (format == LY_PREF_JSON)) { |
| 6992 | /* search all modules for a single match */ |
| 6993 | while ((mod = ly_ctx_get_module_iter(moveto_mod->ctx, &idx))) { |
| 6994 | scnode = lys_find_child(NULL, mod, name, name_len, 0, 0); |
| 6995 | if (scnode) { |
| 6996 | /* we have found a match */ |
| 6997 | break; |
| 6998 | } |
| 6999 | } |
| 7000 | |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7001 | if (!scnode) { |
| 7002 | /* all modules searched */ |
| 7003 | idx = 0; |
| 7004 | } |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7005 | } else { |
| 7006 | /* search in top-level */ |
| 7007 | scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0); |
| 7008 | } |
| 7009 | } else if (!*found || (lysc_data_parent(*found) != node->schema)) { |
| 7010 | if (no_prefix && (format == LY_PREF_JSON)) { |
| 7011 | /* we must adjust the module to inherit the one from the context node */ |
| 7012 | moveto_mod = node->schema->module; |
| 7013 | } |
| 7014 | |
| 7015 | /* search in children, do not repeat the same search */ |
| 7016 | 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] | 7017 | } /* else skip redundant search */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7018 | |
| 7019 | /* additional context check */ |
| 7020 | if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 7021 | scnode = NULL; |
| 7022 | } |
| 7023 | |
| 7024 | if (scnode) { |
| 7025 | if (*found) { |
| 7026 | /* we found a schema node with the same name but at different level, give up, too complicated |
| 7027 | * (more hash-based searches would be required, not supported) */ |
| 7028 | return LY_ENOT; |
| 7029 | } else { |
| 7030 | /* remember the found schema node and continue to make sure it can be used */ |
| 7031 | *found = scnode; |
| 7032 | } |
| 7033 | } |
| 7034 | |
| 7035 | if (idx) { |
| 7036 | /* continue searching all the following models */ |
| 7037 | goto continue_search; |
| 7038 | } |
| 7039 | |
| 7040 | return LY_SUCCESS; |
| 7041 | } |
| 7042 | |
| 7043 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7044 | * @brief Evaluate NameTest and any following Predicates. Logs directly on error. |
| 7045 | * |
| 7046 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7047 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7048 | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
| 7049 | * |
| 7050 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7051 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7052 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 7053 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7054 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7055 | * @param[in] options XPath options. |
| 7056 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7057 | */ |
| 7058 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7059 | 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] | 7060 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7061 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7062 | char *path; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7063 | const char *ncname, *ncname_dict = NULL; |
| 7064 | uint16_t ncname_len; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7065 | const struct lys_module *moveto_mod = NULL; |
| 7066 | const struct lysc_node *scnode = NULL, *ctx_scnode; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7067 | struct ly_path_predicate *predicates = NULL; |
| 7068 | enum ly_path_pred_type pred_type = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7069 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7070 | ly_bool no_prefix; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7071 | |
| 7072 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7073 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7074 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7075 | |
| 7076 | if (!set) { |
| 7077 | goto moveto; |
| 7078 | } |
| 7079 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7080 | ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]]; |
| 7081 | ncname_len = exp->tok_len[*tok_idx - 1]; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7082 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7083 | /* get the first schema context node */ |
| 7084 | if (set->used) { |
| 7085 | if (set->type == LYXP_SET_NODE_SET) { |
| 7086 | ctx_scnode = set->val.nodes[0].node ? set->val.nodes[0].node->schema : NULL; |
| 7087 | } else { |
| 7088 | ctx_scnode = set->val.scnodes[0].scnode; |
| 7089 | } |
| 7090 | } else { |
| 7091 | ctx_scnode = NULL; |
| 7092 | } |
| 7093 | |
| 7094 | /* parse (and skip) module name, use any context node (if available) just so we get some moveto_mod if there |
| 7095 | * is no prefix for ::LY_PREF_JSON */ |
| 7096 | rc = moveto_resolve_model(&ncname, &ncname_len, set, ctx_scnode, &moveto_mod); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7097 | LY_CHECK_GOTO(rc, cleanup); |
| 7098 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7099 | if (ncname_len == exp->tok_len[*tok_idx - 1]) { |
| 7100 | /* remember that there is no prefix */ |
| 7101 | no_prefix = 1; |
| 7102 | } else { |
| 7103 | no_prefix = 0; |
| 7104 | } |
| 7105 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7106 | if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) { |
| 7107 | /* find the matching schema node in some parent in the context */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7108 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7109 | if (eval_name_test_with_predicate_get_scnode(set->val.nodes[i].node, ncname, ncname_len, moveto_mod, |
| 7110 | set->root_type, no_prefix, set->format, &scnode)) { |
| 7111 | /* check failed */ |
| 7112 | scnode = NULL; |
| 7113 | break; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7114 | } |
| 7115 | } |
| 7116 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7117 | if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 7118 | /* try to create the predicates */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7119 | if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ? |
| 7120 | 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] | 7121 | /* hashes cannot be used */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7122 | scnode = NULL; |
| 7123 | } |
| 7124 | } |
| 7125 | } |
| 7126 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7127 | if (!scnode && moveto_mod) { |
| 7128 | /* we are not able to match based on a schema node and not all the modules match, |
| 7129 | * use dictionary for efficient comparison */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 7130 | 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] | 7131 | } |
| 7132 | |
| 7133 | moveto: |
| 7134 | /* move to the attribute(s), data node(s), or schema node(s) */ |
| 7135 | if (attr_axis) { |
| 7136 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7137 | set_scnode_clear_ctx(set); |
| 7138 | } else { |
| 7139 | if (all_desc) { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7140 | rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7141 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7142 | rc = moveto_attr(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7143 | } |
| 7144 | LY_CHECK_GOTO(rc, cleanup); |
| 7145 | } |
| 7146 | } else { |
| 7147 | if (set && (options & LYXP_SCNODE_ALL)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7148 | int64_t i; |
| 7149 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7150 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7151 | rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7152 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7153 | rc = moveto_scnode(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7154 | } |
| 7155 | LY_CHECK_GOTO(rc, cleanup); |
| 7156 | |
| 7157 | for (i = set->used - 1; i > -1; --i) { |
| 7158 | if (set->val.scnodes[i].in_ctx > 0) { |
| 7159 | break; |
| 7160 | } |
| 7161 | } |
| 7162 | if (i == -1) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7163 | path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7164 | 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] | 7165 | ncname_len, ncname, ncname - exp->expr, exp->expr, path); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7166 | free(path); |
| 7167 | } |
| 7168 | } else { |
| 7169 | if (all_desc) { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7170 | rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7171 | } else { |
| 7172 | if (scnode) { |
| 7173 | /* we can find the nodes using hashes */ |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7174 | rc = moveto_node_hash(set, scnode, predicates, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7175 | } else { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7176 | rc = moveto_node(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7177 | } |
| 7178 | } |
| 7179 | LY_CHECK_GOTO(rc, cleanup); |
| 7180 | } |
| 7181 | } |
| 7182 | |
| 7183 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7184 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7185 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7186 | LY_CHECK_RET(rc); |
| 7187 | } |
| 7188 | |
| 7189 | cleanup: |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7190 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7191 | lydict_remove(set->ctx, ncname_dict); |
Michal Vasko | f7e16e2 | 2020-10-21 09:24:39 +0200 | [diff] [blame] | 7192 | ly_path_predicates_free(set->ctx, pred_type, predicates); |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7193 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7194 | return rc; |
| 7195 | } |
| 7196 | |
| 7197 | /** |
| 7198 | * @brief Evaluate NodeType and any following Predicates. Logs directly on error. |
| 7199 | * |
| 7200 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7201 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7202 | * [8] NodeType ::= 'text' | 'node' |
| 7203 | * |
| 7204 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7205 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7206 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 7207 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7208 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7209 | * @param[in] options XPath options. |
| 7210 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7211 | */ |
| 7212 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7213 | 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] | 7214 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7215 | { |
| 7216 | LY_ERR rc; |
| 7217 | |
| 7218 | /* TODO */ |
| 7219 | (void)attr_axis; |
| 7220 | (void)all_desc; |
| 7221 | |
| 7222 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7223 | assert(exp->tok_len[*tok_idx] == 4); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7224 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 7225 | set_scnode_clear_ctx(set); |
| 7226 | /* just for the debug message below */ |
| 7227 | set = NULL; |
| 7228 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7229 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7230 | rc = xpath_node(NULL, 0, set, options); |
| 7231 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7232 | assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7233 | rc = xpath_text(NULL, 0, set, options); |
| 7234 | } |
| 7235 | LY_CHECK_RET(rc); |
| 7236 | } |
| 7237 | } |
| 7238 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7239 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7240 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7241 | |
| 7242 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7243 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7244 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7245 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7246 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7247 | |
| 7248 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7249 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7250 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7251 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7252 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7253 | |
| 7254 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7255 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7256 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7257 | LY_CHECK_RET(rc); |
| 7258 | } |
| 7259 | |
| 7260 | return LY_SUCCESS; |
| 7261 | } |
| 7262 | |
| 7263 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7264 | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
| 7265 | * |
| 7266 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 7267 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7268 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7269 | * |
| 7270 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7271 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7272 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7273 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7274 | * @param[in] options XPath options. |
| 7275 | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
| 7276 | */ |
| 7277 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7278 | eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set, |
| 7279 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7280 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7281 | ly_bool attr_axis; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7282 | LY_ERR rc; |
| 7283 | |
| 7284 | goto step; |
| 7285 | do { |
| 7286 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7287 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7288 | all_desc = 0; |
| 7289 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7290 | assert(exp->tok_len[*tok_idx] == 2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7291 | all_desc = 1; |
| 7292 | } |
| 7293 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7294 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7295 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7296 | |
| 7297 | step: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7298 | /* evaluate abbreviated axis '@'? if any */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7299 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7300 | attr_axis = 1; |
| 7301 | |
| 7302 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7303 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7304 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7305 | } else { |
| 7306 | attr_axis = 0; |
| 7307 | } |
| 7308 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7309 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7310 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7311 | case LYXP_TOKEN_DOT: |
| 7312 | /* evaluate '.' */ |
| 7313 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7314 | rc = moveto_scnode_self(set, all_desc, options); |
| 7315 | } else { |
| 7316 | rc = moveto_self(set, all_desc, options); |
| 7317 | } |
| 7318 | LY_CHECK_RET(rc); |
| 7319 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7320 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7321 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7322 | break; |
| 7323 | |
| 7324 | case LYXP_TOKEN_DDOT: |
| 7325 | /* evaluate '..' */ |
| 7326 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7327 | rc = moveto_scnode_parent(set, all_desc, options); |
| 7328 | } else { |
| 7329 | rc = moveto_parent(set, all_desc, options); |
| 7330 | } |
| 7331 | LY_CHECK_RET(rc); |
| 7332 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7333 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7334 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7335 | break; |
| 7336 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7337 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7338 | /* evaluate NameTest Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7339 | 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] | 7340 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7341 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7342 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7343 | case LYXP_TOKEN_NODETYPE: |
| 7344 | /* evaluate NodeType Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7345 | 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] | 7346 | LY_CHECK_RET(rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7347 | break; |
| 7348 | |
| 7349 | default: |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 7350 | LOGINT_RET(set ? set->ctx : NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7351 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7352 | } 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] | 7353 | |
| 7354 | return LY_SUCCESS; |
| 7355 | } |
| 7356 | |
| 7357 | /** |
| 7358 | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
| 7359 | * |
| 7360 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 7361 | * |
| 7362 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7363 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7364 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7365 | * @param[in] options XPath options. |
| 7366 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7367 | */ |
| 7368 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7369 | 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] | 7370 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7371 | ly_bool all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7372 | |
| 7373 | if (set) { |
| 7374 | /* no matter what tokens follow, we need to be at the root */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7375 | LY_CHECK_RET(moveto_root(set, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7376 | } |
| 7377 | |
| 7378 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7379 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7380 | /* evaluate '/' - deferred */ |
| 7381 | all_desc = 0; |
| 7382 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7383 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7384 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7385 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7386 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7387 | return LY_SUCCESS; |
| 7388 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7389 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7390 | case LYXP_TOKEN_DOT: |
| 7391 | case LYXP_TOKEN_DDOT: |
| 7392 | case LYXP_TOKEN_AT: |
| 7393 | case LYXP_TOKEN_NAMETEST: |
| 7394 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7395 | 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] | 7396 | break; |
| 7397 | default: |
| 7398 | break; |
| 7399 | } |
| 7400 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7401 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 7402 | /* '//' RelativeLocationPath */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7403 | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
| 7404 | all_desc = 1; |
| 7405 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7406 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7407 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7408 | |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7409 | 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] | 7410 | } |
| 7411 | |
| 7412 | return LY_SUCCESS; |
| 7413 | } |
| 7414 | |
| 7415 | /** |
| 7416 | * @brief Evaluate FunctionCall. Logs directly on error. |
| 7417 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7418 | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7419 | * |
| 7420 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7421 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7422 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7423 | * @param[in] options XPath options. |
| 7424 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7425 | */ |
| 7426 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7427 | 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] | 7428 | { |
| 7429 | LY_ERR rc; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7430 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7431 | 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] | 7432 | uint16_t arg_count = 0, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7433 | struct lyxp_set **args = NULL, **args_aux; |
| 7434 | |
| 7435 | if (set) { |
| 7436 | /* FunctionName */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7437 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7438 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7439 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7440 | xpath_func = &xpath_not; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7441 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7442 | xpath_func = &xpath_sum; |
| 7443 | } |
| 7444 | break; |
| 7445 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7446 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7447 | xpath_func = &xpath_lang; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7448 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7449 | xpath_func = &xpath_last; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7450 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7451 | xpath_func = &xpath_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7452 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7453 | xpath_func = &xpath_true; |
| 7454 | } |
| 7455 | break; |
| 7456 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7457 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7458 | xpath_func = &xpath_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7459 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7460 | xpath_func = &xpath_false; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7461 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7462 | xpath_func = &xpath_floor; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7463 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7464 | xpath_func = &xpath_round; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7465 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7466 | xpath_func = &xpath_deref; |
| 7467 | } |
| 7468 | break; |
| 7469 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7470 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7471 | xpath_func = &xpath_concat; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7472 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7473 | xpath_func = &xpath_number; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7474 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7475 | xpath_func = &xpath_string; |
| 7476 | } |
| 7477 | break; |
| 7478 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7479 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7480 | xpath_func = &xpath_boolean; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7481 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7482 | xpath_func = &xpath_ceiling; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7483 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7484 | xpath_func = &xpath_current; |
| 7485 | } |
| 7486 | break; |
| 7487 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7488 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7489 | xpath_func = &xpath_contains; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7490 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7491 | xpath_func = &xpath_position; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7492 | } 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] | 7493 | xpath_func = &xpath_re_match; |
| 7494 | } |
| 7495 | break; |
| 7496 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7497 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7498 | xpath_func = &xpath_substring; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7499 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7500 | xpath_func = &xpath_translate; |
| 7501 | } |
| 7502 | break; |
| 7503 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7504 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7505 | xpath_func = &xpath_local_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7506 | } 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] | 7507 | xpath_func = &xpath_enum_value; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7508 | } 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] | 7509 | xpath_func = &xpath_bit_is_set; |
| 7510 | } |
| 7511 | break; |
| 7512 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7513 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7514 | xpath_func = &xpath_starts_with; |
| 7515 | } |
| 7516 | break; |
| 7517 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7518 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7519 | xpath_func = &xpath_derived_from; |
| 7520 | } |
| 7521 | break; |
| 7522 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7523 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7524 | xpath_func = &xpath_namespace_uri; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7525 | } 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] | 7526 | xpath_func = &xpath_string_length; |
| 7527 | } |
| 7528 | break; |
| 7529 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7530 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7531 | xpath_func = &xpath_normalize_space; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7532 | } 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] | 7533 | xpath_func = &xpath_substring_after; |
| 7534 | } |
| 7535 | break; |
| 7536 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7537 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7538 | xpath_func = &xpath_substring_before; |
| 7539 | } |
| 7540 | break; |
| 7541 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7542 | 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] | 7543 | xpath_func = &xpath_derived_from_or_self; |
| 7544 | } |
| 7545 | break; |
| 7546 | } |
| 7547 | |
| 7548 | if (!xpath_func) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7549 | 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] | 7550 | return LY_EVALID; |
| 7551 | } |
| 7552 | } |
| 7553 | |
| 7554 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7555 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7556 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7557 | |
| 7558 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7559 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7560 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7561 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7562 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7563 | |
| 7564 | /* ( Expr ( ',' Expr )* )? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7565 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7566 | if (set) { |
| 7567 | args = malloc(sizeof *args); |
| 7568 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7569 | arg_count = 1; |
| 7570 | args[0] = set_copy(set); |
| 7571 | if (!args[0]) { |
| 7572 | rc = LY_EMEM; |
| 7573 | goto cleanup; |
| 7574 | } |
| 7575 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7576 | rc = eval_expr_select(exp, tok_idx, 0, args[0], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7577 | LY_CHECK_GOTO(rc, cleanup); |
| 7578 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7579 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7580 | LY_CHECK_GOTO(rc, cleanup); |
| 7581 | } |
| 7582 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7583 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7584 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7585 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7586 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7587 | |
| 7588 | if (set) { |
| 7589 | ++arg_count; |
| 7590 | args_aux = realloc(args, arg_count * sizeof *args); |
| 7591 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7592 | args = args_aux; |
| 7593 | args[arg_count - 1] = set_copy(set); |
| 7594 | if (!args[arg_count - 1]) { |
| 7595 | rc = LY_EMEM; |
| 7596 | goto cleanup; |
| 7597 | } |
| 7598 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7599 | rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7600 | LY_CHECK_GOTO(rc, cleanup); |
| 7601 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7602 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7603 | LY_CHECK_GOTO(rc, cleanup); |
| 7604 | } |
| 7605 | } |
| 7606 | |
| 7607 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7608 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7609 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7610 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7611 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7612 | |
| 7613 | if (set) { |
| 7614 | /* evaluate function */ |
| 7615 | rc = xpath_func(args, arg_count, set, options); |
| 7616 | |
| 7617 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7618 | /* merge all nodes from arg evaluations */ |
| 7619 | for (i = 0; i < arg_count; ++i) { |
| 7620 | set_scnode_clear_ctx(args[i]); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7621 | lyxp_set_scnode_merge(set, args[i]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7622 | } |
| 7623 | } |
| 7624 | } else { |
| 7625 | rc = LY_SUCCESS; |
| 7626 | } |
| 7627 | |
| 7628 | cleanup: |
| 7629 | for (i = 0; i < arg_count; ++i) { |
| 7630 | lyxp_set_free(args[i]); |
| 7631 | } |
| 7632 | free(args); |
| 7633 | |
| 7634 | return rc; |
| 7635 | } |
| 7636 | |
| 7637 | /** |
| 7638 | * @brief Evaluate Number. Logs directly on error. |
| 7639 | * |
| 7640 | * @param[in] ctx Context for errors. |
| 7641 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7642 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7643 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7644 | * @return LY_ERR |
| 7645 | */ |
| 7646 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7647 | 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] | 7648 | { |
| 7649 | long double num; |
| 7650 | char *endptr; |
| 7651 | |
| 7652 | if (set) { |
| 7653 | errno = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7654 | num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7655 | if (errno) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7656 | LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 7657 | 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] | 7658 | 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] | 7659 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7660 | } 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] | 7661 | LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 7662 | 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] | 7663 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7664 | return LY_EVALID; |
| 7665 | } |
| 7666 | |
| 7667 | set_fill_number(set, num); |
| 7668 | } |
| 7669 | |
| 7670 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7671 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7672 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7673 | return LY_SUCCESS; |
| 7674 | } |
| 7675 | |
| 7676 | /** |
| 7677 | * @brief Evaluate PathExpr. Logs directly on error. |
| 7678 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7679 | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7680 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 7681 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 7682 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7683 | * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7684 | * |
| 7685 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7686 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7687 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7688 | * @param[in] options XPath options. |
| 7689 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7690 | */ |
| 7691 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7692 | 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] | 7693 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7694 | ly_bool all_desc, parent_pos_pred; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7695 | LY_ERR rc; |
| 7696 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7697 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7698 | case LYXP_TOKEN_PAR1: |
| 7699 | /* '(' Expr ')' */ |
| 7700 | |
| 7701 | /* '(' */ |
| 7702 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7703 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7704 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7705 | |
| 7706 | /* Expr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7707 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7708 | LY_CHECK_RET(rc); |
| 7709 | |
| 7710 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7711 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7712 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7713 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7714 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7715 | |
| 7716 | parent_pos_pred = 0; |
| 7717 | goto predicate; |
| 7718 | |
| 7719 | case LYXP_TOKEN_DOT: |
| 7720 | case LYXP_TOKEN_DDOT: |
| 7721 | case LYXP_TOKEN_AT: |
| 7722 | case LYXP_TOKEN_NAMETEST: |
| 7723 | case LYXP_TOKEN_NODETYPE: |
| 7724 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7725 | rc = eval_relative_location_path(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7726 | LY_CHECK_RET(rc); |
| 7727 | break; |
| 7728 | |
| 7729 | case LYXP_TOKEN_FUNCNAME: |
| 7730 | /* FunctionCall */ |
| 7731 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7732 | rc = eval_function_call(exp, tok_idx, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7733 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7734 | rc = eval_function_call(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7735 | } |
| 7736 | LY_CHECK_RET(rc); |
| 7737 | |
| 7738 | parent_pos_pred = 1; |
| 7739 | goto predicate; |
| 7740 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 7741 | case LYXP_TOKEN_OPER_PATH: |
| 7742 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7743 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7744 | rc = eval_absolute_location_path(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7745 | LY_CHECK_RET(rc); |
| 7746 | break; |
| 7747 | |
| 7748 | case LYXP_TOKEN_LITERAL: |
| 7749 | /* Literal */ |
| 7750 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7751 | if (set) { |
| 7752 | set_scnode_clear_ctx(set); |
| 7753 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7754 | eval_literal(exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7755 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7756 | eval_literal(exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7757 | } |
| 7758 | |
| 7759 | parent_pos_pred = 1; |
| 7760 | goto predicate; |
| 7761 | |
| 7762 | case LYXP_TOKEN_NUMBER: |
| 7763 | /* Number */ |
| 7764 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7765 | if (set) { |
| 7766 | set_scnode_clear_ctx(set); |
| 7767 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7768 | rc = eval_number(NULL, exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7769 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7770 | rc = eval_number(set->ctx, exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7771 | } |
| 7772 | LY_CHECK_RET(rc); |
| 7773 | |
| 7774 | parent_pos_pred = 1; |
| 7775 | goto predicate; |
| 7776 | |
| 7777 | default: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7778 | 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] | 7779 | &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7780 | return LY_EVALID; |
| 7781 | } |
| 7782 | |
| 7783 | return LY_SUCCESS; |
| 7784 | |
| 7785 | predicate: |
| 7786 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7787 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7788 | rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7789 | LY_CHECK_RET(rc); |
| 7790 | } |
| 7791 | |
| 7792 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7793 | 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] | 7794 | |
| 7795 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7796 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7797 | all_desc = 0; |
| 7798 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7799 | all_desc = 1; |
| 7800 | } |
| 7801 | |
| 7802 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7803 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7804 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7805 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7806 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7807 | LY_CHECK_RET(rc); |
| 7808 | } |
| 7809 | |
| 7810 | return LY_SUCCESS; |
| 7811 | } |
| 7812 | |
| 7813 | /** |
| 7814 | * @brief Evaluate UnionExpr. Logs directly on error. |
| 7815 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7816 | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7817 | * |
| 7818 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7819 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7820 | * @param[in] repeat How many times this expression is repeated. |
| 7821 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7822 | * @param[in] options XPath options. |
| 7823 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7824 | */ |
| 7825 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7826 | 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] | 7827 | { |
| 7828 | LY_ERR rc = LY_SUCCESS; |
| 7829 | struct lyxp_set orig_set, set2; |
| 7830 | uint16_t i; |
| 7831 | |
| 7832 | assert(repeat); |
| 7833 | |
| 7834 | set_init(&orig_set, set); |
| 7835 | set_init(&set2, set); |
| 7836 | |
| 7837 | set_fill_set(&orig_set, set); |
| 7838 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7839 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7840 | LY_CHECK_GOTO(rc, cleanup); |
| 7841 | |
| 7842 | /* ('|' PathExpr)* */ |
| 7843 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7844 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7845 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7846 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7847 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7848 | |
| 7849 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7850 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7851 | LY_CHECK_GOTO(rc, cleanup); |
| 7852 | continue; |
| 7853 | } |
| 7854 | |
| 7855 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7856 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7857 | LY_CHECK_GOTO(rc, cleanup); |
| 7858 | |
| 7859 | /* eval */ |
| 7860 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7861 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7862 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7863 | rc = moveto_union(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7864 | LY_CHECK_GOTO(rc, cleanup); |
| 7865 | } |
| 7866 | } |
| 7867 | |
| 7868 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7869 | lyxp_set_free_content(&orig_set); |
| 7870 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7871 | return rc; |
| 7872 | } |
| 7873 | |
| 7874 | /** |
| 7875 | * @brief Evaluate UnaryExpr. Logs directly on error. |
| 7876 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7877 | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7878 | * |
| 7879 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7880 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7881 | * @param[in] repeat How many times this expression is repeated. |
| 7882 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7883 | * @param[in] options XPath options. |
| 7884 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7885 | */ |
| 7886 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7887 | 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] | 7888 | { |
| 7889 | LY_ERR rc; |
| 7890 | uint16_t this_op, i; |
| 7891 | |
| 7892 | assert(repeat); |
| 7893 | |
| 7894 | /* ('-')+ */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7895 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7896 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7897 | 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] | 7898 | |
| 7899 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7900 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7901 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7902 | } |
| 7903 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7904 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7905 | LY_CHECK_RET(rc); |
| 7906 | |
| 7907 | if (set && (repeat % 2)) { |
| 7908 | if (options & LYXP_SCNODE_ALL) { |
| 7909 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
| 7910 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7911 | 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] | 7912 | LY_CHECK_RET(rc); |
| 7913 | } |
| 7914 | } |
| 7915 | |
| 7916 | return LY_SUCCESS; |
| 7917 | } |
| 7918 | |
| 7919 | /** |
| 7920 | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
| 7921 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7922 | * [18] MultiplicativeExpr ::= UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7923 | * | MultiplicativeExpr '*' UnaryExpr |
| 7924 | * | MultiplicativeExpr 'div' UnaryExpr |
| 7925 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 7926 | * |
| 7927 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7928 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7929 | * @param[in] repeat How many times this expression is repeated. |
| 7930 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7931 | * @param[in] options XPath options. |
| 7932 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7933 | */ |
| 7934 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7935 | eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, |
| 7936 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7937 | { |
| 7938 | LY_ERR rc; |
| 7939 | uint16_t this_op; |
| 7940 | struct lyxp_set orig_set, set2; |
| 7941 | uint16_t i; |
| 7942 | |
| 7943 | assert(repeat); |
| 7944 | |
| 7945 | set_init(&orig_set, set); |
| 7946 | set_init(&set2, set); |
| 7947 | |
| 7948 | set_fill_set(&orig_set, set); |
| 7949 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7950 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7951 | LY_CHECK_GOTO(rc, cleanup); |
| 7952 | |
| 7953 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 7954 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7955 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7956 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7957 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7958 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7959 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7960 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7961 | |
| 7962 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7963 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7964 | LY_CHECK_GOTO(rc, cleanup); |
| 7965 | continue; |
| 7966 | } |
| 7967 | |
| 7968 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7969 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7970 | LY_CHECK_GOTO(rc, cleanup); |
| 7971 | |
| 7972 | /* eval */ |
| 7973 | if (options & LYXP_SCNODE_ALL) { |
| 7974 | 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] | 7975 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7976 | set_scnode_clear_ctx(set); |
| 7977 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7978 | 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] | 7979 | LY_CHECK_GOTO(rc, cleanup); |
| 7980 | } |
| 7981 | } |
| 7982 | |
| 7983 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7984 | lyxp_set_free_content(&orig_set); |
| 7985 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7986 | return rc; |
| 7987 | } |
| 7988 | |
| 7989 | /** |
| 7990 | * @brief Evaluate AdditiveExpr. Logs directly on error. |
| 7991 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7992 | * [17] AdditiveExpr ::= MultiplicativeExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7993 | * | AdditiveExpr '+' MultiplicativeExpr |
| 7994 | * | AdditiveExpr '-' MultiplicativeExpr |
| 7995 | * |
| 7996 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7997 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7998 | * @param[in] repeat How many times this expression is repeated. |
| 7999 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8000 | * @param[in] options XPath options. |
| 8001 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8002 | */ |
| 8003 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8004 | 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] | 8005 | { |
| 8006 | LY_ERR rc; |
| 8007 | uint16_t this_op; |
| 8008 | struct lyxp_set orig_set, set2; |
| 8009 | uint16_t i; |
| 8010 | |
| 8011 | assert(repeat); |
| 8012 | |
| 8013 | set_init(&orig_set, set); |
| 8014 | set_init(&set2, set); |
| 8015 | |
| 8016 | set_fill_set(&orig_set, set); |
| 8017 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8018 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8019 | LY_CHECK_GOTO(rc, cleanup); |
| 8020 | |
| 8021 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 8022 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8023 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8024 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8025 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8026 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8027 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8028 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8029 | |
| 8030 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8031 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8032 | LY_CHECK_GOTO(rc, cleanup); |
| 8033 | continue; |
| 8034 | } |
| 8035 | |
| 8036 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8037 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8038 | LY_CHECK_GOTO(rc, cleanup); |
| 8039 | |
| 8040 | /* eval */ |
| 8041 | if (options & LYXP_SCNODE_ALL) { |
| 8042 | 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] | 8043 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8044 | set_scnode_clear_ctx(set); |
| 8045 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8046 | 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] | 8047 | LY_CHECK_GOTO(rc, cleanup); |
| 8048 | } |
| 8049 | } |
| 8050 | |
| 8051 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8052 | lyxp_set_free_content(&orig_set); |
| 8053 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8054 | return rc; |
| 8055 | } |
| 8056 | |
| 8057 | /** |
| 8058 | * @brief Evaluate RelationalExpr. Logs directly on error. |
| 8059 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8060 | * [16] RelationalExpr ::= AdditiveExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8061 | * | RelationalExpr '<' AdditiveExpr |
| 8062 | * | RelationalExpr '>' AdditiveExpr |
| 8063 | * | RelationalExpr '<=' AdditiveExpr |
| 8064 | * | RelationalExpr '>=' AdditiveExpr |
| 8065 | * |
| 8066 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8067 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8068 | * @param[in] repeat How many times this expression is repeated. |
| 8069 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8070 | * @param[in] options XPath options. |
| 8071 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8072 | */ |
| 8073 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8074 | 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] | 8075 | { |
| 8076 | LY_ERR rc; |
| 8077 | uint16_t this_op; |
| 8078 | struct lyxp_set orig_set, set2; |
| 8079 | uint16_t i; |
| 8080 | |
| 8081 | assert(repeat); |
| 8082 | |
| 8083 | set_init(&orig_set, set); |
| 8084 | set_init(&set2, set); |
| 8085 | |
| 8086 | set_fill_set(&orig_set, set); |
| 8087 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8088 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8089 | LY_CHECK_GOTO(rc, cleanup); |
| 8090 | |
| 8091 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 8092 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8093 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8094 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8095 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8096 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8097 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8098 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8099 | |
| 8100 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8101 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8102 | LY_CHECK_GOTO(rc, cleanup); |
| 8103 | continue; |
| 8104 | } |
| 8105 | |
| 8106 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8107 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8108 | LY_CHECK_GOTO(rc, cleanup); |
| 8109 | |
| 8110 | /* eval */ |
| 8111 | if (options & LYXP_SCNODE_ALL) { |
| 8112 | 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] | 8113 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8114 | set_scnode_clear_ctx(set); |
| 8115 | } else { |
| 8116 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8117 | LY_CHECK_GOTO(rc, cleanup); |
| 8118 | } |
| 8119 | } |
| 8120 | |
| 8121 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8122 | lyxp_set_free_content(&orig_set); |
| 8123 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8124 | return rc; |
| 8125 | } |
| 8126 | |
| 8127 | /** |
| 8128 | * @brief Evaluate EqualityExpr. Logs directly on error. |
| 8129 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8130 | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8131 | * | EqualityExpr '!=' RelationalExpr |
| 8132 | * |
| 8133 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8134 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8135 | * @param[in] repeat How many times this expression is repeated. |
| 8136 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8137 | * @param[in] options XPath options. |
| 8138 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8139 | */ |
| 8140 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8141 | 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] | 8142 | { |
| 8143 | LY_ERR rc; |
| 8144 | uint16_t this_op; |
| 8145 | struct lyxp_set orig_set, set2; |
| 8146 | uint16_t i; |
| 8147 | |
| 8148 | assert(repeat); |
| 8149 | |
| 8150 | set_init(&orig_set, set); |
| 8151 | set_init(&set2, set); |
| 8152 | |
| 8153 | set_fill_set(&orig_set, set); |
| 8154 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8155 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8156 | LY_CHECK_GOTO(rc, cleanup); |
| 8157 | |
| 8158 | /* ('=' / '!=' RelationalExpr)* */ |
| 8159 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8160 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8161 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8162 | 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] | 8163 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8164 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8165 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8166 | |
| 8167 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8168 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8169 | LY_CHECK_GOTO(rc, cleanup); |
| 8170 | continue; |
| 8171 | } |
| 8172 | |
| 8173 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8174 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8175 | LY_CHECK_GOTO(rc, cleanup); |
| 8176 | |
| 8177 | /* eval */ |
| 8178 | if (options & LYXP_SCNODE_ALL) { |
| 8179 | 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] | 8180 | warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1); |
| 8181 | 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] | 8182 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8183 | set_scnode_clear_ctx(set); |
| 8184 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8185 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8186 | LY_CHECK_GOTO(rc, cleanup); |
| 8187 | } |
| 8188 | } |
| 8189 | |
| 8190 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8191 | lyxp_set_free_content(&orig_set); |
| 8192 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8193 | return rc; |
| 8194 | } |
| 8195 | |
| 8196 | /** |
| 8197 | * @brief Evaluate AndExpr. Logs directly on error. |
| 8198 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8199 | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8200 | * |
| 8201 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8202 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8203 | * @param[in] repeat How many times this expression is repeated. |
| 8204 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8205 | * @param[in] options XPath options. |
| 8206 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8207 | */ |
| 8208 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8209 | 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] | 8210 | { |
| 8211 | LY_ERR rc; |
| 8212 | struct lyxp_set orig_set, set2; |
| 8213 | uint16_t i; |
| 8214 | |
| 8215 | assert(repeat); |
| 8216 | |
| 8217 | set_init(&orig_set, set); |
| 8218 | set_init(&set2, set); |
| 8219 | |
| 8220 | set_fill_set(&orig_set, set); |
| 8221 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8222 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8223 | LY_CHECK_GOTO(rc, cleanup); |
| 8224 | |
| 8225 | /* cast to boolean, we know that will be the final result */ |
| 8226 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8227 | set_scnode_clear_ctx(set); |
| 8228 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8229 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8230 | } |
| 8231 | |
| 8232 | /* ('and' EqualityExpr)* */ |
| 8233 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8234 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8235 | 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] | 8236 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8237 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8238 | |
| 8239 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8240 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) { |
| 8241 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8242 | LY_CHECK_GOTO(rc, cleanup); |
| 8243 | continue; |
| 8244 | } |
| 8245 | |
| 8246 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8247 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8248 | LY_CHECK_GOTO(rc, cleanup); |
| 8249 | |
| 8250 | /* eval - just get boolean value actually */ |
| 8251 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8252 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8253 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8254 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8255 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8256 | set_fill_set(set, &set2); |
| 8257 | } |
| 8258 | } |
| 8259 | |
| 8260 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8261 | lyxp_set_free_content(&orig_set); |
| 8262 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8263 | return rc; |
| 8264 | } |
| 8265 | |
| 8266 | /** |
| 8267 | * @brief Evaluate OrExpr. Logs directly on error. |
| 8268 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8269 | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8270 | * |
| 8271 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8272 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8273 | * @param[in] repeat How many times this expression is repeated. |
| 8274 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8275 | * @param[in] options XPath options. |
| 8276 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8277 | */ |
| 8278 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8279 | 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] | 8280 | { |
| 8281 | LY_ERR rc; |
| 8282 | struct lyxp_set orig_set, set2; |
| 8283 | uint16_t i; |
| 8284 | |
| 8285 | assert(repeat); |
| 8286 | |
| 8287 | set_init(&orig_set, set); |
| 8288 | set_init(&set2, set); |
| 8289 | |
| 8290 | set_fill_set(&orig_set, set); |
| 8291 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8292 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8293 | LY_CHECK_GOTO(rc, cleanup); |
| 8294 | |
| 8295 | /* cast to boolean, we know that will be the final result */ |
| 8296 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8297 | set_scnode_clear_ctx(set); |
| 8298 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8299 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8300 | } |
| 8301 | |
| 8302 | /* ('or' AndExpr)* */ |
| 8303 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8304 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8305 | 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] | 8306 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8307 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8308 | |
| 8309 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8310 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) { |
| 8311 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8312 | LY_CHECK_GOTO(rc, cleanup); |
| 8313 | continue; |
| 8314 | } |
| 8315 | |
| 8316 | set_fill_set(&set2, &orig_set); |
| 8317 | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
| 8318 | * but it does not matter */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8319 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8320 | LY_CHECK_GOTO(rc, cleanup); |
| 8321 | |
| 8322 | /* eval - just get boolean value actually */ |
| 8323 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8324 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8325 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8326 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8327 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8328 | set_fill_set(set, &set2); |
| 8329 | } |
| 8330 | } |
| 8331 | |
| 8332 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8333 | lyxp_set_free_content(&orig_set); |
| 8334 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8335 | return rc; |
| 8336 | } |
| 8337 | |
| 8338 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8339 | * @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] | 8340 | * |
| 8341 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8342 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8343 | * @param[in] etype Expression type to evaluate. |
| 8344 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8345 | * @param[in] options XPath options. |
| 8346 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8347 | */ |
| 8348 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8349 | eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, |
| 8350 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8351 | { |
| 8352 | uint16_t i, count; |
| 8353 | enum lyxp_expr_type next_etype; |
| 8354 | LY_ERR rc; |
| 8355 | |
| 8356 | /* process operator repeats */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8357 | if (!exp->repeat[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8358 | next_etype = LYXP_EXPR_NONE; |
| 8359 | } else { |
| 8360 | /* find etype repeat */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8361 | for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8362 | |
| 8363 | /* select one-priority lower because etype expression called us */ |
| 8364 | if (i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8365 | next_etype = exp->repeat[*tok_idx][i - 1]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8366 | /* count repeats for that expression */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8367 | 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] | 8368 | } else { |
| 8369 | next_etype = LYXP_EXPR_NONE; |
| 8370 | } |
| 8371 | } |
| 8372 | |
| 8373 | /* decide what expression are we parsing based on the repeat */ |
| 8374 | switch (next_etype) { |
| 8375 | case LYXP_EXPR_OR: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8376 | rc = eval_or_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8377 | break; |
| 8378 | case LYXP_EXPR_AND: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8379 | rc = eval_and_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8380 | break; |
| 8381 | case LYXP_EXPR_EQUALITY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8382 | rc = eval_equality_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8383 | break; |
| 8384 | case LYXP_EXPR_RELATIONAL: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8385 | rc = eval_relational_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8386 | break; |
| 8387 | case LYXP_EXPR_ADDITIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8388 | rc = eval_additive_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8389 | break; |
| 8390 | case LYXP_EXPR_MULTIPLICATIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8391 | rc = eval_multiplicative_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_UNARY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8394 | rc = eval_unary_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_UNION: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8397 | rc = eval_union_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_NONE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8400 | rc = eval_path_expr(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8401 | break; |
| 8402 | default: |
| 8403 | LOGINT_RET(set->ctx); |
| 8404 | } |
| 8405 | |
| 8406 | return rc; |
| 8407 | } |
| 8408 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8409 | /** |
| 8410 | * @brief Get root type. |
| 8411 | * |
| 8412 | * @param[in] ctx_node Context node. |
| 8413 | * @param[in] ctx_scnode Schema context node. |
| 8414 | * @param[in] options XPath options. |
| 8415 | * @return Root type. |
| 8416 | */ |
| 8417 | static enum lyxp_node_type |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8418 | 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] | 8419 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8420 | const struct lysc_node *op; |
| 8421 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8422 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8423 | /* schema */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8424 | 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] | 8425 | |
| 8426 | if (op || (options & LYXP_SCNODE)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8427 | /* general root that can access everything */ |
| 8428 | return LYXP_NODE_ROOT; |
| 8429 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
| 8430 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8431 | return LYXP_NODE_ROOT_CONFIG; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8432 | } |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8433 | return LYXP_NODE_ROOT; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8434 | } |
| 8435 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8436 | /* data */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8437 | op = ctx_node ? ctx_node->schema : NULL; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 8438 | 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] | 8439 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8440 | if (op || !(options & LYXP_SCHEMA)) { |
| 8441 | /* general root that can access everything */ |
| 8442 | return LYXP_NODE_ROOT; |
| 8443 | } else if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8444 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8445 | return LYXP_NODE_ROOT_CONFIG; |
| 8446 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8447 | return LYXP_NODE_ROOT; |
| 8448 | } |
| 8449 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8450 | LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8451 | 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] | 8452 | 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] | 8453 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8454 | uint16_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8455 | LY_ERR rc; |
| 8456 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8457 | LY_CHECK_ARG_RET(NULL, exp, set, LY_EINVAL); |
| 8458 | if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) { |
| 8459 | LOGARG(NULL, "Current module must be set if schema format is used."); |
| 8460 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8461 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8462 | |
| 8463 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8464 | memset(set, 0, sizeof *set); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8465 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8466 | set->root_type = lyxp_get_root_type(ctx_node, NULL, options); |
| 8467 | set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0); |
| 8468 | |
| 8469 | set->ctx = (struct ly_ctx *)(cur_mod ? cur_mod->ctx : (ctx_node ? LYD_CTX(ctx_node) : (tree ? LYD_CTX(tree) : NULL))); |
| 8470 | set->cur_node = ctx_node; |
| 8471 | for (set->context_op = ctx_node ? ctx_node->schema : NULL; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 8472 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8473 | set->context_op = set->context_op->parent) {} |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 8474 | set->tree = tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8475 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8476 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8477 | set->prefix_data = prefix_data; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8478 | |
| 8479 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8480 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8481 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8482 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8483 | } |
| 8484 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8485 | return rc; |
| 8486 | } |
| 8487 | |
| 8488 | #if 0 |
| 8489 | |
| 8490 | /* full xml printing of set elements, not used currently */ |
| 8491 | |
| 8492 | void |
| 8493 | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
| 8494 | { |
| 8495 | uint32_t i; |
| 8496 | char *str_num; |
| 8497 | struct lyout out; |
| 8498 | |
| 8499 | memset(&out, 0, sizeof out); |
| 8500 | |
| 8501 | out.type = LYOUT_STREAM; |
| 8502 | out.method.f = f; |
| 8503 | |
| 8504 | switch (set->type) { |
| 8505 | case LYXP_SET_EMPTY: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8506 | ly_print_(&out, "Empty XPath set\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8507 | break; |
| 8508 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8509 | ly_print_(&out, "Boolean XPath set:\n"); |
| 8510 | ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8511 | break; |
| 8512 | case LYXP_SET_STRING: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8513 | ly_print_(&out, "String XPath set:\n"); |
| 8514 | ly_print_(&out, "\"%s\"\n\n", set->value.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8515 | break; |
| 8516 | case LYXP_SET_NUMBER: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8517 | ly_print_(&out, "Number XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8518 | |
| 8519 | if (isnan(set->value.num)) { |
| 8520 | str_num = strdup("NaN"); |
| 8521 | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
| 8522 | str_num = strdup("0"); |
| 8523 | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
| 8524 | str_num = strdup("Infinity"); |
| 8525 | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
| 8526 | str_num = strdup("-Infinity"); |
| 8527 | } else if ((long long)set->value.num == set->value.num) { |
| 8528 | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
| 8529 | str_num = NULL; |
| 8530 | } |
| 8531 | } else { |
| 8532 | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
| 8533 | str_num = NULL; |
| 8534 | } |
| 8535 | } |
| 8536 | if (!str_num) { |
| 8537 | LOGMEM; |
| 8538 | return; |
| 8539 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8540 | ly_print_(&out, "%s\n\n", str_num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8541 | free(str_num); |
| 8542 | break; |
| 8543 | case LYXP_SET_NODE_SET: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8544 | ly_print_(&out, "Node XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8545 | |
| 8546 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8547 | ly_print_(&out, "%d. ", i + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8548 | switch (set->node_type[i]) { |
| 8549 | case LYXP_NODE_ROOT_ALL: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8550 | ly_print_(&out, "ROOT all\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8551 | break; |
| 8552 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8553 | ly_print_(&out, "ROOT config\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8554 | break; |
| 8555 | case LYXP_NODE_ROOT_STATE: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8556 | ly_print_(&out, "ROOT state\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8557 | break; |
| 8558 | case LYXP_NODE_ROOT_NOTIF: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8559 | 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] | 8560 | break; |
| 8561 | case LYXP_NODE_ROOT_RPC: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8562 | 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] | 8563 | break; |
| 8564 | case LYXP_NODE_ROOT_OUTPUT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8565 | 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] | 8566 | break; |
| 8567 | case LYXP_NODE_ELEM: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8568 | ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8569 | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8570 | ly_print_(&out, "\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8571 | break; |
| 8572 | case LYXP_NODE_TEXT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8573 | 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] | 8574 | break; |
| 8575 | case LYXP_NODE_ATTR: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8576 | 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] | 8577 | break; |
| 8578 | } |
| 8579 | } |
| 8580 | break; |
| 8581 | } |
| 8582 | } |
| 8583 | |
| 8584 | #endif |
| 8585 | |
| 8586 | LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8587 | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8588 | { |
| 8589 | long double num; |
| 8590 | char *str; |
| 8591 | LY_ERR rc; |
| 8592 | |
| 8593 | if (!set || (set->type == target)) { |
| 8594 | return LY_SUCCESS; |
| 8595 | } |
| 8596 | |
| 8597 | /* it's not possible to convert anything into a node set */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8598 | assert(target != LYXP_SET_NODE_SET); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8599 | |
| 8600 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8601 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8602 | return LY_EINVAL; |
| 8603 | } |
| 8604 | |
| 8605 | /* to STRING */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8606 | 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] | 8607 | switch (set->type) { |
| 8608 | case LYXP_SET_NUMBER: |
| 8609 | if (isnan(set->val.num)) { |
| 8610 | set->val.str = strdup("NaN"); |
| 8611 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8612 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 8613 | set->val.str = strdup("0"); |
| 8614 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8615 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 8616 | set->val.str = strdup("Infinity"); |
| 8617 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8618 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 8619 | set->val.str = strdup("-Infinity"); |
| 8620 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8621 | } else if ((long long)set->val.num == set->val.num) { |
| 8622 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 8623 | LOGMEM_RET(set->ctx); |
| 8624 | } |
| 8625 | set->val.str = str; |
| 8626 | } else { |
| 8627 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 8628 | LOGMEM_RET(set->ctx); |
| 8629 | } |
| 8630 | set->val.str = str; |
| 8631 | } |
| 8632 | break; |
| 8633 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8634 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8635 | set->val.str = strdup("true"); |
| 8636 | } else { |
| 8637 | set->val.str = strdup("false"); |
| 8638 | } |
| 8639 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 8640 | break; |
| 8641 | case LYXP_SET_NODE_SET: |
| 8642 | assert(set->used); |
| 8643 | |
| 8644 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8645 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8646 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8647 | rc = cast_node_set_to_string(set, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8648 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8649 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8650 | set->val.str = str; |
| 8651 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8652 | default: |
| 8653 | LOGINT_RET(set->ctx); |
| 8654 | } |
| 8655 | set->type = LYXP_SET_STRING; |
| 8656 | } |
| 8657 | |
| 8658 | /* to NUMBER */ |
| 8659 | if (target == LYXP_SET_NUMBER) { |
| 8660 | switch (set->type) { |
| 8661 | case LYXP_SET_STRING: |
| 8662 | num = cast_string_to_number(set->val.str); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8663 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8664 | set->val.num = num; |
| 8665 | break; |
| 8666 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8667 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8668 | set->val.num = 1; |
| 8669 | } else { |
| 8670 | set->val.num = 0; |
| 8671 | } |
| 8672 | break; |
| 8673 | default: |
| 8674 | LOGINT_RET(set->ctx); |
| 8675 | } |
| 8676 | set->type = LYXP_SET_NUMBER; |
| 8677 | } |
| 8678 | |
| 8679 | /* to BOOLEAN */ |
| 8680 | if (target == LYXP_SET_BOOLEAN) { |
| 8681 | switch (set->type) { |
| 8682 | case LYXP_SET_NUMBER: |
| 8683 | 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] | 8684 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8685 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8686 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8687 | } |
| 8688 | break; |
| 8689 | case LYXP_SET_STRING: |
| 8690 | if (set->val.str[0]) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8691 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8692 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8693 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8694 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8695 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8696 | } |
| 8697 | break; |
| 8698 | case LYXP_SET_NODE_SET: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8699 | if (set->used) { |
| 8700 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8701 | set->val.bln = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8702 | } else { |
| 8703 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8704 | set->val.bln = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8705 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8706 | break; |
| 8707 | default: |
| 8708 | LOGINT_RET(set->ctx); |
| 8709 | } |
| 8710 | set->type = LYXP_SET_BOOLEAN; |
| 8711 | } |
| 8712 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8713 | return LY_SUCCESS; |
| 8714 | } |
| 8715 | |
| 8716 | LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8717 | 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] | 8718 | 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] | 8719 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8720 | uint16_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8721 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8722 | LY_CHECK_ARG_RET(NULL, exp, set, LY_EINVAL); |
| 8723 | if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) { |
| 8724 | LOGARG(NULL, "Current module must be set if schema format is used."); |
| 8725 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8726 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8727 | |
| 8728 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8729 | memset(set, 0, sizeof *set); |
| 8730 | set->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8731 | set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options); |
| 8732 | 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] | 8733 | set->val.scnodes[0].in_ctx = -2; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8734 | |
| 8735 | set->ctx = cur_mod ? cur_mod->ctx : (ctx_scnode ? ctx_scnode->module->ctx : NULL); |
| 8736 | set->cur_scnode = ctx_scnode; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8737 | for (set->context_op = ctx_scnode; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 8738 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8739 | set->context_op = set->context_op->parent) {} |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8740 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8741 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8742 | set->prefix_data = prefix_data; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8743 | |
| 8744 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8745 | return eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8746 | } |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 8747 | |
| 8748 | API const char * |
| 8749 | lyxp_get_expr(const struct lyxp_expr *path) |
| 8750 | { |
| 8751 | if (!path) { |
| 8752 | return NULL; |
| 8753 | } |
| 8754 | |
| 8755 | return path->expr; |
| 8756 | } |