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