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, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 235 | uint8_t begin, uint8_t lref, 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)); |
| 243 | assert((lref == LY_PATH_LREF_TRUE) || (lref == LY_PATH_LREF_FALSE)); |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 244 | assert((prefix == LY_PATH_PREFIX_OPTIONAL) || (prefix == LY_PATH_PREFIX_MANDATORY) || |
| 245 | (prefix == LY_PATH_PREFIX_STRICT_INHERIT)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 246 | assert((pred == LY_PATH_PRED_KEYS) || (pred == LY_PATH_PRED_SIMPLE) || (pred == LY_PATH_PRED_LEAFREF)); |
| 247 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 248 | LOG_LOCSET(ctx_node, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 249 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 250 | /* parse as a generic XPath expression */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 251 | 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] | 252 | tok_idx = 0; |
| 253 | |
| 254 | if (begin == LY_PATH_BEGIN_EITHER) { |
| 255 | /* is the path relative? */ |
| 256 | if (lyxp_next_token(NULL, exp, &tok_idx, LYXP_TOKEN_OPER_PATH)) { |
Michal Vasko | cb8c6d4 | 2020-10-16 11:58:30 +0200 | [diff] [blame] | 257 | /* relative path check specific to leafref */ |
| 258 | if (lref == LY_PATH_LREF_TRUE) { |
| 259 | /* mandatory '..' */ |
| 260 | LY_CHECK_ERR_GOTO(lyxp_next_token(ctx, exp, &tok_idx, LYXP_TOKEN_DDOT), ret = LY_EVALID, error); |
| 261 | |
| 262 | do { |
| 263 | /* '/' */ |
| 264 | LY_CHECK_ERR_GOTO(lyxp_next_token(ctx, exp, &tok_idx, LYXP_TOKEN_OPER_PATH), ret = LY_EVALID, error); |
| 265 | |
| 266 | /* optional '..' */ |
| 267 | } while (!lyxp_next_token(NULL, exp, &tok_idx, LYXP_TOKEN_DDOT)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | } else { |
| 271 | /* '/' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 272 | 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] | 273 | } |
| 274 | |
| 275 | do { |
| 276 | /* NameTest */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 277 | 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] | 278 | |
| 279 | /* check prefix based on the options */ |
Michal Vasko | 8b06a5e | 2020-08-06 12:13:08 +0200 | [diff] [blame] | 280 | cur_node = exp->expr + exp->tok_pos[tok_idx]; |
| 281 | cur_len = exp->tok_len[tok_idx]; |
| 282 | if (prefix == LY_PATH_PREFIX_MANDATORY) { |
| 283 | if (!strnstr(cur_node, ":", cur_len)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 284 | 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] | 285 | ret = LY_EVALID; |
Michal Vasko | 8b06a5e | 2020-08-06 12:13:08 +0200 | [diff] [blame] | 286 | goto error; |
| 287 | } |
| 288 | } else if (prefix == LY_PATH_PREFIX_STRICT_INHERIT) { |
| 289 | if (!prev_prefix) { |
| 290 | /* the first node must have a prefix */ |
| 291 | if (!strnstr(cur_node, ":", cur_len)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 292 | 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] | 293 | ret = LY_EVALID; |
Michal Vasko | 8b06a5e | 2020-08-06 12:13:08 +0200 | [diff] [blame] | 294 | goto error; |
| 295 | } |
| 296 | |
| 297 | /* remember the first prefix */ |
| 298 | prev_prefix = cur_node; |
| 299 | } else { |
| 300 | /* the prefix must be different, if any */ |
| 301 | ptr = strnstr(cur_node, ":", cur_len); |
| 302 | if (ptr) { |
| 303 | 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] | 304 | 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] | 305 | ret = LY_EVALID; |
Michal Vasko | 8b06a5e | 2020-08-06 12:13:08 +0200 | [diff] [blame] | 306 | goto error; |
| 307 | } |
| 308 | |
| 309 | /* remember this next prefix */ |
| 310 | prev_prefix = cur_node; |
| 311 | } |
| 312 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | ++tok_idx; |
| 316 | |
| 317 | /* Predicate* */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 318 | 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] | 319 | |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 320 | /* '/' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 321 | } while (!lyxp_next_token(NULL, exp, &tok_idx, LYXP_TOKEN_OPER_PATH)); |
| 322 | |
| 323 | /* trailing token check */ |
| 324 | if (exp->used > tok_idx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 325 | 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] | 326 | ret = LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 327 | goto error; |
| 328 | } |
| 329 | |
| 330 | *expr = exp; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 331 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 332 | LOG_LOCBACK(ctx_node ? 1 : 0, 0, 0, 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 333 | return LY_SUCCESS; |
| 334 | |
| 335 | error: |
| 336 | lyxp_expr_free(ctx, exp); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 337 | LOG_LOCBACK(ctx_node ? 1 : 0, 0, 0, 0); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 338 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | LY_ERR |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 342 | 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] | 343 | 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] | 344 | { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 345 | LY_ERR ret = LY_SUCCESS; |
| 346 | struct lyxp_expr *exp = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 347 | uint16_t tok_idx; |
| 348 | |
| 349 | assert((prefix == LY_PATH_PREFIX_OPTIONAL) || (prefix == LY_PATH_PREFIX_MANDATORY)); |
| 350 | assert((pred == LY_PATH_PRED_KEYS) || (pred == LY_PATH_PRED_SIMPLE) || (pred == LY_PATH_PRED_LEAFREF)); |
| 351 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 352 | LOG_LOCSET(cur_node, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 353 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 354 | /* parse as a generic XPath expression */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 355 | 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] | 356 | tok_idx = 0; |
| 357 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 358 | 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] | 359 | |
| 360 | /* trailing token check */ |
| 361 | if (exp->used > tok_idx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 362 | 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] | 363 | exp->expr + exp->tok_pos[tok_idx]); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 364 | ret = LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 365 | goto error; |
| 366 | } |
| 367 | |
| 368 | *expr = exp; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 369 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 370 | LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 371 | return LY_SUCCESS; |
| 372 | |
| 373 | error: |
| 374 | lyxp_expr_free(ctx, exp); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 375 | LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 376 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /** |
| 380 | * @brief Parse prefix from a NameTest, if any, and node name, and return expected module of the node. |
| 381 | * |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 382 | * @param[in] ctx libyang context. |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 383 | * @param[in] cur_node Optional current (original context) node. |
Radek Krejci | 09c7744 | 2021-04-26 11:10:34 +0200 | [diff] [blame] | 384 | * @param[in] cur_mod Current module of the path (where the path is "instantiated"). Needed for ::LY_VALUE_SCHEMA*. |
| 385 | * @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] | 386 | * @param[in] expr Parsed path. |
| 387 | * @param[in] tok_idx Index in @p expr. |
| 388 | * @param[in] lref Lref option. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 389 | * @param[in] format Format of the path. |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 390 | * @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] | 391 | * @param[out] mod Resolved module. |
| 392 | * @param[out] name Parsed node name. |
| 393 | * @param[out] name_len Length of @p name. |
| 394 | * @return LY_ERR value. |
| 395 | */ |
| 396 | static LY_ERR |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 397 | ly_path_compile_prefix(const struct ly_ctx *ctx, const struct lysc_node *cur_node, const struct lys_module *cur_mod, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 398 | const struct lysc_node *prev_ctx_node, const struct lyxp_expr *expr, uint16_t tok_idx, uint8_t lref, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 399 | LY_VALUE_FORMAT format, void *prefix_data, struct lys_glob_unres *unres, const struct lys_module **mod, |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 400 | const char **name, size_t *name_len) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 401 | { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 402 | LY_ERR ret; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 403 | const char *pref; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 404 | size_t len; |
| 405 | |
| 406 | assert(expr->tokens[tok_idx] == LYXP_TOKEN_NAMETEST); |
| 407 | |
| 408 | /* get prefix */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 409 | if ((pref = strnstr(expr->expr + expr->tok_pos[tok_idx], ":", expr->tok_len[tok_idx]))) { |
| 410 | len = pref - (expr->expr + expr->tok_pos[tok_idx]); |
| 411 | pref = expr->expr + expr->tok_pos[tok_idx]; |
| 412 | } else { |
| 413 | len = 0; |
| 414 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 415 | |
| 416 | /* find next node module */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 417 | if (pref) { |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 418 | LOG_LOCSET(cur_node, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 419 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 420 | *mod = ly_resolve_prefix(ctx, pref, len, format, prefix_data); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 421 | if (!*mod) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 422 | 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] | 423 | (int)len, pref, ly_format2str(format)); |
Michal Vasko | 825a044 | 2021-04-16 16:11:53 +0200 | [diff] [blame] | 424 | ret = LY_EVALID; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 425 | goto error; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 426 | } else if (!(*mod)->implemented) { |
| 427 | if (lref == LY_PATH_LREF_FALSE) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 428 | LOGVAL(ctx, LYVE_XPATH, "Not implemented module \"%s\" in path.", (*mod)->name); |
Michal Vasko | 825a044 | 2021-04-16 16:11:53 +0200 | [diff] [blame] | 429 | ret = LY_EVALID; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 430 | goto error; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 431 | } |
Michal Vasko | 825a044 | 2021-04-16 16:11:53 +0200 | [diff] [blame] | 432 | |
Michal Vasko | b3e2253 | 2021-02-03 10:50:04 +0100 | [diff] [blame] | 433 | assert(unres); |
Michal Vasko | 6982750 | 2021-04-22 09:15:23 +0200 | [diff] [blame] | 434 | LY_CHECK_GOTO(ret = lys_set_implemented_r((struct lys_module *)*mod, NULL, unres), error); |
Michal Vasko | 40c158c | 2021-04-28 17:01:03 +0200 | [diff] [blame^] | 435 | if (unres->recompile) { |
| 436 | return LY_ERECOMPILE; |
| 437 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 438 | } |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 439 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 440 | LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 441 | } else { |
| 442 | switch (format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 443 | case LY_VALUE_SCHEMA: |
| 444 | case LY_VALUE_SCHEMA_RESOLVED: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 445 | if (!cur_mod) { |
| 446 | LOGINT_RET(ctx); |
| 447 | } |
| 448 | /* use current module */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 449 | *mod = cur_mod; |
| 450 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 451 | case LY_VALUE_JSON: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 452 | if (!prev_ctx_node) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 453 | LOGINT_RET(ctx); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 454 | } |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 455 | /* inherit module of the previous node */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 456 | *mod = prev_ctx_node->module; |
| 457 | break; |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 458 | case LY_VALUE_CANON: |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 459 | case LY_VALUE_XML: |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 460 | case LY_VALUE_LYB: |
| 461 | /* not really defined or accepted */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 462 | LOGINT_RET(ctx); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | |
| 466 | /* set name */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 467 | if (pref) { |
| 468 | *name = pref + len + 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 469 | *name_len = expr->tok_len[tok_idx] - len - 1; |
| 470 | } else { |
| 471 | *name = expr->expr + expr->tok_pos[tok_idx]; |
| 472 | *name_len = expr->tok_len[tok_idx]; |
| 473 | } |
| 474 | |
| 475 | return LY_SUCCESS; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 476 | |
| 477 | error: |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 478 | LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0); |
Michal Vasko | 825a044 | 2021-04-16 16:11:53 +0200 | [diff] [blame] | 479 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | LY_ERR |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 483 | 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] | 484 | 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] | 485 | 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] | 486 | { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 487 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 488 | struct ly_path_predicate *p; |
| 489 | const struct lysc_node *key; |
Radek Krejci | 2b18bf1 | 2020-11-06 11:20:20 +0100 | [diff] [blame] | 490 | const struct lys_module *mod = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 491 | const char *name; |
| 492 | size_t name_len, key_count; |
| 493 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 494 | assert(ctx && ctx_node); |
| 495 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 496 | LOG_LOCSET(cur_node, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 497 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 498 | *pred_type = 0; |
| 499 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 500 | if (lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1)) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 501 | /* '[', no predicate */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 502 | goto cleanup; /* LY_SUCCESS */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | if (expr->tokens[*tok_idx] == LYXP_TOKEN_NAMETEST) { |
| 506 | if (ctx_node->nodetype != LYS_LIST) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 507 | LOGVAL(ctx, LYVE_XPATH, "List predicate defined for %s \"%s\" in path.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 508 | lys_nodetype2str(ctx_node->nodetype), ctx_node->name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 509 | ret = LY_EVALID; |
| 510 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 511 | } else if (ctx_node->flags & LYS_KEYLESS) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 512 | 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] | 513 | lys_nodetype2str(ctx_node->nodetype), ctx_node->name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 514 | ret = LY_EVALID; |
| 515 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | do { |
| 519 | /* NameTest, find the key */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 520 | LY_CHECK_RET(ly_path_compile_prefix(ctx, cur_node, cur_mod, ctx_node, expr, *tok_idx, LY_PATH_LREF_FALSE, |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 521 | format, prefix_data, NULL, &mod, &name, &name_len)); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 522 | key = lys_find_child(ctx_node, mod, name, name_len, 0, 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 523 | if (!key) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 524 | 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] | 525 | ret = LY_ENOTFOUND; |
| 526 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 527 | } else if ((key->nodetype != LYS_LEAF) || !(key->flags & LYS_KEY)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 528 | LOGVAL(ctx, LYVE_XPATH, "Key expected instead of %s \"%s\" in path.", lys_nodetype2str(key->nodetype), |
| 529 | key->name); |
| 530 | ret = LY_EVALID; |
| 531 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 532 | } |
| 533 | ++(*tok_idx); |
| 534 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 535 | if (!*pred_type) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 536 | /* new predicate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 537 | *pred_type = LY_PATH_PREDTYPE_LIST; |
| 538 | } |
| 539 | assert(*pred_type == LY_PATH_PREDTYPE_LIST); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 540 | LY_ARRAY_NEW_GOTO(ctx, *predicates, p, ret, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 541 | p->key = key; |
| 542 | |
| 543 | /* '=' */ |
| 544 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL); |
| 545 | ++(*tok_idx); |
| 546 | |
| 547 | /* Literal */ |
| 548 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_LITERAL); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 549 | LOG_LOCSET(key, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 550 | 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] | 551 | 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] | 552 | LYD_HINT_DATA, key, NULL); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 553 | LOG_LOCBACK(key ? 1 : 0, 0, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 554 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 555 | ++(*tok_idx); |
| 556 | |
Michal Vasko | ae87566 | 2020-10-21 10:33:17 +0200 | [diff] [blame] | 557 | /* "allocate" the type to avoid problems when freeing the value after the type was freed */ |
| 558 | ++((struct lysc_type *)p->value.realtype)->refcount; |
| 559 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 560 | /* ']' */ |
| 561 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
| 562 | ++(*tok_idx); |
| 563 | |
| 564 | /* another predicate follows? */ |
| 565 | } while (!lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1)); |
| 566 | |
| 567 | /* check that all keys were set */ |
| 568 | key_count = 0; |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 569 | 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] | 570 | ++key_count; |
| 571 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 572 | if (LY_ARRAY_COUNT(*predicates) != key_count) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 573 | /* names (keys) are unique - it was checked when parsing */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 574 | 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] | 575 | lys_nodetype2str(ctx_node->nodetype), ctx_node->name); |
Michal Vasko | f7e16e2 | 2020-10-21 09:24:39 +0200 | [diff] [blame] | 576 | ly_path_predicates_free(ctx, LY_PATH_PREDTYPE_LIST, *predicates); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 577 | *predicates = NULL; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 578 | ret = LY_EVALID; |
| 579 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | } else if (expr->tokens[*tok_idx] == LYXP_TOKEN_DOT) { |
| 583 | if (ctx_node->nodetype != LYS_LEAFLIST) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 584 | 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] | 585 | lys_nodetype2str(ctx_node->nodetype), ctx_node->name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 586 | ret = LY_EVALID; |
| 587 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 588 | } |
| 589 | ++(*tok_idx); |
| 590 | |
| 591 | /* new predicate */ |
| 592 | *pred_type = LY_PATH_PREDTYPE_LEAFLIST; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 593 | LY_ARRAY_NEW_GOTO(ctx, *predicates, p, ret, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 594 | |
| 595 | /* '=' */ |
| 596 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL); |
| 597 | ++(*tok_idx); |
| 598 | |
| 599 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_LITERAL); |
| 600 | /* store the value */ |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 601 | LOG_LOCSET(ctx_node, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 602 | 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] | 603 | 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] | 604 | LYD_HINT_DATA, ctx_node, NULL); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 605 | LOG_LOCBACK(ctx_node ? 1 : 0, 0, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 606 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 607 | ++(*tok_idx); |
| 608 | |
Michal Vasko | ae87566 | 2020-10-21 10:33:17 +0200 | [diff] [blame] | 609 | /* "allocate" the type to avoid problems when freeing the value after the type was freed */ |
| 610 | ++((struct lysc_type *)p->value.realtype)->refcount; |
| 611 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 612 | /* ']' */ |
| 613 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
| 614 | ++(*tok_idx); |
| 615 | } else { |
| 616 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_NUMBER); |
| 617 | if (!(ctx_node->nodetype & (LYS_LEAFLIST | LYS_LIST))) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 618 | ret = LY_EVALID; |
| 619 | LOGVAL(ctx, LYVE_XPATH, "Positional predicate defined for %s \"%s\" in path.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 620 | lys_nodetype2str(ctx_node->nodetype), ctx_node->name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 621 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 622 | } else if (ctx_node->flags & LYS_CONFIG_W) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 623 | ret = LY_EVALID; |
| 624 | 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] | 625 | lys_nodetype2str(ctx_node->nodetype), ctx_node->name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 626 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 627 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 628 | |
| 629 | /* new predicate */ |
| 630 | *pred_type = LY_PATH_PREDTYPE_POSITION; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 631 | LY_ARRAY_NEW_GOTO(ctx, *predicates, p, ret, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 632 | |
| 633 | /* syntax was already checked */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 634 | 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] | 635 | ++(*tok_idx); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 636 | |
| 637 | /* ']' */ |
| 638 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
| 639 | ++(*tok_idx); |
| 640 | } |
| 641 | |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 642 | cleanup: |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 643 | LOG_LOCBACK(cur_node ? 1 : 0, 0, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 644 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | /** |
| 648 | * @brief Compile leafref predicate. Actually, it is only checked. |
| 649 | * |
| 650 | * @param[in] ctx_node Context node, node for which the predicate is defined. |
| 651 | * @param[in] cur_node Current (original context) node. |
| 652 | * @param[in] expr Parsed path. |
| 653 | * @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] | 654 | * @param[in] format Format of the path. |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 655 | * @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] | 656 | * @param[in,out] unres Global unres structure for newly implemented modules. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 657 | * @return LY_ERR value. |
| 658 | */ |
| 659 | static LY_ERR |
| 660 | 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] | 661 | 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] | 662 | struct lys_glob_unres *unres) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 663 | { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 664 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 665 | const struct lysc_node *key, *node, *node2; |
| 666 | const struct lys_module *mod; |
| 667 | const char *name; |
| 668 | size_t name_len; |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 669 | struct ly_ctx *ctx = cur_node->module->ctx; |
| 670 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 671 | LOG_LOCSET(cur_node, NULL, NULL, NULL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 672 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 673 | if (lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1)) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 674 | /* '[', no predicate */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 675 | goto cleanup; /* LY_SUCCESS */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | if (ctx_node->nodetype != LYS_LIST) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 679 | LOGVAL(ctx, LYVE_XPATH, "List predicate defined for %s \"%s\" in path.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 680 | lys_nodetype2str(ctx_node->nodetype), ctx_node->name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 681 | ret = LY_EVALID; |
| 682 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 683 | } else if (ctx_node->flags & LYS_KEYLESS) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 684 | 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] | 685 | lys_nodetype2str(ctx_node->nodetype), ctx_node->name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 686 | ret = LY_EVALID; |
| 687 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | do { |
| 691 | /* NameTest, find the key */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 692 | ret = ly_path_compile_prefix(ctx, cur_node, cur_node->module, ctx_node, expr, *tok_idx, |
| 693 | LY_PATH_LREF_TRUE, format, prefix_data, unres, &mod, &name, &name_len); |
| 694 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 695 | key = lys_find_child(ctx_node, mod, name, name_len, 0, 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 696 | if (!key) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 697 | 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] | 698 | ret = LY_EVALID; |
| 699 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 700 | } else if ((key->nodetype != LYS_LEAF) || !(key->flags & LYS_KEY)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 701 | LOGVAL(ctx, LYVE_XPATH, "Key expected instead of %s \"%s\" in path.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 702 | lys_nodetype2str(key->nodetype), key->name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 703 | ret = LY_EVALID; |
| 704 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 705 | } |
| 706 | ++(*tok_idx); |
| 707 | |
| 708 | /* we are not actually compiling, throw the key away */ |
| 709 | (void)key; |
| 710 | |
| 711 | /* '=' */ |
| 712 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL); |
| 713 | ++(*tok_idx); |
| 714 | |
| 715 | /* FuncName */ |
| 716 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_FUNCNAME); |
| 717 | ++(*tok_idx); |
| 718 | |
| 719 | /* evaluating from the "current()" node */ |
| 720 | node = cur_node; |
| 721 | |
| 722 | /* '(' */ |
| 723 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
| 724 | ++(*tok_idx); |
| 725 | |
| 726 | /* ')' */ |
| 727 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
| 728 | ++(*tok_idx); |
| 729 | |
| 730 | do { |
| 731 | /* '/' */ |
| 732 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH); |
| 733 | ++(*tok_idx); |
| 734 | |
| 735 | /* go to parent */ |
| 736 | if (!node) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 737 | LOGVAL(ctx, LYVE_XPATH, "Too many parent references in path."); |
| 738 | ret = LY_EVALID; |
| 739 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 740 | } |
| 741 | node = lysc_data_parent(node); |
| 742 | |
| 743 | /* '..' */ |
| 744 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_DDOT); |
| 745 | ++(*tok_idx); |
| 746 | } while (expr->tokens[*tok_idx + 1] == LYXP_TOKEN_DDOT); |
| 747 | |
| 748 | do { |
| 749 | /* '/' */ |
| 750 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH); |
| 751 | ++(*tok_idx); |
| 752 | |
| 753 | /* NameTest */ |
| 754 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_NAMETEST); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 755 | LY_CHECK_RET(ly_path_compile_prefix(ctx, cur_node, cur_node->module, node, expr, *tok_idx, |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 756 | LY_PATH_LREF_TRUE, format, prefix_data, unres, &mod, &name, &name_len)); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 757 | node2 = lys_find_child(node, mod, name, name_len, 0, 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 758 | if (!node2) { |
Radek Krejci | 422afb1 | 2021-03-04 16:38:16 +0100 | [diff] [blame] | 759 | 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] | 760 | ret = LY_EVALID; |
| 761 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 762 | } |
| 763 | node = node2; |
| 764 | ++(*tok_idx); |
| 765 | } while ((*tok_idx + 1 < expr->used) && (expr->tokens[*tok_idx + 1] == LYXP_TOKEN_NAMETEST)); |
| 766 | |
| 767 | /* check the last target node */ |
| 768 | if (node->nodetype != LYS_LEAF) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 769 | 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] | 770 | lys_nodetype2str(node->nodetype), node->name); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 771 | ret = LY_EVALID; |
| 772 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | /* we are not actually compiling, throw the rightside node away */ |
| 776 | (void)node; |
| 777 | |
| 778 | /* ']' */ |
| 779 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
| 780 | ++(*tok_idx); |
| 781 | |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 782 | /* another predicate follows? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 783 | } while (!lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1)); |
| 784 | |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 785 | cleanup: |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 786 | LOG_LOCBACK(1, 0, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 787 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | LY_ERR |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 791 | ly_path_compile(const struct ly_ctx *ctx, const struct lys_module *cur_mod, const struct lysc_node *ctx_node, |
Radek Krejci | d5d3743 | 2021-03-12 13:46:40 +0100 | [diff] [blame] | 792 | const struct lysc_ext_instance *ext, const struct lyxp_expr *expr, uint8_t lref, uint8_t oper, uint8_t target, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 793 | 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] | 794 | { |
| 795 | LY_ERR ret = LY_SUCCESS; |
| 796 | uint16_t tok_idx = 0; |
Radek Krejci | 2b18bf1 | 2020-11-06 11:20:20 +0100 | [diff] [blame] | 797 | const struct lys_module *mod = NULL; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 798 | const struct lysc_node *node2, *cur_node, *op; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 799 | struct ly_path *p = NULL; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 800 | uint32_t getnext_opts; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 801 | const char *name; |
| 802 | size_t name_len; |
| 803 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 804 | assert(ctx); |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 805 | assert((lref == LY_PATH_LREF_FALSE) || ctx_node); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 806 | assert((lref == LY_PATH_LREF_TRUE) || (lref == LY_PATH_LREF_FALSE)); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 807 | assert((oper == LY_PATH_OPER_INPUT) || (oper == LY_PATH_OPER_OUTPUT)); |
| 808 | assert((target == LY_PATH_TARGET_SINGLE) || (target == LY_PATH_TARGET_MANY)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 809 | |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 810 | /* find operation, if we are in any */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 811 | 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] | 812 | |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 813 | *path = NULL; |
| 814 | |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 815 | /* remember original context node */ |
| 816 | cur_node = ctx_node; |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 817 | LOG_LOCINIT(cur_node, NULL, NULL, NULL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 818 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 819 | if (oper == LY_PATH_OPER_OUTPUT) { |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 820 | getnext_opts = LYS_GETNEXT_OUTPUT; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 821 | } else { |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 822 | getnext_opts = 0; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 823 | } |
| 824 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 825 | if (expr->tokens[tok_idx] == LYXP_TOKEN_OPER_PATH) { |
| 826 | /* absolute path */ |
| 827 | ctx_node = NULL; |
| 828 | |
| 829 | ++tok_idx; |
| 830 | } else { |
| 831 | /* relative path */ |
| 832 | while ((lref == LY_PATH_LREF_TRUE) && (expr->tokens[tok_idx] == LYXP_TOKEN_DDOT)) { |
| 833 | if (!ctx_node) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 834 | LOGVAL(ctx, LYVE_XPATH, "Too many parent references in path."); |
Michal Vasko | 14424ba | 2020-12-09 18:09:51 +0100 | [diff] [blame] | 835 | ret = LY_EVALID; |
| 836 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | /* get parent */ |
| 840 | ctx_node = lysc_data_parent(ctx_node); |
| 841 | |
| 842 | ++tok_idx; |
| 843 | |
| 844 | assert(expr->tokens[tok_idx] == LYXP_TOKEN_OPER_PATH); |
| 845 | ++tok_idx; |
| 846 | } |
| 847 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 848 | /* we are not storing the parent */ |
| 849 | (void)ctx_node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | do { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 853 | /* check last compiled inner node, whether it is uniquely identified (even key-less list) */ |
Michal Vasko | d456cf6 | 2020-11-23 16:48:43 +0100 | [diff] [blame] | 854 | if (p && (lref == LY_PATH_LREF_FALSE) && (target == LY_PATH_TARGET_SINGLE) && |
| 855 | (p->node->nodetype == LYS_LIST) && !p->predicates) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 856 | LOGVAL(ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 857 | lys_nodetype2str(p->node->nodetype), p->node->name); |
Michal Vasko | 14424ba | 2020-12-09 18:09:51 +0100 | [diff] [blame] | 858 | ret = LY_EVALID; |
| 859 | goto cleanup; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 860 | } |
| 861 | |
Michal Vasko | 14424ba | 2020-12-09 18:09:51 +0100 | [diff] [blame] | 862 | /* NameTest */ |
| 863 | LY_CHECK_ERR_GOTO(lyxp_check_token(ctx, expr, tok_idx, LYXP_TOKEN_NAMETEST), ret = LY_EVALID, cleanup); |
| 864 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 865 | /* get module and node name */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 866 | LY_CHECK_GOTO(ret = ly_path_compile_prefix(ctx, cur_node, cur_mod, ctx_node, expr, tok_idx, lref, format, |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 867 | prefix_data, unres, &mod, &name, &name_len), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 868 | ++tok_idx; |
| 869 | |
| 870 | /* find the next node */ |
Radek Krejci | d5d3743 | 2021-03-12 13:46:40 +0100 | [diff] [blame] | 871 | if (!ctx_node && ext) { |
| 872 | node2 = lysc_ext_find_node(ext, mod, name, name_len, 0, getnext_opts); |
| 873 | } else { |
| 874 | node2 = lys_find_child(ctx_node, mod, name, name_len, 0, getnext_opts); |
| 875 | } |
Radek Krejci | 25fe3dd | 2020-11-10 22:02:26 +0100 | [diff] [blame] | 876 | 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] | 877 | 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] | 878 | ret = LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 879 | goto cleanup; |
| 880 | } |
| 881 | ctx_node = node2; |
| 882 | |
| 883 | /* new path segment */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 884 | LY_ARRAY_NEW_GOTO(ctx, *path, p, ret, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 885 | p->node = ctx_node; |
| 886 | |
| 887 | /* compile any predicates */ |
| 888 | if (lref == LY_PATH_LREF_TRUE) { |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 889 | 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] | 890 | } else { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 891 | 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] | 892 | &p->predicates, &p->pred_type); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 893 | } |
| 894 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 895 | } while (!lyxp_next_token(NULL, expr, &tok_idx, LYXP_TOKEN_OPER_PATH)); |
| 896 | |
Michal Vasko | 14424ba | 2020-12-09 18:09:51 +0100 | [diff] [blame] | 897 | /* check leftover tokens */ |
| 898 | if (tok_idx < expr->used) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 899 | 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] | 900 | ret = LY_EVALID; |
| 901 | goto cleanup; |
| 902 | } |
| 903 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 904 | /* check last compiled node */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 905 | if ((lref == LY_PATH_LREF_FALSE) && (target == LY_PATH_TARGET_SINGLE) && |
| 906 | (p->node->nodetype & (LYS_LIST | LYS_LEAFLIST)) && !p->predicates) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 907 | LOGVAL(ctx, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 908 | lys_nodetype2str(p->node->nodetype), p->node->name); |
Michal Vasko | 14424ba | 2020-12-09 18:09:51 +0100 | [diff] [blame] | 909 | ret = LY_EVALID; |
| 910 | goto cleanup; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 911 | } |
| 912 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 913 | cleanup: |
| 914 | if (ret) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 915 | ly_path_free(ctx, *path); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 916 | *path = NULL; |
| 917 | } |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 918 | LOG_LOCBACK(1, 0, 0, 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 919 | return ret; |
| 920 | } |
| 921 | |
| 922 | LY_ERR |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 923 | 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] | 924 | struct lyd_node **match) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 925 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 926 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | acfc928 | 2021-02-04 12:08:23 +0100 | [diff] [blame] | 927 | struct lyd_node *prev_node = NULL, *node = NULL, *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 928 | uint64_t pos; |
| 929 | |
| 930 | assert(path && start); |
| 931 | |
| 932 | if (lysc_data_parent(path[0].node)) { |
| 933 | /* relative path, start from the parent children */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 934 | start = lyd_child(start); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 935 | } else { |
| 936 | /* absolute path, start from the first top-level sibling */ |
| 937 | while (start->parent) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 938 | start = lyd_parent(start); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 939 | } |
| 940 | while (start->prev->next) { |
| 941 | start = start->prev; |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | LY_ARRAY_FOR(path, u) { |
| 946 | switch (path[u].pred_type) { |
| 947 | case LY_PATH_PREDTYPE_POSITION: |
| 948 | /* we cannot use hashes and want an instance on a specific position */ |
| 949 | pos = 1; |
Michal Vasko | 4c583e8 | 2020-07-17 12:16:14 +0200 | [diff] [blame] | 950 | LYD_LIST_FOR_INST(start, path[u].node, node) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 951 | if (pos == path[u].predicates[0].position) { |
| 952 | break; |
| 953 | } |
| 954 | ++pos; |
| 955 | } |
| 956 | break; |
| 957 | case LY_PATH_PREDTYPE_LEAFLIST: |
| 958 | /* we will use hashes to find one leaf-list instance */ |
| 959 | LY_CHECK_RET(lyd_create_term2(path[u].node, &path[u].predicates[0].value, &target)); |
| 960 | lyd_find_sibling_first(start, target, &node); |
| 961 | lyd_free_tree(target); |
| 962 | break; |
| 963 | case LY_PATH_PREDTYPE_LIST: |
| 964 | /* we will use hashes to find one list instance */ |
| 965 | LY_CHECK_RET(lyd_create_list(path[u].node, path[u].predicates, &target)); |
| 966 | lyd_find_sibling_first(start, target, &node); |
| 967 | lyd_free_tree(target); |
| 968 | break; |
| 969 | case LY_PATH_PREDTYPE_NONE: |
| 970 | /* we will use hashes to find one any/container/leaf instance */ |
| 971 | lyd_find_sibling_val(start, path[u].node, NULL, 0, &node); |
| 972 | break; |
| 973 | } |
| 974 | |
| 975 | if (!node) { |
| 976 | /* no matching nodes */ |
| 977 | break; |
| 978 | } |
| 979 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 980 | /* rememeber previous node */ |
| 981 | prev_node = node; |
| 982 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 983 | /* next path segment, if any */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 984 | start = lyd_child(node); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 985 | } |
| 986 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 987 | if (node) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 988 | /* we have found the full path */ |
| 989 | if (path_idx) { |
| 990 | *path_idx = u; |
| 991 | } |
| 992 | if (match) { |
| 993 | *match = node; |
| 994 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 995 | return LY_SUCCESS; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 996 | |
| 997 | } else if (prev_node) { |
| 998 | /* we have found only some partial match */ |
| 999 | if (path_idx) { |
| 1000 | *path_idx = u - 1; |
| 1001 | } |
| 1002 | if (match) { |
| 1003 | *match = prev_node; |
| 1004 | } |
| 1005 | return LY_EINCOMPLETE; |
| 1006 | } |
| 1007 | |
| 1008 | /* we have not found any nodes */ |
| 1009 | if (path_idx) { |
| 1010 | *path_idx = 0; |
| 1011 | } |
| 1012 | if (match) { |
| 1013 | *match = NULL; |
| 1014 | } |
| 1015 | return LY_ENOTFOUND; |
| 1016 | } |
| 1017 | |
| 1018 | LY_ERR |
| 1019 | ly_path_eval(const struct ly_path *path, const struct lyd_node *start, struct lyd_node **match) |
| 1020 | { |
| 1021 | LY_ERR ret; |
| 1022 | struct lyd_node *m; |
| 1023 | |
| 1024 | ret = ly_path_eval_partial(path, start, NULL, &m); |
| 1025 | |
| 1026 | if (ret == LY_SUCCESS) { |
| 1027 | /* last node was found */ |
| 1028 | if (match) { |
| 1029 | *match = m; |
| 1030 | } |
| 1031 | return LY_SUCCESS; |
| 1032 | } |
| 1033 | |
| 1034 | /* not a full match */ |
| 1035 | if (match) { |
| 1036 | *match = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1037 | } |
| 1038 | return LY_ENOTFOUND; |
| 1039 | } |
| 1040 | |
| 1041 | LY_ERR |
| 1042 | ly_path_dup(const struct ly_ctx *ctx, const struct ly_path *path, struct ly_path **dup) |
| 1043 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1044 | LY_ARRAY_COUNT_TYPE u, v; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1045 | |
| 1046 | if (!path) { |
| 1047 | return LY_SUCCESS; |
| 1048 | } |
| 1049 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1050 | LY_ARRAY_CREATE_RET(ctx, *dup, LY_ARRAY_COUNT(path), LY_EMEM); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1051 | LY_ARRAY_FOR(path, u) { |
| 1052 | LY_ARRAY_INCREMENT(*dup); |
| 1053 | (*dup)[u].node = path[u].node; |
| 1054 | if (path[u].predicates) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1055 | 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] | 1056 | (*dup)[u].pred_type = path[u].pred_type; |
| 1057 | LY_ARRAY_FOR(path[u].predicates, v) { |
| 1058 | struct ly_path_predicate *pred = &path[u].predicates[v]; |
| 1059 | |
| 1060 | LY_ARRAY_INCREMENT((*dup)[u].predicates); |
| 1061 | switch (path[u].pred_type) { |
| 1062 | case LY_PATH_PREDTYPE_POSITION: |
| 1063 | /* position-predicate */ |
| 1064 | (*dup)[u].predicates[v].position = pred->position; |
| 1065 | break; |
| 1066 | case LY_PATH_PREDTYPE_LIST: |
| 1067 | case LY_PATH_PREDTYPE_LEAFLIST: |
| 1068 | /* key-predicate or leaf-list-predicate */ |
| 1069 | (*dup)[u].predicates[v].key = pred->key; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1070 | 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] | 1071 | ++((struct lysc_type *)pred->value.realtype)->refcount; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1072 | break; |
| 1073 | case LY_PATH_PREDTYPE_NONE: |
| 1074 | break; |
| 1075 | } |
| 1076 | } |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | return LY_SUCCESS; |
| 1081 | } |
| 1082 | |
| 1083 | void |
Michal Vasko | f7e16e2 | 2020-10-21 09:24:39 +0200 | [diff] [blame] | 1084 | 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] | 1085 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1086 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1087 | |
| 1088 | if (!predicates) { |
| 1089 | return; |
| 1090 | } |
| 1091 | |
| 1092 | LY_ARRAY_FOR(predicates, u) { |
| 1093 | switch (pred_type) { |
| 1094 | case LY_PATH_PREDTYPE_POSITION: |
| 1095 | case LY_PATH_PREDTYPE_NONE: |
| 1096 | /* nothing to free */ |
| 1097 | break; |
| 1098 | case LY_PATH_PREDTYPE_LIST: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1099 | case LY_PATH_PREDTYPE_LEAFLIST: |
Michal Vasko | ae87566 | 2020-10-21 10:33:17 +0200 | [diff] [blame] | 1100 | if (predicates[u].value.realtype) { |
| 1101 | predicates[u].value.realtype->plugin->free(ctx, &predicates[u].value); |
| 1102 | lysc_type_free((struct ly_ctx *)ctx, (struct lysc_type *)predicates[u].value.realtype); |
| 1103 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1104 | break; |
| 1105 | } |
| 1106 | } |
| 1107 | LY_ARRAY_FREE(predicates); |
| 1108 | } |
| 1109 | |
| 1110 | void |
| 1111 | ly_path_free(const struct ly_ctx *ctx, struct ly_path *path) |
| 1112 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1113 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1114 | |
| 1115 | LY_ARRAY_FOR(path, u) { |
Michal Vasko | f7e16e2 | 2020-10-21 09:24:39 +0200 | [diff] [blame] | 1116 | ly_path_predicates_free(ctx, path[u].pred_type, path[u].predicates); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1117 | } |
| 1118 | LY_ARRAY_FREE(path); |
| 1119 | } |