Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file path.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief Path functions |
| 5 | * |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 6 | * Copyright (c) 2020 - 2023 CESNET, z.s.p.o. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [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 | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 14 | |
| 15 | #define _GNU_SOURCE |
| 16 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 17 | #include "path.h" |
| 18 | |
| 19 | #include <assert.h> |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 20 | #include <stdlib.h> |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 21 | #include <string.h> |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 22 | |
| 23 | #include "common.h" |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 24 | #include "compat.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 25 | #include "log.h" |
| 26 | #include "plugins_types.h" |
Michal Vasko | 40c158c | 2021-04-28 17:01:03 +0200 | [diff] [blame] | 27 | #include "schema_compile.h" |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 28 | #include "set.h" |
Michal Vasko | 4c583e8 | 2020-07-17 12:16:14 +0200 | [diff] [blame] | 29 | #include "tree.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 30 | #include "tree_data_internal.h" |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 31 | #include "tree_edit.h" |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 32 | #include "tree_schema.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 33 | #include "tree_schema_internal.h" |
| 34 | #include "xpath.h" |
| 35 | |
Radek Krejci | c0c6641 | 2020-08-21 13:53:50 +0200 | [diff] [blame] | 36 | #define LOGVAL_P(CTX, CUR_NODE, CODE, ...) ly_vlog(CTX, (CUR_NODE) ? LY_VLOG_LYSC : LY_VLOG_NONE, CUR_NODE, CODE, ##__VA_ARGS__) |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 37 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 38 | /** |
| 39 | * @brief Check predicate syntax. |
| 40 | * |
| 41 | * @param[in] ctx libyang context. |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 42 | * @param[in] cur_node Current (original context) node. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 43 | * @param[in] exp Parsed predicate. |
| 44 | * @param[in,out] tok_idx Index in @p exp, is adjusted. |
| 45 | * @param[in] prefix Prefix option. |
| 46 | * @param[in] pred Predicate option. |
| 47 | * @return LY_ERR value. |
| 48 | */ |
| 49 | static LY_ERR |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 50 | ly_path_check_predicate(const struct ly_ctx *ctx, const struct lysc_node *cur_node, const struct lyxp_expr *exp, |
Michal Vasko | 32ca49b | 2023-02-17 15:11:35 +0100 | [diff] [blame] | 51 | uint32_t *tok_idx, uint16_t prefix, uint16_t pred) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 52 | { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 53 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 54 | struct ly_set *set = NULL; |
| 55 | uint32_t i; |
| 56 | const char *name; |
| 57 | size_t name_len; |
| 58 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 59 | LOG_LOCSET(cur_node, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 60 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 61 | if (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_BRACK1)) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 62 | /* '[' */ |
| 63 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 64 | if (((pred == LY_PATH_PRED_SIMPLE) || (pred == LY_PATH_PRED_KEYS)) && |
| 65 | !lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NAMETEST)) { |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 66 | ret = ly_set_new(&set); |
| 67 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 68 | |
| 69 | do { |
| 70 | /* NameTest is always expected here */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 71 | LY_CHECK_GOTO(lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NAMETEST), token_error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 72 | |
| 73 | /* check prefix based on the options */ |
| 74 | name = strnstr(exp->expr + exp->tok_pos[*tok_idx], ":", exp->tok_len[*tok_idx]); |
| 75 | if ((prefix == LY_PATH_PREFIX_MANDATORY) && !name) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 76 | LOGVAL(ctx, LYVE_XPATH, "Prefix missing for \"%.*s\" in path.", exp->tok_len[*tok_idx], |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 77 | exp->expr + exp->tok_pos[*tok_idx]); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 78 | goto token_error; |
Michal Vasko | 8b06a5e | 2020-08-06 12:13:08 +0200 | [diff] [blame] | 79 | } else if ((prefix == LY_PATH_PREFIX_STRICT_INHERIT) && name) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 80 | LOGVAL(ctx, LYVE_XPATH, "Redundant prefix for \"%.*s\" in path.", exp->tok_len[*tok_idx], |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 81 | exp->expr + exp->tok_pos[*tok_idx]); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 82 | goto token_error; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 83 | } |
| 84 | if (!name) { |
| 85 | name = exp->expr + exp->tok_pos[*tok_idx]; |
| 86 | name_len = exp->tok_len[*tok_idx]; |
| 87 | } else { |
| 88 | ++name; |
| 89 | name_len = exp->tok_len[*tok_idx] - (name - (exp->expr + exp->tok_pos[*tok_idx])); |
| 90 | } |
| 91 | |
| 92 | /* check whether it was not already specified */ |
| 93 | for (i = 0; i < set->count; ++i) { |
| 94 | /* all the keys must be from the same module so this comparison should be fine */ |
Michal Vasko | 9ff8d2d | 2022-09-29 13:41:14 +0200 | [diff] [blame] | 95 | if (!strncmp(set->objs[i], name, name_len) && |
| 96 | lysp_check_identifierchar(NULL, ((char *)set->objs[i])[name_len], 0, NULL)) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 97 | LOGVAL(ctx, LYVE_XPATH, "Duplicate predicate key \"%.*s\" in path.", (int)name_len, name); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 98 | goto token_error; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
| 102 | /* add it into the set */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 103 | ret = ly_set_add(set, (void *)name, 1, NULL); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 104 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 105 | |
| 106 | /* NameTest */ |
| 107 | ++(*tok_idx); |
| 108 | |
| 109 | /* '=' */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 110 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_EQUAL), token_error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 111 | |
Michal Vasko | b01983e | 2023-04-24 10:43:13 +0200 | [diff] [blame] | 112 | /* fill repeat */ |
| 113 | exp->repeat[*tok_idx - 2] = calloc(2, sizeof *exp->repeat[*tok_idx]); |
| 114 | LY_CHECK_ERR_GOTO(!exp->repeat[*tok_idx - 2], LOGMEM(NULL); ret = LY_EMEM, cleanup); |
| 115 | exp->repeat[*tok_idx - 2][0] = LYXP_EXPR_EQUALITY; |
| 116 | |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 117 | /* Literal, Number, or VariableReference */ |
| 118 | if (lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_LITERAL) && |
| 119 | lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_NUMBER) && |
| 120 | lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_VARREF)) { |
| 121 | /* error */ |
| 122 | lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_LITERAL); |
| 123 | goto token_error; |
| 124 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 125 | |
| 126 | /* ']' */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 127 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_BRACK2), token_error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 128 | |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 129 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 130 | } while (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_BRACK1)); |
| 131 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 132 | } else if ((pred == LY_PATH_PRED_SIMPLE) && !lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_DOT)) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 133 | /* '.' */ |
| 134 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 135 | /* '=' */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 136 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_EQUAL), token_error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 137 | |
Michal Vasko | b01983e | 2023-04-24 10:43:13 +0200 | [diff] [blame] | 138 | /* fill repeat */ |
| 139 | exp->repeat[*tok_idx - 2] = calloc(2, sizeof *exp->repeat[*tok_idx]); |
| 140 | LY_CHECK_ERR_GOTO(!exp->repeat[*tok_idx - 2], LOGMEM(NULL); ret = LY_EMEM, cleanup); |
| 141 | exp->repeat[*tok_idx - 2][0] = LYXP_EXPR_EQUALITY; |
| 142 | |
Michal Vasko | 4911eeb | 2021-06-28 11:23:05 +0200 | [diff] [blame] | 143 | /* Literal or Number */ |
| 144 | LY_CHECK_GOTO(lyxp_next_token2(ctx, exp, tok_idx, LYXP_TOKEN_LITERAL, LYXP_TOKEN_NUMBER), token_error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 145 | |
| 146 | /* ']' */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 147 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_BRACK2), token_error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 148 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 149 | } else if ((pred == LY_PATH_PRED_SIMPLE) && !lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_NUMBER)) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 150 | /* Number */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 151 | |
Michal Vasko | 2261dfa | 2022-09-29 12:29:20 +0200 | [diff] [blame] | 152 | /* check for index 0 */ |
| 153 | if (!atoi(exp->expr + exp->tok_pos[*tok_idx - 1])) { |
| 154 | LOGVAL(ctx, LYVE_XPATH, "Invalid positional predicate \"%.*s\".", (int)exp->tok_len[*tok_idx - 1], |
| 155 | exp->expr + exp->tok_pos[*tok_idx - 1]); |
| 156 | goto token_error; |
| 157 | } |
| 158 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 159 | /* ']' */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 160 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_BRACK2), token_error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 161 | |
| 162 | } else if ((pred == LY_PATH_PRED_LEAFREF) && !lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NAMETEST)) { |
| 163 | assert(prefix == LY_PATH_PREFIX_OPTIONAL); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 164 | ret = ly_set_new(&set); |
| 165 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 166 | |
| 167 | do { |
| 168 | /* NameTest is always expected here */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 169 | LY_CHECK_GOTO(lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NAMETEST), token_error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 170 | |
| 171 | name = strnstr(exp->expr + exp->tok_pos[*tok_idx], ":", exp->tok_len[*tok_idx]); |
| 172 | if (!name) { |
| 173 | name = exp->expr + exp->tok_pos[*tok_idx]; |
| 174 | name_len = exp->tok_len[*tok_idx]; |
| 175 | } else { |
| 176 | ++name; |
| 177 | name_len = exp->tok_len[*tok_idx] - (name - (exp->expr + exp->tok_pos[*tok_idx])); |
| 178 | } |
| 179 | |
| 180 | /* check whether it was not already specified */ |
| 181 | for (i = 0; i < set->count; ++i) { |
| 182 | /* all the keys must be from the same module so this comparison should be fine */ |
Michal Vasko | 9ff8d2d | 2022-09-29 13:41:14 +0200 | [diff] [blame] | 183 | if (!strncmp(set->objs[i], name, name_len) && |
| 184 | lysp_check_identifierchar(NULL, ((char *)set->objs[i])[name_len], 0, NULL)) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 185 | LOGVAL(ctx, LYVE_XPATH, "Duplicate predicate key \"%.*s\" in path.", (int)name_len, name); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 186 | goto token_error; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
| 190 | /* add it into the set */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 191 | ret = ly_set_add(set, (void *)name, 1, NULL); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 192 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 193 | |
| 194 | /* NameTest */ |
| 195 | ++(*tok_idx); |
| 196 | |
| 197 | /* '=' */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 198 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_EQUAL), token_error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 199 | |
Michal Vasko | b01983e | 2023-04-24 10:43:13 +0200 | [diff] [blame] | 200 | /* fill repeat */ |
| 201 | exp->repeat[*tok_idx - 2] = calloc(2, sizeof *exp->repeat[*tok_idx]); |
| 202 | LY_CHECK_ERR_GOTO(!exp->repeat[*tok_idx - 2], LOGMEM(NULL); ret = LY_EMEM, cleanup); |
| 203 | exp->repeat[*tok_idx - 2][0] = LYXP_EXPR_EQUALITY; |
| 204 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 205 | /* FuncName */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 206 | LY_CHECK_GOTO(lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME), token_error); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 207 | if ((exp->tok_len[*tok_idx] != ly_strlen_const("current")) || |
| 208 | strncmp(exp->expr + exp->tok_pos[*tok_idx], "current", ly_strlen_const("current"))) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 209 | LOGVAL(ctx, LYVE_XPATH, "Invalid function \"%.*s\" invocation in path.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 210 | exp->tok_len[*tok_idx], exp->expr + exp->tok_pos[*tok_idx]); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 211 | goto token_error; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 212 | } |
| 213 | ++(*tok_idx); |
| 214 | |
| 215 | /* '(' */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 216 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_PAR1), token_error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 217 | |
| 218 | /* ')' */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 219 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_PAR2), token_error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 220 | |
| 221 | /* '/' */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 222 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_PATH), token_error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 223 | |
| 224 | /* '..' */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 225 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_DDOT), token_error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 226 | do { |
| 227 | /* '/' */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 228 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_PATH), token_error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 229 | } while (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_DDOT)); |
| 230 | |
| 231 | /* NameTest */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 232 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_NAMETEST), token_error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 233 | |
| 234 | /* '/' */ |
| 235 | while (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_OPER_PATH)) { |
| 236 | /* NameTest */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 237 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_NAMETEST), token_error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /* ']' */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 241 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_BRACK2), token_error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 242 | |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 243 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 244 | } while (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_BRACK1)); |
| 245 | |
Michal Vasko | 3c19b49 | 2022-09-05 08:48:10 +0200 | [diff] [blame] | 246 | } else if (lyxp_check_token(ctx, exp, *tok_idx, 0)) { |
| 247 | /* unexpected EOF */ |
| 248 | goto token_error; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 249 | } else { |
Michal Vasko | 3c19b49 | 2022-09-05 08:48:10 +0200 | [diff] [blame] | 250 | /* invalid token */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 251 | LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[*tok_idx]), exp->expr + exp->tok_pos[*tok_idx]); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 252 | goto token_error; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 256 | cleanup: |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 257 | LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 258 | ly_set_free(set, NULL); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 259 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 260 | |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 261 | token_error: |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 262 | LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 263 | ly_set_free(set, NULL); |
| 264 | return LY_EVALID; |
| 265 | } |
| 266 | |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 267 | /** |
| 268 | * @brief Parse deref XPath function and perform all additional checks. |
| 269 | * |
| 270 | * @param[in] ctx libyang context. |
| 271 | * @param[in] ctx_node Optional context node, used for logging. |
| 272 | * @param[in] exp Parsed path. |
| 273 | * @param[in,out] tok_idx Index in @p exp, is adjusted. |
| 274 | * @return LY_ERR value. |
| 275 | */ |
| 276 | static LY_ERR |
| 277 | ly_path_parse_deref(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const struct lyxp_expr *exp, |
| 278 | uint32_t *tok_idx) |
| 279 | { |
| 280 | size_t arg_len; |
| 281 | uint32_t begin_token, end_token; |
| 282 | struct lyxp_expr *arg_expr = NULL; |
| 283 | |
| 284 | /* mandatory FunctionName */ |
| 285 | LY_CHECK_RET(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_FUNCNAME), LY_EVALID); |
| 286 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
| 287 | LOGVAL(ctx, LYVE_XPATH, "Unexpected XPath function \"%.*s\" in path, expected \"deref(...)\"", |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 288 | exp->tok_len[*tok_idx], exp->expr + exp->tok_pos[*tok_idx]); |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 289 | return LY_EVALID; |
| 290 | } |
| 291 | |
| 292 | /* mandatory '(' */ |
| 293 | LY_CHECK_RET(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_PAR1), LY_EVALID); |
| 294 | begin_token = *tok_idx; |
| 295 | |
| 296 | /* count tokens till ')' */ |
| 297 | while (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_PAR2) && *tok_idx < exp->used) { |
| 298 | /* emebedded functions are not allowed */ |
| 299 | if (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_FUNCNAME)) { |
| 300 | LOGVAL(ctx, LYVE_XPATH, "Embedded function XPath function inside deref function within the path" |
| 301 | "is not allowed"); |
| 302 | return LY_EVALID; |
| 303 | } |
| 304 | |
| 305 | (*tok_idx)++; |
| 306 | } |
| 307 | |
| 308 | /* mandatory ')' */ |
| 309 | LY_CHECK_RET(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_PAR2), LY_EVALID); |
| 310 | end_token = *tok_idx - 1; |
| 311 | |
| 312 | /* parse the path of deref argument */ |
| 313 | arg_len = exp->tok_pos[end_token] - exp->tok_pos[begin_token]; |
| 314 | LY_CHECK_RET(ly_path_parse(ctx, ctx_node, &exp->expr[exp->tok_pos[begin_token]], arg_len, 1, |
| 315 | LY_PATH_BEGIN_EITHER, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &arg_expr), LY_EVALID); |
| 316 | lyxp_expr_free(ctx, arg_expr); |
| 317 | |
| 318 | return LY_SUCCESS; |
| 319 | } |
| 320 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 321 | LY_ERR |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 322 | ly_path_parse(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *str_path, size_t path_len, |
Michal Vasko | 32ca49b | 2023-02-17 15:11:35 +0100 | [diff] [blame] | 323 | ly_bool lref, uint16_t begin, uint16_t prefix, uint16_t pred, struct lyxp_expr **expr) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 324 | { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 325 | LY_ERR ret = LY_SUCCESS; |
| 326 | struct lyxp_expr *exp = NULL; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 327 | uint32_t tok_idx, cur_len; |
Michal Vasko | 8b06a5e | 2020-08-06 12:13:08 +0200 | [diff] [blame] | 328 | const char *cur_node, *prev_prefix = NULL, *ptr; |
Michal Vasko | 32ca49b | 2023-02-17 15:11:35 +0100 | [diff] [blame] | 329 | ly_bool is_abs; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 330 | |
| 331 | assert((begin == LY_PATH_BEGIN_ABSOLUTE) || (begin == LY_PATH_BEGIN_EITHER)); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 332 | assert((prefix == LY_PATH_PREFIX_OPTIONAL) || (prefix == LY_PATH_PREFIX_MANDATORY) || |
Michal Vasko | 32ca49b | 2023-02-17 15:11:35 +0100 | [diff] [blame] | 333 | (prefix == LY_PATH_PREFIX_FIRST) || (prefix == LY_PATH_PREFIX_STRICT_INHERIT)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 334 | assert((pred == LY_PATH_PRED_KEYS) || (pred == LY_PATH_PRED_SIMPLE) || (pred == LY_PATH_PRED_LEAFREF)); |
| 335 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 336 | LOG_LOCSET(ctx_node, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 337 | |
Michal Vasko | b01983e | 2023-04-24 10:43:13 +0200 | [diff] [blame] | 338 | /* parse as a generic XPath expression, reparse is performed manually */ |
| 339 | LY_CHECK_GOTO(ret = lyxp_expr_parse(ctx, str_path, path_len, 0, &exp), error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 340 | tok_idx = 0; |
| 341 | |
Michal Vasko | b01983e | 2023-04-24 10:43:13 +0200 | [diff] [blame] | 342 | /* alloc empty repeat (only '=', filled manually) */ |
| 343 | exp->repeat = calloc(exp->size, sizeof *exp->repeat); |
| 344 | LY_CHECK_ERR_GOTO(!exp->repeat, LOGMEM(ctx); ret = LY_EMEM, error); |
| 345 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 346 | if (begin == LY_PATH_BEGIN_EITHER) { |
| 347 | /* is the path relative? */ |
| 348 | if (lyxp_next_token(NULL, exp, &tok_idx, LYXP_TOKEN_OPER_PATH)) { |
Michal Vasko | cb8c6d4 | 2020-10-16 11:58:30 +0200 | [diff] [blame] | 349 | /* relative path check specific to leafref */ |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 350 | if (lref) { |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 351 | /* optional function 'deref..' */ |
| 352 | if ((ly_ctx_get_options(ctx) & LY_CTX_LEAFREF_EXTENDED) && |
| 353 | !lyxp_check_token(NULL, exp, tok_idx, LYXP_TOKEN_FUNCNAME)) { |
| 354 | LY_CHECK_ERR_GOTO(ly_path_parse_deref(ctx, ctx_node, exp, &tok_idx), ret = LY_EVALID, error); |
| 355 | |
| 356 | /* '/' */ |
| 357 | LY_CHECK_ERR_GOTO(lyxp_next_token(ctx, exp, &tok_idx, LYXP_TOKEN_OPER_PATH), ret = LY_EVALID, |
| 358 | error); |
| 359 | } |
| 360 | |
Michal Vasko | cb8c6d4 | 2020-10-16 11:58:30 +0200 | [diff] [blame] | 361 | /* mandatory '..' */ |
| 362 | LY_CHECK_ERR_GOTO(lyxp_next_token(ctx, exp, &tok_idx, LYXP_TOKEN_DDOT), ret = LY_EVALID, error); |
| 363 | |
| 364 | do { |
| 365 | /* '/' */ |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 366 | LY_CHECK_ERR_GOTO(lyxp_next_token(ctx, exp, &tok_idx, LYXP_TOKEN_OPER_PATH), ret = LY_EVALID, |
| 367 | error); |
Michal Vasko | cb8c6d4 | 2020-10-16 11:58:30 +0200 | [diff] [blame] | 368 | |
| 369 | /* optional '..' */ |
| 370 | } while (!lyxp_next_token(NULL, exp, &tok_idx, LYXP_TOKEN_DDOT)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 371 | } |
Michal Vasko | 32ca49b | 2023-02-17 15:11:35 +0100 | [diff] [blame] | 372 | |
| 373 | is_abs = 0; |
| 374 | } else { |
| 375 | is_abs = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 376 | } |
| 377 | } else { |
| 378 | /* '/' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 379 | LY_CHECK_ERR_GOTO(lyxp_next_token(ctx, exp, &tok_idx, LYXP_TOKEN_OPER_PATH), ret = LY_EVALID, error); |
Michal Vasko | 32ca49b | 2023-02-17 15:11:35 +0100 | [diff] [blame] | 380 | |
| 381 | is_abs = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | do { |
| 385 | /* NameTest */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 386 | LY_CHECK_ERR_GOTO(lyxp_check_token(ctx, exp, tok_idx, LYXP_TOKEN_NAMETEST), ret = LY_EVALID, error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 387 | |
| 388 | /* check prefix based on the options */ |
Michal Vasko | 8b06a5e | 2020-08-06 12:13:08 +0200 | [diff] [blame] | 389 | cur_node = exp->expr + exp->tok_pos[tok_idx]; |
| 390 | cur_len = exp->tok_len[tok_idx]; |
| 391 | if (prefix == LY_PATH_PREFIX_MANDATORY) { |
| 392 | if (!strnstr(cur_node, ":", cur_len)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 393 | LOGVAL(ctx, LYVE_XPATH, "Prefix missing for \"%.*s\" in path.", cur_len, cur_node); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 394 | ret = LY_EVALID; |
Michal Vasko | 8b06a5e | 2020-08-06 12:13:08 +0200 | [diff] [blame] | 395 | goto error; |
| 396 | } |
Michal Vasko | 32ca49b | 2023-02-17 15:11:35 +0100 | [diff] [blame] | 397 | } else if ((prefix == LY_PATH_PREFIX_FIRST) || (prefix == LY_PATH_PREFIX_STRICT_INHERIT)) { |
| 398 | if (!prev_prefix && is_abs) { |
Michal Vasko | 8b06a5e | 2020-08-06 12:13:08 +0200 | [diff] [blame] | 399 | /* the first node must have a prefix */ |
| 400 | if (!strnstr(cur_node, ":", cur_len)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 401 | LOGVAL(ctx, LYVE_XPATH, "Prefix missing for \"%.*s\" in path.", cur_len, cur_node); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 402 | ret = LY_EVALID; |
Michal Vasko | 8b06a5e | 2020-08-06 12:13:08 +0200 | [diff] [blame] | 403 | goto error; |
| 404 | } |
| 405 | |
| 406 | /* remember the first prefix */ |
| 407 | prev_prefix = cur_node; |
Michal Vasko | 32ca49b | 2023-02-17 15:11:35 +0100 | [diff] [blame] | 408 | } else if (prev_prefix && (prefix == LY_PATH_PREFIX_STRICT_INHERIT)) { |
Michal Vasko | 8b06a5e | 2020-08-06 12:13:08 +0200 | [diff] [blame] | 409 | /* the prefix must be different, if any */ |
| 410 | ptr = strnstr(cur_node, ":", cur_len); |
| 411 | if (ptr) { |
| 412 | if (!strncmp(prev_prefix, cur_node, ptr - cur_node) && (prev_prefix[ptr - cur_node] == ':')) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 413 | LOGVAL(ctx, LYVE_XPATH, "Duplicate prefix for \"%.*s\" in path.", cur_len, cur_node); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 414 | ret = LY_EVALID; |
Michal Vasko | 8b06a5e | 2020-08-06 12:13:08 +0200 | [diff] [blame] | 415 | goto error; |
| 416 | } |
| 417 | |
| 418 | /* remember this next prefix */ |
| 419 | prev_prefix = cur_node; |
| 420 | } |
| 421 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | ++tok_idx; |
| 425 | |
| 426 | /* Predicate* */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 427 | LY_CHECK_GOTO(ret = ly_path_check_predicate(ctx, ctx_node, exp, &tok_idx, prefix, pred), error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 428 | |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 429 | /* '/' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 430 | } while (!lyxp_next_token(NULL, exp, &tok_idx, LYXP_TOKEN_OPER_PATH)); |
| 431 | |
| 432 | /* trailing token check */ |
| 433 | if (exp->used > tok_idx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 434 | LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of path.", exp->expr + exp->tok_pos[tok_idx]); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 435 | ret = LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 436 | goto error; |
| 437 | } |
| 438 | |
| 439 | *expr = exp; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 440 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 441 | LOG_LOCBACK(ctx_node ? 1 : 0, 0, 0, 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 442 | return LY_SUCCESS; |
| 443 | |
| 444 | error: |
| 445 | lyxp_expr_free(ctx, exp); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 446 | LOG_LOCBACK(ctx_node ? 1 : 0, 0, 0, 0); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 447 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | LY_ERR |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 451 | ly_path_parse_predicate(const struct ly_ctx *ctx, const struct lysc_node *cur_node, const char *str_path, |
Michal Vasko | 32ca49b | 2023-02-17 15:11:35 +0100 | [diff] [blame] | 452 | size_t path_len, uint16_t prefix, uint16_t pred, struct lyxp_expr **expr) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 453 | { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 454 | LY_ERR ret = LY_SUCCESS; |
| 455 | struct lyxp_expr *exp = NULL; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 456 | uint32_t tok_idx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 457 | |
| 458 | assert((prefix == LY_PATH_PREFIX_OPTIONAL) || (prefix == LY_PATH_PREFIX_MANDATORY)); |
| 459 | assert((pred == LY_PATH_PRED_KEYS) || (pred == LY_PATH_PRED_SIMPLE) || (pred == LY_PATH_PRED_LEAFREF)); |
| 460 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 461 | LOG_LOCSET(cur_node, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 462 | |
Michal Vasko | b01983e | 2023-04-24 10:43:13 +0200 | [diff] [blame] | 463 | /* parse as a generic XPath expression, reparse is performed manually */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 464 | LY_CHECK_GOTO(ret = lyxp_expr_parse(ctx, str_path, path_len, 0, &exp), error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 465 | tok_idx = 0; |
| 466 | |
Michal Vasko | b01983e | 2023-04-24 10:43:13 +0200 | [diff] [blame] | 467 | /* alloc empty repeat (only '=', filled manually) */ |
| 468 | exp->repeat = calloc(exp->size, sizeof *exp->repeat); |
| 469 | LY_CHECK_ERR_GOTO(!exp->repeat, LOGMEM(ctx); ret = LY_EMEM, error); |
| 470 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 471 | LY_CHECK_GOTO(ret = ly_path_check_predicate(ctx, cur_node, exp, &tok_idx, prefix, pred), error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 472 | |
| 473 | /* trailing token check */ |
| 474 | if (exp->used > tok_idx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 475 | LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of predicate.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 476 | exp->expr + exp->tok_pos[tok_idx]); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 477 | ret = LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 478 | goto error; |
| 479 | } |
| 480 | |
| 481 | *expr = exp; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 482 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 483 | LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 484 | return LY_SUCCESS; |
| 485 | |
| 486 | error: |
| 487 | lyxp_expr_free(ctx, exp); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 488 | LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 489 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | /** |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 493 | * @brief Parse NameTest and get the corresponding schema node. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 494 | * |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 495 | * @param[in] ctx libyang context. |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 496 | * @param[in] cur_node Optional current (original context) node. |
Radek Krejci | 84d7fd7 | 2021-07-14 18:32:21 +0200 | [diff] [blame] | 497 | * @param[in] cur_mod Current module of the path (where the path is "instantiated"). Needed for ::LY_VALUE_SCHEMA |
| 498 | * and ::LY_VALUE_SCHEMA_RESOLVED. |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 499 | * @param[in] prev_ctx_node Previous context node. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 500 | * @param[in] expr Parsed path. |
| 501 | * @param[in] tok_idx Index in @p expr. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 502 | * @param[in] format Format of the path. |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 503 | * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix). |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 504 | * @param[in] top_ext Optional top-level extension to use for searching the schema node. |
| 505 | * @param[in] getnext_opts Options to be used for ::lys_getnext() calls. |
| 506 | * @param[out] snode Resolved schema node. |
| 507 | * @param[out] ext Optional extension instance of @p snode, if any. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 508 | * @return LY_ERR value. |
| 509 | */ |
| 510 | static LY_ERR |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 511 | ly_path_compile_snode(const struct ly_ctx *ctx, const struct lysc_node *cur_node, const struct lys_module *cur_mod, |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 512 | const struct lysc_node *prev_ctx_node, const struct lyxp_expr *expr, uint32_t tok_idx, LY_VALUE_FORMAT format, |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 513 | void *prefix_data, const struct lysc_ext_instance *top_ext, uint32_t getnext_opts, const struct lysc_node **snode, |
| 514 | struct lysc_ext_instance **ext) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 515 | { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 516 | LY_ERR ret; |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 517 | const struct lys_module *mod = NULL; |
| 518 | struct lysc_ext_instance *e = NULL; |
| 519 | const char *pref, *name; |
| 520 | size_t len, name_len; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 521 | |
| 522 | assert(expr->tokens[tok_idx] == LYXP_TOKEN_NAMETEST); |
| 523 | |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 524 | *snode = NULL; |
| 525 | if (ext) { |
| 526 | *ext = NULL; |
| 527 | } |
| 528 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 529 | /* get prefix */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 530 | if ((pref = strnstr(expr->expr + expr->tok_pos[tok_idx], ":", expr->tok_len[tok_idx]))) { |
| 531 | len = pref - (expr->expr + expr->tok_pos[tok_idx]); |
| 532 | pref = expr->expr + expr->tok_pos[tok_idx]; |
| 533 | } else { |
| 534 | len = 0; |
| 535 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 536 | |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 537 | /* set name */ |
| 538 | if (pref) { |
| 539 | name = pref + len + 1; |
| 540 | name_len = expr->tok_len[tok_idx] - len - 1; |
| 541 | } else { |
| 542 | name = expr->expr + expr->tok_pos[tok_idx]; |
| 543 | name_len = expr->tok_len[tok_idx]; |
| 544 | } |
| 545 | |
| 546 | /* find node module */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 547 | if (pref) { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 548 | LOG_LOCSET(cur_node, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 549 | |
Michal Vasko | 35b2962 | 2022-07-22 14:12:56 +0200 | [diff] [blame] | 550 | mod = ly_resolve_prefix(prev_ctx_node ? prev_ctx_node->module->ctx : ctx, pref, len, format, prefix_data); |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 551 | if ((!mod || !mod->implemented) && prev_ctx_node) { |
| 552 | /* check for nested ext data */ |
| 553 | ret = ly_nested_ext_schema(NULL, prev_ctx_node, pref, len, format, prefix_data, name, name_len, snode, &e); |
| 554 | if (!ret) { |
| 555 | goto success; |
| 556 | } else if (ret != LY_ENOT) { |
| 557 | goto error; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | if (!mod) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 562 | LOGVAL(ctx, LYVE_XPATH, "No module connected with the prefix \"%.*s\" found (prefix format %s).", |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 563 | (int)len, pref, ly_format2str(format)); |
Michal Vasko | 825a044 | 2021-04-16 16:11:53 +0200 | [diff] [blame] | 564 | ret = LY_EVALID; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 565 | goto error; |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 566 | } else if (!mod->implemented) { |
| 567 | LOGVAL(ctx, LYVE_XPATH, "Not implemented module \"%s\" in path.", mod->name); |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 568 | ret = LY_EVALID; |
| 569 | goto error; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 570 | } |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 571 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 572 | LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 573 | } else { |
| 574 | switch (format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 575 | case LY_VALUE_SCHEMA: |
| 576 | case LY_VALUE_SCHEMA_RESOLVED: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 577 | if (!cur_mod) { |
| 578 | LOGINT_RET(ctx); |
| 579 | } |
| 580 | /* use current module */ |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 581 | mod = cur_mod; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 582 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 583 | case LY_VALUE_JSON: |
Michal Vasko | 79228af | 2021-08-26 14:44:28 +0200 | [diff] [blame] | 584 | case LY_VALUE_LYB: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 585 | if (!prev_ctx_node) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 586 | LOGINT_RET(ctx); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 587 | } |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 588 | /* inherit module of the previous node */ |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 589 | mod = prev_ctx_node->module; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 590 | break; |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 591 | case LY_VALUE_CANON: |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 592 | case LY_VALUE_XML: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 593 | case LY_VALUE_STR_NS: |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 594 | /* not really defined or accepted */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 595 | LOGINT_RET(ctx); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 599 | /* find schema node */ |
| 600 | if (!prev_ctx_node && top_ext) { |
| 601 | *snode = lysc_ext_find_node(top_ext, mod, name, name_len, 0, getnext_opts); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 602 | } else { |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 603 | *snode = lys_find_child(prev_ctx_node, mod, name, name_len, 0, getnext_opts); |
| 604 | if (!(*snode) && prev_ctx_node) { |
| 605 | ret = ly_nested_ext_schema(NULL, prev_ctx_node, pref, len, format, prefix_data, name, name_len, snode, &e); |
| 606 | LY_CHECK_RET(ret && (ret != LY_ENOT), ret); |
| 607 | } |
| 608 | } |
| 609 | if (!(*snode)) { |
| 610 | LOGVAL(ctx, LYVE_XPATH, "Not found node \"%.*s\" in path.", (int)name_len, name); |
| 611 | return LY_ENOTFOUND; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 612 | } |
| 613 | |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 614 | success: |
| 615 | if (ext) { |
| 616 | *ext = e; |
| 617 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 618 | return LY_SUCCESS; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 619 | |
| 620 | error: |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 621 | LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0); |
Michal Vasko | 825a044 | 2021-04-16 16:11:53 +0200 | [diff] [blame] | 622 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | LY_ERR |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 626 | ly_path_compile_predicate(const struct ly_ctx *ctx, const struct lysc_node *cur_node, const struct lys_module *cur_mod, |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 627 | const struct lysc_node *ctx_node, const struct lyxp_expr *expr, uint32_t *tok_idx, LY_VALUE_FORMAT format, |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 628 | void *prefix_data, struct ly_path_predicate **predicates) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 629 | { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 630 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 631 | struct ly_path_predicate *p; |
| 632 | const struct lysc_node *key; |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 633 | const char *val; |
| 634 | size_t val_len, key_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 635 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 636 | assert(ctx && ctx_node); |
| 637 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 638 | LOG_LOCSET(cur_node, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 639 | |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 640 | *predicates = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 641 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 642 | if (lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1)) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 643 | /* '[', no predicate */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 644 | goto cleanup; /* LY_SUCCESS */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | if (expr->tokens[*tok_idx] == LYXP_TOKEN_NAMETEST) { |
| 648 | if (ctx_node->nodetype != LYS_LIST) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 649 | LOGVAL(ctx, LYVE_XPATH, "List predicate defined for %s \"%s\" in path.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 650 | lys_nodetype2str(ctx_node->nodetype), ctx_node->name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 651 | ret = LY_EVALID; |
| 652 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 653 | } else if (ctx_node->flags & LYS_KEYLESS) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 654 | LOGVAL(ctx, LYVE_XPATH, "List predicate defined for keyless %s \"%s\" in path.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 655 | lys_nodetype2str(ctx_node->nodetype), ctx_node->name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 656 | ret = LY_EVALID; |
| 657 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | do { |
| 661 | /* NameTest, find the key */ |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 662 | LY_CHECK_RET(ly_path_compile_snode(ctx, cur_node, cur_mod, ctx_node, expr, *tok_idx, format, prefix_data, |
| 663 | NULL, 0, &key, NULL)); |
| 664 | if ((key->nodetype != LYS_LEAF) || !(key->flags & LYS_KEY)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 665 | LOGVAL(ctx, LYVE_XPATH, "Key expected instead of %s \"%s\" in path.", lys_nodetype2str(key->nodetype), |
| 666 | key->name); |
| 667 | ret = LY_EVALID; |
| 668 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 669 | } |
| 670 | ++(*tok_idx); |
| 671 | |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 672 | /* new predicate */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 673 | LY_ARRAY_NEW_GOTO(ctx, *predicates, p, ret, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 674 | p->key = key; |
| 675 | |
| 676 | /* '=' */ |
| 677 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL); |
| 678 | ++(*tok_idx); |
| 679 | |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 680 | /* Literal, Number, or VariableReference */ |
| 681 | if (expr->tokens[*tok_idx] == LYXP_TOKEN_VARREF) { |
| 682 | /* store the variable name */ |
| 683 | p->variable = strndup(expr->expr + expr->tok_pos[*tok_idx], expr->tok_len[*tok_idx]); |
| 684 | LY_CHECK_ERR_GOTO(!p->variable, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 685 | |
| 686 | p->type = LY_PATH_PREDTYPE_LIST_VAR; |
| 687 | ++(*tok_idx); |
Michal Vasko | 4911eeb | 2021-06-28 11:23:05 +0200 | [diff] [blame] | 688 | } else { |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 689 | if (expr->tokens[*tok_idx] == LYXP_TOKEN_LITERAL) { |
| 690 | /* skip quotes */ |
| 691 | val = expr->expr + expr->tok_pos[*tok_idx] + 1; |
| 692 | val_len = expr->tok_len[*tok_idx] - 2; |
| 693 | } else { |
| 694 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_NUMBER); |
| 695 | val = expr->expr + expr->tok_pos[*tok_idx]; |
| 696 | val_len = expr->tok_len[*tok_idx]; |
| 697 | } |
| 698 | |
| 699 | /* store the value */ |
| 700 | LOG_LOCSET(key, NULL, NULL, NULL); |
Michal Vasko | 989cdb4 | 2023-10-06 15:32:37 +0200 | [diff] [blame^] | 701 | ret = lyd_value_store(ctx, &p->value, ((struct lysc_node_leaf *)key)->type, val, val_len, 0, NULL, |
| 702 | format, prefix_data, LYD_HINT_DATA, key, NULL); |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 703 | LOG_LOCBACK(key ? 1 : 0, 0, 0, 0); |
| 704 | LY_CHECK_ERR_GOTO(ret, p->value.realtype = NULL, cleanup); |
| 705 | |
| 706 | /* "allocate" the type to avoid problems when freeing the value after the type was freed */ |
| 707 | LY_ATOMIC_INC_BARRIER(((struct lysc_type *)p->value.realtype)->refcount); |
| 708 | |
| 709 | p->type = LY_PATH_PREDTYPE_LIST; |
| 710 | ++(*tok_idx); |
Michal Vasko | 4911eeb | 2021-06-28 11:23:05 +0200 | [diff] [blame] | 711 | } |
| 712 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 713 | /* ']' */ |
| 714 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
| 715 | ++(*tok_idx); |
| 716 | |
| 717 | /* another predicate follows? */ |
| 718 | } while (!lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1)); |
| 719 | |
| 720 | /* check that all keys were set */ |
| 721 | key_count = 0; |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 722 | for (key = lysc_node_child(ctx_node); key && (key->flags & LYS_KEY); key = key->next) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 723 | ++key_count; |
| 724 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 725 | if (LY_ARRAY_COUNT(*predicates) != key_count) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 726 | /* names (keys) are unique - it was checked when parsing */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 727 | LOGVAL(ctx, LYVE_XPATH, "Predicate missing for a key of %s \"%s\" in path.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 728 | lys_nodetype2str(ctx_node->nodetype), ctx_node->name); |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 729 | ly_path_predicates_free(ctx, *predicates); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 730 | *predicates = NULL; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 731 | ret = LY_EVALID; |
| 732 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | } else if (expr->tokens[*tok_idx] == LYXP_TOKEN_DOT) { |
| 736 | if (ctx_node->nodetype != LYS_LEAFLIST) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 737 | LOGVAL(ctx, LYVE_XPATH, "Leaf-list predicate defined for %s \"%s\" in path.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 738 | lys_nodetype2str(ctx_node->nodetype), ctx_node->name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 739 | ret = LY_EVALID; |
| 740 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 741 | } |
| 742 | ++(*tok_idx); |
| 743 | |
| 744 | /* new predicate */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 745 | LY_ARRAY_NEW_GOTO(ctx, *predicates, p, ret, cleanup); |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 746 | p->type = LY_PATH_PREDTYPE_LEAFLIST; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 747 | |
| 748 | /* '=' */ |
| 749 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL); |
| 750 | ++(*tok_idx); |
| 751 | |
Michal Vasko | 4911eeb | 2021-06-28 11:23:05 +0200 | [diff] [blame] | 752 | /* Literal or Number */ |
| 753 | assert((expr->tokens[*tok_idx] == LYXP_TOKEN_LITERAL) || (expr->tokens[*tok_idx] == LYXP_TOKEN_NUMBER)); |
| 754 | if (expr->tokens[*tok_idx] == LYXP_TOKEN_LITERAL) { |
| 755 | /* skip quotes */ |
| 756 | val = expr->expr + expr->tok_pos[*tok_idx] + 1; |
| 757 | val_len = expr->tok_len[*tok_idx] - 2; |
| 758 | } else { |
| 759 | val = expr->expr + expr->tok_pos[*tok_idx]; |
| 760 | val_len = expr->tok_len[*tok_idx]; |
| 761 | } |
| 762 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 763 | /* store the value */ |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 764 | LOG_LOCSET(ctx_node, NULL, NULL, NULL); |
Michal Vasko | 989cdb4 | 2023-10-06 15:32:37 +0200 | [diff] [blame^] | 765 | ret = lyd_value_store(ctx, &p->value, ((struct lysc_node_leaflist *)ctx_node)->type, val, val_len, 0, NULL, |
| 766 | format, prefix_data, LYD_HINT_DATA, ctx_node, NULL); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 767 | LOG_LOCBACK(ctx_node ? 1 : 0, 0, 0, 0); |
Michal Vasko | 55b8481 | 2021-05-11 09:23:58 +0200 | [diff] [blame] | 768 | LY_CHECK_ERR_GOTO(ret, p->value.realtype = NULL, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 769 | ++(*tok_idx); |
| 770 | |
Michal Vasko | ae87566 | 2020-10-21 10:33:17 +0200 | [diff] [blame] | 771 | /* "allocate" the type to avoid problems when freeing the value after the type was freed */ |
Michal Vasko | 04338d9 | 2021-09-01 07:58:14 +0200 | [diff] [blame] | 772 | LY_ATOMIC_INC_BARRIER(((struct lysc_type *)p->value.realtype)->refcount); |
Michal Vasko | ae87566 | 2020-10-21 10:33:17 +0200 | [diff] [blame] | 773 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 774 | /* ']' */ |
| 775 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
| 776 | ++(*tok_idx); |
| 777 | } else { |
| 778 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_NUMBER); |
| 779 | if (!(ctx_node->nodetype & (LYS_LEAFLIST | LYS_LIST))) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 780 | ret = LY_EVALID; |
| 781 | LOGVAL(ctx, LYVE_XPATH, "Positional predicate defined for %s \"%s\" in path.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 782 | lys_nodetype2str(ctx_node->nodetype), ctx_node->name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 783 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 784 | } else if (ctx_node->flags & LYS_CONFIG_W) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 785 | ret = LY_EVALID; |
| 786 | LOGVAL(ctx, LYVE_XPATH, "Positional predicate defined for configuration %s \"%s\" in path.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 787 | lys_nodetype2str(ctx_node->nodetype), ctx_node->name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 788 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 789 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 790 | |
| 791 | /* new predicate */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 792 | LY_ARRAY_NEW_GOTO(ctx, *predicates, p, ret, cleanup); |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 793 | p->type = LY_PATH_PREDTYPE_POSITION; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 794 | |
| 795 | /* syntax was already checked */ |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 796 | p->position = strtoull(expr->expr + expr->tok_pos[*tok_idx], (char **)&val, LY_BASE_DEC); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 797 | ++(*tok_idx); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 798 | |
| 799 | /* ']' */ |
| 800 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
| 801 | ++(*tok_idx); |
| 802 | } |
| 803 | |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 804 | cleanup: |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 805 | LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 806 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | /** |
| 810 | * @brief Compile leafref predicate. Actually, it is only checked. |
| 811 | * |
| 812 | * @param[in] ctx_node Context node, node for which the predicate is defined. |
| 813 | * @param[in] cur_node Current (original context) node. |
| 814 | * @param[in] expr Parsed path. |
| 815 | * @param[in,out] tok_idx Index in @p expr, is adjusted for parsed tokens. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 816 | * @param[in] format Format of the path. |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 817 | * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix). |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 818 | * @return LY_ERR value. |
| 819 | */ |
| 820 | static LY_ERR |
| 821 | ly_path_compile_predicate_leafref(const struct lysc_node *ctx_node, const struct lysc_node *cur_node, |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 822 | const struct lyxp_expr *expr, uint32_t *tok_idx, LY_VALUE_FORMAT format, void *prefix_data) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 823 | { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 824 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 825 | const struct lysc_node *key, *node, *node2; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 826 | struct ly_ctx *ctx = cur_node->module->ctx; |
| 827 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 828 | if (lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1)) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 829 | /* '[', no predicate */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 830 | goto cleanup; /* LY_SUCCESS */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | if (ctx_node->nodetype != LYS_LIST) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 834 | LOGVAL(ctx, LYVE_XPATH, "List predicate defined for %s \"%s\" in path.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 835 | lys_nodetype2str(ctx_node->nodetype), ctx_node->name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 836 | ret = LY_EVALID; |
| 837 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 838 | } else if (ctx_node->flags & LYS_KEYLESS) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 839 | LOGVAL(ctx, LYVE_XPATH, "List predicate defined for keyless %s \"%s\" in path.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 840 | lys_nodetype2str(ctx_node->nodetype), ctx_node->name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 841 | ret = LY_EVALID; |
| 842 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | do { |
| 846 | /* NameTest, find the key */ |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 847 | ret = ly_path_compile_snode(ctx, cur_node, cur_node->module, ctx_node, expr, *tok_idx, format, prefix_data, |
| 848 | NULL, 0, &key, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 849 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 850 | if ((key->nodetype != LYS_LEAF) || !(key->flags & LYS_KEY)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 851 | LOGVAL(ctx, LYVE_XPATH, "Key expected instead of %s \"%s\" in path.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 852 | lys_nodetype2str(key->nodetype), key->name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 853 | ret = LY_EVALID; |
| 854 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 855 | } |
| 856 | ++(*tok_idx); |
| 857 | |
| 858 | /* we are not actually compiling, throw the key away */ |
| 859 | (void)key; |
| 860 | |
| 861 | /* '=' */ |
| 862 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL); |
| 863 | ++(*tok_idx); |
| 864 | |
| 865 | /* FuncName */ |
| 866 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_FUNCNAME); |
| 867 | ++(*tok_idx); |
| 868 | |
| 869 | /* evaluating from the "current()" node */ |
| 870 | node = cur_node; |
| 871 | |
| 872 | /* '(' */ |
| 873 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
| 874 | ++(*tok_idx); |
| 875 | |
| 876 | /* ')' */ |
| 877 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
| 878 | ++(*tok_idx); |
| 879 | |
| 880 | do { |
| 881 | /* '/' */ |
| 882 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH); |
| 883 | ++(*tok_idx); |
| 884 | |
| 885 | /* go to parent */ |
| 886 | if (!node) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 887 | LOGVAL(ctx, LYVE_XPATH, "Too many parent references in path."); |
| 888 | ret = LY_EVALID; |
| 889 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 890 | } |
| 891 | node = lysc_data_parent(node); |
| 892 | |
| 893 | /* '..' */ |
| 894 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_DDOT); |
| 895 | ++(*tok_idx); |
| 896 | } while (expr->tokens[*tok_idx + 1] == LYXP_TOKEN_DDOT); |
| 897 | |
| 898 | do { |
| 899 | /* '/' */ |
| 900 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH); |
| 901 | ++(*tok_idx); |
| 902 | |
| 903 | /* NameTest */ |
| 904 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_NAMETEST); |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 905 | LY_CHECK_RET(ly_path_compile_snode(ctx, cur_node, cur_node->module, node, expr, *tok_idx, format, |
| 906 | prefix_data, NULL, 0, &node2, NULL)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 907 | node = node2; |
| 908 | ++(*tok_idx); |
| 909 | } while ((*tok_idx + 1 < expr->used) && (expr->tokens[*tok_idx + 1] == LYXP_TOKEN_NAMETEST)); |
| 910 | |
| 911 | /* check the last target node */ |
| 912 | if (node->nodetype != LYS_LEAF) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 913 | LOGVAL(ctx, LYVE_XPATH, "Leaf expected instead of %s \"%s\" in leafref predicate in path.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 914 | lys_nodetype2str(node->nodetype), node->name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 915 | ret = LY_EVALID; |
| 916 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | /* we are not actually compiling, throw the rightside node away */ |
| 920 | (void)node; |
| 921 | |
| 922 | /* ']' */ |
| 923 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
| 924 | ++(*tok_idx); |
| 925 | |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 926 | /* another predicate follows? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 927 | } while (!lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1)); |
| 928 | |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 929 | cleanup: |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 930 | return (ret == LY_ENOTFOUND) ? LY_EVALID : ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 931 | } |
| 932 | |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 933 | /** |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 934 | * @brief Duplicate ly_path_predicate structure. |
| 935 | * |
| 936 | * @param[in] ctx libyang context. |
| 937 | * @param[in] pred The array of path predicates. |
| 938 | * @param[out] dup Duplicated predicates. |
| 939 | * @return LY_ERR value. |
| 940 | */ |
| 941 | static LY_ERR |
| 942 | ly_path_dup_predicates(const struct ly_ctx *ctx, const struct ly_path_predicate *pred, struct ly_path_predicate **dup) |
| 943 | { |
| 944 | LY_ARRAY_COUNT_TYPE u; |
| 945 | |
| 946 | if (!pred) { |
| 947 | return LY_SUCCESS; |
| 948 | } |
| 949 | |
| 950 | LY_ARRAY_CREATE_RET(ctx, *dup, LY_ARRAY_COUNT(pred), LY_EMEM); |
| 951 | LY_ARRAY_FOR(pred, u) { |
| 952 | LY_ARRAY_INCREMENT(*dup); |
| 953 | (*dup)[u].type = pred->type; |
| 954 | |
Michal Vasko | 5339b4f | 2023-09-29 14:38:08 +0200 | [diff] [blame] | 955 | switch (pred[u].type) { |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 956 | case LY_PATH_PREDTYPE_POSITION: |
| 957 | /* position-predicate */ |
Michal Vasko | 5339b4f | 2023-09-29 14:38:08 +0200 | [diff] [blame] | 958 | (*dup)[u].position = pred[u].position; |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 959 | break; |
| 960 | case LY_PATH_PREDTYPE_LIST: |
| 961 | case LY_PATH_PREDTYPE_LEAFLIST: |
| 962 | /* key-predicate or leaf-list-predicate */ |
Michal Vasko | 5339b4f | 2023-09-29 14:38:08 +0200 | [diff] [blame] | 963 | (*dup)[u].key = pred[u].key; |
| 964 | pred[u].value.realtype->plugin->duplicate(ctx, &pred[u].value, &(*dup)[u].value); |
| 965 | LY_ATOMIC_INC_BARRIER(((struct lysc_type *)pred[u].value.realtype)->refcount); |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 966 | break; |
| 967 | case LY_PATH_PREDTYPE_LIST_VAR: |
| 968 | /* key-predicate with a variable */ |
Michal Vasko | 5339b4f | 2023-09-29 14:38:08 +0200 | [diff] [blame] | 969 | (*dup)[u].key = pred[u].key; |
| 970 | (*dup)[u].variable = strdup(pred[u].variable); |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 971 | break; |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | return LY_SUCCESS; |
| 976 | } |
| 977 | |
| 978 | /** |
| 979 | * @brief Appends path elements from source to destination array |
| 980 | * |
| 981 | * @param[in] ctx libyang context. |
| 982 | * @param[in] src The source path |
| 983 | * @param[in,out] dst The destination path |
| 984 | * @return LY_ERR value. |
| 985 | */ |
| 986 | static LY_ERR |
| 987 | ly_path_append(const struct ly_ctx *ctx, const struct ly_path *src, struct ly_path **dst) |
| 988 | { |
| 989 | LY_ERR ret = LY_SUCCESS; |
| 990 | LY_ARRAY_COUNT_TYPE u; |
| 991 | struct ly_path *p; |
| 992 | |
| 993 | if (!src) { |
| 994 | return LY_SUCCESS; |
| 995 | } |
| 996 | |
| 997 | LY_ARRAY_CREATE_RET(ctx, *dst, LY_ARRAY_COUNT(src), LY_EMEM); |
| 998 | LY_ARRAY_FOR(src, u) { |
Michal Vasko | 696915b | 2023-05-31 10:12:11 +0200 | [diff] [blame] | 999 | LY_ARRAY_NEW_GOTO(ctx, *dst, p, ret, cleanup); |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 1000 | p->node = src[u].node; |
| 1001 | p->ext = src[u].ext; |
Michal Vasko | 696915b | 2023-05-31 10:12:11 +0200 | [diff] [blame] | 1002 | LY_CHECK_GOTO(ret = ly_path_dup_predicates(ctx, src[u].predicates, &p->predicates), cleanup); |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 1003 | } |
| 1004 | |
Michal Vasko | 696915b | 2023-05-31 10:12:11 +0200 | [diff] [blame] | 1005 | cleanup: |
| 1006 | return ret; |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | /** |
| 1010 | * @brief Compile deref XPath function into ly_path structure. |
| 1011 | * |
| 1012 | * @param[in] ctx libyang context. |
| 1013 | * @param[in] ctx_node Optional context node, mandatory of @p lref. |
| 1014 | * @param[in] top_ext Extension instance containing the definition of the data being created. It is used to find |
| 1015 | * the top-level node inside the extension instance instead of a module. Note that this is the case not only if |
| 1016 | * the @p ctx_node is NULL, but also if the relative path starting in @p ctx_node reaches the document root |
| 1017 | * via double dots. |
| 1018 | * @param[in] expr Parsed path. |
| 1019 | * @param[in] oper Oper option (@ref path_oper_options). |
| 1020 | * @param[in] target Target option (@ref path_target_options). |
| 1021 | * @param[in] format Format of the path. |
| 1022 | * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix). |
| 1023 | * @param[in,out] tok_idx Index in @p exp, is adjusted. |
| 1024 | * @param[out] path Compiled path. |
| 1025 | * @return LY_ERR value. |
| 1026 | */ |
| 1027 | static LY_ERR |
| 1028 | ly_path_compile_deref(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, |
| 1029 | const struct lysc_ext_instance *top_ext, const struct lyxp_expr *expr, uint16_t oper, uint16_t target, |
| 1030 | LY_VALUE_FORMAT format, void *prefix_data, uint32_t *tok_idx, struct ly_path **path) |
| 1031 | { |
| 1032 | LY_ERR ret = LY_SUCCESS; |
| 1033 | struct lyxp_expr expr2; |
| 1034 | struct ly_path *path2 = NULL; |
| 1035 | const struct lysc_node *node2; |
| 1036 | const struct lysc_node_leaf *deref_leaf_node; |
| 1037 | const struct lysc_type_leafref *lref; |
| 1038 | uint32_t begin_token; |
| 1039 | |
Michal Vasko | 696915b | 2023-05-31 10:12:11 +0200 | [diff] [blame] | 1040 | *path = NULL; |
| 1041 | |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 1042 | /* properly parsed path must always starts with 'deref' and '(' */ |
| 1043 | assert(!lyxp_check_token(NULL, expr, *tok_idx, LYXP_TOKEN_FUNCNAME)); |
| 1044 | assert(!strncmp(&expr->expr[expr->tok_pos[*tok_idx]], "deref", 5)); |
| 1045 | (*tok_idx)++; |
| 1046 | assert(!lyxp_check_token(NULL, expr, *tok_idx, LYXP_TOKEN_PAR1)); |
| 1047 | (*tok_idx)++; |
| 1048 | begin_token = *tok_idx; |
| 1049 | |
| 1050 | /* emebedded functions were already identified count tokens till ')' */ |
| 1051 | while (lyxp_check_token(NULL, expr, *tok_idx, LYXP_TOKEN_PAR2) && (*tok_idx < expr->used)) { |
| 1052 | (*tok_idx)++; |
| 1053 | } |
| 1054 | |
| 1055 | /* properly parsed path must have ')' within the tokens */ |
| 1056 | assert(!lyxp_check_token(NULL, expr, *tok_idx, LYXP_TOKEN_PAR2)); |
| 1057 | |
| 1058 | /* prepare expr representing just deref arg */ |
| 1059 | expr2.tokens = &expr->tokens[begin_token]; |
| 1060 | expr2.tok_pos = &expr->tok_pos[begin_token]; |
| 1061 | expr2.tok_len = &expr->tok_len[begin_token]; |
| 1062 | expr2.repeat = &expr->repeat[begin_token]; |
| 1063 | expr2.used = *tok_idx - begin_token; |
| 1064 | expr2.size = expr->size - begin_token; |
| 1065 | expr2.expr = expr->expr; |
| 1066 | |
| 1067 | /* compile just deref arg, append it to the path and find dereferenced lref for next operations */ |
Michal Vasko | 696915b | 2023-05-31 10:12:11 +0200 | [diff] [blame] | 1068 | LY_CHECK_GOTO(ret = ly_path_compile_leafref(ctx, ctx_node, top_ext, &expr2, oper, target, format, prefix_data, |
| 1069 | &path2), cleanup); |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 1070 | node2 = path2[LY_ARRAY_COUNT(path2) - 1].node; |
| 1071 | deref_leaf_node = (const struct lysc_node_leaf *)node2; |
| 1072 | lref = (const struct lysc_type_leafref *)deref_leaf_node->type; |
Michal Vasko | 696915b | 2023-05-31 10:12:11 +0200 | [diff] [blame] | 1073 | LY_CHECK_GOTO(ret = ly_path_append(ctx, path2, path), cleanup); |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 1074 | ly_path_free(ctx, path2); |
| 1075 | path2 = NULL; |
| 1076 | |
| 1077 | /* compile dereferenced leafref expression and append it to the path */ |
Michal Vasko | 696915b | 2023-05-31 10:12:11 +0200 | [diff] [blame] | 1078 | LY_CHECK_GOTO(ret = ly_path_compile_leafref(ctx, node2, top_ext, lref->path, oper, target, format, prefix_data, |
| 1079 | &path2), cleanup); |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 1080 | node2 = path2[LY_ARRAY_COUNT(path2) - 1].node; |
Michal Vasko | 696915b | 2023-05-31 10:12:11 +0200 | [diff] [blame] | 1081 | LY_CHECK_GOTO(ret = ly_path_append(ctx, path2, path), cleanup); |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 1082 | ly_path_free(ctx, path2); |
| 1083 | path2 = NULL; |
| 1084 | |
| 1085 | /* properly parsed path must always continue with ')' and '/' */ |
| 1086 | assert(!lyxp_check_token(NULL, expr, *tok_idx, LYXP_TOKEN_PAR2)); |
| 1087 | (*tok_idx)++; |
| 1088 | assert(!lyxp_check_token(NULL, expr, *tok_idx, LYXP_TOKEN_OPER_PATH)); |
| 1089 | (*tok_idx)++; |
| 1090 | |
| 1091 | /* prepare expr representing rest of the path after deref */ |
| 1092 | expr2.tokens = &expr->tokens[*tok_idx]; |
| 1093 | expr2.tok_pos = &expr->tok_pos[*tok_idx]; |
| 1094 | expr2.tok_len = &expr->tok_len[*tok_idx]; |
| 1095 | expr2.repeat = &expr->repeat[*tok_idx]; |
| 1096 | expr2.used = expr->used - *tok_idx; |
| 1097 | expr2.size = expr->size - *tok_idx; |
| 1098 | expr2.expr = expr->expr; |
| 1099 | |
| 1100 | /* compile rest of the path and append it to the path */ |
Michal Vasko | 696915b | 2023-05-31 10:12:11 +0200 | [diff] [blame] | 1101 | LY_CHECK_GOTO(ret = ly_path_compile_leafref(ctx, node2, top_ext, &expr2, oper, target, format, prefix_data, &path2), |
| 1102 | cleanup); |
| 1103 | LY_CHECK_GOTO(ret = ly_path_append(ctx, path2, path), cleanup); |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 1104 | |
| 1105 | cleanup: |
| 1106 | ly_path_free(ctx, path2); |
| 1107 | if (ret) { |
| 1108 | ly_path_free(ctx, *path); |
| 1109 | *path = NULL; |
| 1110 | } |
Michal Vasko | 696915b | 2023-05-31 10:12:11 +0200 | [diff] [blame] | 1111 | return ret; |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | /** |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 1115 | * @brief Compile path into ly_path structure. Any predicates of a leafref are only checked, not compiled. |
| 1116 | * |
| 1117 | * @param[in] ctx libyang context. |
| 1118 | * @param[in] cur_mod Current module of the path (where it was "instantiated"), ignored of @p lref. Used for nodes |
Radek Krejci | 84d7fd7 | 2021-07-14 18:32:21 +0200 | [diff] [blame] | 1119 | * without a prefix for ::LY_VALUE_SCHEMA and ::LY_VALUE_SCHEMA_RESOLVED format. |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 1120 | * @param[in] ctx_node Optional context node, mandatory of @p lref. |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1121 | * @param[in] top_ext Extension instance containing the definition of the data being created. It is used to find the top-level |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 1122 | * node inside the extension instance instead of a module. Note that this is the case not only if the @p ctx_node is NULL, |
| 1123 | * but also if the relative path starting in @p ctx_node reaches the document root via double dots. |
| 1124 | * @param[in] expr Parsed path. |
| 1125 | * @param[in] lref Whether leafref is being compiled or not. |
| 1126 | * @param[in] oper Oper option (@ref path_oper_options). |
| 1127 | * @param[in] target Target option (@ref path_target_options). |
Michal Vasko | 0884d21 | 2021-10-14 09:21:46 +0200 | [diff] [blame] | 1128 | * @param[in] limit_access_tree Whether to limit accessible tree as described in |
| 1129 | * [XPath context](https://datatracker.ietf.org/doc/html/rfc7950#section-6.4.1). |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 1130 | * @param[in] format Format of the path. |
| 1131 | * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix). |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 1132 | * @param[out] path Compiled path. |
| 1133 | * @return LY_ERECOMPILE, only if @p lref. |
| 1134 | * @return LY_ERR value. |
| 1135 | */ |
| 1136 | static LY_ERR |
| 1137 | _ly_path_compile(const struct ly_ctx *ctx, const struct lys_module *cur_mod, const struct lysc_node *ctx_node, |
Michal Vasko | 32ca49b | 2023-02-17 15:11:35 +0100 | [diff] [blame] | 1138 | const struct lysc_ext_instance *top_ext, const struct lyxp_expr *expr, ly_bool lref, uint16_t oper, uint16_t target, |
Michal Vasko | 0884d21 | 2021-10-14 09:21:46 +0200 | [diff] [blame] | 1139 | ly_bool limit_access_tree, LY_VALUE_FORMAT format, void *prefix_data, struct ly_path **path) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1140 | { |
| 1141 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1142 | uint32_t tok_idx = 0, getnext_opts; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 1143 | const struct lysc_node *node2, *cur_node, *op; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1144 | struct ly_path *p = NULL; |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1145 | struct lysc_ext_instance *ext = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1146 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1147 | assert(ctx); |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 1148 | assert(!lref || ctx_node); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1149 | assert((oper == LY_PATH_OPER_INPUT) || (oper == LY_PATH_OPER_OUTPUT)); |
| 1150 | assert((target == LY_PATH_TARGET_SINGLE) || (target == LY_PATH_TARGET_MANY)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1151 | |
Michal Vasko | 0884d21 | 2021-10-14 09:21:46 +0200 | [diff] [blame] | 1152 | if (!limit_access_tree) { |
| 1153 | op = NULL; |
| 1154 | } else { |
| 1155 | /* find operation, if we are in any */ |
| 1156 | for (op = ctx_node; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {} |
| 1157 | } |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 1158 | |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1159 | *path = NULL; |
| 1160 | |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 1161 | /* remember original context node */ |
| 1162 | cur_node = ctx_node; |
Michal Vasko | 62225e2 | 2022-08-05 10:06:50 +0200 | [diff] [blame] | 1163 | LOG_LOCSET(cur_node, NULL, NULL, NULL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1164 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1165 | if (oper == LY_PATH_OPER_OUTPUT) { |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1166 | getnext_opts = LYS_GETNEXT_OUTPUT; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1167 | } else { |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 1168 | getnext_opts = 0; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1169 | } |
| 1170 | |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 1171 | if (lref && (ly_ctx_get_options(ctx) & LY_CTX_LEAFREF_EXTENDED) && |
| 1172 | (expr->tokens[tok_idx] == LYXP_TOKEN_FUNCNAME)) { |
| 1173 | /* deref function */ |
| 1174 | ret = ly_path_compile_deref(ctx, ctx_node, top_ext, expr, oper, target, format, prefix_data, &tok_idx, path); |
| 1175 | ctx_node = (*path)[LY_ARRAY_COUNT(*path) - 1].node; |
| 1176 | goto cleanup; |
| 1177 | } else if (expr->tokens[tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1178 | /* absolute path */ |
| 1179 | ctx_node = NULL; |
| 1180 | |
| 1181 | ++tok_idx; |
| 1182 | } else { |
| 1183 | /* relative path */ |
Michal Vasko | 8023979 | 2021-11-02 11:48:32 +0100 | [diff] [blame] | 1184 | if (!ctx_node) { |
| 1185 | LOGVAL(ctx, LYVE_XPATH, "No initial schema parent for a relative path."); |
| 1186 | ret = LY_EVALID; |
| 1187 | goto cleanup; |
| 1188 | } |
| 1189 | |
| 1190 | /* go up the parents for leafref */ |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 1191 | while (lref && (expr->tokens[tok_idx] == LYXP_TOKEN_DDOT)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1192 | if (!ctx_node) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1193 | LOGVAL(ctx, LYVE_XPATH, "Too many parent references in path."); |
Michal Vasko | 14424ba | 2020-12-09 18:09:51 +0100 | [diff] [blame] | 1194 | ret = LY_EVALID; |
| 1195 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | /* get parent */ |
| 1199 | ctx_node = lysc_data_parent(ctx_node); |
| 1200 | |
| 1201 | ++tok_idx; |
| 1202 | |
| 1203 | assert(expr->tokens[tok_idx] == LYXP_TOKEN_OPER_PATH); |
| 1204 | ++tok_idx; |
| 1205 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | do { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1209 | /* check last compiled inner node, whether it is uniquely identified (even key-less list) */ |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 1210 | if (p && !lref && (target == LY_PATH_TARGET_SINGLE) && (p->node->nodetype == LYS_LIST) && !p->predicates) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1211 | LOGVAL(ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1212 | lys_nodetype2str(p->node->nodetype), p->node->name); |
Michal Vasko | 14424ba | 2020-12-09 18:09:51 +0100 | [diff] [blame] | 1213 | ret = LY_EVALID; |
| 1214 | goto cleanup; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1215 | } |
| 1216 | |
Michal Vasko | 14424ba | 2020-12-09 18:09:51 +0100 | [diff] [blame] | 1217 | /* NameTest */ |
| 1218 | LY_CHECK_ERR_GOTO(lyxp_check_token(ctx, expr, tok_idx, LYXP_TOKEN_NAMETEST), ret = LY_EVALID, cleanup); |
| 1219 | |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1220 | /* get schema node */ |
| 1221 | LY_CHECK_GOTO(ret = ly_path_compile_snode(ctx, cur_node, cur_mod, ctx_node, expr, tok_idx, format, prefix_data, |
| 1222 | top_ext, getnext_opts, &node2, &ext), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1223 | ++tok_idx; |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1224 | if ((op && (node2->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node2 != op))) { |
| 1225 | LOGVAL(ctx, LYVE_XPATH, "Not found node \"%s\" in path.", node2->name); |
Radek Krejci | 8de005f | 2020-06-25 17:02:07 +0200 | [diff] [blame] | 1226 | ret = LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1227 | goto cleanup; |
| 1228 | } |
| 1229 | ctx_node = node2; |
| 1230 | |
| 1231 | /* new path segment */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1232 | LY_ARRAY_NEW_GOTO(ctx, *path, p, ret, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1233 | p->node = ctx_node; |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1234 | p->ext = ext; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1235 | |
| 1236 | /* compile any predicates */ |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 1237 | if (lref) { |
Michal Vasko | 24fc4d1 | 2021-07-12 14:41:20 +0200 | [diff] [blame] | 1238 | ret = ly_path_compile_predicate_leafref(ctx_node, cur_node, expr, &tok_idx, format, prefix_data); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1239 | } else { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 1240 | ret = ly_path_compile_predicate(ctx, cur_node, cur_mod, ctx_node, expr, &tok_idx, format, prefix_data, |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 1241 | &p->predicates); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1242 | } |
| 1243 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1244 | } while (!lyxp_next_token(NULL, expr, &tok_idx, LYXP_TOKEN_OPER_PATH)); |
| 1245 | |
Michal Vasko | 14424ba | 2020-12-09 18:09:51 +0100 | [diff] [blame] | 1246 | /* check leftover tokens */ |
| 1247 | if (tok_idx < expr->used) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 1248 | LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(expr->tokens[tok_idx]), &expr->expr[expr->tok_pos[tok_idx]]); |
Michal Vasko | 14424ba | 2020-12-09 18:09:51 +0100 | [diff] [blame] | 1249 | ret = LY_EVALID; |
| 1250 | goto cleanup; |
| 1251 | } |
| 1252 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1253 | /* check last compiled node */ |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 1254 | if (!lref && (target == LY_PATH_TARGET_SINGLE) && (p->node->nodetype & (LYS_LIST | LYS_LEAFLIST)) && !p->predicates) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1255 | LOGVAL(ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1256 | lys_nodetype2str(p->node->nodetype), p->node->name); |
Michal Vasko | 14424ba | 2020-12-09 18:09:51 +0100 | [diff] [blame] | 1257 | ret = LY_EVALID; |
| 1258 | goto cleanup; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1259 | } |
| 1260 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1261 | cleanup: |
| 1262 | if (ret) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1263 | ly_path_free(ctx, *path); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1264 | *path = NULL; |
| 1265 | } |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1266 | LOG_LOCBACK(1, 0, 0, 0); |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1267 | return (ret == LY_ENOTFOUND) ? LY_EVALID : ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | LY_ERR |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 1271 | ly_path_compile(const struct ly_ctx *ctx, const struct lys_module *cur_mod, const struct lysc_node *ctx_node, |
Michal Vasko | 32ca49b | 2023-02-17 15:11:35 +0100 | [diff] [blame] | 1272 | const struct lysc_ext_instance *top_ext, const struct lyxp_expr *expr, uint16_t oper, uint16_t target, |
Michal Vasko | 0884d21 | 2021-10-14 09:21:46 +0200 | [diff] [blame] | 1273 | ly_bool limit_access_tree, LY_VALUE_FORMAT format, void *prefix_data, struct ly_path **path) |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 1274 | { |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1275 | return _ly_path_compile(ctx, cur_mod, ctx_node, top_ext, expr, 0, oper, target, limit_access_tree, format, |
| 1276 | prefix_data, path); |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | LY_ERR |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1280 | ly_path_compile_leafref(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const struct lysc_ext_instance *top_ext, |
Michal Vasko | 32ca49b | 2023-02-17 15:11:35 +0100 | [diff] [blame] | 1281 | const struct lyxp_expr *expr, uint16_t oper, uint16_t target, LY_VALUE_FORMAT format, void *prefix_data, |
Michal Vasko | 24fc4d1 | 2021-07-12 14:41:20 +0200 | [diff] [blame] | 1282 | struct ly_path **path) |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 1283 | { |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1284 | return _ly_path_compile(ctx, ctx_node->module, ctx_node, top_ext, expr, 1, oper, target, 1, format, prefix_data, path); |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | LY_ERR |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 1288 | ly_path_eval_partial(const struct ly_path *path, const struct lyd_node *start, const struct lyxp_var *vars, |
| 1289 | LY_ARRAY_COUNT_TYPE *path_idx, struct lyd_node **match) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1290 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1291 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | e43e21f | 2022-06-07 12:26:36 +0200 | [diff] [blame] | 1292 | struct lyd_node *prev_node = NULL, *elem, *node = NULL, *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1293 | uint64_t pos; |
| 1294 | |
| 1295 | assert(path && start); |
| 1296 | |
| 1297 | if (lysc_data_parent(path[0].node)) { |
| 1298 | /* relative path, start from the parent children */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 1299 | start = lyd_child(start); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1300 | } else { |
| 1301 | /* absolute path, start from the first top-level sibling */ |
| 1302 | while (start->parent) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 1303 | start = lyd_parent(start); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1304 | } |
| 1305 | while (start->prev->next) { |
| 1306 | start = start->prev; |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | LY_ARRAY_FOR(path, u) { |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 1311 | if (path[u].predicates) { |
| 1312 | switch (path[u].predicates[0].type) { |
| 1313 | case LY_PATH_PREDTYPE_POSITION: |
| 1314 | /* we cannot use hashes and want an instance on a specific position */ |
| 1315 | pos = 1; |
| 1316 | node = NULL; |
| 1317 | LYD_LIST_FOR_INST(start, path[u].node, elem) { |
| 1318 | if (pos == path[u].predicates[0].position) { |
| 1319 | node = elem; |
| 1320 | break; |
| 1321 | } |
| 1322 | ++pos; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1323 | } |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 1324 | break; |
| 1325 | case LY_PATH_PREDTYPE_LEAFLIST: |
| 1326 | /* we will use hashes to find one leaf-list instance */ |
| 1327 | LY_CHECK_RET(lyd_create_term2(path[u].node, &path[u].predicates[0].value, &target)); |
| 1328 | lyd_find_sibling_first(start, target, &node); |
| 1329 | lyd_free_tree(target); |
| 1330 | break; |
| 1331 | case LY_PATH_PREDTYPE_LIST_VAR: |
| 1332 | case LY_PATH_PREDTYPE_LIST: |
| 1333 | /* we will use hashes to find one list instance */ |
| 1334 | LY_CHECK_RET(lyd_create_list(path[u].node, path[u].predicates, vars, &target)); |
| 1335 | lyd_find_sibling_first(start, target, &node); |
| 1336 | lyd_free_tree(target); |
| 1337 | break; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1338 | } |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 1339 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1340 | /* we will use hashes to find one any/container/leaf instance */ |
| 1341 | lyd_find_sibling_val(start, path[u].node, NULL, 0, &node); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | if (!node) { |
| 1345 | /* no matching nodes */ |
| 1346 | break; |
| 1347 | } |
| 1348 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1349 | /* rememeber previous node */ |
| 1350 | prev_node = node; |
| 1351 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1352 | /* next path segment, if any */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 1353 | start = lyd_child(node); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1354 | } |
| 1355 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1356 | if (node) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1357 | /* we have found the full path */ |
| 1358 | if (path_idx) { |
| 1359 | *path_idx = u; |
| 1360 | } |
| 1361 | if (match) { |
| 1362 | *match = node; |
| 1363 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1364 | return LY_SUCCESS; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1365 | |
| 1366 | } else if (prev_node) { |
| 1367 | /* we have found only some partial match */ |
| 1368 | if (path_idx) { |
| 1369 | *path_idx = u - 1; |
| 1370 | } |
| 1371 | if (match) { |
| 1372 | *match = prev_node; |
| 1373 | } |
| 1374 | return LY_EINCOMPLETE; |
| 1375 | } |
| 1376 | |
| 1377 | /* we have not found any nodes */ |
| 1378 | if (path_idx) { |
| 1379 | *path_idx = 0; |
| 1380 | } |
| 1381 | if (match) { |
| 1382 | *match = NULL; |
| 1383 | } |
| 1384 | return LY_ENOTFOUND; |
| 1385 | } |
| 1386 | |
| 1387 | LY_ERR |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 1388 | ly_path_eval(const struct ly_path *path, const struct lyd_node *start, const struct lyxp_var *vars, struct lyd_node **match) |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1389 | { |
| 1390 | LY_ERR ret; |
| 1391 | struct lyd_node *m; |
| 1392 | |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 1393 | ret = ly_path_eval_partial(path, start, vars, NULL, &m); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1394 | |
| 1395 | if (ret == LY_SUCCESS) { |
| 1396 | /* last node was found */ |
| 1397 | if (match) { |
| 1398 | *match = m; |
| 1399 | } |
| 1400 | return LY_SUCCESS; |
| 1401 | } |
| 1402 | |
| 1403 | /* not a full match */ |
| 1404 | if (match) { |
| 1405 | *match = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1406 | } |
| 1407 | return LY_ENOTFOUND; |
| 1408 | } |
| 1409 | |
| 1410 | LY_ERR |
| 1411 | ly_path_dup(const struct ly_ctx *ctx, const struct ly_path *path, struct ly_path **dup) |
| 1412 | { |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 1413 | LY_ERR ret = LY_SUCCESS; |
| 1414 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1415 | |
| 1416 | if (!path) { |
| 1417 | return LY_SUCCESS; |
| 1418 | } |
| 1419 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1420 | LY_ARRAY_CREATE_RET(ctx, *dup, LY_ARRAY_COUNT(path), LY_EMEM); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1421 | LY_ARRAY_FOR(path, u) { |
| 1422 | LY_ARRAY_INCREMENT(*dup); |
| 1423 | (*dup)[u].node = path[u].node; |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 1424 | (*dup)[u].ext = path[u].ext; |
| 1425 | LY_CHECK_RET(ret = ly_path_dup_predicates(ctx, path[u].predicates, &(*dup)[u].predicates), ret); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | return LY_SUCCESS; |
| 1429 | } |
| 1430 | |
| 1431 | void |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 1432 | ly_path_predicates_free(const struct ly_ctx *ctx, struct ly_path_predicate *predicates) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1433 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1434 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1435 | struct lysf_ctx fctx = {.ctx = (struct ly_ctx *)ctx}; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1436 | |
| 1437 | if (!predicates) { |
| 1438 | return; |
| 1439 | } |
| 1440 | |
| 1441 | LY_ARRAY_FOR(predicates, u) { |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 1442 | switch (predicates[u].type) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1443 | case LY_PATH_PREDTYPE_POSITION: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1444 | /* nothing to free */ |
| 1445 | break; |
| 1446 | case LY_PATH_PREDTYPE_LIST: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1447 | case LY_PATH_PREDTYPE_LEAFLIST: |
Michal Vasko | ae87566 | 2020-10-21 10:33:17 +0200 | [diff] [blame] | 1448 | if (predicates[u].value.realtype) { |
| 1449 | predicates[u].value.realtype->plugin->free(ctx, &predicates[u].value); |
Michal Vasko | c636ea4 | 2022-09-16 10:20:31 +0200 | [diff] [blame] | 1450 | lysc_type_free(&fctx, (struct lysc_type *)predicates[u].value.realtype); |
Michal Vasko | ae87566 | 2020-10-21 10:33:17 +0200 | [diff] [blame] | 1451 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1452 | break; |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 1453 | case LY_PATH_PREDTYPE_LIST_VAR: |
| 1454 | free(predicates[u].variable); |
| 1455 | break; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1456 | } |
| 1457 | } |
| 1458 | LY_ARRAY_FREE(predicates); |
| 1459 | } |
| 1460 | |
| 1461 | void |
| 1462 | ly_path_free(const struct ly_ctx *ctx, struct ly_path *path) |
| 1463 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1464 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1465 | |
Michal Vasko | 55b8481 | 2021-05-11 09:23:58 +0200 | [diff] [blame] | 1466 | if (!path) { |
| 1467 | return; |
| 1468 | } |
| 1469 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1470 | LY_ARRAY_FOR(path, u) { |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 1471 | ly_path_predicates_free(ctx, path[u].predicates); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1472 | } |
| 1473 | LY_ARRAY_FREE(path); |
| 1474 | } |