Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file xpath.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief YANG XPath evaluation functions |
| 5 | * |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2020 CESNET, z.s.p.o. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 14 | #define _GNU_SOURCE |
| 15 | |
| 16 | /* needed by libmath functions isfinite(), isinf(), isnan(), signbit(), ... */ |
| 17 | #define _ISOC99_SOURCE |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 18 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 19 | #include "xpath.h" |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 20 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 21 | #include <assert.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 22 | #include <ctype.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 23 | #include <errno.h> |
| 24 | #include <limits.h> |
| 25 | #include <math.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 26 | #include <stdint.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 29 | #include <string.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 30 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 31 | #include "common.h" |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 32 | #include "compat.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 33 | #include "context.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 34 | #include "dict.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 35 | #include "hash_table.h" |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 36 | #include "parser_data.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 37 | #include "path.h" |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 38 | #include "plugins_types.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 39 | #include "printer.h" |
| 40 | #include "printer_data.h" |
| 41 | #include "tree.h" |
| 42 | #include "tree_data_internal.h" |
| 43 | #include "tree_schema_internal.h" |
| 44 | #include "xml.h" |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 45 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 46 | static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx); |
| 47 | static LY_ERR eval_expr_select(struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, int options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * @brief Print the type of an XPath \p set. |
| 51 | * |
| 52 | * @param[in] set Set to use. |
| 53 | * @return Set type string. |
| 54 | */ |
| 55 | static const char * |
| 56 | print_set_type(struct lyxp_set *set) |
| 57 | { |
| 58 | switch (set->type) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 59 | case LYXP_SET_NODE_SET: |
| 60 | return "node set"; |
| 61 | case LYXP_SET_SCNODE_SET: |
| 62 | return "schema node set"; |
| 63 | case LYXP_SET_BOOLEAN: |
| 64 | return "boolean"; |
| 65 | case LYXP_SET_NUMBER: |
| 66 | return "number"; |
| 67 | case LYXP_SET_STRING: |
| 68 | return "string"; |
| 69 | } |
| 70 | |
| 71 | return NULL; |
| 72 | } |
| 73 | |
Michal Vasko | 24cddf8 | 2020-06-01 08:17:01 +0200 | [diff] [blame] | 74 | const char * |
| 75 | lyxp_print_token(enum lyxp_token tok) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 76 | { |
| 77 | switch (tok) { |
| 78 | case LYXP_TOKEN_PAR1: |
| 79 | return "("; |
| 80 | case LYXP_TOKEN_PAR2: |
| 81 | return ")"; |
| 82 | case LYXP_TOKEN_BRACK1: |
| 83 | return "["; |
| 84 | case LYXP_TOKEN_BRACK2: |
| 85 | return "]"; |
| 86 | case LYXP_TOKEN_DOT: |
| 87 | return "."; |
| 88 | case LYXP_TOKEN_DDOT: |
| 89 | return ".."; |
| 90 | case LYXP_TOKEN_AT: |
| 91 | return "@"; |
| 92 | case LYXP_TOKEN_COMMA: |
| 93 | return ","; |
| 94 | case LYXP_TOKEN_NAMETEST: |
| 95 | return "NameTest"; |
| 96 | case LYXP_TOKEN_NODETYPE: |
| 97 | return "NodeType"; |
| 98 | case LYXP_TOKEN_FUNCNAME: |
| 99 | return "FunctionName"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 100 | case LYXP_TOKEN_OPER_LOG: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 101 | return "Operator(Logic)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 102 | case LYXP_TOKEN_OPER_EQUAL: |
| 103 | return "Operator(Equal)"; |
| 104 | case LYXP_TOKEN_OPER_NEQUAL: |
| 105 | return "Operator(Non-equal)"; |
| 106 | case LYXP_TOKEN_OPER_COMP: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 107 | return "Operator(Comparison)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 108 | case LYXP_TOKEN_OPER_MATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 109 | return "Operator(Math)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 110 | case LYXP_TOKEN_OPER_UNI: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 111 | return "Operator(Union)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 112 | case LYXP_TOKEN_OPER_PATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 113 | return "Operator(Path)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 114 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 115 | return "Operator(Recursive Path)"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 116 | case LYXP_TOKEN_LITERAL: |
| 117 | return "Literal"; |
| 118 | case LYXP_TOKEN_NUMBER: |
| 119 | return "Number"; |
| 120 | default: |
| 121 | LOGINT(NULL); |
| 122 | return ""; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * @brief Print the whole expression \p exp to debug output. |
| 128 | * |
| 129 | * @param[in] exp Expression to use. |
| 130 | */ |
| 131 | static void |
| 132 | print_expr_struct_debug(struct lyxp_expr *exp) |
| 133 | { |
| 134 | uint16_t i, j; |
| 135 | char tmp[128]; |
| 136 | |
| 137 | if (!exp || (ly_log_level < LY_LLDBG)) { |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr); |
| 142 | for (i = 0; i < exp->used; ++i) { |
Michal Vasko | 24cddf8 | 2020-06-01 08:17:01 +0200 | [diff] [blame] | 143 | sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i], |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 144 | &exp->expr[exp->tok_pos[i]]); |
| 145 | if (exp->repeat[i]) { |
| 146 | sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]); |
| 147 | for (j = 1; exp->repeat[i][j]; ++j) { |
| 148 | sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]); |
| 149 | } |
| 150 | strcat(tmp, ")"); |
| 151 | } |
| 152 | LOGDBG(LY_LDGXPATH, tmp); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | #ifndef NDEBUG |
| 157 | |
| 158 | /** |
| 159 | * @brief Print XPath set content to debug output. |
| 160 | * |
| 161 | * @param[in] set Set to print. |
| 162 | */ |
| 163 | static void |
| 164 | print_set_debug(struct lyxp_set *set) |
| 165 | { |
| 166 | uint32_t i; |
| 167 | char *str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 168 | struct lyxp_set_node *item; |
| 169 | struct lyxp_set_scnode *sitem; |
| 170 | |
| 171 | if (ly_log_level < LY_LLDBG) { |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | switch (set->type) { |
| 176 | case LYXP_SET_NODE_SET: |
| 177 | LOGDBG(LY_LDGXPATH, "set NODE SET:"); |
| 178 | for (i = 0; i < set->used; ++i) { |
| 179 | item = &set->val.nodes[i]; |
| 180 | |
| 181 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 182 | case LYXP_NODE_NONE: |
| 183 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos); |
| 184 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 185 | case LYXP_NODE_ROOT: |
| 186 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos); |
| 187 | break; |
| 188 | case LYXP_NODE_ROOT_CONFIG: |
| 189 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos); |
| 190 | break; |
| 191 | case LYXP_NODE_ELEM: |
| 192 | if ((item->node->schema->nodetype == LYS_LIST) |
| 193 | && (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) { |
| 194 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos, |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 195 | item->node->schema->name, LYD_CANON_VALUE(lyd_node_children(item->node, 0))); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 196 | } else if (((struct lyd_node_inner *)item->node)->schema->nodetype == LYS_LEAFLIST) { |
| 197 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos, |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 198 | item->node->schema->name, LYD_CANON_VALUE(item->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 199 | } else { |
| 200 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name); |
| 201 | } |
| 202 | break; |
| 203 | case LYXP_NODE_TEXT: |
| 204 | if (item->node->schema->nodetype & LYS_ANYDATA) { |
| 205 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos, |
| 206 | item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata"); |
| 207 | } else { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 208 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos, LYD_CANON_VALUE(item->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 209 | } |
| 210 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 211 | case LYXP_NODE_META: |
| 212 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name, |
| 213 | set->val.meta[i].meta->value); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 214 | break; |
| 215 | } |
| 216 | } |
| 217 | break; |
| 218 | |
| 219 | case LYXP_SET_SCNODE_SET: |
| 220 | LOGDBG(LY_LDGXPATH, "set SCNODE SET:"); |
| 221 | for (i = 0; i < set->used; ++i) { |
| 222 | sitem = &set->val.scnodes[i]; |
| 223 | |
| 224 | switch (sitem->type) { |
| 225 | case LYXP_NODE_ROOT: |
| 226 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx); |
| 227 | break; |
| 228 | case LYXP_NODE_ROOT_CONFIG: |
| 229 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx); |
| 230 | break; |
| 231 | case LYXP_NODE_ELEM: |
| 232 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name); |
| 233 | break; |
| 234 | default: |
| 235 | LOGINT(NULL); |
| 236 | break; |
| 237 | } |
| 238 | } |
| 239 | break; |
| 240 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 241 | case LYXP_SET_BOOLEAN: |
| 242 | LOGDBG(LY_LDGXPATH, "set BOOLEAN"); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 243 | LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false")); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 244 | break; |
| 245 | |
| 246 | case LYXP_SET_STRING: |
| 247 | LOGDBG(LY_LDGXPATH, "set STRING"); |
| 248 | LOGDBG(LY_LDGXPATH, "\t%s", set->val.str); |
| 249 | break; |
| 250 | |
| 251 | case LYXP_SET_NUMBER: |
| 252 | LOGDBG(LY_LDGXPATH, "set NUMBER"); |
| 253 | |
| 254 | if (isnan(set->val.num)) { |
| 255 | str = strdup("NaN"); |
| 256 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 257 | str = strdup("0"); |
| 258 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 259 | str = strdup("Infinity"); |
| 260 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 261 | str = strdup("-Infinity"); |
| 262 | } else if ((long long)set->val.num == set->val.num) { |
| 263 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 264 | str = NULL; |
| 265 | } |
| 266 | } else { |
| 267 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 268 | str = NULL; |
| 269 | } |
| 270 | } |
| 271 | LY_CHECK_ERR_RET(!str, LOGMEM(NULL), ); |
| 272 | |
| 273 | LOGDBG(LY_LDGXPATH, "\t%s", str); |
| 274 | free(str); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | #endif |
| 279 | |
| 280 | /** |
| 281 | * @brief Realloc the string \p str. |
| 282 | * |
| 283 | * @param[in] ctx libyang context for logging. |
| 284 | * @param[in] needed How much free space is required. |
| 285 | * @param[in,out] str Pointer to the string to use. |
| 286 | * @param[in,out] used Used bytes in \p str. |
| 287 | * @param[in,out] size Allocated bytes in \p str. |
| 288 | * @return LY_ERR |
| 289 | */ |
| 290 | static LY_ERR |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 291 | cast_string_realloc(const struct ly_ctx *ctx, uint16_t needed, char **str, uint16_t *used, uint16_t *size) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 292 | { |
| 293 | if (*size - *used < needed) { |
| 294 | do { |
| 295 | if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) { |
| 296 | LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX); |
| 297 | return LY_EINVAL; |
| 298 | } |
| 299 | *size += LYXP_STRING_CAST_SIZE_STEP; |
| 300 | } while (*size - *used < needed); |
| 301 | *str = ly_realloc(*str, *size * sizeof(char)); |
| 302 | LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM); |
| 303 | } |
| 304 | |
| 305 | return LY_SUCCESS; |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * @brief Cast nodes recursively to one string @p str. |
| 310 | * |
| 311 | * @param[in] node Node to cast. |
| 312 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 313 | * @param[in] root_type Type of the XPath root. |
| 314 | * @param[in] indent Current indent. |
| 315 | * @param[in,out] str Resulting string. |
| 316 | * @param[in,out] used Used bytes in @p str. |
| 317 | * @param[in,out] size Allocated bytes in @p str. |
| 318 | * @return LY_ERR |
| 319 | */ |
| 320 | static LY_ERR |
| 321 | cast_string_recursive(const struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame^] | 322 | uint16_t *used, uint16_t *size) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 323 | { |
Radek Krejci | 7f769d7 | 2020-07-11 23:13:56 +0200 | [diff] [blame] | 324 | char *buf, *line, *ptr = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 325 | const char *value_str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 326 | const struct lyd_node *child; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 327 | struct lyd_node *tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 328 | struct lyd_node_any *any; |
| 329 | LY_ERR rc; |
| 330 | |
| 331 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) { |
| 332 | return LY_SUCCESS; |
| 333 | } |
| 334 | |
| 335 | if (fake_cont) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 336 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 337 | LY_CHECK_RET(rc); |
| 338 | strcpy(*str + (*used - 1), "\n"); |
| 339 | ++(*used); |
| 340 | |
| 341 | ++indent; |
| 342 | } |
| 343 | |
| 344 | switch (node->schema->nodetype) { |
| 345 | case LYS_CONTAINER: |
| 346 | case LYS_LIST: |
| 347 | case LYS_RPC: |
| 348 | case LYS_NOTIF: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 349 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 350 | LY_CHECK_RET(rc); |
| 351 | strcpy(*str + (*used - 1), "\n"); |
| 352 | ++(*used); |
| 353 | |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 354 | for (child = lyd_node_children(node, 0); child; child = child->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 355 | rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size); |
| 356 | LY_CHECK_RET(rc); |
| 357 | } |
| 358 | |
| 359 | break; |
| 360 | |
| 361 | case LYS_LEAF: |
| 362 | case LYS_LEAFLIST: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 363 | value_str = LYD_CANON_VALUE(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 364 | |
| 365 | /* print indent */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 366 | LY_CHECK_RET(cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(value_str) + 1, str, used, size)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 367 | memset(*str + (*used - 1), ' ', indent * 2); |
| 368 | *used += indent * 2; |
| 369 | |
| 370 | /* print value */ |
| 371 | if (*used == 1) { |
| 372 | sprintf(*str + (*used - 1), "%s", value_str); |
| 373 | *used += strlen(value_str); |
| 374 | } else { |
| 375 | sprintf(*str + (*used - 1), "%s\n", value_str); |
| 376 | *used += strlen(value_str) + 1; |
| 377 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 378 | |
| 379 | break; |
| 380 | |
| 381 | case LYS_ANYXML: |
| 382 | case LYS_ANYDATA: |
| 383 | any = (struct lyd_node_any *)node; |
| 384 | if (!(void *)any->value.tree) { |
| 385 | /* no content */ |
| 386 | buf = strdup(""); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 387 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 388 | } else { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 389 | struct ly_out *out; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 390 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 391 | if (any->value_type == LYD_ANYDATA_LYB) { |
| 392 | /* try to parse it into a data tree */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 393 | if (lyd_parse_data_mem((struct ly_ctx *)LYD_CTX(node), any->value.mem, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 394 | /* successfully parsed */ |
| 395 | free(any->value.mem); |
| 396 | any->value.tree = tree; |
| 397 | any->value_type = LYD_ANYDATA_DATATREE; |
| 398 | } |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 399 | /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */ |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 400 | } |
| 401 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 402 | switch (any->value_type) { |
| 403 | case LYD_ANYDATA_STRING: |
| 404 | case LYD_ANYDATA_XML: |
| 405 | case LYD_ANYDATA_JSON: |
| 406 | buf = strdup(any->value.json); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 407 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 408 | break; |
| 409 | case LYD_ANYDATA_DATATREE: |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 410 | LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 411 | rc = lyd_print_all(out, any->value.tree, LYD_XML, 0); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 412 | ly_out_free(out, NULL, 0); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 413 | LY_CHECK_RET(rc < 0, -rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 414 | break; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 415 | case LYD_ANYDATA_LYB: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 416 | LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string."); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 417 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
| 421 | line = strtok_r(buf, "\n", &ptr); |
| 422 | do { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 423 | rc = cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(line) + 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 424 | if (rc != LY_SUCCESS) { |
| 425 | free(buf); |
| 426 | return rc; |
| 427 | } |
| 428 | memset(*str + (*used - 1), ' ', indent * 2); |
| 429 | *used += indent * 2; |
| 430 | |
| 431 | strcpy(*str + (*used - 1), line); |
| 432 | *used += strlen(line); |
| 433 | |
| 434 | strcpy(*str + (*used - 1), "\n"); |
| 435 | *used += 1; |
| 436 | } while ((line = strtok_r(NULL, "\n", &ptr))); |
| 437 | |
| 438 | free(buf); |
| 439 | break; |
| 440 | |
| 441 | default: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 442 | LOGINT_RET(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | if (fake_cont) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 446 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 447 | LY_CHECK_RET(rc); |
| 448 | strcpy(*str + (*used - 1), "\n"); |
| 449 | ++(*used); |
| 450 | |
| 451 | --indent; |
| 452 | } |
| 453 | |
| 454 | return LY_SUCCESS; |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * @brief Cast an element into a string. |
| 459 | * |
| 460 | * @param[in] node Node to cast. |
| 461 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 462 | * @param[in] root_type Type of the XPath root. |
| 463 | * @param[out] str Element cast to dynamically-allocated string. |
| 464 | * @return LY_ERR |
| 465 | */ |
| 466 | static LY_ERR |
| 467 | cast_string_elem(struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, char **str) |
| 468 | { |
| 469 | uint16_t used, size; |
| 470 | LY_ERR rc; |
| 471 | |
| 472 | *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char)); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 473 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 474 | (*str)[0] = '\0'; |
| 475 | used = 1; |
| 476 | size = LYXP_STRING_CAST_SIZE_START; |
| 477 | |
| 478 | rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size); |
| 479 | if (rc != LY_SUCCESS) { |
| 480 | free(*str); |
| 481 | return rc; |
| 482 | } |
| 483 | |
| 484 | if (size > used) { |
| 485 | *str = ly_realloc(*str, used * sizeof(char)); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 486 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 487 | } |
| 488 | return LY_SUCCESS; |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * @brief Cast a LYXP_SET_NODE_SET set into a string. |
| 493 | * Context position aware. |
| 494 | * |
| 495 | * @param[in] set Set to cast. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 496 | * @param[out] str Cast dynamically-allocated string. |
| 497 | * @return LY_ERR |
| 498 | */ |
| 499 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 500 | cast_node_set_to_string(struct lyxp_set *set, char **str) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 501 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 502 | switch (set->val.nodes[0].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 503 | case LYXP_NODE_NONE: |
| 504 | /* invalid */ |
| 505 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 506 | case LYXP_NODE_ROOT: |
| 507 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 508 | return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 509 | case LYXP_NODE_ELEM: |
| 510 | case LYXP_NODE_TEXT: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 511 | return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 512 | case LYXP_NODE_META: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 513 | *str = strdup(set->val.meta[0].meta->value.canonical); |
| 514 | if (!*str) { |
| 515 | LOGMEM_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 516 | } |
| 517 | return LY_SUCCESS; |
| 518 | } |
| 519 | |
| 520 | LOGINT_RET(set->ctx); |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * @brief Cast a string into an XPath number. |
| 525 | * |
| 526 | * @param[in] str String to use. |
| 527 | * @return Cast number. |
| 528 | */ |
| 529 | static long double |
| 530 | cast_string_to_number(const char *str) |
| 531 | { |
| 532 | long double num; |
| 533 | char *ptr; |
| 534 | |
| 535 | errno = 0; |
| 536 | num = strtold(str, &ptr); |
| 537 | if (errno || *ptr) { |
| 538 | num = NAN; |
| 539 | } |
| 540 | return num; |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * @brief Callback for checking value equality. |
| 545 | * |
| 546 | * @param[in] val1_p First value. |
| 547 | * @param[in] val2_p Second value. |
| 548 | * @param[in] mod Whether hash table is being modified. |
| 549 | * @param[in] cb_data Callback data. |
| 550 | * @return 0 if not equal, non-zero if equal. |
| 551 | */ |
| 552 | static int |
| 553 | set_values_equal_cb(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data)) |
| 554 | { |
| 555 | struct lyxp_set_hash_node *val1, *val2; |
| 556 | |
| 557 | val1 = (struct lyxp_set_hash_node *)val1_p; |
| 558 | val2 = (struct lyxp_set_hash_node *)val2_p; |
| 559 | |
| 560 | if ((val1->node == val2->node) && (val1->type == val2->type)) { |
| 561 | return 1; |
| 562 | } |
| 563 | |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * @brief Insert node and its hash into set. |
| 569 | * |
| 570 | * @param[in] set et to insert to. |
| 571 | * @param[in] node Node with hash. |
| 572 | * @param[in] type Node type. |
| 573 | */ |
| 574 | static void |
| 575 | set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 576 | { |
| 577 | int r; |
| 578 | uint32_t i, hash; |
| 579 | struct lyxp_set_hash_node hnode; |
| 580 | |
| 581 | if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) { |
| 582 | /* create hash table and add all the nodes */ |
| 583 | set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1); |
| 584 | for (i = 0; i < set->used; ++i) { |
| 585 | hnode.node = set->val.nodes[i].node; |
| 586 | hnode.type = set->val.nodes[i].type; |
| 587 | |
| 588 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 589 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 590 | hash = dict_hash_multi(hash, NULL, 0); |
| 591 | |
| 592 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 593 | assert(!r); |
| 594 | (void)r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 595 | |
Michal Vasko | 9d6befd | 2019-12-11 14:56:56 +0100 | [diff] [blame] | 596 | if (hnode.node == node) { |
| 597 | /* it was just added, do not add it twice */ |
| 598 | node = NULL; |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 604 | /* add the new node into hash table */ |
| 605 | hnode.node = node; |
| 606 | hnode.type = type; |
| 607 | |
| 608 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 609 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 610 | hash = dict_hash_multi(hash, NULL, 0); |
| 611 | |
| 612 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 613 | assert(!r); |
| 614 | (void)r; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | /** |
| 619 | * @brief Remove node and its hash from set. |
| 620 | * |
| 621 | * @param[in] set Set to remove from. |
| 622 | * @param[in] node Node to remove. |
| 623 | * @param[in] type Node type. |
| 624 | */ |
| 625 | static void |
| 626 | set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 627 | { |
| 628 | int r; |
| 629 | struct lyxp_set_hash_node hnode; |
| 630 | uint32_t hash; |
| 631 | |
| 632 | if (set->ht) { |
| 633 | hnode.node = node; |
| 634 | hnode.type = type; |
| 635 | |
| 636 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 637 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 638 | hash = dict_hash_multi(hash, NULL, 0); |
| 639 | |
| 640 | r = lyht_remove(set->ht, &hnode, hash); |
| 641 | assert(!r); |
| 642 | (void)r; |
| 643 | |
| 644 | if (!set->ht->used) { |
| 645 | lyht_free(set->ht); |
| 646 | set->ht = NULL; |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * @brief Check whether node is in set based on its hash. |
| 653 | * |
| 654 | * @param[in] set Set to search in. |
| 655 | * @param[in] node Node to search for. |
| 656 | * @param[in] type Node type. |
| 657 | * @param[in] skip_idx Index in @p set to skip. |
| 658 | * @return LY_ERR |
| 659 | */ |
| 660 | static LY_ERR |
| 661 | set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx) |
| 662 | { |
| 663 | struct lyxp_set_hash_node hnode, *match_p; |
| 664 | uint32_t hash; |
| 665 | |
| 666 | hnode.node = node; |
| 667 | hnode.type = type; |
| 668 | |
| 669 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 670 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 671 | hash = dict_hash_multi(hash, NULL, 0); |
| 672 | |
| 673 | if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) { |
| 674 | if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) { |
| 675 | /* we found it on the index that should be skipped, find another */ |
| 676 | hnode = *match_p; |
| 677 | if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) { |
| 678 | /* none other found */ |
| 679 | return LY_SUCCESS; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | return LY_EEXIST; |
| 684 | } |
| 685 | |
| 686 | /* not found */ |
| 687 | return LY_SUCCESS; |
| 688 | } |
| 689 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 690 | void |
| 691 | lyxp_set_free_content(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 692 | { |
| 693 | if (!set) { |
| 694 | return; |
| 695 | } |
| 696 | |
| 697 | if (set->type == LYXP_SET_NODE_SET) { |
| 698 | free(set->val.nodes); |
| 699 | lyht_free(set->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 700 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 701 | free(set->val.scnodes); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 702 | lyht_free(set->ht); |
| 703 | } else { |
| 704 | if (set->type == LYXP_SET_STRING) { |
| 705 | free(set->val.str); |
| 706 | } |
| 707 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 708 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 709 | |
| 710 | set->val.nodes = NULL; |
| 711 | set->used = 0; |
| 712 | set->size = 0; |
| 713 | set->ht = NULL; |
| 714 | set->ctx_pos = 0; |
| 715 | set->ctx_pos = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 716 | } |
| 717 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 718 | /** |
| 719 | * @brief Free dynamically-allocated set. |
| 720 | * |
| 721 | * @param[in] set Set to free. |
| 722 | */ |
| 723 | static void |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 724 | lyxp_set_free(struct lyxp_set *set) |
| 725 | { |
| 726 | if (!set) { |
| 727 | return; |
| 728 | } |
| 729 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 730 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 731 | free(set); |
| 732 | } |
| 733 | |
| 734 | /** |
| 735 | * @brief Initialize set context. |
| 736 | * |
| 737 | * @param[in] new Set to initialize. |
| 738 | * @param[in] set Arbitrary initialized set. |
| 739 | */ |
| 740 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 741 | set_init(struct lyxp_set *new, const struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 742 | { |
| 743 | memset(new, 0, sizeof *new); |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 744 | if (set) { |
| 745 | new->ctx = set->ctx; |
| 746 | new->ctx_node = set->ctx_node; |
Michal Vasko | 588112f | 2019-12-10 14:51:53 +0100 | [diff] [blame] | 747 | new->root_type = set->root_type; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 748 | new->context_op = set->context_op; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 749 | new->local_mod = set->local_mod; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 750 | new->tree = set->tree; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 751 | new->format = set->format; |
| 752 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | /** |
| 756 | * @brief Create a deep copy of a set. |
| 757 | * |
| 758 | * @param[in] set Set to copy. |
| 759 | * @return Copy of @p set. |
| 760 | */ |
| 761 | static struct lyxp_set * |
| 762 | set_copy(struct lyxp_set *set) |
| 763 | { |
| 764 | struct lyxp_set *ret; |
| 765 | uint16_t i; |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 766 | int idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 767 | |
| 768 | if (!set) { |
| 769 | return NULL; |
| 770 | } |
| 771 | |
| 772 | ret = malloc(sizeof *ret); |
| 773 | LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL); |
| 774 | set_init(ret, set); |
| 775 | |
| 776 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 777 | ret->type = set->type; |
| 778 | |
| 779 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 780 | if ((set->val.scnodes[i].in_ctx == 1) || (set->val.scnodes[i].in_ctx == -2)) { |
| 781 | idx = lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type); |
Michal Vasko | 3f27c52 | 2020-01-06 08:37:49 +0100 | [diff] [blame] | 782 | /* coverity seems to think scnodes can be NULL */ |
| 783 | if ((idx == -1) || !ret->val.scnodes) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 784 | lyxp_set_free(ret); |
| 785 | return NULL; |
| 786 | } |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 787 | ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 788 | } |
| 789 | } |
| 790 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 791 | ret->type = set->type; |
| 792 | ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes); |
| 793 | LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL); |
| 794 | memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes); |
| 795 | |
| 796 | ret->used = ret->size = set->used; |
| 797 | ret->ctx_pos = set->ctx_pos; |
| 798 | ret->ctx_size = set->ctx_size; |
| 799 | ret->ht = lyht_dup(set->ht); |
| 800 | } else { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame^] | 801 | memcpy(ret, set, sizeof *ret); |
| 802 | if (set->type == LYXP_SET_STRING) { |
| 803 | ret->val.str = strdup(set->val.str); |
| 804 | LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL); |
| 805 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | return ret; |
| 809 | } |
| 810 | |
| 811 | /** |
| 812 | * @brief Fill XPath set with a string. Any current data are disposed of. |
| 813 | * |
| 814 | * @param[in] set Set to fill. |
| 815 | * @param[in] string String to fill into \p set. |
| 816 | * @param[in] str_len Length of \p string. 0 is a valid value! |
| 817 | */ |
| 818 | static void |
| 819 | set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len) |
| 820 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 821 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 822 | |
| 823 | set->type = LYXP_SET_STRING; |
| 824 | if ((str_len == 0) && (string[0] != '\0')) { |
| 825 | string = ""; |
| 826 | } |
| 827 | set->val.str = strndup(string, str_len); |
| 828 | } |
| 829 | |
| 830 | /** |
| 831 | * @brief Fill XPath set with a number. Any current data are disposed of. |
| 832 | * |
| 833 | * @param[in] set Set to fill. |
| 834 | * @param[in] number Number to fill into \p set. |
| 835 | */ |
| 836 | static void |
| 837 | set_fill_number(struct lyxp_set *set, long double number) |
| 838 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 839 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 840 | |
| 841 | set->type = LYXP_SET_NUMBER; |
| 842 | set->val.num = number; |
| 843 | } |
| 844 | |
| 845 | /** |
| 846 | * @brief Fill XPath set with a boolean. Any current data are disposed of. |
| 847 | * |
| 848 | * @param[in] set Set to fill. |
| 849 | * @param[in] boolean Boolean to fill into \p set. |
| 850 | */ |
| 851 | static void |
| 852 | set_fill_boolean(struct lyxp_set *set, int boolean) |
| 853 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 854 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 855 | |
| 856 | set->type = LYXP_SET_BOOLEAN; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 857 | set->val.bln = boolean; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | /** |
| 861 | * @brief Fill XPath set with the value from another set (deep assign). |
| 862 | * Any current data are disposed of. |
| 863 | * |
| 864 | * @param[in] trg Set to fill. |
| 865 | * @param[in] src Source set to copy into \p trg. |
| 866 | */ |
| 867 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 868 | set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 869 | { |
| 870 | if (!trg || !src) { |
| 871 | return; |
| 872 | } |
| 873 | |
| 874 | if (trg->type == LYXP_SET_NODE_SET) { |
| 875 | free(trg->val.nodes); |
| 876 | } else if (trg->type == LYXP_SET_STRING) { |
| 877 | free(trg->val.str); |
| 878 | } |
| 879 | set_init(trg, src); |
| 880 | |
| 881 | if (src->type == LYXP_SET_SCNODE_SET) { |
| 882 | trg->type = LYXP_SET_SCNODE_SET; |
| 883 | trg->used = src->used; |
| 884 | trg->size = src->used; |
| 885 | |
| 886 | trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes); |
| 887 | LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 888 | memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes); |
| 889 | } else if (src->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 890 | set_fill_boolean(trg, src->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 891 | } else if (src->type == LYXP_SET_NUMBER) { |
| 892 | set_fill_number(trg, src->val.num); |
| 893 | } else if (src->type == LYXP_SET_STRING) { |
| 894 | set_fill_string(trg, src->val.str, strlen(src->val.str)); |
| 895 | } else { |
| 896 | if (trg->type == LYXP_SET_NODE_SET) { |
| 897 | free(trg->val.nodes); |
| 898 | } else if (trg->type == LYXP_SET_STRING) { |
| 899 | free(trg->val.str); |
| 900 | } |
| 901 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 902 | assert(src->type == LYXP_SET_NODE_SET); |
| 903 | |
| 904 | trg->type = LYXP_SET_NODE_SET; |
| 905 | trg->used = src->used; |
| 906 | trg->size = src->used; |
| 907 | trg->ctx_pos = src->ctx_pos; |
| 908 | trg->ctx_size = src->ctx_size; |
| 909 | |
| 910 | trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes); |
| 911 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 912 | memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes); |
| 913 | if (src->ht) { |
| 914 | trg->ht = lyht_dup(src->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 915 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 916 | trg->ht = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 917 | } |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | /** |
| 922 | * @brief Clear context of all schema nodes. |
| 923 | * |
| 924 | * @param[in] set Set to clear. |
| 925 | */ |
| 926 | static void |
| 927 | set_scnode_clear_ctx(struct lyxp_set *set) |
| 928 | { |
| 929 | uint32_t i; |
| 930 | |
| 931 | for (i = 0; i < set->used; ++i) { |
| 932 | if (set->val.scnodes[i].in_ctx == 1) { |
| 933 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 934 | } else if (set->val.scnodes[i].in_ctx == -2) { |
| 935 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 936 | } |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | /** |
| 941 | * @brief Remove a node from a set. Removing last node changes |
| 942 | * set into LYXP_SET_EMPTY. Context position aware. |
| 943 | * |
| 944 | * @param[in] set Set to use. |
| 945 | * @param[in] idx Index from @p set of the node to be removed. |
| 946 | */ |
| 947 | static void |
| 948 | set_remove_node(struct lyxp_set *set, uint32_t idx) |
| 949 | { |
| 950 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 951 | assert(idx < set->used); |
| 952 | |
| 953 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 954 | |
| 955 | --set->used; |
| 956 | if (set->used) { |
| 957 | memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], |
| 958 | (set->used - idx) * sizeof *set->val.nodes); |
| 959 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 960 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 961 | } |
| 962 | } |
| 963 | |
| 964 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 965 | * @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] | 966 | * |
| 967 | * @param[in] set Set to use. |
| 968 | * @param[in] idx Index from @p set of the node to be removed. |
| 969 | */ |
| 970 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 971 | set_remove_node_none(struct lyxp_set *set, uint32_t idx) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 972 | { |
| 973 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 974 | assert(idx < set->used); |
| 975 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 976 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 977 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 978 | } |
| 979 | set->val.nodes[idx].type = LYXP_NODE_NONE; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 983 | * @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] | 984 | * set into LYXP_SET_EMPTY. Context position aware. |
| 985 | * |
| 986 | * @param[in] set Set to consolidate. |
| 987 | */ |
| 988 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 989 | set_remove_nodes_none(struct lyxp_set *set) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 990 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 991 | uint16_t i, orig_used, end = 0; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 992 | int32_t start; |
| 993 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 994 | assert(set); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 995 | |
| 996 | orig_used = set->used; |
| 997 | set->used = 0; |
| 998 | for (i = 0; i < orig_used;) { |
| 999 | start = -1; |
| 1000 | do { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1001 | if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1002 | start = i; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1003 | } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1004 | end = i; |
| 1005 | ++i; |
| 1006 | break; |
| 1007 | } |
| 1008 | |
| 1009 | ++i; |
| 1010 | if (i == orig_used) { |
| 1011 | end = i; |
| 1012 | } |
| 1013 | } while (i < orig_used); |
| 1014 | |
| 1015 | if (start > -1) { |
| 1016 | /* move the whole chunk of valid nodes together */ |
| 1017 | if (set->used != (unsigned)start) { |
| 1018 | memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes); |
| 1019 | } |
| 1020 | set->used += end - start; |
| 1021 | } |
| 1022 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1026 | * @brief Check for duplicates in a node set. |
| 1027 | * |
| 1028 | * @param[in] set Set to check. |
| 1029 | * @param[in] node Node to look for in @p set. |
| 1030 | * @param[in] node_type Type of @p node. |
| 1031 | * @param[in] skip_idx Index from @p set to skip. |
| 1032 | * @return LY_ERR |
| 1033 | */ |
| 1034 | static LY_ERR |
| 1035 | set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx) |
| 1036 | { |
| 1037 | uint32_t i; |
| 1038 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1039 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1040 | return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx); |
| 1041 | } |
| 1042 | |
| 1043 | for (i = 0; i < set->used; ++i) { |
| 1044 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1045 | continue; |
| 1046 | } |
| 1047 | |
| 1048 | if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) { |
| 1049 | return LY_EEXIST; |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | return LY_SUCCESS; |
| 1054 | } |
| 1055 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1056 | int |
| 1057 | lyxp_set_scnode_dup_node_check(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1058 | { |
| 1059 | uint32_t i; |
| 1060 | |
| 1061 | for (i = 0; i < set->used; ++i) { |
| 1062 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1063 | continue; |
| 1064 | } |
| 1065 | |
| 1066 | if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) { |
| 1067 | return i; |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | return -1; |
| 1072 | } |
| 1073 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1074 | void |
| 1075 | lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1076 | { |
| 1077 | uint32_t orig_used, i, j; |
| 1078 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1079 | 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] | 1080 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1081 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1082 | return; |
| 1083 | } |
| 1084 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1085 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1086 | memcpy(set1, set2, sizeof *set1); |
| 1087 | return; |
| 1088 | } |
| 1089 | |
| 1090 | if (set1->used + set2->used > set1->size) { |
| 1091 | set1->size = set1->used + set2->used; |
| 1092 | set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes); |
| 1093 | LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), ); |
| 1094 | } |
| 1095 | |
| 1096 | orig_used = set1->used; |
| 1097 | |
| 1098 | for (i = 0; i < set2->used; ++i) { |
| 1099 | for (j = 0; j < orig_used; ++j) { |
| 1100 | /* detect duplicities */ |
| 1101 | if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) { |
| 1102 | break; |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | if (j == orig_used) { |
| 1107 | memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes); |
| 1108 | ++set1->used; |
| 1109 | } |
| 1110 | } |
| 1111 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1112 | lyxp_set_free_content(set2); |
| 1113 | set2->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | /** |
| 1117 | * @brief Insert a node into a set. Context position aware. |
| 1118 | * |
| 1119 | * @param[in] set Set to use. |
| 1120 | * @param[in] node Node to insert to @p set. |
| 1121 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1122 | * @param[in] node_type Node type of @p node. |
| 1123 | * @param[in] idx Index in @p set to insert into. |
| 1124 | */ |
| 1125 | static void |
| 1126 | set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1127 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1128 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1129 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1130 | if (!set->size) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1131 | /* first item */ |
| 1132 | if (idx) { |
| 1133 | /* no real harm done, but it is a bug */ |
| 1134 | LOGINT(set->ctx); |
| 1135 | idx = 0; |
| 1136 | } |
| 1137 | set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes); |
| 1138 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1139 | set->type = LYXP_SET_NODE_SET; |
| 1140 | set->used = 0; |
| 1141 | set->size = LYXP_SET_SIZE_START; |
| 1142 | set->ctx_pos = 1; |
| 1143 | set->ctx_size = 1; |
| 1144 | set->ht = NULL; |
| 1145 | } else { |
| 1146 | /* not an empty set */ |
| 1147 | if (set->used == set->size) { |
| 1148 | |
| 1149 | /* set is full */ |
| 1150 | set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes); |
| 1151 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1152 | set->size += LYXP_SET_SIZE_STEP; |
| 1153 | } |
| 1154 | |
| 1155 | if (idx > set->used) { |
| 1156 | LOGINT(set->ctx); |
| 1157 | idx = set->used; |
| 1158 | } |
| 1159 | |
| 1160 | /* make space for the new node */ |
| 1161 | if (idx < set->used) { |
| 1162 | memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes); |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | /* finally assign the value */ |
| 1167 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1168 | set->val.nodes[idx].type = node_type; |
| 1169 | set->val.nodes[idx].pos = pos; |
| 1170 | ++set->used; |
| 1171 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1172 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1173 | set_insert_node_hash(set, (struct lyd_node *)node, node_type); |
| 1174 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1175 | } |
| 1176 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1177 | int |
| 1178 | lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1179 | { |
| 1180 | int ret; |
| 1181 | |
| 1182 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1183 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1184 | ret = lyxp_set_scnode_dup_node_check(set, node, node_type, -1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1185 | if (ret > -1) { |
| 1186 | set->val.scnodes[ret].in_ctx = 1; |
| 1187 | } else { |
| 1188 | if (set->used == set->size) { |
| 1189 | set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes); |
| 1190 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), -1); |
| 1191 | set->size += LYXP_SET_SIZE_STEP; |
| 1192 | } |
| 1193 | |
| 1194 | ret = set->used; |
| 1195 | set->val.scnodes[ret].scnode = (struct lysc_node *)node; |
| 1196 | set->val.scnodes[ret].type = node_type; |
| 1197 | set->val.scnodes[ret].in_ctx = 1; |
| 1198 | ++set->used; |
| 1199 | } |
| 1200 | |
| 1201 | return ret; |
| 1202 | } |
| 1203 | |
| 1204 | /** |
| 1205 | * @brief Replace a node in a set with another. Context position aware. |
| 1206 | * |
| 1207 | * @param[in] set Set to use. |
| 1208 | * @param[in] node Node to insert to @p set. |
| 1209 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1210 | * @param[in] node_type Node type of @p node. |
| 1211 | * @param[in] idx Index in @p set of the node to replace. |
| 1212 | */ |
| 1213 | static void |
| 1214 | set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1215 | { |
| 1216 | assert(set && (idx < set->used)); |
| 1217 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1218 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1219 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1220 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1221 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1222 | set->val.nodes[idx].type = node_type; |
| 1223 | set->val.nodes[idx].pos = pos; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1224 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1225 | set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1226 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | /** |
| 1230 | * @brief Set all nodes with ctx 1 to a new unique context value. |
| 1231 | * |
| 1232 | * @param[in] set Set to modify. |
| 1233 | * @return New context value. |
| 1234 | */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1235 | static int32_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1236 | set_scnode_new_in_ctx(struct lyxp_set *set) |
| 1237 | { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1238 | uint32_t i; |
| 1239 | int32_t ret_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1240 | |
| 1241 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1242 | |
| 1243 | ret_ctx = 3; |
| 1244 | retry: |
| 1245 | for (i = 0; i < set->used; ++i) { |
| 1246 | if (set->val.scnodes[i].in_ctx >= ret_ctx) { |
| 1247 | ret_ctx = set->val.scnodes[i].in_ctx + 1; |
| 1248 | goto retry; |
| 1249 | } |
| 1250 | } |
| 1251 | for (i = 0; i < set->used; ++i) { |
| 1252 | if (set->val.scnodes[i].in_ctx == 1) { |
| 1253 | set->val.scnodes[i].in_ctx = ret_ctx; |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | return ret_ctx; |
| 1258 | } |
| 1259 | |
| 1260 | /** |
| 1261 | * @brief Get unique @p node position in the data. |
| 1262 | * |
| 1263 | * @param[in] node Node to find. |
| 1264 | * @param[in] node_type Node type of @p node. |
| 1265 | * @param[in] root Root node. |
| 1266 | * @param[in] root_type Type of the XPath @p root node. |
| 1267 | * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally |
| 1268 | * be used to increase efficiency and start the DFS from this node. |
| 1269 | * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set. |
| 1270 | * @return Node position. |
| 1271 | */ |
| 1272 | static uint32_t |
| 1273 | 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^] | 1274 | 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] | 1275 | { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1276 | const struct lyd_node *elem = NULL, *top_sibling; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1277 | uint32_t pos = 1; |
| 1278 | |
| 1279 | assert(prev && prev_pos && !root->prev->next); |
| 1280 | |
| 1281 | if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 1282 | return 0; |
| 1283 | } |
| 1284 | |
| 1285 | if (*prev) { |
| 1286 | /* start from the previous element instead from the root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1287 | pos = *prev_pos; |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1288 | for (top_sibling = *prev; top_sibling->parent; top_sibling = (struct lyd_node *)top_sibling->parent) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1289 | goto dfs_search; |
| 1290 | } |
| 1291 | |
| 1292 | for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1293 | LYD_TREE_DFS_BEGIN(top_sibling, elem) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1294 | dfs_search: |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1295 | if (*prev && !elem) { |
| 1296 | /* resume previous DFS */ |
| 1297 | elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev; |
| 1298 | LYD_TREE_DFS_continue = 0; |
| 1299 | } |
| 1300 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1301 | 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] | 1302 | /* skip */ |
| 1303 | LYD_TREE_DFS_continue = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1304 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1305 | if (elem == node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1306 | break; |
| 1307 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1308 | ++pos; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1309 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1310 | |
| 1311 | LYD_TREE_DFS_END(top_sibling, elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | /* node found */ |
| 1315 | if (elem) { |
| 1316 | break; |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | if (!elem) { |
| 1321 | if (!(*prev)) { |
| 1322 | /* we went from root and failed to find it, cannot be */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1323 | LOGINT(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1324 | return 0; |
| 1325 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1326 | /* start the search again from the beginning */ |
| 1327 | *prev = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1328 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1329 | top_sibling = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1330 | pos = 1; |
| 1331 | goto dfs_search; |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | /* remember the last found node for next time */ |
| 1336 | *prev = node; |
| 1337 | *prev_pos = pos; |
| 1338 | |
| 1339 | return pos; |
| 1340 | } |
| 1341 | |
| 1342 | /** |
| 1343 | * @brief Assign (fill) missing node positions. |
| 1344 | * |
| 1345 | * @param[in] set Set to fill positions in. |
| 1346 | * @param[in] root Context root node. |
| 1347 | * @param[in] root_type Context root type. |
| 1348 | * @return LY_ERR |
| 1349 | */ |
| 1350 | static LY_ERR |
| 1351 | set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type) |
| 1352 | { |
| 1353 | const struct lyd_node *prev = NULL, *tmp_node; |
| 1354 | uint32_t i, tmp_pos = 0; |
| 1355 | |
| 1356 | for (i = 0; i < set->used; ++i) { |
| 1357 | if (!set->val.nodes[i].pos) { |
| 1358 | tmp_node = NULL; |
| 1359 | switch (set->val.nodes[i].type) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1360 | case LYXP_NODE_META: |
| 1361 | tmp_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1362 | if (!tmp_node) { |
| 1363 | LOGINT_RET(root->schema->module->ctx); |
| 1364 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame^] | 1365 | /* fallthrough */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1366 | case LYXP_NODE_ELEM: |
| 1367 | case LYXP_NODE_TEXT: |
| 1368 | if (!tmp_node) { |
| 1369 | tmp_node = set->val.nodes[i].node; |
| 1370 | } |
| 1371 | set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos); |
| 1372 | break; |
| 1373 | default: |
| 1374 | /* all roots have position 0 */ |
| 1375 | break; |
| 1376 | } |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | return LY_SUCCESS; |
| 1381 | } |
| 1382 | |
| 1383 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1384 | * @brief Get unique @p meta position in the parent metadata. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1385 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1386 | * @param[in] meta Metadata to use. |
| 1387 | * @return Metadata position. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1388 | */ |
| 1389 | static uint16_t |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1390 | get_meta_pos(struct lyd_meta *meta) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1391 | { |
| 1392 | uint16_t pos = 0; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1393 | struct lyd_meta *meta2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1394 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1395 | for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1396 | ++pos; |
| 1397 | } |
| 1398 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1399 | assert(meta2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1400 | return pos; |
| 1401 | } |
| 1402 | |
| 1403 | /** |
| 1404 | * @brief Compare 2 nodes in respect to XPath document order. |
| 1405 | * |
| 1406 | * @param[in] item1 1st node. |
| 1407 | * @param[in] item2 2nd node. |
| 1408 | * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1. |
| 1409 | */ |
| 1410 | static int |
| 1411 | set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2) |
| 1412 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1413 | uint32_t meta_pos1 = 0, meta_pos2 = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1414 | |
| 1415 | if (item1->pos < item2->pos) { |
| 1416 | return -1; |
| 1417 | } |
| 1418 | |
| 1419 | if (item1->pos > item2->pos) { |
| 1420 | return 1; |
| 1421 | } |
| 1422 | |
| 1423 | /* node positions are equal, the fun case */ |
| 1424 | |
| 1425 | /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */ |
| 1426 | /* special case since text nodes are actually saved as their parents */ |
| 1427 | if ((item1->node == item2->node) && (item1->type != item2->type)) { |
| 1428 | if (item1->type == LYXP_NODE_ELEM) { |
| 1429 | assert(item2->type == LYXP_NODE_TEXT); |
| 1430 | return -1; |
| 1431 | } else { |
| 1432 | assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM)); |
| 1433 | return 1; |
| 1434 | } |
| 1435 | } |
| 1436 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1437 | /* we need meta positions now */ |
| 1438 | if (item1->type == LYXP_NODE_META) { |
| 1439 | meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1440 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1441 | if (item2->type == LYXP_NODE_META) { |
| 1442 | meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1443 | } |
| 1444 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1445 | /* 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] | 1446 | /* check for duplicates */ |
| 1447 | if (item1->node == item2->node) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1448 | 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] | 1449 | return 0; |
| 1450 | } |
| 1451 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1452 | /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1453 | /* elem is always first, 2nd node is after it */ |
| 1454 | if (item1->type == LYXP_NODE_ELEM) { |
| 1455 | assert(item2->type != LYXP_NODE_ELEM); |
| 1456 | return -1; |
| 1457 | } |
| 1458 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1459 | /* 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] | 1460 | /* 2nd is before 1st */ |
| 1461 | if (((item1->type == LYXP_NODE_TEXT) |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1462 | && ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) |
| 1463 | || ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) |
| 1464 | || (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) |
| 1465 | && (meta_pos1 > meta_pos2))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1466 | return 1; |
| 1467 | } |
| 1468 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1469 | /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1470 | /* 2nd is after 1st */ |
| 1471 | return -1; |
| 1472 | } |
| 1473 | |
| 1474 | /** |
| 1475 | * @brief Set cast for comparisons. |
| 1476 | * |
| 1477 | * @param[in] trg Target set to cast source into. |
| 1478 | * @param[in] src Source set. |
| 1479 | * @param[in] type Target set type. |
| 1480 | * @param[in] src_idx Source set node index. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1481 | * @return LY_ERR |
| 1482 | */ |
| 1483 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1484 | 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] | 1485 | { |
| 1486 | assert(src->type == LYXP_SET_NODE_SET); |
| 1487 | |
| 1488 | set_init(trg, src); |
| 1489 | |
| 1490 | /* insert node into target set */ |
| 1491 | set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0); |
| 1492 | |
| 1493 | /* cast target set appropriately */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1494 | return lyxp_set_cast(trg, type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1495 | } |
| 1496 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1497 | /** |
| 1498 | * @brief Set content canonization for comparisons. |
| 1499 | * |
| 1500 | * @param[in] trg Target set to put the canononized source into. |
| 1501 | * @param[in] src Source set. |
| 1502 | * @param[in] xp_node Source XPath node/meta to use for canonization. |
| 1503 | * @return LY_ERR |
| 1504 | */ |
| 1505 | static LY_ERR |
| 1506 | set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node) |
| 1507 | { |
| 1508 | struct lysc_type *type = NULL; |
| 1509 | struct lyd_value val; |
| 1510 | struct ly_err_item *err = NULL; |
| 1511 | char *str, *ptr; |
| 1512 | int dynamic; |
| 1513 | LY_ERR rc; |
| 1514 | |
| 1515 | /* is there anything to canonize even? */ |
| 1516 | if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) { |
| 1517 | /* do we have a type to use for canonization? */ |
| 1518 | if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) { |
| 1519 | type = ((struct lyd_node_term *)xp_node->node)->value.realtype; |
| 1520 | } else if (xp_node->type == LYXP_NODE_META) { |
| 1521 | type = ((struct lyd_meta *)xp_node->node)->value.realtype; |
| 1522 | } |
| 1523 | } |
| 1524 | if (!type) { |
| 1525 | goto fill; |
| 1526 | } |
| 1527 | |
| 1528 | if (src->type == LYXP_SET_NUMBER) { |
| 1529 | /* canonize number */ |
| 1530 | if (asprintf(&str, "%Lf", src->val.num) == -1) { |
| 1531 | LOGMEM(src->ctx); |
| 1532 | return LY_EMEM; |
| 1533 | } |
| 1534 | } else { |
| 1535 | /* canonize string */ |
| 1536 | str = strdup(src->val.str); |
| 1537 | } |
| 1538 | |
| 1539 | /* ignore errors, the value may not satisfy schema constraints */ |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 1540 | rc = type->plugin->store(src->ctx, type, str, strlen(str), LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_DYNAMIC, |
| 1541 | LY_PREF_JSON, NULL, NULL, NULL, &val, &err); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1542 | ly_err_free(err); |
| 1543 | if (rc) { |
| 1544 | /* invalid value */ |
| 1545 | free(str); |
| 1546 | goto fill; |
| 1547 | } |
| 1548 | |
| 1549 | /* storing successful, now print the canonical value */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 1550 | str = (char *)type->plugin->print(&val, LY_PREF_JSON, NULL, &dynamic); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1551 | |
| 1552 | /* use the canonized value */ |
| 1553 | set_init(trg, src); |
| 1554 | trg->type = src->type; |
| 1555 | if (src->type == LYXP_SET_NUMBER) { |
| 1556 | trg->val.num = strtold(str, &ptr); |
| 1557 | if (dynamic) { |
| 1558 | free(str); |
| 1559 | } |
| 1560 | LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT); |
| 1561 | } else { |
| 1562 | trg->val.str = (dynamic ? str : strdup(str)); |
| 1563 | } |
| 1564 | type->plugin->free(src->ctx, &val); |
| 1565 | return LY_SUCCESS; |
| 1566 | |
| 1567 | fill: |
| 1568 | /* no canonization needed/possible */ |
| 1569 | set_fill_set(trg, src); |
| 1570 | return LY_SUCCESS; |
| 1571 | } |
| 1572 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1573 | #ifndef NDEBUG |
| 1574 | |
| 1575 | /** |
| 1576 | * @brief Bubble sort @p set into XPath document order. |
| 1577 | * Context position aware. Unused in the 'Release' build target. |
| 1578 | * |
| 1579 | * @param[in] set Set to sort. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1580 | * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0). |
| 1581 | */ |
| 1582 | static int |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1583 | set_sort(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1584 | { |
| 1585 | uint32_t i, j; |
| 1586 | int ret = 0, cmp, inverted, change; |
| 1587 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1588 | struct lyxp_set_node item; |
| 1589 | struct lyxp_set_hash_node hnode; |
| 1590 | uint64_t hash; |
| 1591 | |
Michal Vasko | 3cf8fbf | 2020-05-27 15:21:21 +0200 | [diff] [blame] | 1592 | if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1593 | return 0; |
| 1594 | } |
| 1595 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1596 | /* find first top-level node to be used as anchor for positions */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1597 | for (root = set->ctx_node; root->parent; root = (const struct lyd_node *)root->parent) {} |
| 1598 | for (; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1599 | |
| 1600 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1601 | if (set_assign_pos(set, root, set->root_type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1602 | return -1; |
| 1603 | } |
| 1604 | |
| 1605 | LOGDBG(LY_LDGXPATH, "SORT BEGIN"); |
| 1606 | print_set_debug(set); |
| 1607 | |
| 1608 | for (i = 0; i < set->used; ++i) { |
| 1609 | inverted = 0; |
| 1610 | change = 0; |
| 1611 | |
| 1612 | for (j = 1; j < set->used - i; ++j) { |
| 1613 | /* compare node positions */ |
| 1614 | if (inverted) { |
| 1615 | cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]); |
| 1616 | } else { |
| 1617 | cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]); |
| 1618 | } |
| 1619 | |
| 1620 | /* swap if needed */ |
| 1621 | if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) { |
| 1622 | change = 1; |
| 1623 | |
| 1624 | item = set->val.nodes[j - 1]; |
| 1625 | set->val.nodes[j - 1] = set->val.nodes[j]; |
| 1626 | set->val.nodes[j] = item; |
| 1627 | } else { |
| 1628 | /* whether node_pos1 should be smaller than node_pos2 or the other way around */ |
| 1629 | inverted = !inverted; |
| 1630 | } |
| 1631 | } |
| 1632 | |
| 1633 | ++ret; |
| 1634 | |
| 1635 | if (!change) { |
| 1636 | break; |
| 1637 | } |
| 1638 | } |
| 1639 | |
| 1640 | LOGDBG(LY_LDGXPATH, "SORT END %d", ret); |
| 1641 | print_set_debug(set); |
| 1642 | |
| 1643 | /* check node hashes */ |
| 1644 | if (set->used >= LYD_HT_MIN_ITEMS) { |
| 1645 | assert(set->ht); |
| 1646 | for (i = 0; i < set->used; ++i) { |
| 1647 | hnode.node = set->val.nodes[i].node; |
| 1648 | hnode.type = set->val.nodes[i].type; |
| 1649 | |
| 1650 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 1651 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 1652 | hash = dict_hash_multi(hash, NULL, 0); |
| 1653 | |
| 1654 | assert(!lyht_find(set->ht, &hnode, hash, NULL)); |
| 1655 | } |
| 1656 | } |
| 1657 | |
| 1658 | return ret - 1; |
| 1659 | } |
| 1660 | |
| 1661 | /** |
| 1662 | * @brief Remove duplicate entries in a sorted node set. |
| 1663 | * |
| 1664 | * @param[in] set Sorted set to check. |
| 1665 | * @return LY_ERR (LY_EEXIST if some duplicates are found) |
| 1666 | */ |
| 1667 | static LY_ERR |
| 1668 | set_sorted_dup_node_clean(struct lyxp_set *set) |
| 1669 | { |
| 1670 | uint32_t i = 0; |
| 1671 | LY_ERR ret = LY_SUCCESS; |
| 1672 | |
| 1673 | if (set->used > 1) { |
| 1674 | while (i < set->used - 1) { |
| 1675 | if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) |
| 1676 | && (set->val.nodes[i].type == set->val.nodes[i + 1].type)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1677 | set_remove_node_none(set, i + 1); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1678 | ret = LY_EEXIST; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1679 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1680 | ++i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1681 | } |
| 1682 | } |
| 1683 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1684 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1685 | return ret; |
| 1686 | } |
| 1687 | |
| 1688 | #endif |
| 1689 | |
| 1690 | /** |
| 1691 | * @brief Merge 2 sorted sets into one. |
| 1692 | * |
| 1693 | * @param[in,out] trg Set to merge into. Duplicates are removed. |
| 1694 | * @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] | 1695 | * @return LY_ERR |
| 1696 | */ |
| 1697 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1698 | set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1699 | { |
| 1700 | uint32_t i, j, k, count, dup_count; |
| 1701 | int cmp; |
| 1702 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1703 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1704 | 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] | 1705 | return LY_EINVAL; |
| 1706 | } |
| 1707 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1708 | if (!src->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1709 | return LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1710 | } else if (!trg->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1711 | set_fill_set(trg, src); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1712 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1713 | return LY_SUCCESS; |
| 1714 | } |
| 1715 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1716 | /* find first top-level node to be used as anchor for positions */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1717 | for (root = trg->ctx_node; root->parent; root = (const struct lyd_node *)root->parent) {} |
| 1718 | for (; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1719 | |
| 1720 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1721 | 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] | 1722 | return LY_EINT; |
| 1723 | } |
| 1724 | |
| 1725 | #ifndef NDEBUG |
| 1726 | LOGDBG(LY_LDGXPATH, "MERGE target"); |
| 1727 | print_set_debug(trg); |
| 1728 | LOGDBG(LY_LDGXPATH, "MERGE source"); |
| 1729 | print_set_debug(src); |
| 1730 | #endif |
| 1731 | |
| 1732 | /* make memory for the merge (duplicates are not detected yet, so space |
| 1733 | * will likely be wasted on them, too bad) */ |
| 1734 | if (trg->size - trg->used < src->used) { |
| 1735 | trg->size = trg->used + src->used; |
| 1736 | |
| 1737 | trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes); |
| 1738 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM); |
| 1739 | } |
| 1740 | |
| 1741 | i = 0; |
| 1742 | j = 0; |
| 1743 | count = 0; |
| 1744 | dup_count = 0; |
| 1745 | do { |
| 1746 | cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]); |
| 1747 | if (!cmp) { |
| 1748 | if (!count) { |
| 1749 | /* duplicate, just skip it */ |
| 1750 | ++i; |
| 1751 | ++j; |
| 1752 | } else { |
| 1753 | /* we are copying something already, so let's copy the duplicate too, |
| 1754 | * we are hoping that afterwards there are some more nodes to |
| 1755 | * copy and this way we can copy them all together */ |
| 1756 | ++count; |
| 1757 | ++dup_count; |
| 1758 | ++i; |
| 1759 | ++j; |
| 1760 | } |
| 1761 | } else if (cmp < 0) { |
| 1762 | /* inserting src node into trg, just remember it for now */ |
| 1763 | ++count; |
| 1764 | ++i; |
| 1765 | |
| 1766 | /* insert the hash now */ |
| 1767 | set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type); |
| 1768 | } else if (count) { |
| 1769 | copy_nodes: |
| 1770 | /* time to actually copy the nodes, we have found the largest block of nodes */ |
| 1771 | memmove(&trg->val.nodes[j + (count - dup_count)], |
| 1772 | &trg->val.nodes[j], |
| 1773 | (trg->used - j) * sizeof *trg->val.nodes); |
| 1774 | memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes); |
| 1775 | |
| 1776 | trg->used += count - dup_count; |
| 1777 | /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */ |
| 1778 | j += count - dup_count; |
| 1779 | |
| 1780 | count = 0; |
| 1781 | dup_count = 0; |
| 1782 | } else { |
| 1783 | ++j; |
| 1784 | } |
| 1785 | } while ((i < src->used) && (j < trg->used)); |
| 1786 | |
| 1787 | if ((i < src->used) || count) { |
| 1788 | /* insert all the hashes first */ |
| 1789 | for (k = i; k < src->used; ++k) { |
| 1790 | set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type); |
| 1791 | } |
| 1792 | |
| 1793 | /* loop ended, but we need to copy something at trg end */ |
| 1794 | count += src->used - i; |
| 1795 | i = src->used; |
| 1796 | goto copy_nodes; |
| 1797 | } |
| 1798 | |
| 1799 | /* we are inserting hashes before the actual node insert, which causes |
| 1800 | * situations when there were initially not enough items for a hash table, |
| 1801 | * but even after some were inserted, hash table was not created (during |
| 1802 | * insertion the number of items is not updated yet) */ |
| 1803 | if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) { |
| 1804 | set_insert_node_hash(trg, NULL, 0); |
| 1805 | } |
| 1806 | |
| 1807 | #ifndef NDEBUG |
| 1808 | LOGDBG(LY_LDGXPATH, "MERGE result"); |
| 1809 | print_set_debug(trg); |
| 1810 | #endif |
| 1811 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1812 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1813 | return LY_SUCCESS; |
| 1814 | } |
| 1815 | |
| 1816 | /* |
| 1817 | * (re)parse functions |
| 1818 | * |
| 1819 | * Parse functions parse the expression into |
| 1820 | * tokens (syntactic analysis). |
| 1821 | * |
| 1822 | * Reparse functions perform semantic analysis |
| 1823 | * (do not save the result, just a check) of |
| 1824 | * the expression and fill repeat indices. |
| 1825 | */ |
| 1826 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1827 | LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1828 | 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] | 1829 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1830 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1831 | if (ctx) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1832 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1833 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1834 | return LY_EINCOMPLETE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1835 | } |
| 1836 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1837 | if (want_tok && (exp->tokens[tok_idx] != want_tok)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1838 | if (ctx) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1839 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1840 | 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] | 1841 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1842 | return LY_ENOT; |
| 1843 | } |
| 1844 | |
| 1845 | return LY_SUCCESS; |
| 1846 | } |
| 1847 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1848 | LY_ERR |
| 1849 | lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok) |
| 1850 | { |
| 1851 | LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok)); |
| 1852 | |
| 1853 | /* skip the token */ |
| 1854 | ++(*tok_idx); |
| 1855 | |
| 1856 | return LY_SUCCESS; |
| 1857 | } |
| 1858 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1859 | /* just like lyxp_check_token() but tests for 2 tokens */ |
| 1860 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1861 | exp_check_token2(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok1, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame^] | 1862 | enum lyxp_token want_tok2) |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1863 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1864 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1865 | if (ctx) { |
| 1866 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1867 | } |
| 1868 | return LY_EINCOMPLETE; |
| 1869 | } |
| 1870 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1871 | 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] | 1872 | if (ctx) { |
| 1873 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1874 | lyxp_print_token(exp->tokens[tok_idx]), &exp->expr[exp->tok_pos[tok_idx]]); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1875 | } |
| 1876 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | return LY_SUCCESS; |
| 1880 | } |
| 1881 | |
| 1882 | /** |
| 1883 | * @brief Stack operation push on the repeat array. |
| 1884 | * |
| 1885 | * @param[in] exp Expression to use. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1886 | * @param[in] tok_idx Position in the expresion \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1887 | * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed. |
| 1888 | */ |
| 1889 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1890 | 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] | 1891 | { |
| 1892 | uint16_t i; |
| 1893 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1894 | if (exp->repeat[tok_idx]) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1895 | for (i = 0; exp->repeat[tok_idx][i]; ++i) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1896 | exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]); |
| 1897 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1898 | exp->repeat[tok_idx][i] = repeat_op_idx; |
| 1899 | exp->repeat[tok_idx][i + 1] = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1900 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1901 | exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]); |
| 1902 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1903 | exp->repeat[tok_idx][0] = repeat_op_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1904 | } |
| 1905 | } |
| 1906 | |
| 1907 | /** |
| 1908 | * @brief Reparse Predicate. Logs directly on error. |
| 1909 | * |
| 1910 | * [7] Predicate ::= '[' Expr ']' |
| 1911 | * |
| 1912 | * @param[in] ctx Context for logging. |
| 1913 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1914 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1915 | * @return LY_ERR |
| 1916 | */ |
| 1917 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1918 | 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] | 1919 | { |
| 1920 | LY_ERR rc; |
| 1921 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1922 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1923 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1924 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1925 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1926 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1927 | LY_CHECK_RET(rc); |
| 1928 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1929 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1930 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1931 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1932 | |
| 1933 | return LY_SUCCESS; |
| 1934 | } |
| 1935 | |
| 1936 | /** |
| 1937 | * @brief Reparse RelativeLocationPath. Logs directly on error. |
| 1938 | * |
| 1939 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 1940 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 1941 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 1942 | * |
| 1943 | * @param[in] ctx Context for logging. |
| 1944 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1945 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1946 | * @return LY_ERR (LY_EINCOMPLETE on forward reference) |
| 1947 | */ |
| 1948 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1949 | 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] | 1950 | { |
| 1951 | LY_ERR rc; |
| 1952 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1953 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1954 | LY_CHECK_RET(rc); |
| 1955 | |
| 1956 | goto step; |
| 1957 | do { |
| 1958 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1959 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 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 | step: |
| 1964 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1965 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1966 | case LYXP_TOKEN_DOT: |
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 | break; |
| 1969 | |
| 1970 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1971 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1972 | break; |
| 1973 | |
| 1974 | case LYXP_TOKEN_AT: |
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 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1977 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1978 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1979 | 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] | 1980 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1981 | 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] | 1982 | return LY_EVALID; |
| 1983 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame^] | 1984 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1985 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1986 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1987 | goto reparse_predicate; |
| 1988 | break; |
| 1989 | |
| 1990 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1991 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1992 | |
| 1993 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1994 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1995 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1996 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1997 | |
| 1998 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1999 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2000 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2001 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2002 | |
| 2003 | reparse_predicate: |
| 2004 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2005 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 2006 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2007 | LY_CHECK_RET(rc); |
| 2008 | } |
| 2009 | break; |
| 2010 | default: |
| 2011 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2012 | 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] | 2013 | return LY_EVALID; |
| 2014 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2015 | } 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] | 2016 | |
| 2017 | return LY_SUCCESS; |
| 2018 | } |
| 2019 | |
| 2020 | /** |
| 2021 | * @brief Reparse AbsoluteLocationPath. Logs directly on error. |
| 2022 | * |
| 2023 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 2024 | * |
| 2025 | * @param[in] ctx Context for logging. |
| 2026 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2027 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2028 | * @return LY_ERR |
| 2029 | */ |
| 2030 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2031 | 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] | 2032 | { |
| 2033 | LY_ERR rc; |
| 2034 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2035 | 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] | 2036 | |
| 2037 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2038 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2039 | /* '/' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2040 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2041 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2042 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2043 | return LY_SUCCESS; |
| 2044 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2045 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2046 | case LYXP_TOKEN_DOT: |
| 2047 | case LYXP_TOKEN_DDOT: |
| 2048 | case LYXP_TOKEN_AT: |
| 2049 | case LYXP_TOKEN_NAMETEST: |
| 2050 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2051 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2052 | LY_CHECK_RET(rc); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame^] | 2053 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2054 | default: |
| 2055 | break; |
| 2056 | } |
| 2057 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2058 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 2059 | /* '//' RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2060 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2061 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2062 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2063 | LY_CHECK_RET(rc); |
| 2064 | } |
| 2065 | |
| 2066 | return LY_SUCCESS; |
| 2067 | } |
| 2068 | |
| 2069 | /** |
| 2070 | * @brief Reparse FunctionCall. Logs directly on error. |
| 2071 | * |
| 2072 | * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
| 2073 | * |
| 2074 | * @param[in] ctx Context for logging. |
| 2075 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2076 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2077 | * @return LY_ERR |
| 2078 | */ |
| 2079 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2080 | 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] | 2081 | { |
| 2082 | int min_arg_count = -1, max_arg_count, arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2083 | uint16_t func_tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2084 | LY_ERR rc; |
| 2085 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2086 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2087 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2088 | func_tok_idx = *tok_idx; |
| 2089 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2090 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2091 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2092 | min_arg_count = 1; |
| 2093 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2094 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2095 | min_arg_count = 1; |
| 2096 | max_arg_count = 1; |
| 2097 | } |
| 2098 | break; |
| 2099 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2100 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
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]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2104 | min_arg_count = 0; |
| 2105 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2106 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2107 | min_arg_count = 0; |
| 2108 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2109 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2110 | min_arg_count = 0; |
| 2111 | max_arg_count = 0; |
| 2112 | } |
| 2113 | break; |
| 2114 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2115 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2116 | min_arg_count = 1; |
| 2117 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2118 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2119 | min_arg_count = 0; |
| 2120 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2121 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2122 | min_arg_count = 1; |
| 2123 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2124 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 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]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2128 | min_arg_count = 1; |
| 2129 | max_arg_count = 1; |
| 2130 | } |
| 2131 | break; |
| 2132 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2133 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2134 | min_arg_count = 2; |
Michal Vasko | be2e356 | 2019-10-15 15:35:35 +0200 | [diff] [blame] | 2135 | max_arg_count = INT_MAX; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2136 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2137 | min_arg_count = 0; |
| 2138 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2139 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2140 | min_arg_count = 0; |
| 2141 | max_arg_count = 1; |
| 2142 | } |
| 2143 | break; |
| 2144 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2145 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2146 | min_arg_count = 1; |
| 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]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2149 | min_arg_count = 1; |
| 2150 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2151 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2152 | min_arg_count = 0; |
| 2153 | max_arg_count = 0; |
| 2154 | } |
| 2155 | break; |
| 2156 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2157 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2158 | min_arg_count = 2; |
| 2159 | max_arg_count = 2; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2160 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2161 | min_arg_count = 0; |
| 2162 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2163 | } 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] | 2164 | min_arg_count = 2; |
| 2165 | max_arg_count = 2; |
| 2166 | } |
| 2167 | break; |
| 2168 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2169 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2170 | min_arg_count = 2; |
| 2171 | max_arg_count = 3; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2172 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2173 | min_arg_count = 3; |
| 2174 | max_arg_count = 3; |
| 2175 | } |
| 2176 | break; |
| 2177 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2178 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2179 | min_arg_count = 0; |
| 2180 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2181 | } 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] | 2182 | min_arg_count = 1; |
| 2183 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2184 | } 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] | 2185 | min_arg_count = 2; |
| 2186 | max_arg_count = 2; |
| 2187 | } |
| 2188 | break; |
| 2189 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2190 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2191 | min_arg_count = 2; |
| 2192 | max_arg_count = 2; |
| 2193 | } |
| 2194 | break; |
| 2195 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2196 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2197 | min_arg_count = 2; |
| 2198 | max_arg_count = 2; |
| 2199 | } |
| 2200 | break; |
| 2201 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2202 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2203 | min_arg_count = 0; |
| 2204 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2205 | } 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] | 2206 | min_arg_count = 0; |
| 2207 | max_arg_count = 1; |
| 2208 | } |
| 2209 | break; |
| 2210 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2211 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
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]], "substring-after", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2215 | min_arg_count = 2; |
| 2216 | max_arg_count = 2; |
| 2217 | } |
| 2218 | break; |
| 2219 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2220 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2221 | min_arg_count = 2; |
| 2222 | max_arg_count = 2; |
| 2223 | } |
| 2224 | break; |
| 2225 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2226 | 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] | 2227 | min_arg_count = 2; |
| 2228 | max_arg_count = 2; |
| 2229 | } |
| 2230 | break; |
| 2231 | } |
| 2232 | if (min_arg_count == -1) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2233 | 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] | 2234 | return LY_EINVAL; |
| 2235 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2236 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2237 | |
| 2238 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2239 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2240 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2241 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2242 | |
| 2243 | /* ( Expr ( ',' Expr )* )? */ |
| 2244 | arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2245 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2246 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2247 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2248 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2249 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2250 | LY_CHECK_RET(rc); |
| 2251 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2252 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
| 2253 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2254 | |
| 2255 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2256 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2257 | LY_CHECK_RET(rc); |
| 2258 | } |
| 2259 | |
| 2260 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2261 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2262 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2263 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2264 | |
| 2265 | if ((arg_count < min_arg_count) || (arg_count > max_arg_count)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2266 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx], |
| 2267 | &exp->expr[exp->tok_pos[func_tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2268 | return LY_EVALID; |
| 2269 | } |
| 2270 | |
| 2271 | return LY_SUCCESS; |
| 2272 | } |
| 2273 | |
| 2274 | /** |
| 2275 | * @brief Reparse PathExpr. Logs directly on error. |
| 2276 | * |
| 2277 | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
| 2278 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 2279 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 2280 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
| 2281 | * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
| 2282 | * |
| 2283 | * @param[in] ctx Context for logging. |
| 2284 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2285 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2286 | * @return LY_ERR |
| 2287 | */ |
| 2288 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2289 | 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] | 2290 | { |
| 2291 | LY_ERR rc; |
| 2292 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2293 | if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2294 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2295 | } |
| 2296 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2297 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2298 | case LYXP_TOKEN_PAR1: |
| 2299 | /* '(' Expr ')' Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2300 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2301 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2302 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2303 | LY_CHECK_RET(rc); |
| 2304 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2305 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2306 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2307 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2308 | goto predicate; |
| 2309 | break; |
| 2310 | case LYXP_TOKEN_DOT: |
| 2311 | case LYXP_TOKEN_DDOT: |
| 2312 | case LYXP_TOKEN_AT: |
| 2313 | case LYXP_TOKEN_NAMETEST: |
| 2314 | case LYXP_TOKEN_NODETYPE: |
| 2315 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2316 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2317 | LY_CHECK_RET(rc); |
| 2318 | break; |
| 2319 | case LYXP_TOKEN_FUNCNAME: |
| 2320 | /* FunctionCall */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2321 | rc = reparse_function_call(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2322 | LY_CHECK_RET(rc); |
| 2323 | goto predicate; |
| 2324 | break; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2325 | case LYXP_TOKEN_OPER_PATH: |
| 2326 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2327 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2328 | rc = reparse_absolute_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2329 | LY_CHECK_RET(rc); |
| 2330 | break; |
| 2331 | case LYXP_TOKEN_LITERAL: |
| 2332 | /* Literal */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2333 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2334 | goto predicate; |
| 2335 | break; |
| 2336 | case LYXP_TOKEN_NUMBER: |
| 2337 | /* Number */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2338 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2339 | goto predicate; |
| 2340 | break; |
| 2341 | default: |
| 2342 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2343 | 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] | 2344 | return LY_EVALID; |
| 2345 | } |
| 2346 | |
| 2347 | return LY_SUCCESS; |
| 2348 | |
| 2349 | predicate: |
| 2350 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2351 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 2352 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2353 | LY_CHECK_RET(rc); |
| 2354 | } |
| 2355 | |
| 2356 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2357 | 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] | 2358 | |
| 2359 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2360 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2361 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2362 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2363 | LY_CHECK_RET(rc); |
| 2364 | } |
| 2365 | |
| 2366 | return LY_SUCCESS; |
| 2367 | } |
| 2368 | |
| 2369 | /** |
| 2370 | * @brief Reparse UnaryExpr. Logs directly on error. |
| 2371 | * |
| 2372 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 2373 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 2374 | * |
| 2375 | * @param[in] ctx Context for logging. |
| 2376 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2377 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2378 | * @return LY_ERR |
| 2379 | */ |
| 2380 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2381 | 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] | 2382 | { |
| 2383 | uint16_t prev_exp; |
| 2384 | LY_ERR rc; |
| 2385 | |
| 2386 | /* ('-')* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2387 | prev_exp = *tok_idx; |
| 2388 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2389 | && (exp->expr[exp->tok_pos[*tok_idx]] == '-')) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2390 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2391 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2392 | } |
| 2393 | |
| 2394 | /* PathExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2395 | prev_exp = *tok_idx; |
| 2396 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2397 | LY_CHECK_RET(rc); |
| 2398 | |
| 2399 | /* ('|' PathExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2400 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2401 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2402 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2403 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2404 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2405 | LY_CHECK_RET(rc); |
| 2406 | } |
| 2407 | |
| 2408 | return LY_SUCCESS; |
| 2409 | } |
| 2410 | |
| 2411 | /** |
| 2412 | * @brief Reparse AdditiveExpr. Logs directly on error. |
| 2413 | * |
| 2414 | * [15] AdditiveExpr ::= MultiplicativeExpr |
| 2415 | * | AdditiveExpr '+' MultiplicativeExpr |
| 2416 | * | AdditiveExpr '-' MultiplicativeExpr |
| 2417 | * [16] MultiplicativeExpr ::= UnaryExpr |
| 2418 | * | MultiplicativeExpr '*' UnaryExpr |
| 2419 | * | MultiplicativeExpr 'div' UnaryExpr |
| 2420 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 2421 | * |
| 2422 | * @param[in] ctx Context for logging. |
| 2423 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2424 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2425 | * @return LY_ERR |
| 2426 | */ |
| 2427 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2428 | 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] | 2429 | { |
| 2430 | uint16_t prev_add_exp, prev_mul_exp; |
| 2431 | LY_ERR rc; |
| 2432 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2433 | prev_add_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2434 | goto reparse_multiplicative_expr; |
| 2435 | |
| 2436 | /* ('+' / '-' MultiplicativeExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2437 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2438 | && ((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] | 2439 | exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2440 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2441 | |
| 2442 | reparse_multiplicative_expr: |
| 2443 | /* UnaryExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2444 | prev_mul_exp = *tok_idx; |
| 2445 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2446 | LY_CHECK_RET(rc); |
| 2447 | |
| 2448 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2449 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2450 | && ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2451 | exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2452 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2453 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 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 | } |
| 2458 | |
| 2459 | return LY_SUCCESS; |
| 2460 | } |
| 2461 | |
| 2462 | /** |
| 2463 | * @brief Reparse EqualityExpr. Logs directly on error. |
| 2464 | * |
| 2465 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
| 2466 | * | EqualityExpr '!=' RelationalExpr |
| 2467 | * [14] RelationalExpr ::= AdditiveExpr |
| 2468 | * | RelationalExpr '<' AdditiveExpr |
| 2469 | * | RelationalExpr '>' AdditiveExpr |
| 2470 | * | RelationalExpr '<=' AdditiveExpr |
| 2471 | * | RelationalExpr '>=' AdditiveExpr |
| 2472 | * |
| 2473 | * @param[in] ctx Context for logging. |
| 2474 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2475 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2476 | * @return LY_ERR |
| 2477 | */ |
| 2478 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2479 | 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] | 2480 | { |
| 2481 | uint16_t prev_eq_exp, prev_rel_exp; |
| 2482 | LY_ERR rc; |
| 2483 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2484 | prev_eq_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2485 | goto reparse_additive_expr; |
| 2486 | |
| 2487 | /* ('=' / '!=' RelationalExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2488 | 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] | 2489 | exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2490 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2491 | |
| 2492 | reparse_additive_expr: |
| 2493 | /* AdditiveExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2494 | prev_rel_exp = *tok_idx; |
| 2495 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2496 | LY_CHECK_RET(rc); |
| 2497 | |
| 2498 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2499 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2500 | exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2501 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2502 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2503 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2504 | LY_CHECK_RET(rc); |
| 2505 | } |
| 2506 | } |
| 2507 | |
| 2508 | return LY_SUCCESS; |
| 2509 | } |
| 2510 | |
| 2511 | /** |
| 2512 | * @brief Reparse OrExpr. Logs directly on error. |
| 2513 | * |
| 2514 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 2515 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 2516 | * |
| 2517 | * @param[in] ctx Context for logging. |
| 2518 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2519 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2520 | * @return LY_ERR |
| 2521 | */ |
| 2522 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2523 | 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] | 2524 | { |
| 2525 | uint16_t prev_or_exp, prev_and_exp; |
| 2526 | LY_ERR rc; |
| 2527 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2528 | prev_or_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2529 | goto reparse_equality_expr; |
| 2530 | |
| 2531 | /* ('or' AndExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2532 | 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] | 2533 | exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2534 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2535 | |
| 2536 | reparse_equality_expr: |
| 2537 | /* EqualityExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2538 | prev_and_exp = *tok_idx; |
| 2539 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2540 | LY_CHECK_RET(rc); |
| 2541 | |
| 2542 | /* ('and' EqualityExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2543 | 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] | 2544 | exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2545 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2546 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2547 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2548 | LY_CHECK_RET(rc); |
| 2549 | } |
| 2550 | } |
| 2551 | |
| 2552 | return LY_SUCCESS; |
| 2553 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2554 | |
| 2555 | /** |
| 2556 | * @brief Parse NCName. |
| 2557 | * |
| 2558 | * @param[in] ncname Name to parse. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2559 | * @return Length of @p ncname valid bytes. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2560 | */ |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2561 | static long int |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2562 | parse_ncname(const char *ncname) |
| 2563 | { |
| 2564 | unsigned int uc; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2565 | size_t size; |
| 2566 | long int len = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2567 | |
| 2568 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0); |
| 2569 | if (!is_xmlqnamestartchar(uc) || (uc == ':')) { |
| 2570 | return len; |
| 2571 | } |
| 2572 | |
| 2573 | do { |
| 2574 | len += size; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2575 | if (!*ncname) { |
| 2576 | break; |
| 2577 | } |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2578 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2579 | } while (is_xmlqnamechar(uc) && (uc != ':')); |
| 2580 | |
| 2581 | return len; |
| 2582 | } |
| 2583 | |
| 2584 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2585 | * @brief Add @p token into the expression @p exp. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2586 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2587 | * @param[in] ctx Context for logging. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2588 | * @param[in] exp Expression to use. |
| 2589 | * @param[in] token Token to add. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2590 | * @param[in] tok_pos Token position in the XPath expression. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2591 | * @param[in] tok_len Token length in the XPath expression. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2592 | * @return LY_ERR |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2593 | */ |
| 2594 | static LY_ERR |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2595 | 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] | 2596 | { |
| 2597 | uint32_t prev; |
| 2598 | |
| 2599 | if (exp->used == exp->size) { |
| 2600 | prev = exp->size; |
| 2601 | exp->size += LYXP_EXPR_SIZE_STEP; |
| 2602 | if (prev > exp->size) { |
| 2603 | LOGINT(ctx); |
| 2604 | return LY_EINT; |
| 2605 | } |
| 2606 | |
| 2607 | exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens); |
| 2608 | LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM); |
| 2609 | exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos); |
| 2610 | LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM); |
| 2611 | exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len); |
| 2612 | LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM); |
| 2613 | } |
| 2614 | |
| 2615 | exp->tokens[exp->used] = token; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2616 | exp->tok_pos[exp->used] = tok_pos; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2617 | exp->tok_len[exp->used] = tok_len; |
| 2618 | ++exp->used; |
| 2619 | return LY_SUCCESS; |
| 2620 | } |
| 2621 | |
| 2622 | void |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2623 | lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2624 | { |
| 2625 | uint16_t i; |
| 2626 | |
| 2627 | if (!expr) { |
| 2628 | return; |
| 2629 | } |
| 2630 | |
| 2631 | lydict_remove(ctx, expr->expr); |
| 2632 | free(expr->tokens); |
| 2633 | free(expr->tok_pos); |
| 2634 | free(expr->tok_len); |
| 2635 | if (expr->repeat) { |
| 2636 | for (i = 0; i < expr->used; ++i) { |
| 2637 | free(expr->repeat[i]); |
| 2638 | } |
| 2639 | } |
| 2640 | free(expr->repeat); |
| 2641 | free(expr); |
| 2642 | } |
| 2643 | |
| 2644 | struct lyxp_expr * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2645 | lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr, size_t expr_len, int reparse) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2646 | { |
| 2647 | struct lyxp_expr *ret; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2648 | size_t parsed = 0, tok_len; |
| 2649 | long int ncname_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2650 | enum lyxp_token tok_type; |
| 2651 | int prev_function_check = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2652 | uint16_t tok_idx = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2653 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2654 | if (!expr[0]) { |
| 2655 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 2656 | return NULL; |
| 2657 | } |
| 2658 | |
| 2659 | if (!expr_len) { |
| 2660 | expr_len = strlen(expr); |
| 2661 | } |
| 2662 | if (expr_len > UINT16_MAX) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2663 | LOGERR(ctx, LY_EINVAL, "XPath expression cannot be longer than %ud characters.", UINT16_MAX); |
| 2664 | return NULL; |
| 2665 | } |
| 2666 | |
| 2667 | /* init lyxp_expr structure */ |
| 2668 | ret = calloc(1, sizeof *ret); |
| 2669 | LY_CHECK_ERR_GOTO(!ret, LOGMEM(ctx), error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2670 | ret->expr = lydict_insert(ctx, expr, expr_len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2671 | LY_CHECK_ERR_GOTO(!ret->expr, LOGMEM(ctx), error); |
| 2672 | ret->used = 0; |
| 2673 | ret->size = LYXP_EXPR_SIZE_START; |
| 2674 | ret->tokens = malloc(ret->size * sizeof *ret->tokens); |
| 2675 | LY_CHECK_ERR_GOTO(!ret->tokens, LOGMEM(ctx), error); |
| 2676 | |
| 2677 | ret->tok_pos = malloc(ret->size * sizeof *ret->tok_pos); |
| 2678 | LY_CHECK_ERR_GOTO(!ret->tok_pos, LOGMEM(ctx), error); |
| 2679 | |
| 2680 | ret->tok_len = malloc(ret->size * sizeof *ret->tok_len); |
| 2681 | LY_CHECK_ERR_GOTO(!ret->tok_len, LOGMEM(ctx), error); |
| 2682 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2683 | /* make expr 0-terminated */ |
| 2684 | expr = ret->expr; |
| 2685 | |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2686 | while (is_xmlws(expr[parsed])) { |
| 2687 | ++parsed; |
| 2688 | } |
| 2689 | |
| 2690 | do { |
| 2691 | if (expr[parsed] == '(') { |
| 2692 | |
| 2693 | /* '(' */ |
| 2694 | tok_len = 1; |
| 2695 | tok_type = LYXP_TOKEN_PAR1; |
| 2696 | |
| 2697 | if (prev_function_check && ret->used && (ret->tokens[ret->used - 1] == LYXP_TOKEN_NAMETEST)) { |
| 2698 | /* it is a NodeType/FunctionName after all */ |
| 2699 | if (((ret->tok_len[ret->used - 1] == 4) |
| 2700 | && (!strncmp(&expr[ret->tok_pos[ret->used - 1]], "node", 4) |
| 2701 | || !strncmp(&expr[ret->tok_pos[ret->used - 1]], "text", 4))) || |
| 2702 | ((ret->tok_len[ret->used - 1] == 7) |
| 2703 | && !strncmp(&expr[ret->tok_pos[ret->used - 1]], "comment", 7))) { |
| 2704 | ret->tokens[ret->used - 1] = LYXP_TOKEN_NODETYPE; |
| 2705 | } else { |
| 2706 | ret->tokens[ret->used - 1] = LYXP_TOKEN_FUNCNAME; |
| 2707 | } |
| 2708 | prev_function_check = 0; |
| 2709 | } |
| 2710 | |
| 2711 | } else if (expr[parsed] == ')') { |
| 2712 | |
| 2713 | /* ')' */ |
| 2714 | tok_len = 1; |
| 2715 | tok_type = LYXP_TOKEN_PAR2; |
| 2716 | |
| 2717 | } else if (expr[parsed] == '[') { |
| 2718 | |
| 2719 | /* '[' */ |
| 2720 | tok_len = 1; |
| 2721 | tok_type = LYXP_TOKEN_BRACK1; |
| 2722 | |
| 2723 | } else if (expr[parsed] == ']') { |
| 2724 | |
| 2725 | /* ']' */ |
| 2726 | tok_len = 1; |
| 2727 | tok_type = LYXP_TOKEN_BRACK2; |
| 2728 | |
| 2729 | } else if (!strncmp(&expr[parsed], "..", 2)) { |
| 2730 | |
| 2731 | /* '..' */ |
| 2732 | tok_len = 2; |
| 2733 | tok_type = LYXP_TOKEN_DDOT; |
| 2734 | |
| 2735 | } else if ((expr[parsed] == '.') && (!isdigit(expr[parsed + 1]))) { |
| 2736 | |
| 2737 | /* '.' */ |
| 2738 | tok_len = 1; |
| 2739 | tok_type = LYXP_TOKEN_DOT; |
| 2740 | |
| 2741 | } else if (expr[parsed] == '@') { |
| 2742 | |
| 2743 | /* '@' */ |
| 2744 | tok_len = 1; |
| 2745 | tok_type = LYXP_TOKEN_AT; |
| 2746 | |
| 2747 | } else if (expr[parsed] == ',') { |
| 2748 | |
| 2749 | /* ',' */ |
| 2750 | tok_len = 1; |
| 2751 | tok_type = LYXP_TOKEN_COMMA; |
| 2752 | |
| 2753 | } else if (expr[parsed] == '\'') { |
| 2754 | |
| 2755 | /* Literal with ' */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2756 | for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\''); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2757 | LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0', |
| 2758 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error); |
| 2759 | ++tok_len; |
| 2760 | tok_type = LYXP_TOKEN_LITERAL; |
| 2761 | |
| 2762 | } else if (expr[parsed] == '\"') { |
| 2763 | |
| 2764 | /* Literal with " */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2765 | for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\"'); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2766 | LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0', |
| 2767 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error); |
| 2768 | ++tok_len; |
| 2769 | tok_type = LYXP_TOKEN_LITERAL; |
| 2770 | |
| 2771 | } else if ((expr[parsed] == '.') || (isdigit(expr[parsed]))) { |
| 2772 | |
| 2773 | /* Number */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2774 | for (tok_len = 0; isdigit(expr[parsed + tok_len]); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2775 | if (expr[parsed + tok_len] == '.') { |
| 2776 | ++tok_len; |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2777 | for (; isdigit(expr[parsed + tok_len]); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2778 | } |
| 2779 | tok_type = LYXP_TOKEN_NUMBER; |
| 2780 | |
| 2781 | } else if (expr[parsed] == '/') { |
| 2782 | |
| 2783 | /* Operator '/', '//' */ |
| 2784 | if (!strncmp(&expr[parsed], "//", 2)) { |
| 2785 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2786 | tok_type = LYXP_TOKEN_OPER_RPATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2787 | } else { |
| 2788 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2789 | tok_type = LYXP_TOKEN_OPER_PATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2790 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2791 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2792 | } else if (!strncmp(&expr[parsed], "!=", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2793 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2794 | /* Operator '!=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2795 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2796 | tok_type = LYXP_TOKEN_OPER_NEQUAL; |
| 2797 | |
| 2798 | } else if (!strncmp(&expr[parsed], "<=", 2) || !strncmp(&expr[parsed], ">=", 2)) { |
| 2799 | |
| 2800 | /* Operator '<=', '>=' */ |
| 2801 | tok_len = 2; |
| 2802 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2803 | |
| 2804 | } else if (expr[parsed] == '|') { |
| 2805 | |
| 2806 | /* Operator '|' */ |
| 2807 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2808 | tok_type = LYXP_TOKEN_OPER_UNI; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2809 | |
| 2810 | } else if ((expr[parsed] == '+') || (expr[parsed] == '-')) { |
| 2811 | |
| 2812 | /* Operator '+', '-' */ |
| 2813 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2814 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2815 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2816 | } else if (expr[parsed] == '=') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2817 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2818 | /* Operator '=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2819 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2820 | tok_type = LYXP_TOKEN_OPER_EQUAL; |
| 2821 | |
| 2822 | } else if ((expr[parsed] == '<') || (expr[parsed] == '>')) { |
| 2823 | |
| 2824 | /* Operator '<', '>' */ |
| 2825 | tok_len = 1; |
| 2826 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2827 | |
| 2828 | } else if (ret->used && (ret->tokens[ret->used - 1] != LYXP_TOKEN_AT) |
| 2829 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_PAR1) |
| 2830 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_BRACK1) |
| 2831 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_COMMA) |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2832 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_LOG) |
| 2833 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_EQUAL) |
| 2834 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_NEQUAL) |
| 2835 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_COMP) |
| 2836 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_MATH) |
| 2837 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_UNI) |
| 2838 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_PATH) |
| 2839 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_RPATH)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2840 | |
| 2841 | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
| 2842 | if (expr[parsed] == '*') { |
| 2843 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2844 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2845 | |
| 2846 | } else if (!strncmp(&expr[parsed], "or", 2)) { |
| 2847 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2848 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2849 | |
| 2850 | } else if (!strncmp(&expr[parsed], "and", 3)) { |
| 2851 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2852 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2853 | |
| 2854 | } else if (!strncmp(&expr[parsed], "mod", 3) || !strncmp(&expr[parsed], "div", 3)) { |
| 2855 | tok_len = 3; |
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 | |
| 2858 | } else if (prev_function_check) { |
Michal Vasko | 5307857 | 2019-05-24 08:50:15 +0200 | [diff] [blame] | 2859 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.", |
| 2860 | expr[parsed], expr[parsed], ret->tok_len[ret->used - 1], &ret->expr[ret->tok_pos[ret->used - 1]]); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2861 | goto error; |
| 2862 | } else { |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2863 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed + 1, expr); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2864 | goto error; |
| 2865 | } |
| 2866 | } else if (expr[parsed] == '*') { |
| 2867 | |
| 2868 | /* NameTest '*' */ |
| 2869 | tok_len = 1; |
| 2870 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2871 | |
| 2872 | } else { |
| 2873 | |
| 2874 | /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */ |
| 2875 | ncname_len = parse_ncname(&expr[parsed]); |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2876 | LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr), error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2877 | tok_len = ncname_len; |
| 2878 | |
| 2879 | if (expr[parsed + tok_len] == ':') { |
| 2880 | ++tok_len; |
| 2881 | if (expr[parsed + tok_len] == '*') { |
| 2882 | ++tok_len; |
| 2883 | } else { |
| 2884 | ncname_len = parse_ncname(&expr[parsed + tok_len]); |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2885 | LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr), error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2886 | tok_len += ncname_len; |
| 2887 | } |
| 2888 | /* remove old flag to prevent ambiguities */ |
| 2889 | prev_function_check = 0; |
| 2890 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2891 | } else { |
| 2892 | /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */ |
| 2893 | prev_function_check = 1; |
| 2894 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2895 | } |
| 2896 | } |
| 2897 | |
| 2898 | /* store the token, move on to the next one */ |
| 2899 | LY_CHECK_GOTO(exp_add_token(ctx, ret, tok_type, parsed, tok_len), error); |
| 2900 | parsed += tok_len; |
| 2901 | while (is_xmlws(expr[parsed])) { |
| 2902 | ++parsed; |
| 2903 | } |
| 2904 | |
| 2905 | } while (expr[parsed]); |
| 2906 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2907 | if (reparse) { |
| 2908 | /* prealloc repeat */ |
| 2909 | ret->repeat = calloc(ret->size, sizeof *ret->repeat); |
| 2910 | LY_CHECK_ERR_GOTO(!ret->repeat, LOGMEM(ctx), error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2911 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2912 | /* fill repeat */ |
| 2913 | LY_CHECK_GOTO(reparse_or_expr(ctx, ret, &tok_idx), error); |
| 2914 | if (ret->used > tok_idx) { |
| 2915 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.", |
| 2916 | &ret->expr[ret->tok_pos[tok_idx]]); |
| 2917 | goto error; |
| 2918 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2919 | } |
| 2920 | |
| 2921 | print_expr_struct_debug(ret); |
| 2922 | |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2923 | return ret; |
| 2924 | |
| 2925 | error: |
| 2926 | lyxp_expr_free(ctx, ret); |
| 2927 | return NULL; |
| 2928 | } |
| 2929 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2930 | struct lyxp_expr * |
| 2931 | lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp) |
| 2932 | { |
| 2933 | struct lyxp_expr *dup; |
| 2934 | uint32_t i, j; |
| 2935 | |
| 2936 | dup = calloc(1, sizeof *dup); |
| 2937 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx), error); |
| 2938 | |
| 2939 | dup->tokens = malloc(exp->used * sizeof *dup->tokens); |
| 2940 | LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx), error); |
| 2941 | memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens); |
| 2942 | |
| 2943 | dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos); |
| 2944 | LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx), error); |
| 2945 | memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos); |
| 2946 | |
| 2947 | dup->tok_len = malloc(exp->used * sizeof *dup->tok_len); |
| 2948 | LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx), error); |
| 2949 | memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len); |
| 2950 | |
| 2951 | dup->repeat = malloc(exp->used * sizeof *dup->repeat); |
| 2952 | LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx), error); |
| 2953 | for (i = 0; i < exp->used; ++i) { |
| 2954 | if (!exp->repeat[i]) { |
| 2955 | dup->repeat[i] = NULL; |
| 2956 | } else { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2957 | for (j = 0; exp->repeat[i][j]; ++j) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2958 | /* the ending 0 as well */ |
| 2959 | ++j; |
| 2960 | |
Michal Vasko | 99c7164 | 2020-07-03 13:33:36 +0200 | [diff] [blame] | 2961 | dup->repeat[i] = malloc(j * sizeof **dup->repeat); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2962 | LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx), error); |
| 2963 | memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat); |
| 2964 | dup->repeat[i][j - 1] = 0; |
| 2965 | } |
| 2966 | } |
| 2967 | |
| 2968 | dup->used = exp->used; |
| 2969 | dup->size = exp->used; |
| 2970 | dup->expr = lydict_insert(ctx, exp->expr, 0); |
| 2971 | |
| 2972 | return dup; |
| 2973 | |
| 2974 | error: |
| 2975 | lyxp_expr_free(ctx, dup); |
| 2976 | return NULL; |
| 2977 | } |
| 2978 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2979 | /* |
| 2980 | * warn functions |
| 2981 | * |
| 2982 | * Warn functions check specific reasonable conditions for schema XPath |
| 2983 | * and print a warning if they are not satisfied. |
| 2984 | */ |
| 2985 | |
| 2986 | /** |
| 2987 | * @brief Get the last-added schema node that is currently in the context. |
| 2988 | * |
| 2989 | * @param[in] set Set to search in. |
| 2990 | * @return Last-added schema context node, NULL if no node is in context. |
| 2991 | */ |
| 2992 | static struct lysc_node * |
| 2993 | warn_get_scnode_in_ctx(struct lyxp_set *set) |
| 2994 | { |
| 2995 | uint32_t i; |
| 2996 | |
| 2997 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
| 2998 | return NULL; |
| 2999 | } |
| 3000 | |
| 3001 | i = set->used; |
| 3002 | do { |
| 3003 | --i; |
| 3004 | if (set->val.scnodes[i].in_ctx == 1) { |
| 3005 | /* if there are more, simply return the first found (last added) */ |
| 3006 | return set->val.scnodes[i].scnode; |
| 3007 | } |
| 3008 | } while (i); |
| 3009 | |
| 3010 | return NULL; |
| 3011 | } |
| 3012 | |
| 3013 | /** |
| 3014 | * @brief Test whether a type is numeric - integer type or decimal64. |
| 3015 | * |
| 3016 | * @param[in] type Type to test. |
| 3017 | * @return 1 if numeric, 0 otherwise. |
| 3018 | */ |
| 3019 | static int |
| 3020 | warn_is_numeric_type(struct lysc_type *type) |
| 3021 | { |
| 3022 | struct lysc_type_union *uni; |
| 3023 | int ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3024 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3025 | |
| 3026 | switch (type->basetype) { |
| 3027 | case LY_TYPE_DEC64: |
| 3028 | case LY_TYPE_INT8: |
| 3029 | case LY_TYPE_UINT8: |
| 3030 | case LY_TYPE_INT16: |
| 3031 | case LY_TYPE_UINT16: |
| 3032 | case LY_TYPE_INT32: |
| 3033 | case LY_TYPE_UINT32: |
| 3034 | case LY_TYPE_INT64: |
| 3035 | case LY_TYPE_UINT64: |
| 3036 | return 1; |
| 3037 | case LY_TYPE_UNION: |
| 3038 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3039 | LY_ARRAY_FOR(uni->types, u) { |
| 3040 | ret = warn_is_numeric_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3041 | if (ret) { |
| 3042 | /* found a suitable type */ |
| 3043 | return 1; |
| 3044 | } |
| 3045 | } |
| 3046 | /* did not find any suitable type */ |
| 3047 | return 0; |
| 3048 | case LY_TYPE_LEAFREF: |
| 3049 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
| 3050 | default: |
| 3051 | return 0; |
| 3052 | } |
| 3053 | } |
| 3054 | |
| 3055 | /** |
| 3056 | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
| 3057 | * |
| 3058 | * @param[in] type Type to test. |
| 3059 | * @return 1 if string, 0 otherwise. |
| 3060 | */ |
| 3061 | static int |
| 3062 | warn_is_string_type(struct lysc_type *type) |
| 3063 | { |
| 3064 | struct lysc_type_union *uni; |
| 3065 | int ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3066 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3067 | |
| 3068 | switch (type->basetype) { |
| 3069 | case LY_TYPE_BITS: |
| 3070 | case LY_TYPE_ENUM: |
| 3071 | case LY_TYPE_IDENT: |
| 3072 | case LY_TYPE_INST: |
| 3073 | case LY_TYPE_STRING: |
| 3074 | return 1; |
| 3075 | case LY_TYPE_UNION: |
| 3076 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3077 | LY_ARRAY_FOR(uni->types, u) { |
| 3078 | ret = warn_is_string_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3079 | if (ret) { |
| 3080 | /* found a suitable type */ |
| 3081 | return 1; |
| 3082 | } |
| 3083 | } |
| 3084 | /* did not find any suitable type */ |
| 3085 | return 0; |
| 3086 | case LY_TYPE_LEAFREF: |
| 3087 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
| 3088 | default: |
| 3089 | return 0; |
| 3090 | } |
| 3091 | } |
| 3092 | |
| 3093 | /** |
| 3094 | * @brief Test whether a type is one specific type. |
| 3095 | * |
| 3096 | * @param[in] type Type to test. |
| 3097 | * @param[in] base Expected type. |
| 3098 | * @return 1 if it is, 0 otherwise. |
| 3099 | */ |
| 3100 | static int |
| 3101 | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
| 3102 | { |
| 3103 | struct lysc_type_union *uni; |
| 3104 | int ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3105 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3106 | |
| 3107 | if (type->basetype == base) { |
| 3108 | return 1; |
| 3109 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3110 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3111 | LY_ARRAY_FOR(uni->types, u) { |
| 3112 | ret = warn_is_specific_type(uni->types[u], base); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3113 | if (ret) { |
| 3114 | /* found a suitable type */ |
| 3115 | return 1; |
| 3116 | } |
| 3117 | } |
| 3118 | /* did not find any suitable type */ |
| 3119 | return 0; |
| 3120 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
| 3121 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
| 3122 | } |
| 3123 | |
| 3124 | return 0; |
| 3125 | } |
| 3126 | |
| 3127 | /** |
| 3128 | * @brief Get next type of a (union) type. |
| 3129 | * |
| 3130 | * @param[in] type Base type. |
| 3131 | * @param[in] prev_type Previously returned type. |
| 3132 | * @return Next type or NULL. |
| 3133 | */ |
| 3134 | static struct lysc_type * |
| 3135 | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type) |
| 3136 | { |
| 3137 | struct lysc_type_union *uni; |
| 3138 | int found = 0; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3139 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3140 | |
| 3141 | switch (type->basetype) { |
| 3142 | case LY_TYPE_UNION: |
| 3143 | uni = (struct lysc_type_union *)type; |
| 3144 | if (!prev_type) { |
| 3145 | return uni->types[0]; |
| 3146 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3147 | LY_ARRAY_FOR(uni->types, u) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3148 | if (found) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3149 | return uni->types[u]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3150 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3151 | if (prev_type == uni->types[u]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3152 | found = 1; |
| 3153 | } |
| 3154 | } |
| 3155 | return NULL; |
| 3156 | default: |
| 3157 | if (prev_type) { |
| 3158 | assert(type == prev_type); |
| 3159 | return NULL; |
| 3160 | } else { |
| 3161 | return type; |
| 3162 | } |
| 3163 | } |
| 3164 | } |
| 3165 | |
| 3166 | /** |
| 3167 | * @brief Test whether 2 types have a common type. |
| 3168 | * |
| 3169 | * @param[in] type1 First type. |
| 3170 | * @param[in] type2 Second type. |
| 3171 | * @return 1 if they do, 0 otherwise. |
| 3172 | */ |
| 3173 | static int |
| 3174 | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
| 3175 | { |
| 3176 | struct lysc_type *t1, *rt1, *t2, *rt2; |
| 3177 | |
| 3178 | t1 = NULL; |
| 3179 | while ((t1 = warn_is_equal_type_next_type(type1, t1))) { |
| 3180 | if (t1->basetype == LY_TYPE_LEAFREF) { |
| 3181 | rt1 = ((struct lysc_type_leafref *)t1)->realtype; |
| 3182 | } else { |
| 3183 | rt1 = t1; |
| 3184 | } |
| 3185 | |
| 3186 | t2 = NULL; |
| 3187 | while ((t2 = warn_is_equal_type_next_type(type2, t2))) { |
| 3188 | if (t2->basetype == LY_TYPE_LEAFREF) { |
| 3189 | rt2 = ((struct lysc_type_leafref *)t2)->realtype; |
| 3190 | } else { |
| 3191 | rt2 = t2; |
| 3192 | } |
| 3193 | |
| 3194 | if (rt2->basetype == rt1->basetype) { |
| 3195 | /* match found */ |
| 3196 | return 1; |
| 3197 | } |
| 3198 | } |
| 3199 | } |
| 3200 | |
| 3201 | return 0; |
| 3202 | } |
| 3203 | |
| 3204 | /** |
| 3205 | * @brief Check both operands of comparison operators. |
| 3206 | * |
| 3207 | * @param[in] ctx Context for errors. |
| 3208 | * @param[in] set1 First operand set. |
| 3209 | * @param[in] set2 Second operand set. |
| 3210 | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
| 3211 | * @param[in] expr Start of the expression to print with the warning. |
| 3212 | * @param[in] tok_pos Token position. |
| 3213 | */ |
| 3214 | static void |
| 3215 | warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, int numbers_only, const char *expr, uint16_t tok_pos) |
| 3216 | { |
| 3217 | struct lysc_node_leaf *node1, *node2; |
| 3218 | int leaves = 1, warning = 0; |
| 3219 | |
| 3220 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
| 3221 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
| 3222 | |
| 3223 | if (!node1 && !node2) { |
| 3224 | /* no node-sets involved, nothing to do */ |
| 3225 | return; |
| 3226 | } |
| 3227 | |
| 3228 | if (node1) { |
| 3229 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3230 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
| 3231 | warning = 1; |
| 3232 | leaves = 0; |
| 3233 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
| 3234 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
| 3235 | warning = 1; |
| 3236 | } |
| 3237 | } |
| 3238 | |
| 3239 | if (node2) { |
| 3240 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3241 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
| 3242 | warning = 1; |
| 3243 | leaves = 0; |
| 3244 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
| 3245 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
| 3246 | warning = 1; |
| 3247 | } |
| 3248 | } |
| 3249 | |
| 3250 | if (node1 && node2 && leaves && !numbers_only) { |
| 3251 | if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) |
| 3252 | || (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) |
| 3253 | || (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) |
| 3254 | && !warn_is_equal_type(node1->type, node2->type))) { |
| 3255 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
| 3256 | warning = 1; |
| 3257 | } |
| 3258 | } |
| 3259 | |
| 3260 | if (warning) { |
| 3261 | LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos); |
| 3262 | } |
| 3263 | } |
| 3264 | |
| 3265 | /** |
| 3266 | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
| 3267 | * |
| 3268 | * @param[in] exp Parsed XPath expression. |
| 3269 | * @param[in] set Set with the leaf/leaf-list. |
| 3270 | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
| 3271 | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
| 3272 | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
| 3273 | */ |
| 3274 | static void |
| 3275 | warn_equality_value(struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, uint16_t last_equal_exp) |
| 3276 | { |
| 3277 | struct lysc_node *scnode; |
| 3278 | struct lysc_type *type; |
| 3279 | char *value; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3280 | struct lyd_value storage; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3281 | LY_ERR rc; |
| 3282 | struct ly_err_item *err = NULL; |
| 3283 | |
| 3284 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 3285 | && ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) { |
| 3286 | /* check that the node can have the specified value */ |
| 3287 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
| 3288 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
| 3289 | } else { |
| 3290 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
| 3291 | } |
| 3292 | if (!value) { |
| 3293 | LOGMEM(set->ctx); |
| 3294 | return; |
| 3295 | } |
| 3296 | |
| 3297 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
| 3298 | LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding" |
| 3299 | " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value); |
| 3300 | 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^] | 3301 | (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp], |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3302 | exp->expr + exp->tok_pos[equal_exp]); |
| 3303 | } |
| 3304 | |
| 3305 | type = ((struct lysc_node_leaf *)scnode)->type; |
| 3306 | if (type->basetype != LY_TYPE_IDENT) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 3307 | rc = type->plugin->store(set->ctx, type, value, strlen(value), LY_TYPE_OPTS_SCHEMA, LY_PREF_SCHEMA, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame^] | 3308 | (void *)set->local_mod, NULL, NULL, &storage, &err); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3309 | |
| 3310 | if (err) { |
| 3311 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
| 3312 | ly_err_free(err); |
| 3313 | } else if (rc != LY_SUCCESS) { |
| 3314 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
| 3315 | } |
| 3316 | if (rc != LY_SUCCESS) { |
| 3317 | 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^] | 3318 | (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp], |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3319 | exp->expr + exp->tok_pos[equal_exp]); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3320 | } else { |
| 3321 | type->plugin->free(set->ctx, &storage); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3322 | } |
| 3323 | } |
| 3324 | free(value); |
| 3325 | } |
| 3326 | } |
| 3327 | |
| 3328 | /* |
| 3329 | * XPath functions |
| 3330 | */ |
| 3331 | |
| 3332 | /** |
| 3333 | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
| 3334 | * depending on whether the first node bit value from the second argument is set. |
| 3335 | * |
| 3336 | * @param[in] args Array of arguments. |
| 3337 | * @param[in] arg_count Count of elements in @p args. |
| 3338 | * @param[in,out] set Context and result set at the same time. |
| 3339 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3340 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3341 | */ |
| 3342 | static LY_ERR |
| 3343 | xpath_bit_is_set(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3344 | { |
| 3345 | struct lyd_node_term *leaf; |
| 3346 | struct lysc_node_leaf *sleaf; |
| 3347 | struct lysc_type_bits *bits; |
| 3348 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3349 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3350 | |
| 3351 | if (options & LYXP_SCNODE_ALL) { |
| 3352 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3353 | 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] | 3354 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3355 | 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] | 3356 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
| 3357 | 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] | 3358 | } |
| 3359 | |
| 3360 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3361 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3362 | 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] | 3363 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3364 | 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] | 3365 | } |
| 3366 | } |
| 3367 | set_scnode_clear_ctx(set); |
| 3368 | return rc; |
| 3369 | } |
| 3370 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3371 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3372 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "bit-is-set(node-set, string)"); |
| 3373 | return LY_EVALID; |
| 3374 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3375 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3376 | LY_CHECK_RET(rc); |
| 3377 | |
| 3378 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3379 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3380 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3381 | if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 3382 | && (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) { |
| 3383 | bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3384 | LY_ARRAY_FOR(bits->bits, u) { |
| 3385 | if (!strcmp(bits->bits[u].name, args[1]->val.str)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3386 | set_fill_boolean(set, 1); |
| 3387 | break; |
| 3388 | } |
| 3389 | } |
| 3390 | } |
| 3391 | } |
| 3392 | |
| 3393 | return LY_SUCCESS; |
| 3394 | } |
| 3395 | |
| 3396 | /** |
| 3397 | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
| 3398 | * with the argument converted to boolean. |
| 3399 | * |
| 3400 | * @param[in] args Array of arguments. |
| 3401 | * @param[in] arg_count Count of elements in @p args. |
| 3402 | * @param[in,out] set Context and result set at the same time. |
| 3403 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3404 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3405 | */ |
| 3406 | static LY_ERR |
| 3407 | xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3408 | { |
| 3409 | LY_ERR rc; |
| 3410 | |
| 3411 | if (options & LYXP_SCNODE_ALL) { |
| 3412 | set_scnode_clear_ctx(set); |
| 3413 | return LY_SUCCESS; |
| 3414 | } |
| 3415 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3416 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3417 | LY_CHECK_RET(rc); |
| 3418 | set_fill_set(set, args[0]); |
| 3419 | |
| 3420 | return LY_SUCCESS; |
| 3421 | } |
| 3422 | |
| 3423 | /** |
| 3424 | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
| 3425 | * with the first argument rounded up to the nearest integer. |
| 3426 | * |
| 3427 | * @param[in] args Array of arguments. |
| 3428 | * @param[in] arg_count Count of elements in @p args. |
| 3429 | * @param[in,out] set Context and result set at the same time. |
| 3430 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3431 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3432 | */ |
| 3433 | static LY_ERR |
| 3434 | xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3435 | { |
| 3436 | struct lysc_node_leaf *sleaf; |
| 3437 | LY_ERR rc = LY_SUCCESS; |
| 3438 | |
| 3439 | if (options & LYXP_SCNODE_ALL) { |
| 3440 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3441 | 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] | 3442 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3443 | 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] | 3444 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 3445 | 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] | 3446 | } |
| 3447 | set_scnode_clear_ctx(set); |
| 3448 | return rc; |
| 3449 | } |
| 3450 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3451 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3452 | LY_CHECK_RET(rc); |
| 3453 | if ((long long)args[0]->val.num != args[0]->val.num) { |
| 3454 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
| 3455 | } else { |
| 3456 | set_fill_number(set, args[0]->val.num); |
| 3457 | } |
| 3458 | |
| 3459 | return LY_SUCCESS; |
| 3460 | } |
| 3461 | |
| 3462 | /** |
| 3463 | * @brief Execute the XPath concat(string, string, string*) function. |
| 3464 | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
| 3465 | * |
| 3466 | * @param[in] args Array of arguments. |
| 3467 | * @param[in] arg_count Count of elements in @p args. |
| 3468 | * @param[in,out] set Context and result set at the same time. |
| 3469 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3470 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3471 | */ |
| 3472 | static LY_ERR |
| 3473 | xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 3474 | { |
| 3475 | uint16_t i; |
| 3476 | char *str = NULL; |
| 3477 | size_t used = 1; |
| 3478 | LY_ERR rc = LY_SUCCESS; |
| 3479 | struct lysc_node_leaf *sleaf; |
| 3480 | |
| 3481 | if (options & LYXP_SCNODE_ALL) { |
| 3482 | for (i = 0; i < arg_count; ++i) { |
| 3483 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
| 3484 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3485 | LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".", |
| 3486 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3487 | } else if (!warn_is_string_type(sleaf->type)) { |
Radek Krejci | 70124c8 | 2020-08-14 22:17:03 +0200 | [diff] [blame] | 3488 | 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] | 3489 | } |
| 3490 | } |
| 3491 | } |
| 3492 | set_scnode_clear_ctx(set); |
| 3493 | return rc; |
| 3494 | } |
| 3495 | |
| 3496 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3497 | rc = lyxp_set_cast(args[i], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3498 | if (rc != LY_SUCCESS) { |
| 3499 | free(str); |
| 3500 | return rc; |
| 3501 | } |
| 3502 | |
| 3503 | str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char)); |
| 3504 | LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM); |
| 3505 | strcpy(str + used - 1, args[i]->val.str); |
| 3506 | used += strlen(args[i]->val.str); |
| 3507 | } |
| 3508 | |
| 3509 | /* free, kind of */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3510 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3511 | set->type = LYXP_SET_STRING; |
| 3512 | set->val.str = str; |
| 3513 | |
| 3514 | return LY_SUCCESS; |
| 3515 | } |
| 3516 | |
| 3517 | /** |
| 3518 | * @brief Execute the XPath contains(string, string) function. |
| 3519 | * Returns LYXP_SET_BOOLEAN whether the second argument can |
| 3520 | * be found in the first or not. |
| 3521 | * |
| 3522 | * @param[in] args Array of arguments. |
| 3523 | * @param[in] arg_count Count of elements in @p args. |
| 3524 | * @param[in,out] set Context and result set at the same time. |
| 3525 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3526 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3527 | */ |
| 3528 | static LY_ERR |
| 3529 | xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3530 | { |
| 3531 | struct lysc_node_leaf *sleaf; |
| 3532 | LY_ERR rc = LY_SUCCESS; |
| 3533 | |
| 3534 | if (options & LYXP_SCNODE_ALL) { |
| 3535 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3536 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3537 | 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] | 3538 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3539 | 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] | 3540 | } |
| 3541 | } |
| 3542 | |
| 3543 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3544 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3545 | 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] | 3546 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3547 | 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] | 3548 | } |
| 3549 | } |
| 3550 | set_scnode_clear_ctx(set); |
| 3551 | return rc; |
| 3552 | } |
| 3553 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3554 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3555 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3556 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3557 | LY_CHECK_RET(rc); |
| 3558 | |
| 3559 | if (strstr(args[0]->val.str, args[1]->val.str)) { |
| 3560 | set_fill_boolean(set, 1); |
| 3561 | } else { |
| 3562 | set_fill_boolean(set, 0); |
| 3563 | } |
| 3564 | |
| 3565 | return LY_SUCCESS; |
| 3566 | } |
| 3567 | |
| 3568 | /** |
| 3569 | * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER |
| 3570 | * with the size of the node-set from the argument. |
| 3571 | * |
| 3572 | * @param[in] args Array of arguments. |
| 3573 | * @param[in] arg_count Count of elements in @p args. |
| 3574 | * @param[in,out] set Context and result set at the same time. |
| 3575 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3576 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3577 | */ |
| 3578 | static LY_ERR |
| 3579 | xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3580 | { |
| 3581 | struct lysc_node *scnode = NULL; |
| 3582 | LY_ERR rc = LY_SUCCESS; |
| 3583 | |
| 3584 | if (options & LYXP_SCNODE_ALL) { |
| 3585 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) { |
| 3586 | 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] | 3587 | } |
| 3588 | set_scnode_clear_ctx(set); |
| 3589 | return rc; |
| 3590 | } |
| 3591 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3592 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 3593 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)"); |
| 3594 | return LY_EVALID; |
| 3595 | } |
| 3596 | |
| 3597 | set_fill_number(set, args[0]->used); |
| 3598 | return LY_SUCCESS; |
| 3599 | } |
| 3600 | |
| 3601 | /** |
| 3602 | * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET |
| 3603 | * with the context with the intial node. |
| 3604 | * |
| 3605 | * @param[in] args Array of arguments. |
| 3606 | * @param[in] arg_count Count of elements in @p args. |
| 3607 | * @param[in,out] set Context and result set at the same time. |
| 3608 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3609 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3610 | */ |
| 3611 | static LY_ERR |
| 3612 | xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 3613 | { |
| 3614 | if (arg_count || args) { |
| 3615 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()"); |
| 3616 | return LY_EVALID; |
| 3617 | } |
| 3618 | |
| 3619 | if (options & LYXP_SCNODE_ALL) { |
| 3620 | set_scnode_clear_ctx(set); |
| 3621 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 3622 | lyxp_set_scnode_insert_node(set, set->ctx_scnode, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3623 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3624 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3625 | |
| 3626 | /* position is filled later */ |
| 3627 | set_insert_node(set, set->ctx_node, 0, LYXP_NODE_ELEM, 0); |
| 3628 | } |
| 3629 | |
| 3630 | return LY_SUCCESS; |
| 3631 | } |
| 3632 | |
| 3633 | /** |
| 3634 | * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either |
| 3635 | * leafref or instance-identifier target node(s). |
| 3636 | * |
| 3637 | * @param[in] args Array of arguments. |
| 3638 | * @param[in] arg_count Count of elements in @p args. |
| 3639 | * @param[in,out] set Context and result set at the same time. |
| 3640 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3641 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3642 | */ |
| 3643 | static LY_ERR |
| 3644 | xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3645 | { |
| 3646 | struct lyd_node_term *leaf; |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3647 | struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3648 | struct lysc_type_leafref *lref; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3649 | const struct lysc_node *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3650 | struct ly_path *p; |
| 3651 | struct lyd_node *node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3652 | char *errmsg = NULL; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3653 | uint8_t oper; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3654 | LY_ERR rc = LY_SUCCESS; |
| 3655 | |
| 3656 | if (options & LYXP_SCNODE_ALL) { |
| 3657 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3658 | 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] | 3659 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3660 | 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] | 3661 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
| 3662 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".", |
| 3663 | __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3664 | } |
| 3665 | set_scnode_clear_ctx(set); |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3666 | if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3667 | lref = (struct lysc_type_leafref *)sleaf->type; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3668 | 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] | 3669 | |
| 3670 | /* it was already evaluated on schema, it must succeed */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 3671 | rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE, |
| 3672 | oper, LY_PATH_TARGET_MANY, set->format, lref->path_context, &p); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3673 | assert(!rc); |
| 3674 | |
| 3675 | /* get the target node */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3676 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3677 | ly_path_free(set->ctx, p); |
| 3678 | |
| 3679 | lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM); |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3680 | } |
| 3681 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3682 | return rc; |
| 3683 | } |
| 3684 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3685 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3686 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)"); |
| 3687 | return LY_EVALID; |
| 3688 | } |
| 3689 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3690 | lyxp_set_free_content(set); |
| 3691 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3692 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3693 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3694 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 3695 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3696 | /* find leafref target */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3697 | if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf, |
| 3698 | &leaf->value, set->tree, &node, &errmsg)) { |
| 3699 | LOGERR(set->ctx, LY_EVALID, errmsg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3700 | free(errmsg); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3701 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3702 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3703 | } else { |
| 3704 | assert(sleaf->type->basetype == LY_TYPE_INST); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3705 | if (ly_path_eval(leaf->value.target, set->tree, &node)) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3706 | LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.", |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 3707 | LYD_CANON_VALUE(leaf)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3708 | return LY_EVALID; |
| 3709 | } |
| 3710 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3711 | |
| 3712 | /* insert it */ |
| 3713 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3714 | } |
| 3715 | } |
| 3716 | |
| 3717 | return LY_SUCCESS; |
| 3718 | } |
| 3719 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3720 | static LY_ERR |
| 3721 | xpath_derived_(struct lyxp_set **args, struct lyxp_set *set, int options, int self_match, const char *func) |
| 3722 | { |
| 3723 | uint16_t i; |
| 3724 | LY_ARRAY_COUNT_TYPE u; |
| 3725 | struct lyd_node_term *leaf; |
| 3726 | struct lysc_node_leaf *sleaf; |
| 3727 | struct lyd_meta *meta; |
| 3728 | struct lyd_value data = {0}, *val; |
| 3729 | struct ly_err_item *err = NULL; |
| 3730 | LY_ERR rc = LY_SUCCESS; |
| 3731 | int found; |
| 3732 | |
| 3733 | if (options & LYXP_SCNODE_ALL) { |
| 3734 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3735 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func); |
| 3736 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3737 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
| 3738 | sleaf->name); |
| 3739 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 3740 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name); |
| 3741 | } |
| 3742 | |
| 3743 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3744 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3745 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
| 3746 | sleaf->name); |
| 3747 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3748 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name); |
| 3749 | } |
| 3750 | } |
| 3751 | set_scnode_clear_ctx(set); |
| 3752 | return rc; |
| 3753 | } |
| 3754 | |
| 3755 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 3756 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 3757 | "derived-from(-or-self)(node-set, string)"); |
| 3758 | return LY_EVALID; |
| 3759 | } |
| 3760 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
| 3761 | LY_CHECK_RET(rc); |
| 3762 | |
| 3763 | set_fill_boolean(set, 0); |
| 3764 | found = 0; |
| 3765 | for (i = 0; i < args[0]->used; ++i) { |
| 3766 | if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) { |
| 3767 | continue; |
| 3768 | } |
| 3769 | |
| 3770 | if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 3771 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 3772 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3773 | val = &leaf->value; |
| 3774 | if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) { |
| 3775 | /* uninteresting */ |
| 3776 | continue; |
| 3777 | } |
| 3778 | |
| 3779 | /* store args[1] as ident */ |
| 3780 | rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str), |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3781 | 0, set->format, (void *)set->local_mod, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame^] | 3782 | (struct lyd_node *)leaf, set->tree, &data, &err); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3783 | } else { |
| 3784 | meta = args[0]->val.meta[i].meta; |
| 3785 | val = &meta->value; |
| 3786 | if (val->realtype->basetype != LY_TYPE_IDENT) { |
| 3787 | /* uninteresting */ |
| 3788 | continue; |
| 3789 | } |
| 3790 | |
| 3791 | /* store args[1] as ident */ |
| 3792 | rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str), |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3793 | 0, set->format, (void *)meta->annotation->module, |
| 3794 | meta->parent, set->tree, &data, &err); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3795 | } |
| 3796 | |
| 3797 | if (err) { |
| 3798 | ly_err_print(err); |
| 3799 | ly_err_free(err); |
| 3800 | } |
| 3801 | LY_CHECK_RET(rc); |
| 3802 | |
| 3803 | /* finally check the identity itself */ |
| 3804 | if (self_match && (data.ident == val->ident)) { |
| 3805 | set_fill_boolean(set, 1); |
| 3806 | found = 1; |
| 3807 | } |
| 3808 | if (!found) { |
| 3809 | LY_ARRAY_FOR(data.ident->derived, u) { |
| 3810 | if (data.ident->derived[u] == val->ident) { |
| 3811 | set_fill_boolean(set, 1); |
| 3812 | found = 1; |
| 3813 | break; |
| 3814 | } |
| 3815 | } |
| 3816 | } |
| 3817 | |
| 3818 | /* free temporary value */ |
| 3819 | val->realtype->plugin->free(set->ctx, &data); |
| 3820 | if (found) { |
| 3821 | break; |
| 3822 | } |
| 3823 | } |
| 3824 | |
| 3825 | return LY_SUCCESS; |
| 3826 | } |
| 3827 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3828 | /** |
| 3829 | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3830 | * on whether the first argument nodes contain a node of an identity derived from the second |
| 3831 | * argument identity. |
| 3832 | * |
| 3833 | * @param[in] args Array of arguments. |
| 3834 | * @param[in] arg_count Count of elements in @p args. |
| 3835 | * @param[in,out] set Context and result set at the same time. |
| 3836 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3837 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3838 | */ |
| 3839 | static LY_ERR |
| 3840 | xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3841 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3842 | return xpath_derived_(args, set, options, 0, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3843 | } |
| 3844 | |
| 3845 | /** |
| 3846 | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3847 | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
| 3848 | * the second argument identity. |
| 3849 | * |
| 3850 | * @param[in] args Array of arguments. |
| 3851 | * @param[in] arg_count Count of elements in @p args. |
| 3852 | * @param[in,out] set Context and result set at the same time. |
| 3853 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3854 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3855 | */ |
| 3856 | static LY_ERR |
| 3857 | xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3858 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3859 | return xpath_derived_(args, set, options, 1, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3860 | } |
| 3861 | |
| 3862 | /** |
| 3863 | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
| 3864 | * with the integer value of the first node's enum value, otherwise NaN. |
| 3865 | * |
| 3866 | * @param[in] args Array of arguments. |
| 3867 | * @param[in] arg_count Count of elements in @p args. |
| 3868 | * @param[in,out] set Context and result set at the same time. |
| 3869 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3870 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3871 | */ |
| 3872 | static LY_ERR |
| 3873 | xpath_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3874 | { |
| 3875 | struct lyd_node_term *leaf; |
| 3876 | struct lysc_node_leaf *sleaf; |
| 3877 | LY_ERR rc = LY_SUCCESS; |
| 3878 | |
| 3879 | if (options & LYXP_SCNODE_ALL) { |
| 3880 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3881 | 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] | 3882 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3883 | 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] | 3884 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
| 3885 | 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] | 3886 | } |
| 3887 | set_scnode_clear_ctx(set); |
| 3888 | return rc; |
| 3889 | } |
| 3890 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3891 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3892 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)"); |
| 3893 | return LY_EVALID; |
| 3894 | } |
| 3895 | |
| 3896 | set_fill_number(set, NAN); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3897 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3898 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3899 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3900 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
| 3901 | set_fill_number(set, leaf->value.enum_item->value); |
| 3902 | } |
| 3903 | } |
| 3904 | |
| 3905 | return LY_SUCCESS; |
| 3906 | } |
| 3907 | |
| 3908 | /** |
| 3909 | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
| 3910 | * with false value. |
| 3911 | * |
| 3912 | * @param[in] args Array of arguments. |
| 3913 | * @param[in] arg_count Count of elements in @p args. |
| 3914 | * @param[in,out] set Context and result set at the same time. |
| 3915 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3916 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3917 | */ |
| 3918 | static LY_ERR |
| 3919 | xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3920 | { |
| 3921 | if (options & LYXP_SCNODE_ALL) { |
| 3922 | set_scnode_clear_ctx(set); |
| 3923 | return LY_SUCCESS; |
| 3924 | } |
| 3925 | |
| 3926 | set_fill_boolean(set, 0); |
| 3927 | return LY_SUCCESS; |
| 3928 | } |
| 3929 | |
| 3930 | /** |
| 3931 | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
| 3932 | * with the first argument floored (truncated). |
| 3933 | * |
| 3934 | * @param[in] args Array of arguments. |
| 3935 | * @param[in] arg_count Count of elements in @p args. |
| 3936 | * @param[in,out] set Context and result set at the same time. |
| 3937 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3938 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3939 | */ |
| 3940 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3941 | xpath_floor(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int UNUSED(options)) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3942 | { |
| 3943 | LY_ERR rc; |
| 3944 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3945 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3946 | LY_CHECK_RET(rc); |
| 3947 | if (isfinite(args[0]->val.num)) { |
| 3948 | set_fill_number(set, (long long)args[0]->val.num); |
| 3949 | } |
| 3950 | |
| 3951 | return LY_SUCCESS; |
| 3952 | } |
| 3953 | |
| 3954 | /** |
| 3955 | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
| 3956 | * whether the language of the text matches the one from the argument. |
| 3957 | * |
| 3958 | * @param[in] args Array of arguments. |
| 3959 | * @param[in] arg_count Count of elements in @p args. |
| 3960 | * @param[in,out] set Context and result set at the same time. |
| 3961 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3962 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3963 | */ |
| 3964 | static LY_ERR |
| 3965 | xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3966 | { |
| 3967 | const struct lyd_node *node; |
| 3968 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 3969 | struct lyd_meta *meta = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3970 | const char *val; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3971 | int i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3972 | LY_ERR rc = LY_SUCCESS; |
| 3973 | |
| 3974 | if (options & LYXP_SCNODE_ALL) { |
| 3975 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3976 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3977 | 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] | 3978 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3979 | 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] | 3980 | } |
| 3981 | } |
| 3982 | set_scnode_clear_ctx(set); |
| 3983 | return rc; |
| 3984 | } |
| 3985 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3986 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3987 | LY_CHECK_RET(rc); |
| 3988 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3989 | if (set->type != LYXP_SET_NODE_SET) { |
| 3990 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)"); |
| 3991 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3992 | } else if (!set->used) { |
| 3993 | set_fill_boolean(set, 0); |
| 3994 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3995 | } |
| 3996 | |
| 3997 | switch (set->val.nodes[0].type) { |
| 3998 | case LYXP_NODE_ELEM: |
| 3999 | case LYXP_NODE_TEXT: |
| 4000 | node = set->val.nodes[0].node; |
| 4001 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4002 | case LYXP_NODE_META: |
| 4003 | node = set->val.meta[0].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4004 | break; |
| 4005 | default: |
| 4006 | /* nothing to do with roots */ |
| 4007 | set_fill_boolean(set, 0); |
| 4008 | return LY_SUCCESS; |
| 4009 | } |
| 4010 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4011 | /* find lang metadata */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4012 | for (; node; node = (struct lyd_node *)node->parent) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4013 | for (meta = node->meta; meta; meta = meta->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4014 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4015 | 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] | 4016 | break; |
| 4017 | } |
| 4018 | } |
| 4019 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4020 | if (meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4021 | break; |
| 4022 | } |
| 4023 | } |
| 4024 | |
| 4025 | /* compare languages */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4026 | if (!meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4027 | set_fill_boolean(set, 0); |
| 4028 | } else { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4029 | val = meta->value.canonical; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4030 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 4031 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
| 4032 | set_fill_boolean(set, 0); |
| 4033 | break; |
| 4034 | } |
| 4035 | } |
| 4036 | if (!args[0]->val.str[i]) { |
| 4037 | if (!val[i] || (val[i] == '-')) { |
| 4038 | set_fill_boolean(set, 1); |
| 4039 | } else { |
| 4040 | set_fill_boolean(set, 0); |
| 4041 | } |
| 4042 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4043 | } |
| 4044 | |
| 4045 | return LY_SUCCESS; |
| 4046 | } |
| 4047 | |
| 4048 | /** |
| 4049 | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
| 4050 | * with the context size. |
| 4051 | * |
| 4052 | * @param[in] args Array of arguments. |
| 4053 | * @param[in] arg_count Count of elements in @p args. |
| 4054 | * @param[in,out] set Context and result set at the same time. |
| 4055 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4056 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4057 | */ |
| 4058 | static LY_ERR |
| 4059 | xpath_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4060 | { |
| 4061 | if (options & LYXP_SCNODE_ALL) { |
| 4062 | set_scnode_clear_ctx(set); |
| 4063 | return LY_SUCCESS; |
| 4064 | } |
| 4065 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4066 | if (set->type != LYXP_SET_NODE_SET) { |
| 4067 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()"); |
| 4068 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4069 | } else if (!set->used) { |
| 4070 | set_fill_number(set, 0); |
| 4071 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4072 | } |
| 4073 | |
| 4074 | set_fill_number(set, set->ctx_size); |
| 4075 | return LY_SUCCESS; |
| 4076 | } |
| 4077 | |
| 4078 | /** |
| 4079 | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
| 4080 | * with the node name without namespace from the argument or the context. |
| 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 |
| 4089 | xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4090 | { |
| 4091 | struct lyxp_set_node *item; |
| 4092 | /* suppress unused variable warning */ |
| 4093 | (void)options; |
| 4094 | |
| 4095 | if (options & LYXP_SCNODE_ALL) { |
| 4096 | set_scnode_clear_ctx(set); |
| 4097 | return LY_SUCCESS; |
| 4098 | } |
| 4099 | |
| 4100 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4101 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4102 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "local-name(node-set?)"); |
| 4103 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4104 | } else if (!args[0]->used) { |
| 4105 | set_fill_string(set, "", 0); |
| 4106 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4107 | } |
| 4108 | |
| 4109 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4110 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4111 | |
| 4112 | item = &args[0]->val.nodes[0]; |
| 4113 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4114 | if (set->type != LYXP_SET_NODE_SET) { |
| 4115 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)"); |
| 4116 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4117 | } else if (!set->used) { |
| 4118 | set_fill_string(set, "", 0); |
| 4119 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4120 | } |
| 4121 | |
| 4122 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4123 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4124 | |
| 4125 | item = &set->val.nodes[0]; |
| 4126 | } |
| 4127 | |
| 4128 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4129 | case LYXP_NODE_NONE: |
| 4130 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4131 | case LYXP_NODE_ROOT: |
| 4132 | case LYXP_NODE_ROOT_CONFIG: |
| 4133 | case LYXP_NODE_TEXT: |
| 4134 | set_fill_string(set, "", 0); |
| 4135 | break; |
| 4136 | case LYXP_NODE_ELEM: |
| 4137 | set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name)); |
| 4138 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4139 | case LYXP_NODE_META: |
| 4140 | 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] | 4141 | break; |
| 4142 | } |
| 4143 | |
| 4144 | return LY_SUCCESS; |
| 4145 | } |
| 4146 | |
| 4147 | /** |
| 4148 | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
| 4149 | * with the node name fully qualified (with namespace) from the argument or the context. |
| 4150 | * |
| 4151 | * @param[in] args Array of arguments. |
| 4152 | * @param[in] arg_count Count of elements in @p args. |
| 4153 | * @param[in,out] set Context and result set at the same time. |
| 4154 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4155 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4156 | */ |
| 4157 | static LY_ERR |
| 4158 | xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4159 | { |
| 4160 | struct lyxp_set_node *item; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4161 | struct lys_module *mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4162 | char *str; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4163 | const char *name = NULL; |
| 4164 | int rc = -1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4165 | |
| 4166 | if (options & LYXP_SCNODE_ALL) { |
| 4167 | set_scnode_clear_ctx(set); |
| 4168 | return LY_SUCCESS; |
| 4169 | } |
| 4170 | |
| 4171 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4172 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4173 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)"); |
| 4174 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4175 | } else if (!args[0]->used) { |
| 4176 | set_fill_string(set, "", 0); |
| 4177 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4178 | } |
| 4179 | |
| 4180 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4181 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4182 | |
| 4183 | item = &args[0]->val.nodes[0]; |
| 4184 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4185 | if (set->type != LYXP_SET_NODE_SET) { |
| 4186 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)"); |
| 4187 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4188 | } else if (!set->used) { |
| 4189 | set_fill_string(set, "", 0); |
| 4190 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4191 | } |
| 4192 | |
| 4193 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4194 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4195 | |
| 4196 | item = &set->val.nodes[0]; |
| 4197 | } |
| 4198 | |
| 4199 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4200 | case LYXP_NODE_NONE: |
| 4201 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4202 | case LYXP_NODE_ROOT: |
| 4203 | case LYXP_NODE_ROOT_CONFIG: |
| 4204 | case LYXP_NODE_TEXT: |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4205 | /* keep NULL */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4206 | break; |
| 4207 | case LYXP_NODE_ELEM: |
| 4208 | mod = item->node->schema->module; |
| 4209 | name = item->node->schema->name; |
| 4210 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4211 | case LYXP_NODE_META: |
| 4212 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
| 4213 | name = ((struct lyd_meta *)item->node)->name; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4214 | break; |
| 4215 | } |
| 4216 | |
| 4217 | if (mod && name) { |
| 4218 | switch (set->format) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 4219 | case LY_PREF_SCHEMA: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4220 | rc = asprintf(&str, "%s:%s", lys_prefix_find_module(set->local_mod, mod), name); |
| 4221 | break; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 4222 | case LY_PREF_JSON: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4223 | rc = asprintf(&str, "%s:%s", mod->name, name); |
| 4224 | break; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 4225 | case LY_PREF_XML: |
Michal Vasko | 9409ef6 | 2019-09-12 11:47:17 +0200 | [diff] [blame] | 4226 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4227 | } |
| 4228 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
| 4229 | set_fill_string(set, str, strlen(str)); |
| 4230 | free(str); |
| 4231 | } else { |
| 4232 | set_fill_string(set, "", 0); |
| 4233 | } |
| 4234 | |
| 4235 | return LY_SUCCESS; |
| 4236 | } |
| 4237 | |
| 4238 | /** |
| 4239 | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
| 4240 | * with the namespace of the node from the argument or the context. |
| 4241 | * |
| 4242 | * @param[in] args Array of arguments. |
| 4243 | * @param[in] arg_count Count of elements in @p args. |
| 4244 | * @param[in,out] set Context and result set at the same time. |
| 4245 | * @param[in] options XPath options. |
| 4246 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4247 | */ |
| 4248 | static LY_ERR |
| 4249 | xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4250 | { |
| 4251 | struct lyxp_set_node *item; |
| 4252 | struct lys_module *mod; |
| 4253 | /* suppress unused variable warning */ |
| 4254 | (void)options; |
| 4255 | |
| 4256 | if (options & LYXP_SCNODE_ALL) { |
| 4257 | set_scnode_clear_ctx(set); |
| 4258 | return LY_SUCCESS; |
| 4259 | } |
| 4260 | |
| 4261 | if (arg_count) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4262 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4263 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 4264 | "namespace-uri(node-set?)"); |
| 4265 | return LY_EVALID; |
| 4266 | } else if (!args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4267 | set_fill_string(set, "", 0); |
| 4268 | return LY_SUCCESS; |
| 4269 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4270 | |
| 4271 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4272 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4273 | |
| 4274 | item = &args[0]->val.nodes[0]; |
| 4275 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4276 | if (set->type != LYXP_SET_NODE_SET) { |
| 4277 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)"); |
| 4278 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4279 | } else if (!set->used) { |
| 4280 | set_fill_string(set, "", 0); |
| 4281 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4282 | } |
| 4283 | |
| 4284 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4285 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4286 | |
| 4287 | item = &set->val.nodes[0]; |
| 4288 | } |
| 4289 | |
| 4290 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4291 | case LYXP_NODE_NONE: |
| 4292 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4293 | case LYXP_NODE_ROOT: |
| 4294 | case LYXP_NODE_ROOT_CONFIG: |
| 4295 | case LYXP_NODE_TEXT: |
| 4296 | set_fill_string(set, "", 0); |
| 4297 | break; |
| 4298 | case LYXP_NODE_ELEM: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4299 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4300 | if (item->type == LYXP_NODE_ELEM) { |
| 4301 | mod = item->node->schema->module; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4302 | } else { /* LYXP_NODE_META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4303 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4304 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4305 | } |
| 4306 | |
| 4307 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
| 4308 | break; |
| 4309 | } |
| 4310 | |
| 4311 | return LY_SUCCESS; |
| 4312 | } |
| 4313 | |
| 4314 | /** |
| 4315 | * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET |
| 4316 | * with only nodes from the context. In practice it either leaves the context |
| 4317 | * as it is or returns an empty node set. |
| 4318 | * |
| 4319 | * @param[in] args Array of arguments. |
| 4320 | * @param[in] arg_count Count of elements in @p args. |
| 4321 | * @param[in,out] set Context and result set at the same time. |
| 4322 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4323 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4324 | */ |
| 4325 | static LY_ERR |
| 4326 | xpath_node(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4327 | { |
| 4328 | if (options & LYXP_SCNODE_ALL) { |
| 4329 | set_scnode_clear_ctx(set); |
| 4330 | return LY_SUCCESS; |
| 4331 | } |
| 4332 | |
| 4333 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4334 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4335 | } |
| 4336 | return LY_SUCCESS; |
| 4337 | } |
| 4338 | |
| 4339 | /** |
| 4340 | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
| 4341 | * with normalized value (no leading, trailing, double white spaces) of the node |
| 4342 | * from the argument or the context. |
| 4343 | * |
| 4344 | * @param[in] args Array of arguments. |
| 4345 | * @param[in] arg_count Count of elements in @p args. |
| 4346 | * @param[in,out] set Context and result set at the same time. |
| 4347 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4348 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4349 | */ |
| 4350 | static LY_ERR |
| 4351 | xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4352 | { |
| 4353 | uint16_t i, new_used; |
| 4354 | char *new; |
| 4355 | int have_spaces = 0, space_before = 0; |
| 4356 | struct lysc_node_leaf *sleaf; |
| 4357 | LY_ERR rc = LY_SUCCESS; |
| 4358 | |
| 4359 | if (options & LYXP_SCNODE_ALL) { |
| 4360 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4361 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4362 | 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] | 4363 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4364 | 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] | 4365 | } |
| 4366 | } |
| 4367 | set_scnode_clear_ctx(set); |
| 4368 | return rc; |
| 4369 | } |
| 4370 | |
| 4371 | if (arg_count) { |
| 4372 | set_fill_set(set, args[0]); |
| 4373 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4374 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4375 | LY_CHECK_RET(rc); |
| 4376 | |
| 4377 | /* is there any normalization necessary? */ |
| 4378 | for (i = 0; set->val.str[i]; ++i) { |
| 4379 | if (is_xmlws(set->val.str[i])) { |
| 4380 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
| 4381 | have_spaces = 1; |
| 4382 | break; |
| 4383 | } |
| 4384 | space_before = 1; |
| 4385 | } else { |
| 4386 | space_before = 0; |
| 4387 | } |
| 4388 | } |
| 4389 | |
| 4390 | /* yep, there is */ |
| 4391 | if (have_spaces) { |
| 4392 | /* it's enough, at least one character will go, makes space for ending '\0' */ |
| 4393 | new = malloc(strlen(set->val.str) * sizeof(char)); |
| 4394 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4395 | new_used = 0; |
| 4396 | |
| 4397 | space_before = 0; |
| 4398 | for (i = 0; set->val.str[i]; ++i) { |
| 4399 | if (is_xmlws(set->val.str[i])) { |
| 4400 | if ((i == 0) || space_before) { |
| 4401 | space_before = 1; |
| 4402 | continue; |
| 4403 | } else { |
| 4404 | space_before = 1; |
| 4405 | } |
| 4406 | } else { |
| 4407 | space_before = 0; |
| 4408 | } |
| 4409 | |
| 4410 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
| 4411 | ++new_used; |
| 4412 | } |
| 4413 | |
| 4414 | /* at worst there is one trailing space now */ |
| 4415 | if (new_used && is_xmlws(new[new_used - 1])) { |
| 4416 | --new_used; |
| 4417 | } |
| 4418 | |
| 4419 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 4420 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4421 | new[new_used] = '\0'; |
| 4422 | |
| 4423 | free(set->val.str); |
| 4424 | set->val.str = new; |
| 4425 | } |
| 4426 | |
| 4427 | return LY_SUCCESS; |
| 4428 | } |
| 4429 | |
| 4430 | /** |
| 4431 | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
| 4432 | * with the argument converted to boolean and logically inverted. |
| 4433 | * |
| 4434 | * @param[in] args Array of arguments. |
| 4435 | * @param[in] arg_count Count of elements in @p args. |
| 4436 | * @param[in,out] set Context and result set at the same time. |
| 4437 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4438 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4439 | */ |
| 4440 | static LY_ERR |
| 4441 | xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4442 | { |
| 4443 | if (options & LYXP_SCNODE_ALL) { |
| 4444 | set_scnode_clear_ctx(set); |
| 4445 | return LY_SUCCESS; |
| 4446 | } |
| 4447 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4448 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4449 | if (args[0]->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4450 | set_fill_boolean(set, 0); |
| 4451 | } else { |
| 4452 | set_fill_boolean(set, 1); |
| 4453 | } |
| 4454 | |
| 4455 | return LY_SUCCESS; |
| 4456 | } |
| 4457 | |
| 4458 | /** |
| 4459 | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
| 4460 | * with the number representation of either the argument or the context. |
| 4461 | * |
| 4462 | * @param[in] args Array of arguments. |
| 4463 | * @param[in] arg_count Count of elements in @p args. |
| 4464 | * @param[in,out] set Context and result set at the same time. |
| 4465 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4466 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4467 | */ |
| 4468 | static LY_ERR |
| 4469 | xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4470 | { |
| 4471 | LY_ERR rc; |
| 4472 | |
| 4473 | if (options & LYXP_SCNODE_ALL) { |
| 4474 | set_scnode_clear_ctx(set); |
| 4475 | return LY_SUCCESS; |
| 4476 | } |
| 4477 | |
| 4478 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4479 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4480 | LY_CHECK_RET(rc); |
| 4481 | set_fill_set(set, args[0]); |
| 4482 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4483 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4484 | LY_CHECK_RET(rc); |
| 4485 | } |
| 4486 | |
| 4487 | return LY_SUCCESS; |
| 4488 | } |
| 4489 | |
| 4490 | /** |
| 4491 | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
| 4492 | * with the context position. |
| 4493 | * |
| 4494 | * @param[in] args Array of arguments. |
| 4495 | * @param[in] arg_count Count of elements in @p args. |
| 4496 | * @param[in,out] set Context and result set at the same time. |
| 4497 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4498 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4499 | */ |
| 4500 | static LY_ERR |
| 4501 | xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4502 | { |
| 4503 | if (options & LYXP_SCNODE_ALL) { |
| 4504 | set_scnode_clear_ctx(set); |
| 4505 | return LY_SUCCESS; |
| 4506 | } |
| 4507 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4508 | if (set->type != LYXP_SET_NODE_SET) { |
| 4509 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()"); |
| 4510 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4511 | } else if (!set->used) { |
| 4512 | set_fill_number(set, 0); |
| 4513 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4514 | } |
| 4515 | |
| 4516 | set_fill_number(set, set->ctx_pos); |
| 4517 | |
| 4518 | /* UNUSED in 'Release' build type */ |
| 4519 | (void)options; |
| 4520 | return LY_SUCCESS; |
| 4521 | } |
| 4522 | |
| 4523 | /** |
| 4524 | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
| 4525 | * depending on whether the second argument regex matches the first argument string. For details refer to |
| 4526 | * YANG 1.1 RFC section 10.2.1. |
| 4527 | * |
| 4528 | * @param[in] args Array of arguments. |
| 4529 | * @param[in] arg_count Count of elements in @p args. |
| 4530 | * @param[in,out] set Context and result set at the same time. |
| 4531 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4532 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4533 | */ |
| 4534 | static LY_ERR |
| 4535 | xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4536 | { |
| 4537 | struct lysc_pattern **patterns = NULL, **pattern; |
| 4538 | struct lysc_node_leaf *sleaf; |
| 4539 | char *path; |
| 4540 | LY_ERR rc = LY_SUCCESS; |
| 4541 | struct ly_err_item *err; |
| 4542 | |
| 4543 | if (options & LYXP_SCNODE_ALL) { |
| 4544 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4545 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4546 | 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] | 4547 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4548 | 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] | 4549 | } |
| 4550 | } |
| 4551 | |
| 4552 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4553 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4554 | 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] | 4555 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4556 | 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] | 4557 | } |
| 4558 | } |
| 4559 | set_scnode_clear_ctx(set); |
| 4560 | return rc; |
| 4561 | } |
| 4562 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4563 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4564 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4565 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4566 | LY_CHECK_RET(rc); |
| 4567 | |
| 4568 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
| 4569 | *pattern = malloc(sizeof **pattern); |
| 4570 | path = lyd_path(set->ctx_node, LYD_PATH_LOG, NULL, 0); |
| 4571 | rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code); |
| 4572 | free(path); |
| 4573 | if (rc != LY_SUCCESS) { |
| 4574 | LY_ARRAY_FREE(patterns); |
| 4575 | return rc; |
| 4576 | } |
| 4577 | |
| 4578 | rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err); |
| 4579 | pcre2_code_free((*pattern)->code); |
| 4580 | free(*pattern); |
| 4581 | LY_ARRAY_FREE(patterns); |
| 4582 | if (rc && (rc != LY_EVALID)) { |
| 4583 | ly_err_print(err); |
| 4584 | ly_err_free(err); |
| 4585 | return rc; |
| 4586 | } |
| 4587 | |
| 4588 | if (rc == LY_EVALID) { |
| 4589 | ly_err_free(err); |
| 4590 | set_fill_boolean(set, 0); |
| 4591 | } else { |
| 4592 | set_fill_boolean(set, 1); |
| 4593 | } |
| 4594 | |
| 4595 | return LY_SUCCESS; |
| 4596 | } |
| 4597 | |
| 4598 | /** |
| 4599 | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
| 4600 | * with the rounded first argument. For details refer to |
| 4601 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
| 4602 | * |
| 4603 | * @param[in] args Array of arguments. |
| 4604 | * @param[in] arg_count Count of elements in @p args. |
| 4605 | * @param[in,out] set Context and result set at the same time. |
| 4606 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4607 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4608 | */ |
| 4609 | static LY_ERR |
| 4610 | xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4611 | { |
| 4612 | struct lysc_node_leaf *sleaf; |
| 4613 | LY_ERR rc = LY_SUCCESS; |
| 4614 | |
| 4615 | if (options & LYXP_SCNODE_ALL) { |
| 4616 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4617 | 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] | 4618 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4619 | 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] | 4620 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4621 | 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] | 4622 | } |
| 4623 | set_scnode_clear_ctx(set); |
| 4624 | return rc; |
| 4625 | } |
| 4626 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4627 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4628 | LY_CHECK_RET(rc); |
| 4629 | |
| 4630 | /* cover only the cases where floor can't be used */ |
| 4631 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
| 4632 | set_fill_number(set, -0.0f); |
| 4633 | } else { |
| 4634 | args[0]->val.num += 0.5; |
| 4635 | rc = xpath_floor(args, 1, args[0], options); |
| 4636 | LY_CHECK_RET(rc); |
| 4637 | set_fill_number(set, args[0]->val.num); |
| 4638 | } |
| 4639 | |
| 4640 | return LY_SUCCESS; |
| 4641 | } |
| 4642 | |
| 4643 | /** |
| 4644 | * @brief Execute the XPath starts-with(string, string) function. |
| 4645 | * Returns LYXP_SET_BOOLEAN whether the second argument is |
| 4646 | * the prefix of the first or not. |
| 4647 | * |
| 4648 | * @param[in] args Array of arguments. |
| 4649 | * @param[in] arg_count Count of elements in @p args. |
| 4650 | * @param[in,out] set Context and result set at the same time. |
| 4651 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4652 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4653 | */ |
| 4654 | static LY_ERR |
| 4655 | xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4656 | { |
| 4657 | struct lysc_node_leaf *sleaf; |
| 4658 | LY_ERR rc = LY_SUCCESS; |
| 4659 | |
| 4660 | if (options & LYXP_SCNODE_ALL) { |
| 4661 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4662 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4663 | 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] | 4664 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4665 | 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] | 4666 | } |
| 4667 | } |
| 4668 | |
| 4669 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4670 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4671 | 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] | 4672 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4673 | 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] | 4674 | } |
| 4675 | } |
| 4676 | set_scnode_clear_ctx(set); |
| 4677 | return rc; |
| 4678 | } |
| 4679 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4680 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4681 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4682 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4683 | LY_CHECK_RET(rc); |
| 4684 | |
| 4685 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
| 4686 | set_fill_boolean(set, 0); |
| 4687 | } else { |
| 4688 | set_fill_boolean(set, 1); |
| 4689 | } |
| 4690 | |
| 4691 | return LY_SUCCESS; |
| 4692 | } |
| 4693 | |
| 4694 | /** |
| 4695 | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
| 4696 | * with the string representation of either the argument or the context. |
| 4697 | * |
| 4698 | * @param[in] args Array of arguments. |
| 4699 | * @param[in] arg_count Count of elements in @p args. |
| 4700 | * @param[in,out] set Context and result set at the same time. |
| 4701 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4702 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4703 | */ |
| 4704 | static LY_ERR |
| 4705 | xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4706 | { |
| 4707 | LY_ERR rc; |
| 4708 | |
| 4709 | if (options & LYXP_SCNODE_ALL) { |
| 4710 | set_scnode_clear_ctx(set); |
| 4711 | return LY_SUCCESS; |
| 4712 | } |
| 4713 | |
| 4714 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4715 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4716 | LY_CHECK_RET(rc); |
| 4717 | set_fill_set(set, args[0]); |
| 4718 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4719 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4720 | LY_CHECK_RET(rc); |
| 4721 | } |
| 4722 | |
| 4723 | return LY_SUCCESS; |
| 4724 | } |
| 4725 | |
| 4726 | /** |
| 4727 | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
| 4728 | * with the length of the string in either the argument or the context. |
| 4729 | * |
| 4730 | * @param[in] args Array of arguments. |
| 4731 | * @param[in] arg_count Count of elements in @p args. |
| 4732 | * @param[in,out] set Context and result set at the same time. |
| 4733 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4734 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4735 | */ |
| 4736 | static LY_ERR |
| 4737 | xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4738 | { |
| 4739 | struct lysc_node_leaf *sleaf; |
| 4740 | LY_ERR rc = LY_SUCCESS; |
| 4741 | |
| 4742 | if (options & LYXP_SCNODE_ALL) { |
| 4743 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4744 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4745 | 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] | 4746 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4747 | 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] | 4748 | } |
| 4749 | } |
| 4750 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
| 4751 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4752 | 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] | 4753 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4754 | 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] | 4755 | } |
| 4756 | } |
| 4757 | set_scnode_clear_ctx(set); |
| 4758 | return rc; |
| 4759 | } |
| 4760 | |
| 4761 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4762 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4763 | LY_CHECK_RET(rc); |
| 4764 | set_fill_number(set, strlen(args[0]->val.str)); |
| 4765 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4766 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4767 | LY_CHECK_RET(rc); |
| 4768 | set_fill_number(set, strlen(set->val.str)); |
| 4769 | } |
| 4770 | |
| 4771 | return LY_SUCCESS; |
| 4772 | } |
| 4773 | |
| 4774 | /** |
| 4775 | * @brief Execute the XPath substring(string, number, number?) function. |
| 4776 | * Returns LYXP_SET_STRING substring of the first argument starting |
| 4777 | * on the second argument index ending on the third argument index, |
| 4778 | * indexed from 1. For exact definition refer to |
| 4779 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
| 4780 | * |
| 4781 | * @param[in] args Array of arguments. |
| 4782 | * @param[in] arg_count Count of elements in @p args. |
| 4783 | * @param[in,out] set Context and result set at the same time. |
| 4784 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4785 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4786 | */ |
| 4787 | static LY_ERR |
| 4788 | xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4789 | { |
| 4790 | int start, len; |
| 4791 | uint16_t str_start, str_len, pos; |
| 4792 | struct lysc_node_leaf *sleaf; |
| 4793 | LY_ERR rc = LY_SUCCESS; |
| 4794 | |
| 4795 | if (options & LYXP_SCNODE_ALL) { |
| 4796 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4797 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4798 | 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] | 4799 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4800 | 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] | 4801 | } |
| 4802 | } |
| 4803 | |
| 4804 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4805 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4806 | 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] | 4807 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4808 | 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] | 4809 | } |
| 4810 | } |
| 4811 | |
| 4812 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame^] | 4813 | && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4814 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4815 | 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] | 4816 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4817 | 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] | 4818 | } |
| 4819 | } |
| 4820 | set_scnode_clear_ctx(set); |
| 4821 | return rc; |
| 4822 | } |
| 4823 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4824 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4825 | LY_CHECK_RET(rc); |
| 4826 | |
| 4827 | /* start */ |
| 4828 | if (xpath_round(&args[1], 1, args[1], options)) { |
| 4829 | return -1; |
| 4830 | } |
| 4831 | if (isfinite(args[1]->val.num)) { |
| 4832 | start = args[1]->val.num - 1; |
| 4833 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
| 4834 | start = INT_MIN; |
| 4835 | } else { |
| 4836 | start = INT_MAX; |
| 4837 | } |
| 4838 | |
| 4839 | /* len */ |
| 4840 | if (arg_count == 3) { |
| 4841 | rc = xpath_round(&args[2], 1, args[2], options); |
| 4842 | LY_CHECK_RET(rc); |
| 4843 | if (isfinite(args[2]->val.num)) { |
| 4844 | len = args[2]->val.num; |
| 4845 | } else if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
| 4846 | len = 0; |
| 4847 | } else { |
| 4848 | len = INT_MAX; |
| 4849 | } |
| 4850 | } else { |
| 4851 | len = INT_MAX; |
| 4852 | } |
| 4853 | |
| 4854 | /* find matching character positions */ |
| 4855 | str_start = 0; |
| 4856 | str_len = 0; |
| 4857 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
| 4858 | if (pos < start) { |
| 4859 | ++str_start; |
| 4860 | } else if (pos < start + len) { |
| 4861 | ++str_len; |
| 4862 | } else { |
| 4863 | break; |
| 4864 | } |
| 4865 | } |
| 4866 | |
| 4867 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
| 4868 | return LY_SUCCESS; |
| 4869 | } |
| 4870 | |
| 4871 | /** |
| 4872 | * @brief Execute the XPath substring-after(string, string) function. |
| 4873 | * Returns LYXP_SET_STRING with the string succeeding the occurance |
| 4874 | * of the second argument in the first or an empty string. |
| 4875 | * |
| 4876 | * @param[in] args Array of arguments. |
| 4877 | * @param[in] arg_count Count of elements in @p args. |
| 4878 | * @param[in,out] set Context and result set at the same time. |
| 4879 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4880 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4881 | */ |
| 4882 | static LY_ERR |
| 4883 | xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4884 | { |
| 4885 | char *ptr; |
| 4886 | struct lysc_node_leaf *sleaf; |
| 4887 | LY_ERR rc = LY_SUCCESS; |
| 4888 | |
| 4889 | if (options & LYXP_SCNODE_ALL) { |
| 4890 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4891 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4892 | 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] | 4893 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4894 | 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] | 4895 | } |
| 4896 | } |
| 4897 | |
| 4898 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4899 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4900 | 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] | 4901 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4902 | 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] | 4903 | } |
| 4904 | } |
| 4905 | set_scnode_clear_ctx(set); |
| 4906 | return rc; |
| 4907 | } |
| 4908 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4909 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4910 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4911 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4912 | LY_CHECK_RET(rc); |
| 4913 | |
| 4914 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4915 | if (ptr) { |
| 4916 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
| 4917 | } else { |
| 4918 | set_fill_string(set, "", 0); |
| 4919 | } |
| 4920 | |
| 4921 | return LY_SUCCESS; |
| 4922 | } |
| 4923 | |
| 4924 | /** |
| 4925 | * @brief Execute the XPath substring-before(string, string) function. |
| 4926 | * Returns LYXP_SET_STRING with the string preceding the occurance |
| 4927 | * of the second argument in the first or an empty string. |
| 4928 | * |
| 4929 | * @param[in] args Array of arguments. |
| 4930 | * @param[in] arg_count Count of elements in @p args. |
| 4931 | * @param[in,out] set Context and result set at the same time. |
| 4932 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4933 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4934 | */ |
| 4935 | static LY_ERR |
| 4936 | xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4937 | { |
| 4938 | char *ptr; |
| 4939 | struct lysc_node_leaf *sleaf; |
| 4940 | LY_ERR rc = LY_SUCCESS; |
| 4941 | |
| 4942 | if (options & LYXP_SCNODE_ALL) { |
| 4943 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4944 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4945 | 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] | 4946 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4947 | 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] | 4948 | } |
| 4949 | } |
| 4950 | |
| 4951 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4952 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4953 | 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] | 4954 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4955 | 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] | 4956 | } |
| 4957 | } |
| 4958 | set_scnode_clear_ctx(set); |
| 4959 | return rc; |
| 4960 | } |
| 4961 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4962 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4963 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4964 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4965 | LY_CHECK_RET(rc); |
| 4966 | |
| 4967 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4968 | if (ptr) { |
| 4969 | set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str); |
| 4970 | } else { |
| 4971 | set_fill_string(set, "", 0); |
| 4972 | } |
| 4973 | |
| 4974 | return LY_SUCCESS; |
| 4975 | } |
| 4976 | |
| 4977 | /** |
| 4978 | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
| 4979 | * with the sum of all the nodes in the context. |
| 4980 | * |
| 4981 | * @param[in] args Array of arguments. |
| 4982 | * @param[in] arg_count Count of elements in @p args. |
| 4983 | * @param[in,out] set Context and result set at the same time. |
| 4984 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4985 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4986 | */ |
| 4987 | static LY_ERR |
| 4988 | xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4989 | { |
| 4990 | long double num; |
| 4991 | char *str; |
| 4992 | uint16_t i; |
| 4993 | struct lyxp_set set_item; |
| 4994 | struct lysc_node_leaf *sleaf; |
| 4995 | LY_ERR rc = LY_SUCCESS; |
| 4996 | |
| 4997 | if (options & LYXP_SCNODE_ALL) { |
| 4998 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
| 4999 | for (i = 0; i < args[0]->used; ++i) { |
| 5000 | if (args[0]->val.scnodes[i].in_ctx == 1) { |
| 5001 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
| 5002 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5003 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, |
| 5004 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5005 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5006 | 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] | 5007 | } |
| 5008 | } |
| 5009 | } |
| 5010 | } |
| 5011 | set_scnode_clear_ctx(set); |
| 5012 | return rc; |
| 5013 | } |
| 5014 | |
| 5015 | set_fill_number(set, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5016 | |
| 5017 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 5018 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)"); |
| 5019 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5020 | } else if (!args[0]->used) { |
| 5021 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5022 | } |
| 5023 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5024 | set_init(&set_item, set); |
| 5025 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5026 | set_item.type = LYXP_SET_NODE_SET; |
| 5027 | set_item.val.nodes = malloc(sizeof *set_item.val.nodes); |
| 5028 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
| 5029 | |
| 5030 | set_item.used = 1; |
| 5031 | set_item.size = 1; |
| 5032 | |
| 5033 | for (i = 0; i < args[0]->used; ++i) { |
| 5034 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
| 5035 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5036 | rc = cast_node_set_to_string(&set_item, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5037 | LY_CHECK_RET(rc); |
| 5038 | num = cast_string_to_number(str); |
| 5039 | free(str); |
| 5040 | set->val.num += num; |
| 5041 | } |
| 5042 | |
| 5043 | free(set_item.val.nodes); |
| 5044 | |
| 5045 | return LY_SUCCESS; |
| 5046 | } |
| 5047 | |
| 5048 | /** |
| 5049 | * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET |
| 5050 | * with the text content of the nodes in the context. |
| 5051 | * |
| 5052 | * @param[in] args Array of arguments. |
| 5053 | * @param[in] arg_count Count of elements in @p args. |
| 5054 | * @param[in,out] set Context and result set at the same time. |
| 5055 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5056 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5057 | */ |
| 5058 | static LY_ERR |
| 5059 | xpath_text(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 5060 | { |
| 5061 | uint32_t i; |
| 5062 | |
| 5063 | if (options & LYXP_SCNODE_ALL) { |
| 5064 | set_scnode_clear_ctx(set); |
| 5065 | return LY_SUCCESS; |
| 5066 | } |
| 5067 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5068 | if (set->type != LYXP_SET_NODE_SET) { |
| 5069 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()"); |
| 5070 | return LY_EVALID; |
| 5071 | } |
| 5072 | |
| 5073 | for (i = 0; i < set->used;) { |
| 5074 | switch (set->val.nodes[i].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 5075 | case LYXP_NODE_NONE: |
| 5076 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5077 | case LYXP_NODE_ELEM: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5078 | if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 5079 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
| 5080 | ++i; |
| 5081 | break; |
| 5082 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame^] | 5083 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5084 | case LYXP_NODE_ROOT: |
| 5085 | case LYXP_NODE_ROOT_CONFIG: |
| 5086 | case LYXP_NODE_TEXT: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5087 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5088 | set_remove_node(set, i); |
| 5089 | break; |
| 5090 | } |
| 5091 | } |
| 5092 | |
| 5093 | return LY_SUCCESS; |
| 5094 | } |
| 5095 | |
| 5096 | /** |
| 5097 | * @brief Execute the XPath translate(string, string, string) function. |
| 5098 | * Returns LYXP_SET_STRING with the first argument with the characters |
| 5099 | * from the second argument replaced by those on the corresponding |
| 5100 | * positions in the third argument. |
| 5101 | * |
| 5102 | * @param[in] args Array of arguments. |
| 5103 | * @param[in] arg_count Count of elements in @p args. |
| 5104 | * @param[in,out] set Context and result set at the same time. |
| 5105 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5106 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5107 | */ |
| 5108 | static LY_ERR |
| 5109 | xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 5110 | { |
| 5111 | uint16_t i, j, new_used; |
| 5112 | char *new; |
| 5113 | int found, have_removed; |
| 5114 | struct lysc_node_leaf *sleaf; |
| 5115 | LY_ERR rc = LY_SUCCESS; |
| 5116 | |
| 5117 | if (options & LYXP_SCNODE_ALL) { |
| 5118 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5119 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5120 | 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] | 5121 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5122 | 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] | 5123 | } |
| 5124 | } |
| 5125 | |
| 5126 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5127 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5128 | 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] | 5129 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5130 | 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] | 5131 | } |
| 5132 | } |
| 5133 | |
| 5134 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 5135 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5136 | 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] | 5137 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5138 | 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] | 5139 | } |
| 5140 | } |
| 5141 | set_scnode_clear_ctx(set); |
| 5142 | return rc; |
| 5143 | } |
| 5144 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5145 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5146 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5147 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5148 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5149 | rc = lyxp_set_cast(args[2], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5150 | LY_CHECK_RET(rc); |
| 5151 | |
| 5152 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
| 5153 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5154 | new_used = 0; |
| 5155 | |
| 5156 | have_removed = 0; |
| 5157 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 5158 | found = 0; |
| 5159 | |
| 5160 | for (j = 0; args[1]->val.str[j]; ++j) { |
| 5161 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
| 5162 | /* removing this char */ |
| 5163 | if (j >= strlen(args[2]->val.str)) { |
| 5164 | have_removed = 1; |
| 5165 | found = 1; |
| 5166 | break; |
| 5167 | } |
| 5168 | /* replacing this char */ |
| 5169 | new[new_used] = args[2]->val.str[j]; |
| 5170 | ++new_used; |
| 5171 | found = 1; |
| 5172 | break; |
| 5173 | } |
| 5174 | } |
| 5175 | |
| 5176 | /* copying this char */ |
| 5177 | if (!found) { |
| 5178 | new[new_used] = args[0]->val.str[i]; |
| 5179 | ++new_used; |
| 5180 | } |
| 5181 | } |
| 5182 | |
| 5183 | if (have_removed) { |
| 5184 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 5185 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5186 | } |
| 5187 | new[new_used] = '\0'; |
| 5188 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5189 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5190 | set->type = LYXP_SET_STRING; |
| 5191 | set->val.str = new; |
| 5192 | |
| 5193 | return LY_SUCCESS; |
| 5194 | } |
| 5195 | |
| 5196 | /** |
| 5197 | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
| 5198 | * with true value. |
| 5199 | * |
| 5200 | * @param[in] args Array of arguments. |
| 5201 | * @param[in] arg_count Count of elements in @p args. |
| 5202 | * @param[in,out] set Context and result set at the same time. |
| 5203 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5204 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5205 | */ |
| 5206 | static LY_ERR |
| 5207 | xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 5208 | { |
| 5209 | if (options & LYXP_SCNODE_ALL) { |
| 5210 | set_scnode_clear_ctx(set); |
| 5211 | return LY_SUCCESS; |
| 5212 | } |
| 5213 | |
| 5214 | set_fill_boolean(set, 1); |
| 5215 | return LY_SUCCESS; |
| 5216 | } |
| 5217 | |
| 5218 | /* |
| 5219 | * moveto functions |
| 5220 | * |
| 5221 | * They and only they actually change the context (set). |
| 5222 | */ |
| 5223 | |
| 5224 | /** |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5225 | * @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] | 5226 | * |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5227 | * XPath @p set is expected to be a (sc)node set! |
| 5228 | * |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5229 | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
| 5230 | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
| 5231 | * @param[in] set Set with XPath context. |
| 5232 | * @param[out] moveto_mod Expected module of a matching node. |
| 5233 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5234 | */ |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5235 | static LY_ERR |
| 5236 | moveto_resolve_model(const char **qname, uint16_t *qname_len, struct lyxp_set *set, const struct lys_module **moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5237 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 5238 | const struct lys_module *mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5239 | const char *ptr; |
| 5240 | int pref_len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5241 | |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5242 | assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET)); |
| 5243 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5244 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
| 5245 | /* specific module */ |
| 5246 | pref_len = ptr - *qname; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 5247 | mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, (void *)set->local_mod); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5248 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5249 | /* check for errors and non-implemented modules, as they are not valid */ |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5250 | if (!mod || !mod->implemented) { |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5251 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 5252 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INMOD, pref_len, *qname); |
| 5253 | } else { |
| 5254 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INMOD, pref_len, *qname); |
| 5255 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5256 | return LY_EVALID; |
| 5257 | } |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5258 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5259 | *qname += pref_len + 1; |
| 5260 | *qname_len -= pref_len + 1; |
| 5261 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
| 5262 | /* all modules - special case */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5263 | mod = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5264 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 5265 | /* current node module */ |
| 5266 | mod = set->ctx_scnode->module; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5267 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5268 | /* current node module */ |
| 5269 | mod = set->ctx_node->schema->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5270 | } |
| 5271 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5272 | *moveto_mod = mod; |
| 5273 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5274 | } |
| 5275 | |
| 5276 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5277 | * @brief Move context @p set to the root. Handles absolute path. |
| 5278 | * Result is LYXP_SET_NODE_SET. |
| 5279 | * |
| 5280 | * @param[in,out] set Set to use. |
| 5281 | * @param[in] options Xpath options. |
| 5282 | */ |
| 5283 | static void |
| 5284 | moveto_root(struct lyxp_set *set, int options) |
| 5285 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5286 | if (!set) { |
| 5287 | return; |
| 5288 | } |
| 5289 | |
| 5290 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5291 | set_scnode_clear_ctx(set); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 5292 | lyxp_set_scnode_insert_node(set, NULL, set->root_type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5293 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5294 | set->type = LYXP_SET_NODE_SET; |
| 5295 | set->used = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5296 | set_insert_node(set, NULL, 0, set->root_type, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5297 | } |
| 5298 | } |
| 5299 | |
| 5300 | /** |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5301 | * @brief Check whether a node has some unresolved "when". |
| 5302 | * |
| 5303 | * @param[in] node Node to check. |
| 5304 | * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when") |
| 5305 | */ |
| 5306 | static LY_ERR |
| 5307 | moveto_when_check(const struct lyd_node *node) |
| 5308 | { |
| 5309 | const struct lysc_node *schema; |
| 5310 | |
| 5311 | if (!node) { |
| 5312 | return LY_SUCCESS; |
| 5313 | } |
| 5314 | |
| 5315 | schema = node->schema; |
| 5316 | do { |
| 5317 | if (schema->when && !(node->flags & LYD_WHEN_TRUE)) { |
| 5318 | return LY_EINCOMPLETE; |
| 5319 | } |
| 5320 | schema = schema->parent; |
| 5321 | } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 5322 | |
| 5323 | return LY_SUCCESS; |
| 5324 | } |
| 5325 | |
| 5326 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5327 | * @brief Check @p node as a part of NameTest processing. |
| 5328 | * |
| 5329 | * @param[in] node Node to check. |
| 5330 | * @param[in] root_type XPath root node type. |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5331 | * @param[in] context_op XPath operation parent. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5332 | * @param[in] node_name Node name in the dictionary to move to, NULL for any node. |
| 5333 | * @param[in] moveto_mod Expected module of the node, NULL for any. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5334 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when, |
| 5335 | * LY_EINVAL if netither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5336 | */ |
| 5337 | static LY_ERR |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5338 | moveto_node_check(const struct lyd_node *node, enum lyxp_node_type root_type, const struct lysc_node *context_op, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame^] | 5339 | const char *node_name, const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5340 | { |
| 5341 | /* module check */ |
| 5342 | if (moveto_mod && (node->schema->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5343 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5344 | } |
| 5345 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5346 | /* context check */ |
| 5347 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5348 | return LY_EINVAL; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5349 | } else if (context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node->schema != context_op)) { |
| 5350 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5351 | } |
| 5352 | |
| 5353 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5354 | if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5355 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5356 | } |
| 5357 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5358 | /* when check */ |
| 5359 | if (moveto_when_check(node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5360 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5361 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5362 | |
| 5363 | /* match */ |
| 5364 | return LY_SUCCESS; |
| 5365 | } |
| 5366 | |
| 5367 | /** |
| 5368 | * @brief Check @p node as a part of schema NameTest processing. |
| 5369 | * |
| 5370 | * @param[in] node Schema node to check. |
| 5371 | * @param[in] root_type XPath root node type. |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5372 | * @param[in] context_op XPath operation parent. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5373 | * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes. |
| 5374 | * @param[in] moveto_mod Expected module of the node, NULL for any. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5375 | * @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] | 5376 | */ |
| 5377 | static LY_ERR |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5378 | moveto_scnode_check(const struct lysc_node *node, enum lyxp_node_type root_type, const struct lysc_node *context_op, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame^] | 5379 | const char *node_name, const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5380 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5381 | /* module check */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5382 | if (moveto_mod && (node->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5383 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5384 | } |
| 5385 | |
| 5386 | /* context check */ |
| 5387 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) { |
| 5388 | return LY_EINVAL; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5389 | } else if (context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != context_op)) { |
| 5390 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5391 | } |
| 5392 | |
| 5393 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5394 | if (node_name && strcmp(node_name, "*") && (node->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5395 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5396 | } |
| 5397 | |
| 5398 | /* match */ |
| 5399 | return LY_SUCCESS; |
| 5400 | } |
| 5401 | |
| 5402 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5403 | * @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] | 5404 | * |
| 5405 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5406 | * @param[in] mod Matching node module, NULL for any. |
| 5407 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5408 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5409 | */ |
| 5410 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5411 | moveto_node(struct lyxp_set *set, const struct lys_module *mod, const char *ncname) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5412 | { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 5413 | uint32_t i; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5414 | int replaced; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5415 | const struct lyd_node *siblings, *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5416 | LY_ERR rc; |
| 5417 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5418 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5419 | return LY_SUCCESS; |
| 5420 | } |
| 5421 | |
| 5422 | if (set->type != LYXP_SET_NODE_SET) { |
| 5423 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5424 | return LY_EVALID; |
| 5425 | } |
| 5426 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5427 | for (i = 0; i < set->used; ) { |
| 5428 | replaced = 0; |
| 5429 | |
| 5430 | 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] | 5431 | assert(!set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5432 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5433 | /* search in all the trees */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5434 | siblings = set->tree; |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5435 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5436 | /* search in children */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5437 | siblings = lyd_node_children(set->val.nodes[i].node, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5438 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5439 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5440 | for (sub = siblings; sub; sub = sub->next) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5441 | rc = moveto_node_check(sub, set->root_type, set->context_op, ncname, mod); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5442 | if (rc == LY_SUCCESS) { |
| 5443 | if (!replaced) { |
| 5444 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
| 5445 | replaced = 1; |
| 5446 | } else { |
| 5447 | set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5448 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5449 | ++i; |
| 5450 | } else if (rc == LY_EINCOMPLETE) { |
| 5451 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5452 | } |
| 5453 | } |
| 5454 | |
| 5455 | if (!replaced) { |
| 5456 | /* no match */ |
| 5457 | set_remove_node(set, i); |
| 5458 | } |
| 5459 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5460 | |
| 5461 | return LY_SUCCESS; |
| 5462 | } |
| 5463 | |
| 5464 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5465 | * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). |
| 5466 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5467 | * |
| 5468 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5469 | * @param[in] scnode Matching node schema. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5470 | * @param[in] predicates If @p scnode is ::LYS_LIST or ::LYS_LEAFLIST, the predicates specifying a single instance. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5471 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5472 | */ |
| 5473 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5474 | moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5475 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5476 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5477 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5478 | const struct lyd_node *siblings; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5479 | struct lyd_node *sub, *inst = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5480 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5481 | assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5482 | |
| 5483 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5484 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5485 | } |
| 5486 | |
| 5487 | if (set->type != LYXP_SET_NODE_SET) { |
| 5488 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5489 | ret = LY_EVALID; |
| 5490 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5491 | } |
| 5492 | |
| 5493 | /* context check for all the nodes since we have the schema node */ |
| 5494 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 5495 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5496 | goto cleanup; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame^] | 5497 | } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5498 | && (scnode != set->context_op)) { |
| 5499 | lyxp_set_free_content(set); |
| 5500 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5501 | } |
| 5502 | |
| 5503 | /* create specific data instance if needed */ |
| 5504 | if (scnode->nodetype == LYS_LIST) { |
| 5505 | LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup); |
| 5506 | } else if (scnode->nodetype == LYS_LEAFLIST) { |
| 5507 | 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] | 5508 | } |
| 5509 | |
| 5510 | for (i = 0; i < set->used; ) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5511 | siblings = NULL; |
| 5512 | |
| 5513 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
| 5514 | assert(!set->val.nodes[i].node); |
| 5515 | |
| 5516 | /* search in all the trees */ |
| 5517 | siblings = set->tree; |
| 5518 | } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 5519 | /* search in children */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5520 | siblings = lyd_node_children(set->val.nodes[i].node, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5521 | } |
| 5522 | |
| 5523 | /* find the node using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5524 | if (inst) { |
| 5525 | lyd_find_sibling_first(siblings, inst, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5526 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5527 | lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5528 | } |
| 5529 | |
| 5530 | /* when check */ |
| 5531 | if (sub && moveto_when_check(sub)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5532 | ret = LY_EINCOMPLETE; |
| 5533 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5534 | } |
| 5535 | |
| 5536 | if (sub) { |
| 5537 | /* pos filled later */ |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5538 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5539 | ++i; |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5540 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5541 | /* no match */ |
| 5542 | set_remove_node(set, i); |
| 5543 | } |
| 5544 | } |
| 5545 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5546 | cleanup: |
| 5547 | lyd_free_tree(inst); |
| 5548 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5549 | } |
| 5550 | |
| 5551 | /** |
| 5552 | * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
| 5553 | * |
| 5554 | * @param[in,out] set Set to use. |
| 5555 | * @param[in] mod Matching node module, NULL for any. |
| 5556 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5557 | * @param[in] options XPath options. |
| 5558 | * @return LY_ERR |
| 5559 | */ |
| 5560 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5561 | moveto_scnode(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5562 | { |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5563 | int i, orig_used, idx, temp_ctx = 0, getnext_opts; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5564 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5565 | const struct lysc_node *iter, *start_parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5566 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5567 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5568 | return LY_SUCCESS; |
| 5569 | } |
| 5570 | |
| 5571 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 5572 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5573 | return LY_EVALID; |
| 5574 | } |
| 5575 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5576 | /* getnext opts */ |
| 5577 | getnext_opts = LYS_GETNEXT_NOSTATECHECK; |
| 5578 | if (options & LYXP_SCNODE_OUTPUT) { |
| 5579 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 5580 | } |
| 5581 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5582 | orig_used = set->used; |
| 5583 | for (i = 0; i < orig_used; ++i) { |
| 5584 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5585 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5586 | continue; |
| 5587 | } |
| 5588 | |
| 5589 | /* remember context node */ |
| 5590 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5591 | } else { |
| 5592 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5593 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5594 | |
| 5595 | start_parent = set->val.scnodes[i].scnode; |
| 5596 | |
| 5597 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5598 | /* it can actually be in any module, it's all <running>, but we know it's mod (if set), |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5599 | * so use it directly (root node itself is useless in this case) */ |
| 5600 | mod_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5601 | while (mod || (mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5602 | iter = NULL; |
Michal Vasko | 509de4d | 2019-12-10 14:51:30 +0100 | [diff] [blame] | 5603 | /* module may not be implemented */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5604 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5605 | if (!moveto_scnode_check(iter, set->root_type, set->context_op, ncname, mod)) { |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5606 | idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5607 | /* we need to prevent these nodes from being considered in this moveto */ |
| 5608 | if ((idx < orig_used) && (idx > i)) { |
| 5609 | set->val.scnodes[idx].in_ctx = 2; |
| 5610 | temp_ctx = 1; |
| 5611 | } |
| 5612 | } |
| 5613 | } |
| 5614 | |
| 5615 | if (!mod_idx) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5616 | /* mod was specified, we are not going through the whole context */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5617 | break; |
| 5618 | } |
| 5619 | /* next iteration */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5620 | mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5621 | } |
| 5622 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5623 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 5624 | iter = NULL; |
| 5625 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5626 | if (!moveto_scnode_check(iter, set->root_type, set->context_op, ncname, (mod ? mod : set->local_mod))) { |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5627 | idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5628 | if ((idx < orig_used) && (idx > i)) { |
| 5629 | set->val.scnodes[idx].in_ctx = 2; |
| 5630 | temp_ctx = 1; |
| 5631 | } |
| 5632 | } |
| 5633 | } |
| 5634 | } |
| 5635 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5636 | |
| 5637 | /* correct temporary in_ctx values */ |
| 5638 | if (temp_ctx) { |
| 5639 | for (i = 0; i < orig_used; ++i) { |
| 5640 | if (set->val.scnodes[i].in_ctx == 2) { |
| 5641 | set->val.scnodes[i].in_ctx = 1; |
| 5642 | } |
| 5643 | } |
| 5644 | } |
| 5645 | |
| 5646 | return LY_SUCCESS; |
| 5647 | } |
| 5648 | |
| 5649 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5650 | * @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] | 5651 | * Context position aware. |
| 5652 | * |
| 5653 | * @param[in] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5654 | * @param[in] mod Matching node module, NULL for any. |
| 5655 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5656 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5657 | */ |
| 5658 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5659 | moveto_node_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5660 | { |
| 5661 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5662 | const struct lyd_node *next, *elem, *start; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5663 | struct lyxp_set ret_set; |
| 5664 | LY_ERR rc; |
| 5665 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5666 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5667 | return LY_SUCCESS; |
| 5668 | } |
| 5669 | |
| 5670 | if (set->type != LYXP_SET_NODE_SET) { |
| 5671 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5672 | return LY_EVALID; |
| 5673 | } |
| 5674 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5675 | /* replace the original nodes (and throws away all text and meta nodes, root is replaced by a child) */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5676 | rc = moveto_node(set, NULL, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5677 | LY_CHECK_RET(rc); |
| 5678 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5679 | /* 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] | 5680 | set_init(&ret_set, set); |
| 5681 | for (i = 0; i < set->used; ++i) { |
| 5682 | |
| 5683 | /* TREE DFS */ |
| 5684 | start = set->val.nodes[i].node; |
| 5685 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5686 | rc = moveto_node_check(elem, set->root_type, set->context_op, ncname, mod); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5687 | if (!rc) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5688 | /* add matching node into result set */ |
| 5689 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
| 5690 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
| 5691 | /* the node is a duplicate, we'll process it later in the set */ |
| 5692 | goto skip_children; |
| 5693 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5694 | } else if (rc == LY_EINCOMPLETE) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5695 | return rc; |
| 5696 | } else if (rc == LY_EINVAL) { |
| 5697 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5698 | } |
| 5699 | |
| 5700 | /* TREE DFS NEXT ELEM */ |
| 5701 | /* select element for the next run - children first */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5702 | next = lyd_node_children(elem, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5703 | if (!next) { |
| 5704 | skip_children: |
| 5705 | /* no children, so try siblings, but only if it's not the start, |
| 5706 | * that is considered to be the root and it's siblings are not traversed */ |
| 5707 | if (elem != start) { |
| 5708 | next = elem->next; |
| 5709 | } else { |
| 5710 | break; |
| 5711 | } |
| 5712 | } |
| 5713 | while (!next) { |
| 5714 | /* no siblings, go back through the parents */ |
| 5715 | if ((struct lyd_node *)elem->parent == start) { |
| 5716 | /* we are done, no next element to process */ |
| 5717 | break; |
| 5718 | } |
| 5719 | /* parent is already processed, go to its sibling */ |
| 5720 | elem = (struct lyd_node *)elem->parent; |
| 5721 | next = elem->next; |
| 5722 | } |
| 5723 | } |
| 5724 | } |
| 5725 | |
| 5726 | /* make the temporary set the current one */ |
| 5727 | ret_set.ctx_pos = set->ctx_pos; |
| 5728 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5729 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5730 | memcpy(set, &ret_set, sizeof *set); |
| 5731 | |
| 5732 | return LY_SUCCESS; |
| 5733 | } |
| 5734 | |
| 5735 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5736 | * @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] | 5737 | * |
| 5738 | * @param[in] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5739 | * @param[in] mod Matching node module, NULL for any. |
| 5740 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5741 | * @param[in] options XPath options. |
| 5742 | * @return LY_ERR |
| 5743 | */ |
| 5744 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5745 | moveto_scnode_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5746 | { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5747 | int i, orig_used, idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5748 | const struct lysc_node *next, *elem, *start; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5749 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5750 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5751 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5752 | return LY_SUCCESS; |
| 5753 | } |
| 5754 | |
| 5755 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 5756 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5757 | return LY_EVALID; |
| 5758 | } |
| 5759 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5760 | orig_used = set->used; |
| 5761 | for (i = 0; i < orig_used; ++i) { |
| 5762 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5763 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5764 | continue; |
| 5765 | } |
| 5766 | |
| 5767 | /* remember context node */ |
| 5768 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5769 | } else { |
| 5770 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5771 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5772 | |
| 5773 | /* TREE DFS */ |
| 5774 | start = set->val.scnodes[i].scnode; |
| 5775 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5776 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 5777 | /* schema-only nodes, skip root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5778 | goto next_iter; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5779 | } |
| 5780 | |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5781 | rc = moveto_scnode_check(elem, set->root_type, set->context_op, ncname, mod); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5782 | if (!rc) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 5783 | if ((idx = lyxp_set_scnode_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) > -1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5784 | set->val.scnodes[idx].in_ctx = 1; |
| 5785 | if (idx > i) { |
| 5786 | /* we will process it later in the set */ |
| 5787 | goto skip_children; |
| 5788 | } |
| 5789 | } else { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 5790 | lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5791 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5792 | } else if (rc == LY_EINVAL) { |
| 5793 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5794 | } |
| 5795 | |
| 5796 | next_iter: |
| 5797 | /* TREE DFS NEXT ELEM */ |
| 5798 | /* select element for the next run - children first */ |
| 5799 | 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] | 5800 | if (!next) { |
| 5801 | skip_children: |
| 5802 | /* no children, so try siblings, but only if it's not the start, |
| 5803 | * that is considered to be the root and it's siblings are not traversed */ |
| 5804 | if (elem != start) { |
| 5805 | next = elem->next; |
| 5806 | } else { |
| 5807 | break; |
| 5808 | } |
| 5809 | } |
| 5810 | while (!next) { |
| 5811 | /* no siblings, go back through the parents */ |
| 5812 | if (elem->parent == start) { |
| 5813 | /* we are done, no next element to process */ |
| 5814 | break; |
| 5815 | } |
| 5816 | /* parent is already processed, go to its sibling */ |
| 5817 | elem = elem->parent; |
| 5818 | next = elem->next; |
| 5819 | } |
| 5820 | } |
| 5821 | } |
| 5822 | |
| 5823 | return LY_SUCCESS; |
| 5824 | } |
| 5825 | |
| 5826 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5827 | * @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] | 5828 | * Indirectly context position aware. |
| 5829 | * |
| 5830 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5831 | * @param[in] mod Matching metadata module, NULL for any. |
| 5832 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5833 | * @return LY_ERR |
| 5834 | */ |
| 5835 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5836 | 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] | 5837 | { |
| 5838 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5839 | int replaced; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5840 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5841 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5842 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5843 | return LY_SUCCESS; |
| 5844 | } |
| 5845 | |
| 5846 | if (set->type != LYXP_SET_NODE_SET) { |
| 5847 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5848 | return LY_EVALID; |
| 5849 | } |
| 5850 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5851 | for (i = 0; i < set->used; ) { |
| 5852 | replaced = 0; |
| 5853 | |
| 5854 | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
| 5855 | * our attributes are always qualified */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5856 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5857 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5858 | |
| 5859 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5860 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5861 | continue; |
| 5862 | } |
| 5863 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5864 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5865 | /* match */ |
| 5866 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5867 | set->val.meta[i].meta = sub; |
| 5868 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5869 | /* pos does not change */ |
| 5870 | replaced = 1; |
| 5871 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5872 | 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] | 5873 | } |
| 5874 | ++i; |
| 5875 | } |
| 5876 | } |
| 5877 | } |
| 5878 | |
| 5879 | if (!replaced) { |
| 5880 | /* no match */ |
| 5881 | set_remove_node(set, i); |
| 5882 | } |
| 5883 | } |
| 5884 | |
| 5885 | return LY_SUCCESS; |
| 5886 | } |
| 5887 | |
| 5888 | /** |
| 5889 | * @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] | 5890 | * Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5891 | * |
| 5892 | * @param[in,out] set1 Set to use for the result. |
| 5893 | * @param[in] set2 Set that is copied to @p set1. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5894 | * @return LY_ERR |
| 5895 | */ |
| 5896 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5897 | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5898 | { |
| 5899 | LY_ERR rc; |
| 5900 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5901 | if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5902 | LOGVAL(set1->ctx, LY_VLOG_LYD, set1->ctx_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2)); |
| 5903 | return LY_EVALID; |
| 5904 | } |
| 5905 | |
| 5906 | /* set2 is empty or both set1 and set2 */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5907 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5908 | return LY_SUCCESS; |
| 5909 | } |
| 5910 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5911 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5912 | memcpy(set1, set2, sizeof *set1); |
| 5913 | /* dynamic memory belongs to set1 now, do not free */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5914 | memset(set2, 0, sizeof *set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5915 | return LY_SUCCESS; |
| 5916 | } |
| 5917 | |
| 5918 | /* we assume sets are sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5919 | assert(!set_sort(set1) && !set_sort(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5920 | |
| 5921 | /* sort, remove duplicates */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5922 | rc = set_sorted_merge(set1, set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5923 | LY_CHECK_RET(rc); |
| 5924 | |
| 5925 | /* final set must be sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5926 | assert(!set_sort(set1)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5927 | |
| 5928 | return LY_SUCCESS; |
| 5929 | } |
| 5930 | |
| 5931 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5932 | * @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] | 5933 | * Context position aware. |
| 5934 | * |
| 5935 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5936 | * @param[in] mod Matching metadata module, NULL for any. |
| 5937 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5938 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5939 | */ |
| 5940 | static int |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5941 | moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5942 | { |
| 5943 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5944 | int replaced; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5945 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5946 | struct lyxp_set *set_all_desc = NULL; |
| 5947 | LY_ERR rc; |
| 5948 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5949 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5950 | return LY_SUCCESS; |
| 5951 | } |
| 5952 | |
| 5953 | if (set->type != LYXP_SET_NODE_SET) { |
| 5954 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5955 | return LY_EVALID; |
| 5956 | } |
| 5957 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5958 | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
| 5959 | * but it likely won't be used much, so it's a waste of time */ |
| 5960 | /* copy the context */ |
| 5961 | set_all_desc = set_copy(set); |
| 5962 | /* get all descendant nodes (the original context nodes are removed) */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5963 | rc = moveto_node_alldesc(set_all_desc, NULL, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5964 | if (rc != LY_SUCCESS) { |
| 5965 | lyxp_set_free(set_all_desc); |
| 5966 | return rc; |
| 5967 | } |
| 5968 | /* prepend the original context nodes */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5969 | rc = moveto_union(set, set_all_desc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5970 | if (rc != LY_SUCCESS) { |
| 5971 | lyxp_set_free(set_all_desc); |
| 5972 | return rc; |
| 5973 | } |
| 5974 | lyxp_set_free(set_all_desc); |
| 5975 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5976 | for (i = 0; i < set->used; ) { |
| 5977 | replaced = 0; |
| 5978 | |
| 5979 | /* only attributes of an elem can be in the result, skip all the rest, |
| 5980 | * we have all attributes qualified in lyd tree */ |
| 5981 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5982 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5983 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5984 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5985 | continue; |
| 5986 | } |
| 5987 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5988 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5989 | /* match */ |
| 5990 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5991 | set->val.meta[i].meta = sub; |
| 5992 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5993 | /* pos does not change */ |
| 5994 | replaced = 1; |
| 5995 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5996 | 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] | 5997 | } |
| 5998 | ++i; |
| 5999 | } |
| 6000 | } |
| 6001 | } |
| 6002 | |
| 6003 | if (!replaced) { |
| 6004 | /* no match */ |
| 6005 | set_remove_node(set, i); |
| 6006 | } |
| 6007 | } |
| 6008 | |
| 6009 | return LY_SUCCESS; |
| 6010 | } |
| 6011 | |
| 6012 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6013 | * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET. |
| 6014 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6015 | * |
| 6016 | * @param[in] parent Current parent. |
| 6017 | * @param[in] parent_pos Position of @p parent. |
| 6018 | * @param[in] parent_type Node type of @p parent. |
| 6019 | * @param[in,out] to_set Set to use. |
| 6020 | * @param[in] dup_check_set Set for checking duplicities. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6021 | * @param[in] options XPath options. |
| 6022 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6023 | */ |
| 6024 | static LY_ERR |
| 6025 | moveto_self_add_children_r(const struct lyd_node *parent, uint32_t parent_pos, enum lyxp_node_type parent_type, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame^] | 6026 | struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6027 | { |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6028 | const struct lyd_node *iter, *first; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6029 | LY_ERR rc; |
| 6030 | |
| 6031 | switch (parent_type) { |
| 6032 | case LYXP_NODE_ROOT: |
| 6033 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6034 | assert(!parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6035 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6036 | /* add all top-level nodes as elements */ |
| 6037 | first = to_set->tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6038 | break; |
| 6039 | case LYXP_NODE_ELEM: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6040 | /* add just the text node of this term element node */ |
| 6041 | if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6042 | if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) { |
| 6043 | set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used); |
| 6044 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6045 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6046 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6047 | |
| 6048 | /* add all the children of this node */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 6049 | first = lyd_node_children(parent, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6050 | break; |
| 6051 | default: |
| 6052 | LOGINT_RET(parent->schema->module->ctx); |
| 6053 | } |
| 6054 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6055 | /* add all top-level nodes as elements */ |
| 6056 | LY_LIST_FOR(first, iter) { |
| 6057 | /* context check */ |
| 6058 | if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) { |
| 6059 | continue; |
| 6060 | } |
| 6061 | |
| 6062 | /* when check */ |
| 6063 | if (moveto_when_check(iter)) { |
| 6064 | return LY_EINCOMPLETE; |
| 6065 | } |
| 6066 | |
| 6067 | if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) { |
| 6068 | set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used); |
| 6069 | |
| 6070 | /* also add all the children of this node, recursively */ |
| 6071 | rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options); |
| 6072 | LY_CHECK_RET(rc); |
| 6073 | } |
| 6074 | } |
| 6075 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6076 | return LY_SUCCESS; |
| 6077 | } |
| 6078 | |
| 6079 | /** |
| 6080 | * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET |
| 6081 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6082 | * |
| 6083 | * @param[in,out] set Set to use. |
| 6084 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6085 | * @param[in] options XPath options. |
| 6086 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6087 | */ |
| 6088 | static LY_ERR |
| 6089 | moveto_self(struct lyxp_set *set, int all_desc, int options) |
| 6090 | { |
| 6091 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6092 | struct lyxp_set ret_set; |
| 6093 | LY_ERR rc; |
| 6094 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6095 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6096 | return LY_SUCCESS; |
| 6097 | } |
| 6098 | |
| 6099 | if (set->type != LYXP_SET_NODE_SET) { |
| 6100 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 6101 | return LY_EVALID; |
| 6102 | } |
| 6103 | |
| 6104 | /* nothing to do */ |
| 6105 | if (!all_desc) { |
| 6106 | return LY_SUCCESS; |
| 6107 | } |
| 6108 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6109 | /* add all the children, they get added recursively */ |
| 6110 | set_init(&ret_set, set); |
| 6111 | for (i = 0; i < set->used; ++i) { |
| 6112 | /* copy the current node to tmp */ |
| 6113 | set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used); |
| 6114 | |
| 6115 | /* do not touch attributes and text nodes */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6116 | 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] | 6117 | continue; |
| 6118 | } |
| 6119 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6120 | /* add all the children */ |
| 6121 | rc = moveto_self_add_children_r(set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, &ret_set, |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6122 | set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6123 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6124 | lyxp_set_free_content(&ret_set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6125 | return rc; |
| 6126 | } |
| 6127 | } |
| 6128 | |
| 6129 | /* use the temporary set as the current one */ |
| 6130 | ret_set.ctx_pos = set->ctx_pos; |
| 6131 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6132 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6133 | memcpy(set, &ret_set, sizeof *set); |
| 6134 | |
| 6135 | return LY_SUCCESS; |
| 6136 | } |
| 6137 | |
| 6138 | /** |
| 6139 | * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET |
| 6140 | * (or LYXP_SET_EMPTY). |
| 6141 | * |
| 6142 | * @param[in,out] set Set to use. |
| 6143 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6144 | * @param[in] options XPath options. |
| 6145 | * @return LY_ERR |
| 6146 | */ |
| 6147 | static LY_ERR |
| 6148 | moveto_scnode_self(struct lyxp_set *set, int all_desc, int options) |
| 6149 | { |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6150 | int getnext_opts; |
| 6151 | uint32_t i, mod_idx; |
| 6152 | const struct lysc_node *iter, *start_parent; |
| 6153 | const struct lys_module *mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 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_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6160 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6161 | return LY_EVALID; |
| 6162 | } |
| 6163 | |
| 6164 | /* nothing to do */ |
| 6165 | if (!all_desc) { |
| 6166 | return LY_SUCCESS; |
| 6167 | } |
| 6168 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6169 | /* getnext opts */ |
| 6170 | getnext_opts = LYS_GETNEXT_NOSTATECHECK; |
| 6171 | if (options & LYXP_SCNODE_OUTPUT) { |
| 6172 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 6173 | } |
| 6174 | |
| 6175 | /* add all the children, recursively as they are being added into the same set */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6176 | for (i = 0; i < set->used; ++i) { |
| 6177 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6178 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6179 | continue; |
| 6180 | } |
| 6181 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6182 | /* remember context node */ |
| 6183 | set->val.scnodes[i].in_ctx = -1; |
| 6184 | } else { |
| 6185 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6186 | } |
| 6187 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6188 | start_parent = set->val.scnodes[i].scnode; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6189 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6190 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
| 6191 | /* it can actually be in any module, it's all <running> */ |
| 6192 | mod_idx = 0; |
| 6193 | while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
| 6194 | iter = NULL; |
| 6195 | /* module may not be implemented */ |
| 6196 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
| 6197 | /* context check */ |
| 6198 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) { |
| 6199 | continue; |
| 6200 | } |
| 6201 | |
| 6202 | lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
| 6203 | /* throw away the insert index, we want to consider that node again, recursively */ |
| 6204 | } |
| 6205 | } |
| 6206 | |
| 6207 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 6208 | iter = NULL; |
| 6209 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6210 | /* context check */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6211 | 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] | 6212 | continue; |
| 6213 | } |
| 6214 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6215 | lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6216 | } |
| 6217 | } |
| 6218 | } |
| 6219 | |
| 6220 | return LY_SUCCESS; |
| 6221 | } |
| 6222 | |
| 6223 | /** |
| 6224 | * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET |
| 6225 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6226 | * |
| 6227 | * @param[in] set Set to use. |
| 6228 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6229 | * @param[in] options XPath options. |
| 6230 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6231 | */ |
| 6232 | static LY_ERR |
| 6233 | moveto_parent(struct lyxp_set *set, int all_desc, int options) |
| 6234 | { |
| 6235 | LY_ERR rc; |
| 6236 | uint32_t i; |
| 6237 | struct lyd_node *node, *new_node; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6238 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6239 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6240 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6241 | return LY_SUCCESS; |
| 6242 | } |
| 6243 | |
| 6244 | if (set->type != LYXP_SET_NODE_SET) { |
| 6245 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 6246 | return LY_EVALID; |
| 6247 | } |
| 6248 | |
| 6249 | if (all_desc) { |
| 6250 | /* <path>//.. == <path>//./.. */ |
| 6251 | rc = moveto_self(set, 1, options); |
| 6252 | LY_CHECK_RET(rc); |
| 6253 | } |
| 6254 | |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 6255 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6256 | node = set->val.nodes[i].node; |
| 6257 | |
| 6258 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 6259 | new_node = (struct lyd_node *)node->parent; |
| 6260 | } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) { |
| 6261 | new_node = node; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6262 | } else if (set->val.nodes[i].type == LYXP_NODE_META) { |
| 6263 | new_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6264 | if (!new_node) { |
| 6265 | LOGINT_RET(set->ctx); |
| 6266 | } |
| 6267 | } else { |
| 6268 | /* root does not have a parent */ |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6269 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6270 | continue; |
| 6271 | } |
| 6272 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6273 | /* when check */ |
| 6274 | if (moveto_when_check(new_node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6275 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6276 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6277 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6278 | if (!new_node) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6279 | /* node already there can also be the root */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6280 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6281 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6282 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6283 | /* 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] | 6284 | new_type = LYXP_NODE_ELEM; |
| 6285 | } |
| 6286 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6287 | if (set_dup_node_check(set, new_node, new_type, -1)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6288 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6289 | } else { |
| 6290 | set_replace_node(set, new_node, 0, new_type, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6291 | } |
| 6292 | } |
| 6293 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6294 | set_remove_nodes_none(set); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6295 | assert(!set_sort(set) && !set_sorted_dup_node_clean(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6296 | |
| 6297 | return LY_SUCCESS; |
| 6298 | } |
| 6299 | |
| 6300 | /** |
| 6301 | * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET |
| 6302 | * (or LYXP_SET_EMPTY). |
| 6303 | * |
| 6304 | * @param[in] set Set to use. |
| 6305 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6306 | * @param[in] options XPath options. |
| 6307 | * @return LY_ERR |
| 6308 | */ |
| 6309 | static LY_ERR |
| 6310 | moveto_scnode_parent(struct lyxp_set *set, int all_desc, int options) |
| 6311 | { |
| 6312 | int idx, i, orig_used, temp_ctx = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6313 | const struct lysc_node *node, *new_node; |
| 6314 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6315 | LY_ERR rc; |
| 6316 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6317 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6318 | return LY_SUCCESS; |
| 6319 | } |
| 6320 | |
| 6321 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6322 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6323 | return LY_EVALID; |
| 6324 | } |
| 6325 | |
| 6326 | if (all_desc) { |
| 6327 | /* <path>//.. == <path>//./.. */ |
| 6328 | rc = moveto_scnode_self(set, 1, options); |
| 6329 | LY_CHECK_RET(rc); |
| 6330 | } |
| 6331 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6332 | orig_used = set->used; |
| 6333 | for (i = 0; i < orig_used; ++i) { |
| 6334 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6335 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6336 | continue; |
| 6337 | } |
| 6338 | |
| 6339 | /* remember context node */ |
| 6340 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 6341 | } else { |
| 6342 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6343 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6344 | |
| 6345 | node = set->val.scnodes[i].scnode; |
| 6346 | |
| 6347 | if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6348 | new_node = lysc_data_parent(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6349 | } else { |
| 6350 | /* root does not have a parent */ |
| 6351 | continue; |
| 6352 | } |
| 6353 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6354 | if (!new_node) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6355 | /* node has no parent */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6356 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6357 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6358 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6359 | /* 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] | 6360 | new_type = LYXP_NODE_ELEM; |
| 6361 | } |
| 6362 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6363 | idx = lyxp_set_scnode_insert_node(set, new_node, new_type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6364 | if ((idx < orig_used) && (idx > i)) { |
| 6365 | set->val.scnodes[idx].in_ctx = 2; |
| 6366 | temp_ctx = 1; |
| 6367 | } |
| 6368 | } |
| 6369 | |
| 6370 | if (temp_ctx) { |
| 6371 | for (i = 0; i < orig_used; ++i) { |
| 6372 | if (set->val.scnodes[i].in_ctx == 2) { |
| 6373 | set->val.scnodes[i].in_ctx = 1; |
| 6374 | } |
| 6375 | } |
| 6376 | } |
| 6377 | |
| 6378 | return LY_SUCCESS; |
| 6379 | } |
| 6380 | |
| 6381 | /** |
| 6382 | * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'. |
| 6383 | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
| 6384 | * |
| 6385 | * @param[in,out] set1 Set to use for the result. |
| 6386 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6387 | * @param[in] op Comparison operator to process. |
| 6388 | * @param[in] options XPath options. |
| 6389 | * @return LY_ERR |
| 6390 | */ |
| 6391 | static LY_ERR |
| 6392 | moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, int options) |
| 6393 | { |
| 6394 | /* |
| 6395 | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
| 6396 | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
| 6397 | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6398 | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6399 | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
| 6400 | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6401 | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6402 | * |
| 6403 | * '=' or '!=' |
| 6404 | * BOOLEAN + BOOLEAN |
| 6405 | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6406 | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6407 | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6408 | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6409 | * NUMBER + NUMBER |
| 6410 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6411 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6412 | * STRING + STRING |
| 6413 | * |
| 6414 | * '<=', '<', '>=', '>' |
| 6415 | * NUMBER + NUMBER |
| 6416 | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6417 | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6418 | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6419 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6420 | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6421 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6422 | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6423 | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6424 | */ |
| 6425 | struct lyxp_set iter1, iter2; |
| 6426 | int result; |
| 6427 | int64_t i; |
| 6428 | LY_ERR rc; |
| 6429 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6430 | memset(&iter1, 0, sizeof iter1); |
| 6431 | memset(&iter2, 0, sizeof iter2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6432 | |
| 6433 | /* iterative evaluation with node-sets */ |
| 6434 | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
| 6435 | if (set1->type == LYXP_SET_NODE_SET) { |
| 6436 | for (i = 0; i < set1->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6437 | /* cast set1 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6438 | switch (set2->type) { |
| 6439 | case LYXP_SET_NUMBER: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6440 | rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6441 | break; |
| 6442 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6443 | rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6444 | break; |
| 6445 | default: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6446 | rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6447 | break; |
| 6448 | } |
| 6449 | LY_CHECK_RET(rc); |
| 6450 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6451 | /* canonize set2 */ |
| 6452 | LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc); |
| 6453 | |
| 6454 | /* compare recursively */ |
| 6455 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
| 6456 | lyxp_set_free_content(&iter2); |
| 6457 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6458 | |
| 6459 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6460 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6461 | set_fill_boolean(set1, 1); |
| 6462 | return LY_SUCCESS; |
| 6463 | } |
| 6464 | } |
| 6465 | } else { |
| 6466 | for (i = 0; i < set2->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6467 | /* set set2 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6468 | switch (set1->type) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6469 | case LYXP_SET_NUMBER: |
| 6470 | rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i); |
| 6471 | break; |
| 6472 | case LYXP_SET_BOOLEAN: |
| 6473 | rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i); |
| 6474 | break; |
| 6475 | default: |
| 6476 | rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i); |
| 6477 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6478 | } |
| 6479 | LY_CHECK_RET(rc); |
| 6480 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6481 | /* canonize set1 */ |
| 6482 | 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] | 6483 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6484 | /* compare recursively */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6485 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6486 | lyxp_set_free_content(&iter2); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6487 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6488 | |
| 6489 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6490 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6491 | set_fill_boolean(set1, 1); |
| 6492 | return LY_SUCCESS; |
| 6493 | } |
| 6494 | } |
| 6495 | } |
| 6496 | |
| 6497 | /* false for all nodes */ |
| 6498 | set_fill_boolean(set1, 0); |
| 6499 | return LY_SUCCESS; |
| 6500 | } |
| 6501 | |
| 6502 | /* first convert properly */ |
| 6503 | if ((op[0] == '=') || (op[0] == '!')) { |
| 6504 | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6505 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
| 6506 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6507 | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6508 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6509 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6510 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6511 | LY_CHECK_RET(rc); |
| 6512 | } /* else we have 2 strings */ |
| 6513 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6514 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6515 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6516 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6517 | LY_CHECK_RET(rc); |
| 6518 | } |
| 6519 | |
| 6520 | assert(set1->type == set2->type); |
| 6521 | |
| 6522 | /* compute result */ |
| 6523 | if (op[0] == '=') { |
| 6524 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6525 | result = (set1->val.bln == set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6526 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6527 | result = (set1->val.num == set2->val.num); |
| 6528 | } else { |
| 6529 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6530 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6531 | } |
| 6532 | } else if (op[0] == '!') { |
| 6533 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6534 | result = (set1->val.bln != set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6535 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6536 | result = (set1->val.num != set2->val.num); |
| 6537 | } else { |
| 6538 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6539 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6540 | } |
| 6541 | } else { |
| 6542 | assert(set1->type == LYXP_SET_NUMBER); |
| 6543 | if (op[0] == '<') { |
| 6544 | if (op[1] == '=') { |
| 6545 | result = (set1->val.num <= set2->val.num); |
| 6546 | } else { |
| 6547 | result = (set1->val.num < set2->val.num); |
| 6548 | } |
| 6549 | } else { |
| 6550 | if (op[1] == '=') { |
| 6551 | result = (set1->val.num >= set2->val.num); |
| 6552 | } else { |
| 6553 | result = (set1->val.num > set2->val.num); |
| 6554 | } |
| 6555 | } |
| 6556 | } |
| 6557 | |
| 6558 | /* assign result */ |
| 6559 | if (result) { |
| 6560 | set_fill_boolean(set1, 1); |
| 6561 | } else { |
| 6562 | set_fill_boolean(set1, 0); |
| 6563 | } |
| 6564 | |
| 6565 | return LY_SUCCESS; |
| 6566 | } |
| 6567 | |
| 6568 | /** |
| 6569 | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
| 6570 | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
| 6571 | * |
| 6572 | * @param[in,out] set1 Set to use for the result. |
| 6573 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6574 | * @param[in] op Operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6575 | * @return LY_ERR |
| 6576 | */ |
| 6577 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6578 | 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] | 6579 | { |
| 6580 | LY_ERR rc; |
| 6581 | |
| 6582 | /* unary '-' */ |
| 6583 | if (!set2 && (op[0] == '-')) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6584 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6585 | LY_CHECK_RET(rc); |
| 6586 | set1->val.num *= -1; |
| 6587 | lyxp_set_free(set2); |
| 6588 | return LY_SUCCESS; |
| 6589 | } |
| 6590 | |
| 6591 | assert(set1 && set2); |
| 6592 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6593 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6594 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6595 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6596 | LY_CHECK_RET(rc); |
| 6597 | |
| 6598 | switch (op[0]) { |
| 6599 | /* '+' */ |
| 6600 | case '+': |
| 6601 | set1->val.num += set2->val.num; |
| 6602 | break; |
| 6603 | |
| 6604 | /* '-' */ |
| 6605 | case '-': |
| 6606 | set1->val.num -= set2->val.num; |
| 6607 | break; |
| 6608 | |
| 6609 | /* '*' */ |
| 6610 | case '*': |
| 6611 | set1->val.num *= set2->val.num; |
| 6612 | break; |
| 6613 | |
| 6614 | /* 'div' */ |
| 6615 | case 'd': |
| 6616 | set1->val.num /= set2->val.num; |
| 6617 | break; |
| 6618 | |
| 6619 | /* 'mod' */ |
| 6620 | case 'm': |
| 6621 | set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num); |
| 6622 | break; |
| 6623 | |
| 6624 | default: |
| 6625 | LOGINT_RET(set1->ctx); |
| 6626 | } |
| 6627 | |
| 6628 | return LY_SUCCESS; |
| 6629 | } |
| 6630 | |
| 6631 | /* |
| 6632 | * eval functions |
| 6633 | * |
| 6634 | * They execute a parsed XPath expression on some data subtree. |
| 6635 | */ |
| 6636 | |
| 6637 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6638 | * @brief Evaluate Predicate. Logs directly on error. |
| 6639 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6640 | * [9] Predicate ::= '[' Expr ']' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6641 | * |
| 6642 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6643 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6644 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6645 | * @param[in] options XPath options. |
| 6646 | * @param[in] parent_pos_pred Whether parent predicate was a positional one. |
| 6647 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6648 | */ |
| 6649 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6650 | eval_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options, int parent_pos_pred) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6651 | { |
| 6652 | LY_ERR rc; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 6653 | uint16_t i, orig_exp; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6654 | uint32_t orig_pos, orig_size; |
| 6655 | int32_t pred_in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6656 | struct lyxp_set set2; |
| 6657 | struct lyd_node *orig_parent; |
| 6658 | |
| 6659 | /* '[' */ |
| 6660 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6661 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6662 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6663 | |
| 6664 | if (!set) { |
| 6665 | only_parse: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6666 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6667 | LY_CHECK_RET(rc); |
| 6668 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 6669 | /* 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] | 6670 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6671 | |
| 6672 | /* empty set, nothing to evaluate */ |
| 6673 | if (!set->used) { |
| 6674 | goto only_parse; |
| 6675 | } |
| 6676 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6677 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6678 | orig_pos = 0; |
| 6679 | orig_size = set->used; |
| 6680 | orig_parent = NULL; |
Michal Vasko | 39dbf35 | 2020-05-21 10:08:59 +0200 | [diff] [blame] | 6681 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6682 | set_init(&set2, set); |
| 6683 | set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0); |
| 6684 | /* remember the node context position for position() and context size for last(), |
| 6685 | * predicates should always be evaluated with respect to the child axis (since we do |
| 6686 | * not support explicit axes) so we assign positions based on their parents */ |
| 6687 | if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) { |
| 6688 | orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent; |
| 6689 | orig_pos = 1; |
| 6690 | } else { |
| 6691 | ++orig_pos; |
| 6692 | } |
| 6693 | |
| 6694 | set2.ctx_pos = orig_pos; |
| 6695 | set2.ctx_size = orig_size; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6696 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6697 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6698 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6699 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6700 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6701 | return rc; |
| 6702 | } |
| 6703 | |
| 6704 | /* number is a position */ |
| 6705 | if (set2.type == LYXP_SET_NUMBER) { |
| 6706 | if ((long long)set2.val.num == orig_pos) { |
| 6707 | set2.val.num = 1; |
| 6708 | } else { |
| 6709 | set2.val.num = 0; |
| 6710 | } |
| 6711 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6712 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6713 | |
| 6714 | /* predicate satisfied or not? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6715 | if (!set2.val.bln) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6716 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6717 | } |
| 6718 | } |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6719 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6720 | |
| 6721 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 6722 | for (i = 0; i < set->used; ++i) { |
| 6723 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6724 | /* there is a currently-valid node */ |
| 6725 | break; |
| 6726 | } |
| 6727 | } |
| 6728 | /* empty set, nothing to evaluate */ |
| 6729 | if (i == set->used) { |
| 6730 | goto only_parse; |
| 6731 | } |
| 6732 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6733 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6734 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6735 | /* set special in_ctx to all the valid snodes */ |
| 6736 | pred_in_ctx = set_scnode_new_in_ctx(set); |
| 6737 | |
| 6738 | /* use the valid snodes one-by-one */ |
| 6739 | for (i = 0; i < set->used; ++i) { |
| 6740 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
| 6741 | continue; |
| 6742 | } |
| 6743 | set->val.scnodes[i].in_ctx = 1; |
| 6744 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6745 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6746 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6747 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6748 | LY_CHECK_RET(rc); |
| 6749 | |
| 6750 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
| 6751 | } |
| 6752 | |
| 6753 | /* restore the state as it was before the predicate */ |
| 6754 | for (i = 0; i < set->used; ++i) { |
| 6755 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6756 | set->val.scnodes[i].in_ctx = 0; |
| 6757 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
| 6758 | set->val.scnodes[i].in_ctx = 1; |
| 6759 | } |
| 6760 | } |
| 6761 | |
| 6762 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6763 | set2.type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6764 | set_fill_set(&set2, set); |
| 6765 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6766 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6767 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6768 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6769 | return rc; |
| 6770 | } |
| 6771 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6772 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6773 | if (!set2.val.bln) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6774 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6775 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6776 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6777 | } |
| 6778 | |
| 6779 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6780 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6781 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6782 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6783 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6784 | |
| 6785 | return LY_SUCCESS; |
| 6786 | } |
| 6787 | |
| 6788 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6789 | * @brief Evaluate Literal. Logs directly on error. |
| 6790 | * |
| 6791 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6792 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6793 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6794 | */ |
| 6795 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6796 | eval_literal(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6797 | { |
| 6798 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6799 | if (exp->tok_len[*tok_idx] == 2) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6800 | set_fill_string(set, "", 0); |
| 6801 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6802 | 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] | 6803 | } |
| 6804 | } |
| 6805 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6806 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6807 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6808 | } |
| 6809 | |
| 6810 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6811 | * @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] | 6812 | * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6813 | * @param[in] exp Full parsed XPath expression. |
| 6814 | * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success. |
| 6815 | * @param[in] scnode Found schema node as context for the predicate. |
| 6816 | * @param[in] format Format of any prefixes in key names/values. |
| 6817 | * @param[out] predicates Parsed predicates. |
| 6818 | * @param[out] pred_type Type of @p predicates. |
| 6819 | * @return LY_SUCCESS on success, |
| 6820 | * @return LY_ERR on any error. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6821 | */ |
| 6822 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6823 | eval_name_test_try_compile_predicates(struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *scnode, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame^] | 6824 | LY_PREFIX_FORMAT format, struct ly_path_predicate **predicates, |
| 6825 | enum ly_path_pred_type *pred_type) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6826 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6827 | LY_ERR ret = LY_SUCCESS; |
| 6828 | uint16_t key_count, e_idx, pred_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6829 | const struct lysc_node *key; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6830 | size_t pred_len; |
| 6831 | int prev_lo; |
| 6832 | struct lyxp_expr *exp2 = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6833 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6834 | assert(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6835 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6836 | if (scnode->nodetype == LYS_LIST) { |
| 6837 | /* get key count */ |
| 6838 | if (scnode->flags & LYS_KEYLESS) { |
| 6839 | return LY_EINVAL; |
| 6840 | } |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 6841 | for (key_count = 0, key = lysc_node_children(scnode, 0); key && (key->flags & LYS_KEY); key = key->next, ++key_count) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6842 | assert(key_count); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6843 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6844 | /* learn where the predicates end */ |
| 6845 | e_idx = *tok_idx; |
| 6846 | while (key_count) { |
| 6847 | /* '[' */ |
| 6848 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6849 | return LY_EINVAL; |
| 6850 | } |
| 6851 | ++e_idx; |
| 6852 | |
| 6853 | /* ']' */ |
| 6854 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6855 | ++e_idx; |
| 6856 | } |
| 6857 | ++e_idx; |
| 6858 | |
| 6859 | /* another presumably key predicate parsed */ |
| 6860 | --key_count; |
| 6861 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6862 | } else { |
| 6863 | /* learn just where this single predicate ends */ |
| 6864 | e_idx = *tok_idx; |
| 6865 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6866 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6867 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6868 | return LY_EINVAL; |
| 6869 | } |
| 6870 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6871 | |
| 6872 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6873 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6874 | ++e_idx; |
| 6875 | } |
| 6876 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6877 | } |
| 6878 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6879 | /* get the joined length of all the keys predicates/of the single leaf-list predicate */ |
| 6880 | pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx]; |
| 6881 | |
| 6882 | /* turn logging off */ |
| 6883 | prev_lo = ly_log_options(0); |
| 6884 | |
| 6885 | /* parse the predicate(s) */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6886 | LY_CHECK_GOTO(ret = ly_path_parse_predicate(scnode->module->ctx, scnode, exp->expr + exp->tok_pos[*tok_idx], pred_len, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6887 | LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup); |
| 6888 | |
| 6889 | /* compile */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6890 | ret = ly_path_compile_predicate(scnode->module->ctx, scnode, scnode->module, scnode, exp2, &pred_idx, |
| 6891 | format, scnode->module, predicates, pred_type); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6892 | LY_CHECK_GOTO(ret, cleanup); |
| 6893 | |
| 6894 | /* success, the predicate must include all the needed information for hash-based search */ |
| 6895 | *tok_idx = e_idx; |
| 6896 | |
| 6897 | cleanup: |
| 6898 | ly_log_options(prev_lo); |
| 6899 | lyxp_expr_free(scnode->module->ctx, exp2); |
| 6900 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6901 | } |
| 6902 | |
| 6903 | /** |
| 6904 | * @brief Evaluate NameTest and any following Predicates. Logs directly on error. |
| 6905 | * |
| 6906 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 6907 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 6908 | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
| 6909 | * |
| 6910 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6911 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6912 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 6913 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 6914 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6915 | * @param[in] options XPath options. |
| 6916 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6917 | */ |
| 6918 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6919 | eval_name_test_with_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, int attr_axis, int all_desc, struct lyxp_set *set, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame^] | 6920 | int options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6921 | { |
| 6922 | int i; |
| 6923 | char *path; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6924 | const char *ncname, *ncname_dict = NULL; |
| 6925 | uint16_t ncname_len; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6926 | const struct lys_module *moveto_mod; |
| 6927 | const struct lysc_node *scnode = NULL, *tmp; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6928 | struct ly_path_predicate *predicates = NULL; |
| 6929 | enum ly_path_pred_type pred_type = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6930 | LY_ERR rc = LY_SUCCESS; |
| 6931 | |
| 6932 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6933 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6934 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6935 | |
| 6936 | if (!set) { |
| 6937 | goto moveto; |
| 6938 | } |
| 6939 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6940 | ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]]; |
| 6941 | ncname_len = exp->tok_len[*tok_idx - 1]; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6942 | |
| 6943 | /* parse (and skip) module name */ |
| 6944 | rc = moveto_resolve_model(&ncname, &ncname_len, set, &moveto_mod); |
| 6945 | LY_CHECK_GOTO(rc, cleanup); |
| 6946 | |
| 6947 | if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) { |
| 6948 | /* find the matching schema node in some parent in the context */ |
| 6949 | for (i = 0; i < (signed)set->used; ++i) { |
| 6950 | if (set->val.nodes[i].type == set->root_type) { |
| 6951 | tmp = lys_find_child(NULL, moveto_mod, ncname, ncname_len, 0, 0); |
| 6952 | } else if ((set->val.nodes[i].type == LYXP_NODE_ELEM) |
| 6953 | && (!scnode || (lysc_data_parent(scnode) != set->val.nodes[i].node->schema))) { |
| 6954 | /* do not repeat the same search */ |
| 6955 | tmp = lys_find_child(set->val.nodes[i].node->schema, moveto_mod, ncname, ncname_len, 0, 0); |
Radek Krejci | b124784 | 2020-07-02 16:22:38 +0200 | [diff] [blame] | 6956 | } else { |
| 6957 | tmp = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6958 | } |
| 6959 | |
| 6960 | /* additional context check */ |
| 6961 | if (tmp && (set->root_type == LYXP_NODE_ROOT_CONFIG) && (tmp->flags & LYS_CONFIG_R)) { |
| 6962 | tmp = NULL; |
| 6963 | } |
| 6964 | |
| 6965 | if (tmp) { |
| 6966 | if (scnode) { |
| 6967 | /* we found a schema node with the same name but at different level, give up, too complicated */ |
| 6968 | scnode = NULL; |
| 6969 | break; |
| 6970 | } else { |
| 6971 | /* remember the found schema node and continue to make sure it can be used */ |
| 6972 | scnode = tmp; |
| 6973 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6974 | } |
| 6975 | } |
| 6976 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6977 | if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 6978 | /* try to create the predicates */ |
| 6979 | if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->format, &predicates, &pred_type)) { |
| 6980 | /* hashes cannot be used */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6981 | scnode = NULL; |
| 6982 | } |
| 6983 | } |
| 6984 | } |
| 6985 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6986 | if (!scnode && moveto_mod) { |
| 6987 | /* we are not able to match based on a schema node and not all the modules match, |
| 6988 | * use dictionary for efficient comparison */ |
| 6989 | ncname_dict = lydict_insert(set->ctx, ncname, ncname_len); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6990 | } |
| 6991 | |
| 6992 | moveto: |
| 6993 | /* move to the attribute(s), data node(s), or schema node(s) */ |
| 6994 | if (attr_axis) { |
| 6995 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 6996 | set_scnode_clear_ctx(set); |
| 6997 | } else { |
| 6998 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6999 | rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7000 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7001 | rc = moveto_attr(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7002 | } |
| 7003 | LY_CHECK_GOTO(rc, cleanup); |
| 7004 | } |
| 7005 | } else { |
| 7006 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7007 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7008 | rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7009 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7010 | rc = moveto_scnode(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7011 | } |
| 7012 | LY_CHECK_GOTO(rc, cleanup); |
| 7013 | |
| 7014 | for (i = set->used - 1; i > -1; --i) { |
| 7015 | if (set->val.scnodes[i].in_ctx > 0) { |
| 7016 | break; |
| 7017 | } |
| 7018 | } |
| 7019 | if (i == -1) { |
| 7020 | path = lysc_path(set->ctx_scnode, LYSC_PATH_LOG, NULL, 0); |
| 7021 | LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".", |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 7022 | ncname_len, ncname, ncname - exp->expr, exp->expr, path); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7023 | free(path); |
| 7024 | } |
| 7025 | } else { |
| 7026 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7027 | rc = moveto_node_alldesc(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7028 | } else { |
| 7029 | if (scnode) { |
| 7030 | /* we can find the nodes using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7031 | rc = moveto_node_hash(set, scnode, predicates); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7032 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7033 | rc = moveto_node(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7034 | } |
| 7035 | } |
| 7036 | LY_CHECK_GOTO(rc, cleanup); |
| 7037 | } |
| 7038 | } |
| 7039 | |
| 7040 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7041 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7042 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7043 | LY_CHECK_RET(rc); |
| 7044 | } |
| 7045 | |
| 7046 | cleanup: |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7047 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7048 | lydict_remove(set->ctx, ncname_dict); |
| 7049 | ly_path_predicates_free(set->ctx, pred_type, scnode, predicates); |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7050 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7051 | return rc; |
| 7052 | } |
| 7053 | |
| 7054 | /** |
| 7055 | * @brief Evaluate NodeType and any following Predicates. Logs directly on error. |
| 7056 | * |
| 7057 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7058 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7059 | * [8] NodeType ::= 'text' | 'node' |
| 7060 | * |
| 7061 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7062 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7063 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 7064 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7065 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7066 | * @param[in] options XPath options. |
| 7067 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7068 | */ |
| 7069 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7070 | eval_node_type_with_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, int attr_axis, int all_desc, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame^] | 7071 | struct lyxp_set *set, int options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7072 | { |
| 7073 | LY_ERR rc; |
| 7074 | |
| 7075 | /* TODO */ |
| 7076 | (void)attr_axis; |
| 7077 | (void)all_desc; |
| 7078 | |
| 7079 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7080 | assert(exp->tok_len[*tok_idx] == 4); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7081 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 7082 | set_scnode_clear_ctx(set); |
| 7083 | /* just for the debug message below */ |
| 7084 | set = NULL; |
| 7085 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7086 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7087 | rc = xpath_node(NULL, 0, set, options); |
| 7088 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7089 | assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7090 | rc = xpath_text(NULL, 0, set, options); |
| 7091 | } |
| 7092 | LY_CHECK_RET(rc); |
| 7093 | } |
| 7094 | } |
| 7095 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7096 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7097 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7098 | |
| 7099 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7100 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7101 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7102 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7103 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7104 | |
| 7105 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7106 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7107 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7108 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7109 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7110 | |
| 7111 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7112 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7113 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7114 | LY_CHECK_RET(rc); |
| 7115 | } |
| 7116 | |
| 7117 | return LY_SUCCESS; |
| 7118 | } |
| 7119 | |
| 7120 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7121 | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
| 7122 | * |
| 7123 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 7124 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7125 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7126 | * |
| 7127 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7128 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7129 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7130 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7131 | * @param[in] options XPath options. |
| 7132 | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
| 7133 | */ |
| 7134 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7135 | eval_relative_location_path(struct lyxp_expr *exp, uint16_t *tok_idx, int all_desc, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7136 | { |
| 7137 | int attr_axis; |
| 7138 | LY_ERR rc; |
| 7139 | |
| 7140 | goto step; |
| 7141 | do { |
| 7142 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7143 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7144 | all_desc = 0; |
| 7145 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7146 | assert(exp->tok_len[*tok_idx] == 2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7147 | all_desc = 1; |
| 7148 | } |
| 7149 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7150 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7151 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7152 | |
| 7153 | step: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7154 | /* evaluate abbreviated axis '@'? if any */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7155 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7156 | attr_axis = 1; |
| 7157 | |
| 7158 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7159 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7160 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7161 | } else { |
| 7162 | attr_axis = 0; |
| 7163 | } |
| 7164 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7165 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7166 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7167 | case LYXP_TOKEN_DOT: |
| 7168 | /* evaluate '.' */ |
| 7169 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7170 | rc = moveto_scnode_self(set, all_desc, options); |
| 7171 | } else { |
| 7172 | rc = moveto_self(set, all_desc, options); |
| 7173 | } |
| 7174 | LY_CHECK_RET(rc); |
| 7175 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7176 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7177 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7178 | break; |
| 7179 | |
| 7180 | case LYXP_TOKEN_DDOT: |
| 7181 | /* evaluate '..' */ |
| 7182 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7183 | rc = moveto_scnode_parent(set, all_desc, options); |
| 7184 | } else { |
| 7185 | rc = moveto_parent(set, all_desc, options); |
| 7186 | } |
| 7187 | LY_CHECK_RET(rc); |
| 7188 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7189 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7190 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7191 | break; |
| 7192 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7193 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7194 | /* evaluate NameTest Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7195 | 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] | 7196 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7197 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7198 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7199 | case LYXP_TOKEN_NODETYPE: |
| 7200 | /* evaluate NodeType Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7201 | 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] | 7202 | LY_CHECK_RET(rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7203 | break; |
| 7204 | |
| 7205 | default: |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 7206 | LOGINT_RET(set ? set->ctx : NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7207 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7208 | } 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] | 7209 | |
| 7210 | return LY_SUCCESS; |
| 7211 | } |
| 7212 | |
| 7213 | /** |
| 7214 | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
| 7215 | * |
| 7216 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 7217 | * |
| 7218 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7219 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7220 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7221 | * @param[in] options XPath options. |
| 7222 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7223 | */ |
| 7224 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7225 | eval_absolute_location_path(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7226 | { |
| 7227 | int all_desc; |
| 7228 | LY_ERR rc; |
| 7229 | |
| 7230 | if (set) { |
| 7231 | /* no matter what tokens follow, we need to be at the root */ |
| 7232 | moveto_root(set, options); |
| 7233 | } |
| 7234 | |
| 7235 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7236 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7237 | /* evaluate '/' - deferred */ |
| 7238 | all_desc = 0; |
| 7239 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7240 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7241 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7242 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7243 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7244 | return LY_SUCCESS; |
| 7245 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7246 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7247 | case LYXP_TOKEN_DOT: |
| 7248 | case LYXP_TOKEN_DDOT: |
| 7249 | case LYXP_TOKEN_AT: |
| 7250 | case LYXP_TOKEN_NAMETEST: |
| 7251 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7252 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7253 | LY_CHECK_RET(rc); |
| 7254 | break; |
| 7255 | default: |
| 7256 | break; |
| 7257 | } |
| 7258 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7259 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 7260 | /* '//' RelativeLocationPath */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7261 | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
| 7262 | all_desc = 1; |
| 7263 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7264 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7265 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7266 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7267 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7268 | LY_CHECK_RET(rc); |
| 7269 | } |
| 7270 | |
| 7271 | return LY_SUCCESS; |
| 7272 | } |
| 7273 | |
| 7274 | /** |
| 7275 | * @brief Evaluate FunctionCall. Logs directly on error. |
| 7276 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7277 | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7278 | * |
| 7279 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7280 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7281 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7282 | * @param[in] options XPath options. |
| 7283 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7284 | */ |
| 7285 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7286 | eval_function_call(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7287 | { |
| 7288 | LY_ERR rc; |
| 7289 | LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, int) = NULL; |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 7290 | uint16_t arg_count = 0, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7291 | struct lyxp_set **args = NULL, **args_aux; |
| 7292 | |
| 7293 | if (set) { |
| 7294 | /* FunctionName */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7295 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7296 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7297 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7298 | xpath_func = &xpath_not; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7299 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7300 | xpath_func = &xpath_sum; |
| 7301 | } |
| 7302 | break; |
| 7303 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7304 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7305 | xpath_func = &xpath_lang; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7306 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7307 | xpath_func = &xpath_last; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7308 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7309 | xpath_func = &xpath_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7310 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7311 | xpath_func = &xpath_true; |
| 7312 | } |
| 7313 | break; |
| 7314 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7315 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7316 | xpath_func = &xpath_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7317 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7318 | xpath_func = &xpath_false; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7319 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7320 | xpath_func = &xpath_floor; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7321 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7322 | xpath_func = &xpath_round; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7323 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7324 | xpath_func = &xpath_deref; |
| 7325 | } |
| 7326 | break; |
| 7327 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7328 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7329 | xpath_func = &xpath_concat; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7330 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7331 | xpath_func = &xpath_number; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7332 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7333 | xpath_func = &xpath_string; |
| 7334 | } |
| 7335 | break; |
| 7336 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7337 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7338 | xpath_func = &xpath_boolean; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7339 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7340 | xpath_func = &xpath_ceiling; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7341 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7342 | xpath_func = &xpath_current; |
| 7343 | } |
| 7344 | break; |
| 7345 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7346 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7347 | xpath_func = &xpath_contains; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7348 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7349 | xpath_func = &xpath_position; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7350 | } 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] | 7351 | xpath_func = &xpath_re_match; |
| 7352 | } |
| 7353 | break; |
| 7354 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7355 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7356 | xpath_func = &xpath_substring; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7357 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7358 | xpath_func = &xpath_translate; |
| 7359 | } |
| 7360 | break; |
| 7361 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7362 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7363 | xpath_func = &xpath_local_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7364 | } 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] | 7365 | xpath_func = &xpath_enum_value; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7366 | } 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] | 7367 | xpath_func = &xpath_bit_is_set; |
| 7368 | } |
| 7369 | break; |
| 7370 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7371 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7372 | xpath_func = &xpath_starts_with; |
| 7373 | } |
| 7374 | break; |
| 7375 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7376 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7377 | xpath_func = &xpath_derived_from; |
| 7378 | } |
| 7379 | break; |
| 7380 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7381 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7382 | xpath_func = &xpath_namespace_uri; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7383 | } 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] | 7384 | xpath_func = &xpath_string_length; |
| 7385 | } |
| 7386 | break; |
| 7387 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7388 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7389 | xpath_func = &xpath_normalize_space; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7390 | } 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] | 7391 | xpath_func = &xpath_substring_after; |
| 7392 | } |
| 7393 | break; |
| 7394 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7395 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7396 | xpath_func = &xpath_substring_before; |
| 7397 | } |
| 7398 | break; |
| 7399 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7400 | 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] | 7401 | xpath_func = &xpath_derived_from_or_self; |
| 7402 | } |
| 7403 | break; |
| 7404 | } |
| 7405 | |
| 7406 | if (!xpath_func) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7407 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7408 | return LY_EVALID; |
| 7409 | } |
| 7410 | } |
| 7411 | |
| 7412 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7413 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7414 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7415 | |
| 7416 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7417 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7418 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7419 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7420 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7421 | |
| 7422 | /* ( Expr ( ',' Expr )* )? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7423 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7424 | if (set) { |
| 7425 | args = malloc(sizeof *args); |
| 7426 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7427 | arg_count = 1; |
| 7428 | args[0] = set_copy(set); |
| 7429 | if (!args[0]) { |
| 7430 | rc = LY_EMEM; |
| 7431 | goto cleanup; |
| 7432 | } |
| 7433 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7434 | rc = eval_expr_select(exp, tok_idx, 0, args[0], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7435 | LY_CHECK_GOTO(rc, cleanup); |
| 7436 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7437 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7438 | LY_CHECK_GOTO(rc, cleanup); |
| 7439 | } |
| 7440 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7441 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7442 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7443 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7444 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7445 | |
| 7446 | if (set) { |
| 7447 | ++arg_count; |
| 7448 | args_aux = realloc(args, arg_count * sizeof *args); |
| 7449 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7450 | args = args_aux; |
| 7451 | args[arg_count - 1] = set_copy(set); |
| 7452 | if (!args[arg_count - 1]) { |
| 7453 | rc = LY_EMEM; |
| 7454 | goto cleanup; |
| 7455 | } |
| 7456 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7457 | 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] | 7458 | LY_CHECK_GOTO(rc, cleanup); |
| 7459 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7460 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7461 | LY_CHECK_GOTO(rc, cleanup); |
| 7462 | } |
| 7463 | } |
| 7464 | |
| 7465 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7466 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7467 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7468 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7469 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7470 | |
| 7471 | if (set) { |
| 7472 | /* evaluate function */ |
| 7473 | rc = xpath_func(args, arg_count, set, options); |
| 7474 | |
| 7475 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7476 | /* merge all nodes from arg evaluations */ |
| 7477 | for (i = 0; i < arg_count; ++i) { |
| 7478 | set_scnode_clear_ctx(args[i]); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7479 | lyxp_set_scnode_merge(set, args[i]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7480 | } |
| 7481 | } |
| 7482 | } else { |
| 7483 | rc = LY_SUCCESS; |
| 7484 | } |
| 7485 | |
| 7486 | cleanup: |
| 7487 | for (i = 0; i < arg_count; ++i) { |
| 7488 | lyxp_set_free(args[i]); |
| 7489 | } |
| 7490 | free(args); |
| 7491 | |
| 7492 | return rc; |
| 7493 | } |
| 7494 | |
| 7495 | /** |
| 7496 | * @brief Evaluate Number. Logs directly on error. |
| 7497 | * |
| 7498 | * @param[in] ctx Context for errors. |
| 7499 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7500 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7501 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7502 | * @return LY_ERR |
| 7503 | */ |
| 7504 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7505 | eval_number(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7506 | { |
| 7507 | long double num; |
| 7508 | char *endptr; |
| 7509 | |
| 7510 | if (set) { |
| 7511 | errno = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7512 | num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7513 | if (errno) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7514 | LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7515 | LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).", |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7516 | 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] | 7517 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7518 | } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) { |
| 7519 | LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7520 | LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.", |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7521 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7522 | return LY_EVALID; |
| 7523 | } |
| 7524 | |
| 7525 | set_fill_number(set, num); |
| 7526 | } |
| 7527 | |
| 7528 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7529 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7530 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7531 | return LY_SUCCESS; |
| 7532 | } |
| 7533 | |
| 7534 | /** |
| 7535 | * @brief Evaluate PathExpr. Logs directly on error. |
| 7536 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7537 | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7538 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 7539 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 7540 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7541 | * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7542 | * |
| 7543 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7544 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7545 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7546 | * @param[in] options XPath options. |
| 7547 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7548 | */ |
| 7549 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7550 | eval_path_expr(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7551 | { |
| 7552 | int all_desc, parent_pos_pred; |
| 7553 | LY_ERR rc; |
| 7554 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7555 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7556 | case LYXP_TOKEN_PAR1: |
| 7557 | /* '(' Expr ')' */ |
| 7558 | |
| 7559 | /* '(' */ |
| 7560 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7561 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7562 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7563 | |
| 7564 | /* Expr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7565 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7566 | LY_CHECK_RET(rc); |
| 7567 | |
| 7568 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7569 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7570 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7571 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7572 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7573 | |
| 7574 | parent_pos_pred = 0; |
| 7575 | goto predicate; |
| 7576 | |
| 7577 | case LYXP_TOKEN_DOT: |
| 7578 | case LYXP_TOKEN_DDOT: |
| 7579 | case LYXP_TOKEN_AT: |
| 7580 | case LYXP_TOKEN_NAMETEST: |
| 7581 | case LYXP_TOKEN_NODETYPE: |
| 7582 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7583 | rc = eval_relative_location_path(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7584 | LY_CHECK_RET(rc); |
| 7585 | break; |
| 7586 | |
| 7587 | case LYXP_TOKEN_FUNCNAME: |
| 7588 | /* FunctionCall */ |
| 7589 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7590 | rc = eval_function_call(exp, tok_idx, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7591 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7592 | rc = eval_function_call(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7593 | } |
| 7594 | LY_CHECK_RET(rc); |
| 7595 | |
| 7596 | parent_pos_pred = 1; |
| 7597 | goto predicate; |
| 7598 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 7599 | case LYXP_TOKEN_OPER_PATH: |
| 7600 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7601 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7602 | rc = eval_absolute_location_path(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7603 | LY_CHECK_RET(rc); |
| 7604 | break; |
| 7605 | |
| 7606 | case LYXP_TOKEN_LITERAL: |
| 7607 | /* Literal */ |
| 7608 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7609 | if (set) { |
| 7610 | set_scnode_clear_ctx(set); |
| 7611 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7612 | eval_literal(exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7613 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7614 | eval_literal(exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7615 | } |
| 7616 | |
| 7617 | parent_pos_pred = 1; |
| 7618 | goto predicate; |
| 7619 | |
| 7620 | case LYXP_TOKEN_NUMBER: |
| 7621 | /* Number */ |
| 7622 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7623 | if (set) { |
| 7624 | set_scnode_clear_ctx(set); |
| 7625 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7626 | rc = eval_number(NULL, exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7627 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7628 | rc = eval_number(set->ctx, exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7629 | } |
| 7630 | LY_CHECK_RET(rc); |
| 7631 | |
| 7632 | parent_pos_pred = 1; |
| 7633 | goto predicate; |
| 7634 | |
| 7635 | default: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7636 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), |
| 7637 | &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7638 | return LY_EVALID; |
| 7639 | } |
| 7640 | |
| 7641 | return LY_SUCCESS; |
| 7642 | |
| 7643 | predicate: |
| 7644 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7645 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7646 | rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7647 | LY_CHECK_RET(rc); |
| 7648 | } |
| 7649 | |
| 7650 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7651 | 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] | 7652 | |
| 7653 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7654 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7655 | all_desc = 0; |
| 7656 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7657 | all_desc = 1; |
| 7658 | } |
| 7659 | |
| 7660 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7661 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7662 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7663 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7664 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7665 | LY_CHECK_RET(rc); |
| 7666 | } |
| 7667 | |
| 7668 | return LY_SUCCESS; |
| 7669 | } |
| 7670 | |
| 7671 | /** |
| 7672 | * @brief Evaluate UnionExpr. Logs directly on error. |
| 7673 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7674 | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7675 | * |
| 7676 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7677 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7678 | * @param[in] repeat How many times this expression is repeated. |
| 7679 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7680 | * @param[in] options XPath options. |
| 7681 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7682 | */ |
| 7683 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7684 | eval_union_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7685 | { |
| 7686 | LY_ERR rc = LY_SUCCESS; |
| 7687 | struct lyxp_set orig_set, set2; |
| 7688 | uint16_t i; |
| 7689 | |
| 7690 | assert(repeat); |
| 7691 | |
| 7692 | set_init(&orig_set, set); |
| 7693 | set_init(&set2, set); |
| 7694 | |
| 7695 | set_fill_set(&orig_set, set); |
| 7696 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7697 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7698 | LY_CHECK_GOTO(rc, cleanup); |
| 7699 | |
| 7700 | /* ('|' PathExpr)* */ |
| 7701 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7702 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7703 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7704 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7705 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7706 | |
| 7707 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7708 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7709 | LY_CHECK_GOTO(rc, cleanup); |
| 7710 | continue; |
| 7711 | } |
| 7712 | |
| 7713 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7714 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7715 | LY_CHECK_GOTO(rc, cleanup); |
| 7716 | |
| 7717 | /* eval */ |
| 7718 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7719 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7720 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7721 | rc = moveto_union(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7722 | LY_CHECK_GOTO(rc, cleanup); |
| 7723 | } |
| 7724 | } |
| 7725 | |
| 7726 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7727 | lyxp_set_free_content(&orig_set); |
| 7728 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7729 | return rc; |
| 7730 | } |
| 7731 | |
| 7732 | /** |
| 7733 | * @brief Evaluate UnaryExpr. Logs directly on error. |
| 7734 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7735 | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7736 | * |
| 7737 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7738 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7739 | * @param[in] repeat How many times this expression is repeated. |
| 7740 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7741 | * @param[in] options XPath options. |
| 7742 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7743 | */ |
| 7744 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7745 | eval_unary_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7746 | { |
| 7747 | LY_ERR rc; |
| 7748 | uint16_t this_op, i; |
| 7749 | |
| 7750 | assert(repeat); |
| 7751 | |
| 7752 | /* ('-')+ */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7753 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7754 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7755 | 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] | 7756 | |
| 7757 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7758 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7759 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7760 | } |
| 7761 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7762 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7763 | LY_CHECK_RET(rc); |
| 7764 | |
| 7765 | if (set && (repeat % 2)) { |
| 7766 | if (options & LYXP_SCNODE_ALL) { |
| 7767 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
| 7768 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7769 | 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] | 7770 | LY_CHECK_RET(rc); |
| 7771 | } |
| 7772 | } |
| 7773 | |
| 7774 | return LY_SUCCESS; |
| 7775 | } |
| 7776 | |
| 7777 | /** |
| 7778 | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
| 7779 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7780 | * [18] MultiplicativeExpr ::= UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7781 | * | MultiplicativeExpr '*' UnaryExpr |
| 7782 | * | MultiplicativeExpr 'div' UnaryExpr |
| 7783 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 7784 | * |
| 7785 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7786 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7787 | * @param[in] repeat How many times this expression is repeated. |
| 7788 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7789 | * @param[in] options XPath options. |
| 7790 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7791 | */ |
| 7792 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7793 | eval_multiplicative_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7794 | { |
| 7795 | LY_ERR rc; |
| 7796 | uint16_t this_op; |
| 7797 | struct lyxp_set orig_set, set2; |
| 7798 | uint16_t i; |
| 7799 | |
| 7800 | assert(repeat); |
| 7801 | |
| 7802 | set_init(&orig_set, set); |
| 7803 | set_init(&set2, set); |
| 7804 | |
| 7805 | set_fill_set(&orig_set, set); |
| 7806 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7807 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7808 | LY_CHECK_GOTO(rc, cleanup); |
| 7809 | |
| 7810 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 7811 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7812 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7813 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7814 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7815 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7816 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7817 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7818 | |
| 7819 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7820 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7821 | LY_CHECK_GOTO(rc, cleanup); |
| 7822 | continue; |
| 7823 | } |
| 7824 | |
| 7825 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7826 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7827 | LY_CHECK_GOTO(rc, cleanup); |
| 7828 | |
| 7829 | /* eval */ |
| 7830 | if (options & LYXP_SCNODE_ALL) { |
| 7831 | 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] | 7832 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7833 | set_scnode_clear_ctx(set); |
| 7834 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7835 | 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] | 7836 | LY_CHECK_GOTO(rc, cleanup); |
| 7837 | } |
| 7838 | } |
| 7839 | |
| 7840 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7841 | lyxp_set_free_content(&orig_set); |
| 7842 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7843 | return rc; |
| 7844 | } |
| 7845 | |
| 7846 | /** |
| 7847 | * @brief Evaluate AdditiveExpr. Logs directly on error. |
| 7848 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7849 | * [17] AdditiveExpr ::= MultiplicativeExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7850 | * | AdditiveExpr '+' MultiplicativeExpr |
| 7851 | * | AdditiveExpr '-' MultiplicativeExpr |
| 7852 | * |
| 7853 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7854 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7855 | * @param[in] repeat How many times this expression is repeated. |
| 7856 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7857 | * @param[in] options XPath options. |
| 7858 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7859 | */ |
| 7860 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7861 | eval_additive_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7862 | { |
| 7863 | LY_ERR rc; |
| 7864 | uint16_t this_op; |
| 7865 | struct lyxp_set orig_set, set2; |
| 7866 | uint16_t i; |
| 7867 | |
| 7868 | assert(repeat); |
| 7869 | |
| 7870 | set_init(&orig_set, set); |
| 7871 | set_init(&set2, set); |
| 7872 | |
| 7873 | set_fill_set(&orig_set, set); |
| 7874 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7875 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7876 | LY_CHECK_GOTO(rc, cleanup); |
| 7877 | |
| 7878 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 7879 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7880 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7881 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7882 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7883 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7884 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7885 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7886 | |
| 7887 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7888 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7889 | LY_CHECK_GOTO(rc, cleanup); |
| 7890 | continue; |
| 7891 | } |
| 7892 | |
| 7893 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7894 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7895 | LY_CHECK_GOTO(rc, cleanup); |
| 7896 | |
| 7897 | /* eval */ |
| 7898 | if (options & LYXP_SCNODE_ALL) { |
| 7899 | 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] | 7900 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7901 | set_scnode_clear_ctx(set); |
| 7902 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7903 | 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] | 7904 | LY_CHECK_GOTO(rc, cleanup); |
| 7905 | } |
| 7906 | } |
| 7907 | |
| 7908 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7909 | lyxp_set_free_content(&orig_set); |
| 7910 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7911 | return rc; |
| 7912 | } |
| 7913 | |
| 7914 | /** |
| 7915 | * @brief Evaluate RelationalExpr. Logs directly on error. |
| 7916 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7917 | * [16] RelationalExpr ::= AdditiveExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7918 | * | RelationalExpr '<' AdditiveExpr |
| 7919 | * | RelationalExpr '>' AdditiveExpr |
| 7920 | * | RelationalExpr '<=' AdditiveExpr |
| 7921 | * | RelationalExpr '>=' AdditiveExpr |
| 7922 | * |
| 7923 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7924 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7925 | * @param[in] repeat How many times this expression is repeated. |
| 7926 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7927 | * @param[in] options XPath options. |
| 7928 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7929 | */ |
| 7930 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7931 | eval_relational_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7932 | { |
| 7933 | LY_ERR rc; |
| 7934 | uint16_t this_op; |
| 7935 | struct lyxp_set orig_set, set2; |
| 7936 | uint16_t i; |
| 7937 | |
| 7938 | assert(repeat); |
| 7939 | |
| 7940 | set_init(&orig_set, set); |
| 7941 | set_init(&set2, set); |
| 7942 | |
| 7943 | set_fill_set(&orig_set, set); |
| 7944 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7945 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7946 | LY_CHECK_GOTO(rc, cleanup); |
| 7947 | |
| 7948 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 7949 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7950 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7951 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7952 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7953 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7954 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7955 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7956 | |
| 7957 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7958 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7959 | LY_CHECK_GOTO(rc, cleanup); |
| 7960 | continue; |
| 7961 | } |
| 7962 | |
| 7963 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7964 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7965 | LY_CHECK_GOTO(rc, cleanup); |
| 7966 | |
| 7967 | /* eval */ |
| 7968 | if (options & LYXP_SCNODE_ALL) { |
| 7969 | 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] | 7970 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7971 | set_scnode_clear_ctx(set); |
| 7972 | } else { |
| 7973 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 7974 | LY_CHECK_GOTO(rc, cleanup); |
| 7975 | } |
| 7976 | } |
| 7977 | |
| 7978 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7979 | lyxp_set_free_content(&orig_set); |
| 7980 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7981 | return rc; |
| 7982 | } |
| 7983 | |
| 7984 | /** |
| 7985 | * @brief Evaluate EqualityExpr. Logs directly on error. |
| 7986 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7987 | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7988 | * | EqualityExpr '!=' RelationalExpr |
| 7989 | * |
| 7990 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7991 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7992 | * @param[in] repeat How many times this expression is repeated. |
| 7993 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7994 | * @param[in] options XPath options. |
| 7995 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7996 | */ |
| 7997 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7998 | eval_equality_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7999 | { |
| 8000 | LY_ERR rc; |
| 8001 | uint16_t this_op; |
| 8002 | struct lyxp_set orig_set, set2; |
| 8003 | uint16_t i; |
| 8004 | |
| 8005 | assert(repeat); |
| 8006 | |
| 8007 | set_init(&orig_set, set); |
| 8008 | set_init(&set2, set); |
| 8009 | |
| 8010 | set_fill_set(&orig_set, set); |
| 8011 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8012 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8013 | LY_CHECK_GOTO(rc, cleanup); |
| 8014 | |
| 8015 | /* ('=' / '!=' RelationalExpr)* */ |
| 8016 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8017 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8018 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8019 | 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] | 8020 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8021 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8022 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8023 | |
| 8024 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8025 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8026 | LY_CHECK_GOTO(rc, cleanup); |
| 8027 | continue; |
| 8028 | } |
| 8029 | |
| 8030 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8031 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8032 | LY_CHECK_GOTO(rc, cleanup); |
| 8033 | |
| 8034 | /* eval */ |
| 8035 | if (options & LYXP_SCNODE_ALL) { |
| 8036 | 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] | 8037 | warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1); |
| 8038 | 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] | 8039 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8040 | set_scnode_clear_ctx(set); |
| 8041 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8042 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8043 | LY_CHECK_GOTO(rc, cleanup); |
| 8044 | } |
| 8045 | } |
| 8046 | |
| 8047 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8048 | lyxp_set_free_content(&orig_set); |
| 8049 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8050 | return rc; |
| 8051 | } |
| 8052 | |
| 8053 | /** |
| 8054 | * @brief Evaluate AndExpr. Logs directly on error. |
| 8055 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8056 | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8057 | * |
| 8058 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8059 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8060 | * @param[in] repeat How many times this expression is repeated. |
| 8061 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8062 | * @param[in] options XPath options. |
| 8063 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8064 | */ |
| 8065 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8066 | eval_and_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8067 | { |
| 8068 | LY_ERR rc; |
| 8069 | struct lyxp_set orig_set, set2; |
| 8070 | uint16_t i; |
| 8071 | |
| 8072 | assert(repeat); |
| 8073 | |
| 8074 | set_init(&orig_set, set); |
| 8075 | set_init(&set2, set); |
| 8076 | |
| 8077 | set_fill_set(&orig_set, set); |
| 8078 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8079 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8080 | LY_CHECK_GOTO(rc, cleanup); |
| 8081 | |
| 8082 | /* cast to boolean, we know that will be the final result */ |
| 8083 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8084 | set_scnode_clear_ctx(set); |
| 8085 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8086 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8087 | } |
| 8088 | |
| 8089 | /* ('and' EqualityExpr)* */ |
| 8090 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8091 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8092 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"), |
| 8093 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8094 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8095 | |
| 8096 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8097 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) { |
| 8098 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8099 | LY_CHECK_GOTO(rc, cleanup); |
| 8100 | continue; |
| 8101 | } |
| 8102 | |
| 8103 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8104 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8105 | LY_CHECK_GOTO(rc, cleanup); |
| 8106 | |
| 8107 | /* eval - just get boolean value actually */ |
| 8108 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8109 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8110 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8111 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8112 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8113 | set_fill_set(set, &set2); |
| 8114 | } |
| 8115 | } |
| 8116 | |
| 8117 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8118 | lyxp_set_free_content(&orig_set); |
| 8119 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8120 | return rc; |
| 8121 | } |
| 8122 | |
| 8123 | /** |
| 8124 | * @brief Evaluate OrExpr. Logs directly on error. |
| 8125 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8126 | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8127 | * |
| 8128 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8129 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8130 | * @param[in] repeat How many times this expression is repeated. |
| 8131 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8132 | * @param[in] options XPath options. |
| 8133 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8134 | */ |
| 8135 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8136 | eval_or_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8137 | { |
| 8138 | LY_ERR rc; |
| 8139 | struct lyxp_set orig_set, set2; |
| 8140 | uint16_t i; |
| 8141 | |
| 8142 | assert(repeat); |
| 8143 | |
| 8144 | set_init(&orig_set, set); |
| 8145 | set_init(&set2, set); |
| 8146 | |
| 8147 | set_fill_set(&orig_set, set); |
| 8148 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8149 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8150 | LY_CHECK_GOTO(rc, cleanup); |
| 8151 | |
| 8152 | /* cast to boolean, we know that will be the final result */ |
| 8153 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8154 | set_scnode_clear_ctx(set); |
| 8155 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8156 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8157 | } |
| 8158 | |
| 8159 | /* ('or' AndExpr)* */ |
| 8160 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8161 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8162 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"), |
| 8163 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8164 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8165 | |
| 8166 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8167 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) { |
| 8168 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, 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); |
| 8174 | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
| 8175 | * but it does not matter */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8176 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8177 | LY_CHECK_GOTO(rc, cleanup); |
| 8178 | |
| 8179 | /* eval - just get boolean value actually */ |
| 8180 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8181 | set_scnode_clear_ctx(&set2); |
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 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8184 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8185 | set_fill_set(set, &set2); |
| 8186 | } |
| 8187 | } |
| 8188 | |
| 8189 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8190 | lyxp_set_free_content(&orig_set); |
| 8191 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8192 | return rc; |
| 8193 | } |
| 8194 | |
| 8195 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8196 | * @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] | 8197 | * |
| 8198 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8199 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8200 | * @param[in] etype Expression type to evaluate. |
| 8201 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8202 | * @param[in] options XPath options. |
| 8203 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8204 | */ |
| 8205 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8206 | eval_expr_select(struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8207 | { |
| 8208 | uint16_t i, count; |
| 8209 | enum lyxp_expr_type next_etype; |
| 8210 | LY_ERR rc; |
| 8211 | |
| 8212 | /* process operator repeats */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8213 | if (!exp->repeat[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8214 | next_etype = LYXP_EXPR_NONE; |
| 8215 | } else { |
| 8216 | /* find etype repeat */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8217 | for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8218 | |
| 8219 | /* select one-priority lower because etype expression called us */ |
| 8220 | if (i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8221 | next_etype = exp->repeat[*tok_idx][i - 1]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8222 | /* count repeats for that expression */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8223 | 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] | 8224 | } else { |
| 8225 | next_etype = LYXP_EXPR_NONE; |
| 8226 | } |
| 8227 | } |
| 8228 | |
| 8229 | /* decide what expression are we parsing based on the repeat */ |
| 8230 | switch (next_etype) { |
| 8231 | case LYXP_EXPR_OR: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8232 | rc = eval_or_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8233 | break; |
| 8234 | case LYXP_EXPR_AND: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8235 | rc = eval_and_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8236 | break; |
| 8237 | case LYXP_EXPR_EQUALITY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8238 | rc = eval_equality_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8239 | break; |
| 8240 | case LYXP_EXPR_RELATIONAL: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8241 | rc = eval_relational_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8242 | break; |
| 8243 | case LYXP_EXPR_ADDITIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8244 | rc = eval_additive_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8245 | break; |
| 8246 | case LYXP_EXPR_MULTIPLICATIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8247 | rc = eval_multiplicative_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8248 | break; |
| 8249 | case LYXP_EXPR_UNARY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8250 | rc = eval_unary_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8251 | break; |
| 8252 | case LYXP_EXPR_UNION: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8253 | rc = eval_union_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8254 | break; |
| 8255 | case LYXP_EXPR_NONE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8256 | rc = eval_path_expr(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8257 | break; |
| 8258 | default: |
| 8259 | LOGINT_RET(set->ctx); |
| 8260 | } |
| 8261 | |
| 8262 | return rc; |
| 8263 | } |
| 8264 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8265 | /** |
| 8266 | * @brief Get root type. |
| 8267 | * |
| 8268 | * @param[in] ctx_node Context node. |
| 8269 | * @param[in] ctx_scnode Schema context node. |
| 8270 | * @param[in] options XPath options. |
| 8271 | * @return Root type. |
| 8272 | */ |
| 8273 | static enum lyxp_node_type |
| 8274 | lyxp_get_root_type(const struct lyd_node *ctx_node, const struct lysc_node *ctx_scnode, int options) |
| 8275 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8276 | const struct lysc_node *op; |
| 8277 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8278 | if (options & LYXP_SCNODE_ALL) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8279 | 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] | 8280 | |
| 8281 | if (op || (options & LYXP_SCNODE)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8282 | /* general root that can access everything */ |
| 8283 | return LYXP_NODE_ROOT; |
| 8284 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
| 8285 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8286 | return LYXP_NODE_ROOT_CONFIG; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8287 | } |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8288 | return LYXP_NODE_ROOT; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8289 | } |
| 8290 | |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8291 | op = ctx_node ? ctx_node->schema : NULL; |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8292 | 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] | 8293 | |
| 8294 | if (!ctx_node || (!op && (ctx_node->schema->flags & LYS_CONFIG_W))) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8295 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8296 | return LYXP_NODE_ROOT_CONFIG; |
| 8297 | } |
| 8298 | |
| 8299 | return LYXP_NODE_ROOT; |
| 8300 | } |
| 8301 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8302 | LY_ERR |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 8303 | lyxp_eval(struct lyxp_expr *exp, LY_PREFIX_FORMAT format, const struct lys_module *local_mod, const struct lyd_node *ctx_node, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame^] | 8304 | enum lyxp_node_type ctx_node_type, const struct lyd_node *tree, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8305 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8306 | uint16_t tok_idx = 0; |
| 8307 | const struct lyd_node *real_ctx_node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8308 | LY_ERR rc; |
| 8309 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8310 | LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_node, set, LY_EINVAL); |
| 8311 | |
| 8312 | if ((ctx_node_type == LYXP_NODE_ROOT) || (ctx_node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 8313 | /* we always need some context node because it is used for resolving unqualified names */ |
| 8314 | real_ctx_node = NULL; |
| 8315 | } else { |
| 8316 | real_ctx_node = ctx_node; |
| 8317 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8318 | |
| 8319 | /* prepare set for evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8320 | tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8321 | memset(set, 0, sizeof *set); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8322 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8323 | set_insert_node(set, (struct lyd_node *)real_ctx_node, 0, ctx_node_type, 0); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8324 | set->ctx = local_mod->ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8325 | set->ctx_node = ctx_node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8326 | set->root_type = lyxp_get_root_type(real_ctx_node, NULL, options); |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8327 | for (set->context_op = ctx_node->schema; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame^] | 8328 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8329 | set->context_op = set->context_op->parent) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8330 | set->local_mod = local_mod; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 8331 | set->tree = tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8332 | set->format = format; |
| 8333 | |
| 8334 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8335 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8336 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8337 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8338 | } |
| 8339 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8340 | return rc; |
| 8341 | } |
| 8342 | |
| 8343 | #if 0 |
| 8344 | |
| 8345 | /* full xml printing of set elements, not used currently */ |
| 8346 | |
| 8347 | void |
| 8348 | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
| 8349 | { |
| 8350 | uint32_t i; |
| 8351 | char *str_num; |
| 8352 | struct lyout out; |
| 8353 | |
| 8354 | memset(&out, 0, sizeof out); |
| 8355 | |
| 8356 | out.type = LYOUT_STREAM; |
| 8357 | out.method.f = f; |
| 8358 | |
| 8359 | switch (set->type) { |
| 8360 | case LYXP_SET_EMPTY: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8361 | ly_print_(&out, "Empty XPath set\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8362 | break; |
| 8363 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8364 | ly_print_(&out, "Boolean XPath set:\n"); |
| 8365 | ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8366 | break; |
| 8367 | case LYXP_SET_STRING: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8368 | ly_print_(&out, "String XPath set:\n"); |
| 8369 | ly_print_(&out, "\"%s\"\n\n", set->value.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8370 | break; |
| 8371 | case LYXP_SET_NUMBER: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8372 | ly_print_(&out, "Number XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8373 | |
| 8374 | if (isnan(set->value.num)) { |
| 8375 | str_num = strdup("NaN"); |
| 8376 | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
| 8377 | str_num = strdup("0"); |
| 8378 | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
| 8379 | str_num = strdup("Infinity"); |
| 8380 | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
| 8381 | str_num = strdup("-Infinity"); |
| 8382 | } else if ((long long)set->value.num == set->value.num) { |
| 8383 | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
| 8384 | str_num = NULL; |
| 8385 | } |
| 8386 | } else { |
| 8387 | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
| 8388 | str_num = NULL; |
| 8389 | } |
| 8390 | } |
| 8391 | if (!str_num) { |
| 8392 | LOGMEM; |
| 8393 | return; |
| 8394 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8395 | ly_print_(&out, "%s\n\n", str_num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8396 | free(str_num); |
| 8397 | break; |
| 8398 | case LYXP_SET_NODE_SET: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8399 | ly_print_(&out, "Node XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8400 | |
| 8401 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8402 | ly_print_(&out, "%d. ", i + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8403 | switch (set->node_type[i]) { |
| 8404 | case LYXP_NODE_ROOT_ALL: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8405 | ly_print_(&out, "ROOT all\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8406 | break; |
| 8407 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8408 | ly_print_(&out, "ROOT config\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8409 | break; |
| 8410 | case LYXP_NODE_ROOT_STATE: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8411 | ly_print_(&out, "ROOT state\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8412 | break; |
| 8413 | case LYXP_NODE_ROOT_NOTIF: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8414 | 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] | 8415 | break; |
| 8416 | case LYXP_NODE_ROOT_RPC: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8417 | 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] | 8418 | break; |
| 8419 | case LYXP_NODE_ROOT_OUTPUT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8420 | 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] | 8421 | break; |
| 8422 | case LYXP_NODE_ELEM: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8423 | ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8424 | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8425 | ly_print_(&out, "\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8426 | break; |
| 8427 | case LYXP_NODE_TEXT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8428 | 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] | 8429 | break; |
| 8430 | case LYXP_NODE_ATTR: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8431 | 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] | 8432 | break; |
| 8433 | } |
| 8434 | } |
| 8435 | break; |
| 8436 | } |
| 8437 | } |
| 8438 | |
| 8439 | #endif |
| 8440 | |
| 8441 | LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8442 | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8443 | { |
| 8444 | long double num; |
| 8445 | char *str; |
| 8446 | LY_ERR rc; |
| 8447 | |
| 8448 | if (!set || (set->type == target)) { |
| 8449 | return LY_SUCCESS; |
| 8450 | } |
| 8451 | |
| 8452 | /* it's not possible to convert anything into a node set */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8453 | assert(target != LYXP_SET_NODE_SET); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8454 | |
| 8455 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8456 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8457 | return LY_EINVAL; |
| 8458 | } |
| 8459 | |
| 8460 | /* to STRING */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8461 | 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] | 8462 | switch (set->type) { |
| 8463 | case LYXP_SET_NUMBER: |
| 8464 | if (isnan(set->val.num)) { |
| 8465 | set->val.str = strdup("NaN"); |
| 8466 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8467 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 8468 | set->val.str = strdup("0"); |
| 8469 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8470 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 8471 | set->val.str = strdup("Infinity"); |
| 8472 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8473 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 8474 | set->val.str = strdup("-Infinity"); |
| 8475 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8476 | } else if ((long long)set->val.num == set->val.num) { |
| 8477 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 8478 | LOGMEM_RET(set->ctx); |
| 8479 | } |
| 8480 | set->val.str = str; |
| 8481 | } else { |
| 8482 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 8483 | LOGMEM_RET(set->ctx); |
| 8484 | } |
| 8485 | set->val.str = str; |
| 8486 | } |
| 8487 | break; |
| 8488 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8489 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8490 | set->val.str = strdup("true"); |
| 8491 | } else { |
| 8492 | set->val.str = strdup("false"); |
| 8493 | } |
| 8494 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 8495 | break; |
| 8496 | case LYXP_SET_NODE_SET: |
| 8497 | assert(set->used); |
| 8498 | |
| 8499 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8500 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8501 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8502 | rc = cast_node_set_to_string(set, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8503 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8504 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8505 | set->val.str = str; |
| 8506 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8507 | default: |
| 8508 | LOGINT_RET(set->ctx); |
| 8509 | } |
| 8510 | set->type = LYXP_SET_STRING; |
| 8511 | } |
| 8512 | |
| 8513 | /* to NUMBER */ |
| 8514 | if (target == LYXP_SET_NUMBER) { |
| 8515 | switch (set->type) { |
| 8516 | case LYXP_SET_STRING: |
| 8517 | num = cast_string_to_number(set->val.str); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8518 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8519 | set->val.num = num; |
| 8520 | break; |
| 8521 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8522 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8523 | set->val.num = 1; |
| 8524 | } else { |
| 8525 | set->val.num = 0; |
| 8526 | } |
| 8527 | break; |
| 8528 | default: |
| 8529 | LOGINT_RET(set->ctx); |
| 8530 | } |
| 8531 | set->type = LYXP_SET_NUMBER; |
| 8532 | } |
| 8533 | |
| 8534 | /* to BOOLEAN */ |
| 8535 | if (target == LYXP_SET_BOOLEAN) { |
| 8536 | switch (set->type) { |
| 8537 | case LYXP_SET_NUMBER: |
| 8538 | 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] | 8539 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8540 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8541 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8542 | } |
| 8543 | break; |
| 8544 | case LYXP_SET_STRING: |
| 8545 | if (set->val.str[0]) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8546 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8547 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8548 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8549 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8550 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8551 | } |
| 8552 | break; |
| 8553 | case LYXP_SET_NODE_SET: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8554 | if (set->used) { |
| 8555 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8556 | set->val.bln = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8557 | } else { |
| 8558 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8559 | set->val.bln = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8560 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8561 | break; |
| 8562 | default: |
| 8563 | LOGINT_RET(set->ctx); |
| 8564 | } |
| 8565 | set->type = LYXP_SET_BOOLEAN; |
| 8566 | } |
| 8567 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8568 | return LY_SUCCESS; |
| 8569 | } |
| 8570 | |
| 8571 | LY_ERR |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 8572 | lyxp_atomize(struct lyxp_expr *exp, LY_PREFIX_FORMAT format, const struct lys_module *local_mod, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame^] | 8573 | const struct lysc_node *ctx_scnode, enum lyxp_node_type ctx_scnode_type, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8574 | { |
| 8575 | struct ly_ctx *ctx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8576 | const struct lysc_node *real_ctx_scnode; |
| 8577 | uint16_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8578 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8579 | LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_scnode, set, LY_EINVAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8580 | |
| 8581 | ctx = local_mod->ctx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8582 | if ((ctx_scnode_type == LYXP_NODE_ROOT) || (ctx_scnode_type == LYXP_NODE_ROOT_CONFIG)) { |
| 8583 | /* we always need some context node because it is used for resolving unqualified names */ |
| 8584 | real_ctx_scnode = NULL; |
| 8585 | } else { |
| 8586 | real_ctx_scnode = ctx_scnode; |
| 8587 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8588 | |
| 8589 | /* prepare set for evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8590 | tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8591 | memset(set, 0, sizeof *set); |
| 8592 | set->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8593 | lyxp_set_scnode_insert_node(set, real_ctx_scnode, ctx_scnode_type); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 8594 | set->val.scnodes[0].in_ctx = -2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8595 | set->ctx = ctx; |
| 8596 | set->ctx_scnode = ctx_scnode; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8597 | set->root_type = lyxp_get_root_type(NULL, real_ctx_scnode, options); |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8598 | for (set->context_op = ctx_scnode; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame^] | 8599 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8600 | set->context_op = set->context_op->parent) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8601 | set->local_mod = local_mod; |
| 8602 | set->format = format; |
| 8603 | |
| 8604 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8605 | return eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8606 | } |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 8607 | |
| 8608 | API const char * |
| 8609 | lyxp_get_expr(const struct lyxp_expr *path) |
| 8610 | { |
| 8611 | if (!path) { |
| 8612 | return NULL; |
| 8613 | } |
| 8614 | |
| 8615 | return path->expr; |
| 8616 | } |