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