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 | * |
| 6 | * Copyright (c) 2020 CESNET, z.s.p.o. |
| 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 | */ |
| 14 | #define _ISOC99_SOURCE /* strtoull */ |
| 15 | |
| 16 | #include "path.h" |
| 17 | |
| 18 | #include <assert.h> |
| 19 | #include <ctype.h> |
| 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, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 51 | uint16_t *tok_idx, uint8_t prefix, uint8_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 */ |
| 95 | if (!strncmp(set->objs[i], name, name_len) && !isalpha(((char *)set->objs[i])[name_len])) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 96 | 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] | 97 | goto token_error; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | /* add it into the set */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 102 | ret = ly_set_add(set, (void *)name, 1, NULL); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 103 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 104 | |
| 105 | /* NameTest */ |
| 106 | ++(*tok_idx); |
| 107 | |
| 108 | /* '=' */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 109 | 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] | 110 | |
| 111 | /* Literal */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 112 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_LITERAL), token_error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 113 | |
| 114 | /* ']' */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 115 | 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] | 116 | |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 117 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 118 | } while (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_BRACK1)); |
| 119 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 120 | } 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] | 121 | /* '.' */ |
| 122 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 123 | /* '=' */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 124 | 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] | 125 | |
| 126 | /* Literal */ |
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_LITERAL), token_error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 128 | |
| 129 | /* ']' */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 130 | 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] | 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_NUMBER)) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 133 | /* Number */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 134 | |
| 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_BRACK2), token_error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 137 | |
| 138 | } else if ((pred == LY_PATH_PRED_LEAFREF) && !lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NAMETEST)) { |
| 139 | assert(prefix == LY_PATH_PREFIX_OPTIONAL); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 140 | ret = ly_set_new(&set); |
| 141 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 142 | |
| 143 | do { |
| 144 | /* NameTest is always expected here */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 145 | 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] | 146 | |
| 147 | name = strnstr(exp->expr + exp->tok_pos[*tok_idx], ":", exp->tok_len[*tok_idx]); |
| 148 | if (!name) { |
| 149 | name = exp->expr + exp->tok_pos[*tok_idx]; |
| 150 | name_len = exp->tok_len[*tok_idx]; |
| 151 | } else { |
| 152 | ++name; |
| 153 | name_len = exp->tok_len[*tok_idx] - (name - (exp->expr + exp->tok_pos[*tok_idx])); |
| 154 | } |
| 155 | |
| 156 | /* check whether it was not already specified */ |
| 157 | for (i = 0; i < set->count; ++i) { |
| 158 | /* all the keys must be from the same module so this comparison should be fine */ |
| 159 | if (!strncmp(set->objs[i], name, name_len) && !isalpha(((char *)set->objs[i])[name_len])) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 160 | 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] | 161 | goto token_error; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| 165 | /* add it into the set */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 166 | ret = ly_set_add(set, (void *)name, 1, NULL); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 167 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 168 | |
| 169 | /* NameTest */ |
| 170 | ++(*tok_idx); |
| 171 | |
| 172 | /* '=' */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 173 | 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] | 174 | |
| 175 | /* FuncName */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 176 | 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] | 177 | if ((exp->tok_len[*tok_idx] != ly_strlen_const("current")) || |
| 178 | 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] | 179 | LOGVAL(ctx, LYVE_XPATH, "Invalid function \"%.*s\" invocation in path.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 180 | exp->tok_len[*tok_idx], exp->expr + exp->tok_pos[*tok_idx]); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 181 | goto token_error; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 182 | } |
| 183 | ++(*tok_idx); |
| 184 | |
| 185 | /* '(' */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 186 | 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] | 187 | |
| 188 | /* ')' */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 189 | 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] | 190 | |
| 191 | /* '/' */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 192 | 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] | 193 | |
| 194 | /* '..' */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 195 | 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] | 196 | do { |
| 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_PATH), token_error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 199 | } while (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_DDOT)); |
| 200 | |
| 201 | /* NameTest */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 202 | 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] | 203 | |
| 204 | /* '/' */ |
| 205 | while (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_OPER_PATH)) { |
| 206 | /* NameTest */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 207 | 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] | 208 | } |
| 209 | |
| 210 | /* ']' */ |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 211 | 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] | 212 | |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 213 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 214 | } while (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_BRACK1)); |
| 215 | |
| 216 | } else { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 217 | LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), exp->expr + exp->tok_pos[*tok_idx]); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 218 | goto token_error; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 222 | cleanup: |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 223 | LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 224 | ly_set_free(set, NULL); |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 225 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 226 | |
Radek Krejci | ba03a5a | 2020-08-27 14:40:41 +0200 | [diff] [blame] | 227 | token_error: |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 228 | LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 229 | ly_set_free(set, NULL); |
| 230 | return LY_EVALID; |
| 231 | } |
| 232 | |
| 233 | LY_ERR |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 234 | ly_path_parse(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *str_path, size_t path_len, |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 235 | ly_bool lref, uint8_t begin, uint8_t prefix, uint8_t pred, struct lyxp_expr **expr) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 236 | { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 237 | LY_ERR ret = LY_SUCCESS; |
| 238 | struct lyxp_expr *exp = NULL; |
Michal Vasko | 8b06a5e | 2020-08-06 12:13:08 +0200 | [diff] [blame] | 239 | uint16_t tok_idx, cur_len; |
| 240 | const char *cur_node, *prev_prefix = NULL, *ptr; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 241 | |
| 242 | assert((begin == LY_PATH_BEGIN_ABSOLUTE) || (begin == LY_PATH_BEGIN_EITHER)); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 243 | assert((prefix == LY_PATH_PREFIX_OPTIONAL) || (prefix == LY_PATH_PREFIX_MANDATORY) || |
| 244 | (prefix == LY_PATH_PREFIX_STRICT_INHERIT)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 245 | assert((pred == LY_PATH_PRED_KEYS) || (pred == LY_PATH_PRED_SIMPLE) || (pred == LY_PATH_PRED_LEAFREF)); |
| 246 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 247 | LOG_LOCSET(ctx_node, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 248 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 249 | /* parse as a generic XPath expression */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 250 | LY_CHECK_GOTO(ret = lyxp_expr_parse(ctx, str_path, path_len, 1, &exp), error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 251 | tok_idx = 0; |
| 252 | |
| 253 | if (begin == LY_PATH_BEGIN_EITHER) { |
| 254 | /* is the path relative? */ |
| 255 | if (lyxp_next_token(NULL, exp, &tok_idx, LYXP_TOKEN_OPER_PATH)) { |
Michal Vasko | cb8c6d4 | 2020-10-16 11:58:30 +0200 | [diff] [blame] | 256 | /* relative path check specific to leafref */ |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 257 | if (lref) { |
Michal Vasko | cb8c6d4 | 2020-10-16 11:58:30 +0200 | [diff] [blame] | 258 | /* mandatory '..' */ |
| 259 | LY_CHECK_ERR_GOTO(lyxp_next_token(ctx, exp, &tok_idx, LYXP_TOKEN_DDOT), ret = LY_EVALID, error); |
| 260 | |
| 261 | do { |
| 262 | /* '/' */ |
| 263 | LY_CHECK_ERR_GOTO(lyxp_next_token(ctx, exp, &tok_idx, LYXP_TOKEN_OPER_PATH), ret = LY_EVALID, error); |
| 264 | |
| 265 | /* optional '..' */ |
| 266 | } while (!lyxp_next_token(NULL, exp, &tok_idx, LYXP_TOKEN_DDOT)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | } else { |
| 270 | /* '/' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 271 | LY_CHECK_ERR_GOTO(lyxp_next_token(ctx, exp, &tok_idx, LYXP_TOKEN_OPER_PATH), ret = LY_EVALID, error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | do { |
| 275 | /* NameTest */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 276 | 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] | 277 | |
| 278 | /* check prefix based on the options */ |
Michal Vasko | 8b06a5e | 2020-08-06 12:13:08 +0200 | [diff] [blame] | 279 | cur_node = exp->expr + exp->tok_pos[tok_idx]; |
| 280 | cur_len = exp->tok_len[tok_idx]; |
| 281 | if (prefix == LY_PATH_PREFIX_MANDATORY) { |
| 282 | if (!strnstr(cur_node, ":", cur_len)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 283 | 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] | 284 | ret = LY_EVALID; |
Michal Vasko | 8b06a5e | 2020-08-06 12:13:08 +0200 | [diff] [blame] | 285 | goto error; |
| 286 | } |
| 287 | } else if (prefix == LY_PATH_PREFIX_STRICT_INHERIT) { |
| 288 | if (!prev_prefix) { |
| 289 | /* the first node must have a prefix */ |
| 290 | if (!strnstr(cur_node, ":", cur_len)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 291 | 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] | 292 | ret = LY_EVALID; |
Michal Vasko | 8b06a5e | 2020-08-06 12:13:08 +0200 | [diff] [blame] | 293 | goto error; |
| 294 | } |
| 295 | |
| 296 | /* remember the first prefix */ |
| 297 | prev_prefix = cur_node; |
| 298 | } else { |
| 299 | /* the prefix must be different, if any */ |
| 300 | ptr = strnstr(cur_node, ":", cur_len); |
| 301 | if (ptr) { |
| 302 | 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] | 303 | 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] | 304 | ret = LY_EVALID; |
Michal Vasko | 8b06a5e | 2020-08-06 12:13:08 +0200 | [diff] [blame] | 305 | goto error; |
| 306 | } |
| 307 | |
| 308 | /* remember this next prefix */ |
| 309 | prev_prefix = cur_node; |
| 310 | } |
| 311 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | ++tok_idx; |
| 315 | |
| 316 | /* Predicate* */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 317 | 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] | 318 | |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 319 | /* '/' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 320 | } while (!lyxp_next_token(NULL, exp, &tok_idx, LYXP_TOKEN_OPER_PATH)); |
| 321 | |
| 322 | /* trailing token check */ |
| 323 | if (exp->used > tok_idx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 324 | 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] | 325 | ret = LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 326 | goto error; |
| 327 | } |
| 328 | |
| 329 | *expr = exp; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 330 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 331 | LOG_LOCBACK(ctx_node ? 1 : 0, 0, 0, 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 332 | return LY_SUCCESS; |
| 333 | |
| 334 | error: |
| 335 | lyxp_expr_free(ctx, exp); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 336 | LOG_LOCBACK(ctx_node ? 1 : 0, 0, 0, 0); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 337 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | LY_ERR |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 341 | ly_path_parse_predicate(const struct ly_ctx *ctx, const struct lysc_node *cur_node, const char *str_path, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 342 | size_t path_len, uint8_t prefix, uint8_t pred, struct lyxp_expr **expr) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 343 | { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 344 | LY_ERR ret = LY_SUCCESS; |
| 345 | struct lyxp_expr *exp = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 346 | uint16_t tok_idx; |
| 347 | |
| 348 | assert((prefix == LY_PATH_PREFIX_OPTIONAL) || (prefix == LY_PATH_PREFIX_MANDATORY)); |
| 349 | assert((pred == LY_PATH_PRED_KEYS) || (pred == LY_PATH_PRED_SIMPLE) || (pred == LY_PATH_PRED_LEAFREF)); |
| 350 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 351 | LOG_LOCSET(cur_node, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 352 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 353 | /* parse as a generic XPath expression */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 354 | 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] | 355 | tok_idx = 0; |
| 356 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 357 | 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] | 358 | |
| 359 | /* trailing token check */ |
| 360 | if (exp->used > tok_idx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 361 | 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] | 362 | exp->expr + exp->tok_pos[tok_idx]); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 363 | ret = LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 364 | goto error; |
| 365 | } |
| 366 | |
| 367 | *expr = exp; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 368 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 369 | LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 370 | return LY_SUCCESS; |
| 371 | |
| 372 | error: |
| 373 | lyxp_expr_free(ctx, exp); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 374 | LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 375 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | /** |
| 379 | * @brief Parse prefix from a NameTest, if any, and node name, and return expected module of the node. |
| 380 | * |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 381 | * @param[in] ctx libyang context. |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 382 | * @param[in] cur_node Optional current (original context) node. |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 383 | * @param[in] cur_mod Current module of the path (where the path is "instantiated"). Needed for ::LY_VALUE_SCHEMA*. |
| 384 | * @param[in] prev_ctx_node Previous context node. Needed for ::LY_VALUE_JSON. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 385 | * @param[in] expr Parsed path. |
| 386 | * @param[in] tok_idx Index in @p expr. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 387 | * @param[in] format Format of the path. |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 388 | * @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] | 389 | * @param[out] mod Resolved module. |
| 390 | * @param[out] name Parsed node name. |
| 391 | * @param[out] name_len Length of @p name. |
| 392 | * @return LY_ERR value. |
| 393 | */ |
| 394 | static LY_ERR |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 395 | ly_path_compile_prefix(const struct ly_ctx *ctx, const struct lysc_node *cur_node, const struct lys_module *cur_mod, |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 396 | const struct lysc_node *prev_ctx_node, const struct lyxp_expr *expr, uint16_t tok_idx, LY_VALUE_FORMAT format, |
| 397 | void *prefix_data, const struct lys_module **mod, const char **name, size_t *name_len) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 398 | { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 399 | LY_ERR ret; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 400 | const char *pref; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 401 | size_t len; |
| 402 | |
| 403 | assert(expr->tokens[tok_idx] == LYXP_TOKEN_NAMETEST); |
| 404 | |
| 405 | /* get prefix */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 406 | if ((pref = strnstr(expr->expr + expr->tok_pos[tok_idx], ":", expr->tok_len[tok_idx]))) { |
| 407 | len = pref - (expr->expr + expr->tok_pos[tok_idx]); |
| 408 | pref = expr->expr + expr->tok_pos[tok_idx]; |
| 409 | } else { |
| 410 | len = 0; |
| 411 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 412 | |
| 413 | /* find next node module */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 414 | if (pref) { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 415 | LOG_LOCSET(cur_node, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 416 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 417 | *mod = ly_resolve_prefix(ctx, pref, len, format, prefix_data); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 418 | if (!*mod) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 419 | 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] | 420 | (int)len, pref, ly_format2str(format)); |
Michal Vasko | 825a044 | 2021-04-16 16:11:53 +0200 | [diff] [blame] | 421 | ret = LY_EVALID; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 422 | goto error; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 423 | } else if (!(*mod)->implemented) { |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 424 | LOGVAL(ctx, LYVE_XPATH, "Not implemented module \"%s\" in path.", (*mod)->name); |
| 425 | ret = LY_EVALID; |
| 426 | goto error; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 427 | } |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 428 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 429 | LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 430 | } else { |
| 431 | switch (format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 432 | case LY_VALUE_SCHEMA: |
| 433 | case LY_VALUE_SCHEMA_RESOLVED: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 434 | if (!cur_mod) { |
| 435 | LOGINT_RET(ctx); |
| 436 | } |
| 437 | /* use current module */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 438 | *mod = cur_mod; |
| 439 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 440 | case LY_VALUE_JSON: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 441 | if (!prev_ctx_node) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 442 | LOGINT_RET(ctx); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 443 | } |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 444 | /* inherit module of the previous node */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 445 | *mod = prev_ctx_node->module; |
| 446 | break; |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 447 | case LY_VALUE_CANON: |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 448 | case LY_VALUE_XML: |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 449 | case LY_VALUE_LYB: |
| 450 | /* not really defined or accepted */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 451 | LOGINT_RET(ctx); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
| 455 | /* set name */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 456 | if (pref) { |
| 457 | *name = pref + len + 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 458 | *name_len = expr->tok_len[tok_idx] - len - 1; |
| 459 | } else { |
| 460 | *name = expr->expr + expr->tok_pos[tok_idx]; |
| 461 | *name_len = expr->tok_len[tok_idx]; |
| 462 | } |
| 463 | |
| 464 | return LY_SUCCESS; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 465 | |
| 466 | error: |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 467 | LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0); |
Michal Vasko | 825a044 | 2021-04-16 16:11:53 +0200 | [diff] [blame] | 468 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 469 | } |
| 470 | |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 471 | /** |
| 472 | * @brief Parse prefix from a NameTest in a leafref, if any, and node name, and return expected module of the node. |
| 473 | * |
| 474 | * @param[in] ctx libyang context. |
| 475 | * @param[in] cur_node Optional current (original context) node. |
| 476 | * @param[in] prev_ctx_node Previous context node. Needed for ::LY_VALUE_JSON. |
| 477 | * @param[in] expr Parsed path. |
| 478 | * @param[in] tok_idx Index in @p expr. |
| 479 | * @param[in] format Format of the path. |
| 480 | * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix). |
| 481 | * @param[in,out] unres Global unres to use. |
| 482 | * @param[out] mod Resolved module. |
| 483 | * @param[out] name Parsed node name. |
| 484 | * @param[out] name_len Length of @p name. |
| 485 | * @return LY_ERR value. |
| 486 | */ |
| 487 | static LY_ERR |
| 488 | ly_path_compile_prefix_leafref(const struct ly_ctx *ctx, const struct lysc_node *cur_node, |
| 489 | const struct lysc_node *prev_ctx_node, const struct lyxp_expr *expr, uint16_t tok_idx, LY_VALUE_FORMAT format, |
| 490 | void *prefix_data, struct lys_glob_unres *unres, const struct lys_module **mod, const char **name, |
| 491 | size_t *name_len) |
| 492 | { |
| 493 | LY_ERR ret; |
| 494 | const char *pref; |
| 495 | size_t len; |
| 496 | |
| 497 | assert(expr->tokens[tok_idx] == LYXP_TOKEN_NAMETEST); |
| 498 | |
| 499 | /* get prefix */ |
| 500 | if ((pref = strnstr(expr->expr + expr->tok_pos[tok_idx], ":", expr->tok_len[tok_idx]))) { |
| 501 | len = pref - (expr->expr + expr->tok_pos[tok_idx]); |
| 502 | pref = expr->expr + expr->tok_pos[tok_idx]; |
| 503 | } else { |
| 504 | len = 0; |
| 505 | } |
| 506 | |
| 507 | /* find next node module */ |
| 508 | if (pref) { |
| 509 | LOG_LOCSET(cur_node, NULL, NULL, NULL); |
| 510 | |
| 511 | *mod = ly_resolve_prefix(ctx, pref, len, format, prefix_data); |
| 512 | if (!*mod) { |
| 513 | LOGVAL(ctx, LYVE_XPATH, "No module connected with the prefix \"%.*s\" found (prefix format %s).", |
| 514 | (int)len, pref, ly_format2str(format)); |
| 515 | ret = LY_EVALID; |
| 516 | goto error; |
| 517 | } else if (!(*mod)->implemented) { |
| 518 | assert(unres); |
| 519 | LY_CHECK_GOTO(ret = lys_implement((struct lys_module *)*mod, NULL, unres), error); |
| 520 | LY_CHECK_GOTO(ret = lys_compile((struct lys_module *)*mod, &unres->ds_unres), error); |
| 521 | } |
| 522 | |
| 523 | LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0); |
| 524 | } else { |
| 525 | switch (format) { |
| 526 | case LY_VALUE_SCHEMA: |
| 527 | case LY_VALUE_SCHEMA_RESOLVED: |
| 528 | /* use original context node module */ |
| 529 | *mod = cur_node->module; |
| 530 | break; |
| 531 | case LY_VALUE_JSON: |
| 532 | if (!prev_ctx_node) { |
| 533 | LOGINT_RET(ctx); |
| 534 | } |
| 535 | /* inherit module of the previous node */ |
| 536 | *mod = prev_ctx_node->module; |
| 537 | break; |
| 538 | case LY_VALUE_CANON: |
| 539 | case LY_VALUE_XML: |
| 540 | case LY_VALUE_LYB: |
| 541 | /* not really defined or accepted */ |
| 542 | LOGINT_RET(ctx); |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | /* set name */ |
| 547 | if (pref) { |
| 548 | *name = pref + len + 1; |
| 549 | *name_len = expr->tok_len[tok_idx] - len - 1; |
| 550 | } else { |
| 551 | *name = expr->expr + expr->tok_pos[tok_idx]; |
| 552 | *name_len = expr->tok_len[tok_idx]; |
| 553 | } |
| 554 | |
| 555 | return LY_SUCCESS; |
| 556 | |
| 557 | error: |
| 558 | LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0); |
| 559 | return ret; |
| 560 | } |
| 561 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 562 | LY_ERR |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 563 | ly_path_compile_predicate(const struct ly_ctx *ctx, const struct lysc_node *cur_node, const struct lys_module *cur_mod, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 564 | const struct lysc_node *ctx_node, const struct lyxp_expr *expr, uint16_t *tok_idx, LY_VALUE_FORMAT format, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 565 | void *prefix_data, struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 566 | { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 567 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 568 | struct ly_path_predicate *p; |
| 569 | const struct lysc_node *key; |
Radek Krejci | 2b18bf1 | 2020-11-06 11:20:20 +0100 | [diff] [blame] | 570 | const struct lys_module *mod = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 571 | const char *name; |
| 572 | size_t name_len, key_count; |
| 573 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 574 | assert(ctx && ctx_node); |
| 575 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 576 | LOG_LOCSET(cur_node, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 577 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 578 | *pred_type = 0; |
| 579 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 580 | if (lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1)) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 581 | /* '[', no predicate */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 582 | goto cleanup; /* LY_SUCCESS */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | if (expr->tokens[*tok_idx] == LYXP_TOKEN_NAMETEST) { |
| 586 | if (ctx_node->nodetype != LYS_LIST) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 587 | LOGVAL(ctx, LYVE_XPATH, "List predicate defined for %s \"%s\" in path.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 588 | lys_nodetype2str(ctx_node->nodetype), ctx_node->name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 589 | ret = LY_EVALID; |
| 590 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 591 | } else if (ctx_node->flags & LYS_KEYLESS) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 592 | 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] | 593 | lys_nodetype2str(ctx_node->nodetype), ctx_node->name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 594 | ret = LY_EVALID; |
| 595 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | do { |
| 599 | /* NameTest, find the key */ |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 600 | LY_CHECK_RET(ly_path_compile_prefix(ctx, cur_node, cur_mod, ctx_node, expr, *tok_idx, format, prefix_data, |
| 601 | &mod, &name, &name_len)); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 602 | key = lys_find_child(ctx_node, mod, name, name_len, 0, 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 603 | if (!key) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 604 | LOGVAL(ctx, LYVE_XPATH, "Not found node \"%.*s\" in path.", (int)name_len, name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 605 | ret = LY_ENOTFOUND; |
| 606 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 607 | } else if ((key->nodetype != LYS_LEAF) || !(key->flags & LYS_KEY)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 608 | LOGVAL(ctx, LYVE_XPATH, "Key expected instead of %s \"%s\" in path.", lys_nodetype2str(key->nodetype), |
| 609 | key->name); |
| 610 | ret = LY_EVALID; |
| 611 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 612 | } |
| 613 | ++(*tok_idx); |
| 614 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 615 | if (!*pred_type) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 616 | /* new predicate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 617 | *pred_type = LY_PATH_PREDTYPE_LIST; |
| 618 | } |
| 619 | assert(*pred_type == LY_PATH_PREDTYPE_LIST); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 620 | LY_ARRAY_NEW_GOTO(ctx, *predicates, p, ret, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 621 | p->key = key; |
| 622 | |
| 623 | /* '=' */ |
| 624 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL); |
| 625 | ++(*tok_idx); |
| 626 | |
| 627 | /* Literal */ |
| 628 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_LITERAL); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 629 | LOG_LOCSET(key, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 630 | ret = lyd_value_store(ctx, &p->value, ((struct lysc_node_leaf *)key)->type, |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 631 | expr->expr + expr->tok_pos[*tok_idx] + 1, expr->tok_len[*tok_idx] - 2, NULL, format, prefix_data, |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 632 | LYD_HINT_DATA, key, NULL); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 633 | LOG_LOCBACK(key ? 1 : 0, 0, 0, 0); |
Michal Vasko | 55b8481 | 2021-05-11 09:23:58 +0200 | [diff] [blame] | 634 | LY_CHECK_ERR_GOTO(ret, p->value.realtype = NULL, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 635 | ++(*tok_idx); |
| 636 | |
Michal Vasko | ae87566 | 2020-10-21 10:33:17 +0200 | [diff] [blame] | 637 | /* "allocate" the type to avoid problems when freeing the value after the type was freed */ |
| 638 | ++((struct lysc_type *)p->value.realtype)->refcount; |
| 639 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 640 | /* ']' */ |
| 641 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
| 642 | ++(*tok_idx); |
| 643 | |
| 644 | /* another predicate follows? */ |
| 645 | } while (!lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1)); |
| 646 | |
| 647 | /* check that all keys were set */ |
| 648 | key_count = 0; |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 649 | 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] | 650 | ++key_count; |
| 651 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 652 | if (LY_ARRAY_COUNT(*predicates) != key_count) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 653 | /* names (keys) are unique - it was checked when parsing */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 654 | 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] | 655 | lys_nodetype2str(ctx_node->nodetype), ctx_node->name); |
Michal Vasko | f7e16e2 | 2020-10-21 09:24:39 +0200 | [diff] [blame] | 656 | ly_path_predicates_free(ctx, LY_PATH_PREDTYPE_LIST, *predicates); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 657 | *predicates = NULL; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 658 | ret = LY_EVALID; |
| 659 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | } else if (expr->tokens[*tok_idx] == LYXP_TOKEN_DOT) { |
| 663 | if (ctx_node->nodetype != LYS_LEAFLIST) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 664 | 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] | 665 | lys_nodetype2str(ctx_node->nodetype), ctx_node->name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 666 | ret = LY_EVALID; |
| 667 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 668 | } |
| 669 | ++(*tok_idx); |
| 670 | |
| 671 | /* new predicate */ |
| 672 | *pred_type = LY_PATH_PREDTYPE_LEAFLIST; |
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 | |
| 675 | /* '=' */ |
| 676 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL); |
| 677 | ++(*tok_idx); |
| 678 | |
| 679 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_LITERAL); |
| 680 | /* store the value */ |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 681 | LOG_LOCSET(ctx_node, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 682 | ret = lyd_value_store(ctx, &p->value, ((struct lysc_node_leaflist *)ctx_node)->type, |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 683 | expr->expr + expr->tok_pos[*tok_idx] + 1, expr->tok_len[*tok_idx] - 2, NULL, format, prefix_data, |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 684 | LYD_HINT_DATA, ctx_node, NULL); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 685 | LOG_LOCBACK(ctx_node ? 1 : 0, 0, 0, 0); |
Michal Vasko | 55b8481 | 2021-05-11 09:23:58 +0200 | [diff] [blame] | 686 | LY_CHECK_ERR_GOTO(ret, p->value.realtype = NULL, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 687 | ++(*tok_idx); |
| 688 | |
Michal Vasko | ae87566 | 2020-10-21 10:33:17 +0200 | [diff] [blame] | 689 | /* "allocate" the type to avoid problems when freeing the value after the type was freed */ |
| 690 | ++((struct lysc_type *)p->value.realtype)->refcount; |
| 691 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 692 | /* ']' */ |
| 693 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
| 694 | ++(*tok_idx); |
| 695 | } else { |
| 696 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_NUMBER); |
| 697 | if (!(ctx_node->nodetype & (LYS_LEAFLIST | LYS_LIST))) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 698 | ret = LY_EVALID; |
| 699 | LOGVAL(ctx, LYVE_XPATH, "Positional predicate defined for %s \"%s\" in path.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 700 | lys_nodetype2str(ctx_node->nodetype), ctx_node->name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 701 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 702 | } else if (ctx_node->flags & LYS_CONFIG_W) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 703 | ret = LY_EVALID; |
| 704 | 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] | 705 | lys_nodetype2str(ctx_node->nodetype), ctx_node->name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 706 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 707 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 708 | |
| 709 | /* new predicate */ |
| 710 | *pred_type = LY_PATH_PREDTYPE_POSITION; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 711 | LY_ARRAY_NEW_GOTO(ctx, *predicates, p, ret, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 712 | |
| 713 | /* syntax was already checked */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 714 | p->position = strtoull(expr->expr + expr->tok_pos[*tok_idx], (char **)&name, LY_BASE_DEC); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 715 | ++(*tok_idx); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 716 | |
| 717 | /* ']' */ |
| 718 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
| 719 | ++(*tok_idx); |
| 720 | } |
| 721 | |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 722 | cleanup: |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 723 | LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 724 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | /** |
| 728 | * @brief Compile leafref predicate. Actually, it is only checked. |
| 729 | * |
| 730 | * @param[in] ctx_node Context node, node for which the predicate is defined. |
| 731 | * @param[in] cur_node Current (original context) node. |
| 732 | * @param[in] expr Parsed path. |
| 733 | * @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] | 734 | * @param[in] format Format of the path. |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 735 | * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix). |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 736 | * @param[in,out] unres Global unres structure for newly implemented modules. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 737 | * @return LY_ERR value. |
| 738 | */ |
| 739 | static LY_ERR |
| 740 | ly_path_compile_predicate_leafref(const struct lysc_node *ctx_node, const struct lysc_node *cur_node, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 741 | const struct lyxp_expr *expr, uint16_t *tok_idx, LY_VALUE_FORMAT format, void *prefix_data, |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 742 | struct lys_glob_unres *unres) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 743 | { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 744 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 745 | const struct lysc_node *key, *node, *node2; |
| 746 | const struct lys_module *mod; |
| 747 | const char *name; |
| 748 | size_t name_len; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 749 | struct ly_ctx *ctx = cur_node->module->ctx; |
| 750 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 751 | LOG_LOCSET(cur_node, NULL, NULL, NULL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 752 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 753 | if (lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1)) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 754 | /* '[', no predicate */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 755 | goto cleanup; /* LY_SUCCESS */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | if (ctx_node->nodetype != LYS_LIST) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 759 | LOGVAL(ctx, LYVE_XPATH, "List predicate defined for %s \"%s\" in path.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 760 | lys_nodetype2str(ctx_node->nodetype), ctx_node->name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 761 | ret = LY_EVALID; |
| 762 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 763 | } else if (ctx_node->flags & LYS_KEYLESS) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 764 | 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] | 765 | lys_nodetype2str(ctx_node->nodetype), ctx_node->name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 766 | ret = LY_EVALID; |
| 767 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | do { |
| 771 | /* NameTest, find the key */ |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 772 | ret = ly_path_compile_prefix_leafref(ctx, cur_node, ctx_node, expr, *tok_idx, format, prefix_data, unres, &mod, |
| 773 | &name, &name_len); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 774 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 775 | key = lys_find_child(ctx_node, mod, name, name_len, 0, 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 776 | if (!key) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 777 | LOGVAL(ctx, LYVE_XPATH, "Not found node \"%.*s\" in path.", (int)name_len, name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 778 | ret = LY_EVALID; |
| 779 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 780 | } else if ((key->nodetype != LYS_LEAF) || !(key->flags & LYS_KEY)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 781 | LOGVAL(ctx, LYVE_XPATH, "Key expected instead of %s \"%s\" in path.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 782 | lys_nodetype2str(key->nodetype), key->name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 783 | ret = LY_EVALID; |
| 784 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 785 | } |
| 786 | ++(*tok_idx); |
| 787 | |
| 788 | /* we are not actually compiling, throw the key away */ |
| 789 | (void)key; |
| 790 | |
| 791 | /* '=' */ |
| 792 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL); |
| 793 | ++(*tok_idx); |
| 794 | |
| 795 | /* FuncName */ |
| 796 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_FUNCNAME); |
| 797 | ++(*tok_idx); |
| 798 | |
| 799 | /* evaluating from the "current()" node */ |
| 800 | node = cur_node; |
| 801 | |
| 802 | /* '(' */ |
| 803 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
| 804 | ++(*tok_idx); |
| 805 | |
| 806 | /* ')' */ |
| 807 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
| 808 | ++(*tok_idx); |
| 809 | |
| 810 | do { |
| 811 | /* '/' */ |
| 812 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH); |
| 813 | ++(*tok_idx); |
| 814 | |
| 815 | /* go to parent */ |
| 816 | if (!node) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 817 | LOGVAL(ctx, LYVE_XPATH, "Too many parent references in path."); |
| 818 | ret = LY_EVALID; |
| 819 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 820 | } |
| 821 | node = lysc_data_parent(node); |
| 822 | |
| 823 | /* '..' */ |
| 824 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_DDOT); |
| 825 | ++(*tok_idx); |
| 826 | } while (expr->tokens[*tok_idx + 1] == LYXP_TOKEN_DDOT); |
| 827 | |
| 828 | do { |
| 829 | /* '/' */ |
| 830 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH); |
| 831 | ++(*tok_idx); |
| 832 | |
| 833 | /* NameTest */ |
| 834 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_NAMETEST); |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 835 | LY_CHECK_RET(ly_path_compile_prefix_leafref(ctx, cur_node, node, expr, *tok_idx, format, prefix_data, unres, |
| 836 | &mod, &name, &name_len)); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 837 | node2 = lys_find_child(node, mod, name, name_len, 0, 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 838 | if (!node2) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 839 | LOGVAL(ctx, LYVE_XPATH, "Not found node \"%.*s\" in path.", (int)name_len, name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 840 | ret = LY_EVALID; |
| 841 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 842 | } |
| 843 | node = node2; |
| 844 | ++(*tok_idx); |
| 845 | } while ((*tok_idx + 1 < expr->used) && (expr->tokens[*tok_idx + 1] == LYXP_TOKEN_NAMETEST)); |
| 846 | |
| 847 | /* check the last target node */ |
| 848 | if (node->nodetype != LYS_LEAF) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 849 | 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] | 850 | lys_nodetype2str(node->nodetype), node->name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 851 | ret = LY_EVALID; |
| 852 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | /* we are not actually compiling, throw the rightside node away */ |
| 856 | (void)node; |
| 857 | |
| 858 | /* ']' */ |
| 859 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
| 860 | ++(*tok_idx); |
| 861 | |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 862 | /* another predicate follows? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 863 | } while (!lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1)); |
| 864 | |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 865 | cleanup: |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 866 | LOG_LOCBACK(1, 0, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 867 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 868 | } |
| 869 | |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 870 | /** |
| 871 | * @brief Compile path into ly_path structure. Any predicates of a leafref are only checked, not compiled. |
| 872 | * |
| 873 | * @param[in] ctx libyang context. |
| 874 | * @param[in] cur_mod Current module of the path (where it was "instantiated"), ignored of @p lref. Used for nodes |
| 875 | * without a prefix for ::LY_PREF_SCHEMA* format. |
| 876 | * @param[in] ctx_node Optional context node, mandatory of @p lref. |
| 877 | * @param[in] ext Extension instance containing the definition of the data being created. It is used to find the top-level |
| 878 | * node inside the extension instance instead of a module. Note that this is the case not only if the @p ctx_node is NULL, |
| 879 | * but also if the relative path starting in @p ctx_node reaches the document root via double dots. |
| 880 | * @param[in] expr Parsed path. |
| 881 | * @param[in] lref Whether leafref is being compiled or not. |
| 882 | * @param[in] oper Oper option (@ref path_oper_options). |
| 883 | * @param[in] target Target option (@ref path_target_options). |
| 884 | * @param[in] format Format of the path. |
| 885 | * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix). |
| 886 | * @param[in,out] unres Global unres structure for newly implemented modules, needed only if @p lref. |
| 887 | * @param[out] path Compiled path. |
| 888 | * @return LY_ERECOMPILE, only if @p lref. |
| 889 | * @return LY_ERR value. |
| 890 | */ |
| 891 | static LY_ERR |
| 892 | _ly_path_compile(const struct ly_ctx *ctx, const struct lys_module *cur_mod, const struct lysc_node *ctx_node, |
| 893 | const struct lysc_ext_instance *ext, const struct lyxp_expr *expr, ly_bool lref, uint8_t oper, uint8_t target, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 894 | LY_VALUE_FORMAT format, void *prefix_data, struct lys_glob_unres *unres, struct ly_path **path) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 895 | { |
| 896 | LY_ERR ret = LY_SUCCESS; |
| 897 | uint16_t tok_idx = 0; |
Radek Krejci | 2b18bf1 | 2020-11-06 11:20:20 +0100 | [diff] [blame] | 898 | const struct lys_module *mod = NULL; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 899 | const struct lysc_node *node2, *cur_node, *op; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 900 | struct ly_path *p = NULL; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 901 | uint32_t getnext_opts; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 902 | const char *name; |
| 903 | size_t name_len; |
| 904 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 905 | assert(ctx); |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 906 | assert(!lref || ctx_node); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 907 | assert((oper == LY_PATH_OPER_INPUT) || (oper == LY_PATH_OPER_OUTPUT)); |
| 908 | assert((target == LY_PATH_TARGET_SINGLE) || (target == LY_PATH_TARGET_MANY)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 909 | |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 910 | /* find operation, if we are in any */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 911 | for (op = ctx_node; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {} |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 912 | |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 913 | *path = NULL; |
| 914 | |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 915 | /* remember original context node */ |
| 916 | cur_node = ctx_node; |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 917 | LOG_LOCINIT(cur_node, NULL, NULL, NULL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 918 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 919 | if (oper == LY_PATH_OPER_OUTPUT) { |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 920 | getnext_opts = LYS_GETNEXT_OUTPUT; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 921 | } else { |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 922 | getnext_opts = 0; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 923 | } |
| 924 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 925 | if (expr->tokens[tok_idx] == LYXP_TOKEN_OPER_PATH) { |
| 926 | /* absolute path */ |
| 927 | ctx_node = NULL; |
| 928 | |
| 929 | ++tok_idx; |
| 930 | } else { |
| 931 | /* relative path */ |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 932 | while (lref && (expr->tokens[tok_idx] == LYXP_TOKEN_DDOT)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 933 | if (!ctx_node) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 934 | LOGVAL(ctx, LYVE_XPATH, "Too many parent references in path."); |
Michal Vasko | 14424ba | 2020-12-09 18:09:51 +0100 | [diff] [blame] | 935 | ret = LY_EVALID; |
| 936 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | /* get parent */ |
| 940 | ctx_node = lysc_data_parent(ctx_node); |
| 941 | |
| 942 | ++tok_idx; |
| 943 | |
| 944 | assert(expr->tokens[tok_idx] == LYXP_TOKEN_OPER_PATH); |
| 945 | ++tok_idx; |
| 946 | } |
| 947 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 948 | /* we are not storing the parent */ |
| 949 | (void)ctx_node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | do { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 953 | /* 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] | 954 | 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] | 955 | LOGVAL(ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 956 | lys_nodetype2str(p->node->nodetype), p->node->name); |
Michal Vasko | 14424ba | 2020-12-09 18:09:51 +0100 | [diff] [blame] | 957 | ret = LY_EVALID; |
| 958 | goto cleanup; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 959 | } |
| 960 | |
Michal Vasko | 14424ba | 2020-12-09 18:09:51 +0100 | [diff] [blame] | 961 | /* NameTest */ |
| 962 | LY_CHECK_ERR_GOTO(lyxp_check_token(ctx, expr, tok_idx, LYXP_TOKEN_NAMETEST), ret = LY_EVALID, cleanup); |
| 963 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 964 | /* get module and node name */ |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 965 | if (lref) { |
| 966 | LY_CHECK_GOTO(ret = ly_path_compile_prefix_leafref(ctx, cur_node, ctx_node, expr, tok_idx, format, |
| 967 | prefix_data, unres, &mod, &name, &name_len), cleanup); |
| 968 | } else { |
| 969 | LY_CHECK_GOTO(ret = ly_path_compile_prefix(ctx, cur_node, cur_mod, ctx_node, expr, tok_idx, format, |
| 970 | prefix_data, &mod, &name, &name_len), cleanup); |
| 971 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 972 | ++tok_idx; |
| 973 | |
| 974 | /* find the next node */ |
Radek Krejci | d5d3743 | 2021-03-12 13:46:40 +0100 | [diff] [blame] | 975 | if (!ctx_node && ext) { |
| 976 | node2 = lysc_ext_find_node(ext, mod, name, name_len, 0, getnext_opts); |
| 977 | } else { |
| 978 | node2 = lys_find_child(ctx_node, mod, name, name_len, 0, getnext_opts); |
| 979 | } |
Radek Krejci | 25fe3dd | 2020-11-10 22:02:26 +0100 | [diff] [blame] | 980 | if (!node2 || (op && (node2->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node2 != op))) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 981 | LOGVAL(ctx, LYVE_XPATH, "Not found node \"%.*s\" in path.", (int)name_len, name); |
Radek Krejci | 8de005f | 2020-06-25 17:02:07 +0200 | [diff] [blame] | 982 | ret = LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 983 | goto cleanup; |
| 984 | } |
| 985 | ctx_node = node2; |
| 986 | |
| 987 | /* new path segment */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 988 | LY_ARRAY_NEW_GOTO(ctx, *path, p, ret, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 989 | p->node = ctx_node; |
| 990 | |
| 991 | /* compile any predicates */ |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 992 | if (lref) { |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 993 | ret = ly_path_compile_predicate_leafref(ctx_node, cur_node, expr, &tok_idx, format, prefix_data, unres); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 994 | } else { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 995 | ret = ly_path_compile_predicate(ctx, cur_node, cur_mod, ctx_node, expr, &tok_idx, format, prefix_data, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 996 | &p->predicates, &p->pred_type); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 997 | } |
| 998 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 999 | } while (!lyxp_next_token(NULL, expr, &tok_idx, LYXP_TOKEN_OPER_PATH)); |
| 1000 | |
Michal Vasko | 14424ba | 2020-12-09 18:09:51 +0100 | [diff] [blame] | 1001 | /* check leftover tokens */ |
| 1002 | if (tok_idx < expr->used) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1003 | LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(expr->tokens[tok_idx]), &expr->expr[expr->tok_pos[tok_idx]]); |
Michal Vasko | 14424ba | 2020-12-09 18:09:51 +0100 | [diff] [blame] | 1004 | ret = LY_EVALID; |
| 1005 | goto cleanup; |
| 1006 | } |
| 1007 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1008 | /* check last compiled node */ |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 1009 | 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] | 1010 | LOGVAL(ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1011 | lys_nodetype2str(p->node->nodetype), p->node->name); |
Michal Vasko | 14424ba | 2020-12-09 18:09:51 +0100 | [diff] [blame] | 1012 | ret = LY_EVALID; |
| 1013 | goto cleanup; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1014 | } |
| 1015 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1016 | cleanup: |
| 1017 | if (ret) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1018 | ly_path_free(ctx, *path); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1019 | *path = NULL; |
| 1020 | } |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 1021 | LOG_LOCBACK(1, 0, 0, 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1022 | return ret; |
| 1023 | } |
| 1024 | |
| 1025 | LY_ERR |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 1026 | ly_path_compile(const struct ly_ctx *ctx, const struct lys_module *cur_mod, const struct lysc_node *ctx_node, |
| 1027 | const struct lysc_ext_instance *ext, const struct lyxp_expr *expr, uint8_t oper, uint8_t target, |
| 1028 | LY_VALUE_FORMAT format, void *prefix_data, struct ly_path **path) |
| 1029 | { |
| 1030 | return _ly_path_compile(ctx, cur_mod, ctx_node, ext, expr, 0, oper, target, format, prefix_data, NULL, path); |
| 1031 | } |
| 1032 | |
| 1033 | LY_ERR |
| 1034 | ly_path_compile_leafref(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const struct lysc_ext_instance *ext, |
| 1035 | const struct lyxp_expr *expr, uint8_t oper, uint8_t target, LY_VALUE_FORMAT format, void *prefix_data, |
| 1036 | struct lys_glob_unres *unres, struct ly_path **path) |
| 1037 | { |
| 1038 | return _ly_path_compile(ctx, NULL, ctx_node, ext, expr, 1, oper, target, format, prefix_data, unres, path); |
| 1039 | } |
| 1040 | |
| 1041 | LY_ERR |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1042 | ly_path_eval_partial(const struct ly_path *path, const struct lyd_node *start, LY_ARRAY_COUNT_TYPE *path_idx, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1043 | struct lyd_node **match) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1044 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1045 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | acfc928 | 2021-02-04 12:08:23 +0100 | [diff] [blame] | 1046 | struct lyd_node *prev_node = NULL, *node = NULL, *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1047 | uint64_t pos; |
| 1048 | |
| 1049 | assert(path && start); |
| 1050 | |
| 1051 | if (lysc_data_parent(path[0].node)) { |
| 1052 | /* relative path, start from the parent children */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 1053 | start = lyd_child(start); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1054 | } else { |
| 1055 | /* absolute path, start from the first top-level sibling */ |
| 1056 | while (start->parent) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 1057 | start = lyd_parent(start); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1058 | } |
| 1059 | while (start->prev->next) { |
| 1060 | start = start->prev; |
| 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | LY_ARRAY_FOR(path, u) { |
| 1065 | switch (path[u].pred_type) { |
| 1066 | case LY_PATH_PREDTYPE_POSITION: |
| 1067 | /* we cannot use hashes and want an instance on a specific position */ |
| 1068 | pos = 1; |
Michal Vasko | 4c583e8 | 2020-07-17 12:16:14 +0200 | [diff] [blame] | 1069 | LYD_LIST_FOR_INST(start, path[u].node, node) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1070 | if (pos == path[u].predicates[0].position) { |
| 1071 | break; |
| 1072 | } |
| 1073 | ++pos; |
| 1074 | } |
| 1075 | break; |
| 1076 | case LY_PATH_PREDTYPE_LEAFLIST: |
| 1077 | /* we will use hashes to find one leaf-list instance */ |
| 1078 | LY_CHECK_RET(lyd_create_term2(path[u].node, &path[u].predicates[0].value, &target)); |
| 1079 | lyd_find_sibling_first(start, target, &node); |
| 1080 | lyd_free_tree(target); |
| 1081 | break; |
| 1082 | case LY_PATH_PREDTYPE_LIST: |
| 1083 | /* we will use hashes to find one list instance */ |
| 1084 | LY_CHECK_RET(lyd_create_list(path[u].node, path[u].predicates, &target)); |
| 1085 | lyd_find_sibling_first(start, target, &node); |
| 1086 | lyd_free_tree(target); |
| 1087 | break; |
| 1088 | case LY_PATH_PREDTYPE_NONE: |
| 1089 | /* we will use hashes to find one any/container/leaf instance */ |
| 1090 | lyd_find_sibling_val(start, path[u].node, NULL, 0, &node); |
| 1091 | break; |
| 1092 | } |
| 1093 | |
| 1094 | if (!node) { |
| 1095 | /* no matching nodes */ |
| 1096 | break; |
| 1097 | } |
| 1098 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1099 | /* rememeber previous node */ |
| 1100 | prev_node = node; |
| 1101 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1102 | /* next path segment, if any */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 1103 | start = lyd_child(node); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1104 | } |
| 1105 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1106 | if (node) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1107 | /* we have found the full path */ |
| 1108 | if (path_idx) { |
| 1109 | *path_idx = u; |
| 1110 | } |
| 1111 | if (match) { |
| 1112 | *match = node; |
| 1113 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1114 | return LY_SUCCESS; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 1115 | |
| 1116 | } else if (prev_node) { |
| 1117 | /* we have found only some partial match */ |
| 1118 | if (path_idx) { |
| 1119 | *path_idx = u - 1; |
| 1120 | } |
| 1121 | if (match) { |
| 1122 | *match = prev_node; |
| 1123 | } |
| 1124 | return LY_EINCOMPLETE; |
| 1125 | } |
| 1126 | |
| 1127 | /* we have not found any nodes */ |
| 1128 | if (path_idx) { |
| 1129 | *path_idx = 0; |
| 1130 | } |
| 1131 | if (match) { |
| 1132 | *match = NULL; |
| 1133 | } |
| 1134 | return LY_ENOTFOUND; |
| 1135 | } |
| 1136 | |
| 1137 | LY_ERR |
| 1138 | ly_path_eval(const struct ly_path *path, const struct lyd_node *start, struct lyd_node **match) |
| 1139 | { |
| 1140 | LY_ERR ret; |
| 1141 | struct lyd_node *m; |
| 1142 | |
| 1143 | ret = ly_path_eval_partial(path, start, NULL, &m); |
| 1144 | |
| 1145 | if (ret == LY_SUCCESS) { |
| 1146 | /* last node was found */ |
| 1147 | if (match) { |
| 1148 | *match = m; |
| 1149 | } |
| 1150 | return LY_SUCCESS; |
| 1151 | } |
| 1152 | |
| 1153 | /* not a full match */ |
| 1154 | if (match) { |
| 1155 | *match = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1156 | } |
| 1157 | return LY_ENOTFOUND; |
| 1158 | } |
| 1159 | |
| 1160 | LY_ERR |
| 1161 | ly_path_dup(const struct ly_ctx *ctx, const struct ly_path *path, struct ly_path **dup) |
| 1162 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1163 | LY_ARRAY_COUNT_TYPE u, v; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1164 | |
| 1165 | if (!path) { |
| 1166 | return LY_SUCCESS; |
| 1167 | } |
| 1168 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1169 | LY_ARRAY_CREATE_RET(ctx, *dup, LY_ARRAY_COUNT(path), LY_EMEM); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1170 | LY_ARRAY_FOR(path, u) { |
| 1171 | LY_ARRAY_INCREMENT(*dup); |
| 1172 | (*dup)[u].node = path[u].node; |
| 1173 | if (path[u].predicates) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1174 | LY_ARRAY_CREATE_RET(ctx, (*dup)[u].predicates, LY_ARRAY_COUNT(path[u].predicates), LY_EMEM); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1175 | (*dup)[u].pred_type = path[u].pred_type; |
| 1176 | LY_ARRAY_FOR(path[u].predicates, v) { |
| 1177 | struct ly_path_predicate *pred = &path[u].predicates[v]; |
| 1178 | |
| 1179 | LY_ARRAY_INCREMENT((*dup)[u].predicates); |
| 1180 | switch (path[u].pred_type) { |
| 1181 | case LY_PATH_PREDTYPE_POSITION: |
| 1182 | /* position-predicate */ |
| 1183 | (*dup)[u].predicates[v].position = pred->position; |
| 1184 | break; |
| 1185 | case LY_PATH_PREDTYPE_LIST: |
| 1186 | case LY_PATH_PREDTYPE_LEAFLIST: |
| 1187 | /* key-predicate or leaf-list-predicate */ |
| 1188 | (*dup)[u].predicates[v].key = pred->key; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1189 | pred->value.realtype->plugin->duplicate(ctx, &pred->value, &(*dup)[u].predicates[v].value); |
Michal Vasko | ae87566 | 2020-10-21 10:33:17 +0200 | [diff] [blame] | 1190 | ++((struct lysc_type *)pred->value.realtype)->refcount; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1191 | break; |
| 1192 | case LY_PATH_PREDTYPE_NONE: |
| 1193 | break; |
| 1194 | } |
| 1195 | } |
| 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | return LY_SUCCESS; |
| 1200 | } |
| 1201 | |
| 1202 | void |
Michal Vasko | f7e16e2 | 2020-10-21 09:24:39 +0200 | [diff] [blame] | 1203 | ly_path_predicates_free(const struct ly_ctx *ctx, enum ly_path_pred_type pred_type, struct ly_path_predicate *predicates) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1204 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1205 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1206 | |
| 1207 | if (!predicates) { |
| 1208 | return; |
| 1209 | } |
| 1210 | |
| 1211 | LY_ARRAY_FOR(predicates, u) { |
| 1212 | switch (pred_type) { |
| 1213 | case LY_PATH_PREDTYPE_POSITION: |
| 1214 | case LY_PATH_PREDTYPE_NONE: |
| 1215 | /* nothing to free */ |
| 1216 | break; |
| 1217 | case LY_PATH_PREDTYPE_LIST: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1218 | case LY_PATH_PREDTYPE_LEAFLIST: |
Michal Vasko | ae87566 | 2020-10-21 10:33:17 +0200 | [diff] [blame] | 1219 | if (predicates[u].value.realtype) { |
| 1220 | predicates[u].value.realtype->plugin->free(ctx, &predicates[u].value); |
| 1221 | lysc_type_free((struct ly_ctx *)ctx, (struct lysc_type *)predicates[u].value.realtype); |
| 1222 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1223 | break; |
| 1224 | } |
| 1225 | } |
| 1226 | LY_ARRAY_FREE(predicates); |
| 1227 | } |
| 1228 | |
| 1229 | void |
| 1230 | ly_path_free(const struct ly_ctx *ctx, struct ly_path *path) |
| 1231 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1232 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1233 | |
Michal Vasko | 55b8481 | 2021-05-11 09:23:58 +0200 | [diff] [blame] | 1234 | if (!path) { |
| 1235 | return; |
| 1236 | } |
| 1237 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1238 | LY_ARRAY_FOR(path, u) { |
Michal Vasko | f7e16e2 | 2020-10-21 09:24:39 +0200 | [diff] [blame] | 1239 | ly_path_predicates_free(ctx, path[u].pred_type, path[u].predicates); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1240 | } |
| 1241 | LY_ARRAY_FREE(path); |
| 1242 | } |