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 | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 28 | #include "tree_data_internal.h" |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 29 | #include "tree_schema.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 30 | #include "tree_schema_internal.h" |
| 31 | #include "xpath.h" |
| 32 | |
| 33 | /** |
| 34 | * @brief Check predicate syntax. |
| 35 | * |
| 36 | * @param[in] ctx libyang context. |
| 37 | * @param[in] exp Parsed predicate. |
| 38 | * @param[in,out] tok_idx Index in @p exp, is adjusted. |
| 39 | * @param[in] prefix Prefix option. |
| 40 | * @param[in] pred Predicate option. |
| 41 | * @return LY_ERR value. |
| 42 | */ |
| 43 | static LY_ERR |
| 44 | ly_path_check_predicate(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, uint8_t prefix, |
| 45 | uint8_t pred) |
| 46 | { |
| 47 | struct ly_set *set = NULL; |
| 48 | uint32_t i; |
| 49 | const char *name; |
| 50 | size_t name_len; |
| 51 | |
| 52 | /* '[' */ |
| 53 | if (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_BRACK1)) { |
| 54 | if (((pred == LY_PATH_PRED_SIMPLE) || (pred == LY_PATH_PRED_KEYS)) |
| 55 | && !lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NAMETEST)) { |
| 56 | set = ly_set_new(); |
| 57 | LY_CHECK_ERR_GOTO(!set, LOGMEM(ctx), error); |
| 58 | |
| 59 | do { |
| 60 | /* NameTest is always expected here */ |
| 61 | LY_CHECK_GOTO(lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NAMETEST), error); |
| 62 | |
| 63 | /* check prefix based on the options */ |
| 64 | name = strnstr(exp->expr + exp->tok_pos[*tok_idx], ":", exp->tok_len[*tok_idx]); |
| 65 | if ((prefix == LY_PATH_PREFIX_MANDATORY) && !name) { |
| 66 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Prefix missing for \"%.*s\" in path.", exp->tok_len[*tok_idx], |
| 67 | exp->expr + exp->tok_pos[*tok_idx]); |
| 68 | goto error; |
| 69 | } |
| 70 | if (!name) { |
| 71 | name = exp->expr + exp->tok_pos[*tok_idx]; |
| 72 | name_len = exp->tok_len[*tok_idx]; |
| 73 | } else { |
| 74 | ++name; |
| 75 | name_len = exp->tok_len[*tok_idx] - (name - (exp->expr + exp->tok_pos[*tok_idx])); |
| 76 | } |
| 77 | |
| 78 | /* check whether it was not already specified */ |
| 79 | for (i = 0; i < set->count; ++i) { |
| 80 | /* all the keys must be from the same module so this comparison should be fine */ |
| 81 | if (!strncmp(set->objs[i], name, name_len) && !isalpha(((char *)set->objs[i])[name_len])) { |
| 82 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Duplicate predicate key \"%.*s\" in path.", |
| 83 | name_len, name); |
| 84 | goto error; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /* add it into the set */ |
| 89 | ly_set_add(set, (void *)name, LY_SET_OPT_USEASLIST); |
| 90 | |
| 91 | /* NameTest */ |
| 92 | ++(*tok_idx); |
| 93 | |
| 94 | /* '=' */ |
| 95 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_EQUAL), error); |
| 96 | |
| 97 | /* Literal */ |
| 98 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_LITERAL), error); |
| 99 | |
| 100 | /* ']' */ |
| 101 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_BRACK2), error); |
| 102 | |
| 103 | /* '[' */ |
| 104 | } while (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_BRACK1)); |
| 105 | |
| 106 | /* '.' */ |
| 107 | } else if ((pred == LY_PATH_PRED_SIMPLE) && !lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_DOT)) { |
| 108 | /* '=' */ |
| 109 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_EQUAL), error); |
| 110 | |
| 111 | /* Literal */ |
| 112 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_LITERAL), error); |
| 113 | |
| 114 | /* ']' */ |
| 115 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_BRACK2), error); |
| 116 | |
| 117 | /* Number */ |
| 118 | } else if ((pred == LY_PATH_PRED_SIMPLE) && !lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_NUMBER)) { |
| 119 | |
| 120 | /* ']' */ |
| 121 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_BRACK2), error); |
| 122 | |
| 123 | } else if ((pred == LY_PATH_PRED_LEAFREF) && !lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NAMETEST)) { |
| 124 | assert(prefix == LY_PATH_PREFIX_OPTIONAL); |
| 125 | set = ly_set_new(); |
| 126 | LY_CHECK_ERR_GOTO(!set, LOGMEM(ctx), error); |
| 127 | |
| 128 | do { |
| 129 | /* NameTest is always expected here */ |
| 130 | LY_CHECK_GOTO(lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NAMETEST), error); |
| 131 | |
| 132 | name = strnstr(exp->expr + exp->tok_pos[*tok_idx], ":", exp->tok_len[*tok_idx]); |
| 133 | if (!name) { |
| 134 | name = exp->expr + exp->tok_pos[*tok_idx]; |
| 135 | name_len = exp->tok_len[*tok_idx]; |
| 136 | } else { |
| 137 | ++name; |
| 138 | name_len = exp->tok_len[*tok_idx] - (name - (exp->expr + exp->tok_pos[*tok_idx])); |
| 139 | } |
| 140 | |
| 141 | /* check whether it was not already specified */ |
| 142 | for (i = 0; i < set->count; ++i) { |
| 143 | /* all the keys must be from the same module so this comparison should be fine */ |
| 144 | if (!strncmp(set->objs[i], name, name_len) && !isalpha(((char *)set->objs[i])[name_len])) { |
| 145 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Duplicate predicate key \"%.*s\" in path.", |
| 146 | name_len, name); |
| 147 | goto error; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /* add it into the set */ |
| 152 | ly_set_add(set, (void *)name, LY_SET_OPT_USEASLIST); |
| 153 | |
| 154 | /* NameTest */ |
| 155 | ++(*tok_idx); |
| 156 | |
| 157 | /* '=' */ |
| 158 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_EQUAL), error); |
| 159 | |
| 160 | /* FuncName */ |
| 161 | LY_CHECK_GOTO(lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME), error); |
| 162 | if ((exp->tok_len[*tok_idx] != 7) || strncmp(exp->expr + exp->tok_pos[*tok_idx], "current", 7)) { |
| 163 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid function \"%.*s\" invocation in path.", |
| 164 | exp->tok_len[*tok_idx], exp->expr + exp->tok_pos[*tok_idx]); |
| 165 | goto error; |
| 166 | } |
| 167 | ++(*tok_idx); |
| 168 | |
| 169 | /* '(' */ |
| 170 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_PAR1), error); |
| 171 | |
| 172 | /* ')' */ |
| 173 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_PAR2), error); |
| 174 | |
| 175 | /* '/' */ |
| 176 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_PATH), error); |
| 177 | |
| 178 | /* '..' */ |
| 179 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_DDOT), error); |
| 180 | do { |
| 181 | /* '/' */ |
| 182 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_OPER_PATH), error); |
| 183 | } while (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_DDOT)); |
| 184 | |
| 185 | /* NameTest */ |
| 186 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_NAMETEST), error); |
| 187 | |
| 188 | /* '/' */ |
| 189 | while (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_OPER_PATH)) { |
| 190 | /* NameTest */ |
| 191 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_NAMETEST), error); |
| 192 | } |
| 193 | |
| 194 | /* ']' */ |
| 195 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_BRACK2), error); |
| 196 | |
| 197 | /* '[' */ |
| 198 | } while (!lyxp_next_token(NULL, exp, tok_idx, LYXP_TOKEN_BRACK1)); |
| 199 | |
| 200 | } else { |
| 201 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), |
| 202 | exp->expr + exp->tok_pos[*tok_idx]); |
| 203 | goto error; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | ly_set_free(set, NULL); |
| 208 | return LY_SUCCESS; |
| 209 | |
| 210 | error: |
| 211 | ly_set_free(set, NULL); |
| 212 | return LY_EVALID; |
| 213 | } |
| 214 | |
| 215 | LY_ERR |
| 216 | ly_path_parse(const struct ly_ctx *ctx, const char *str_path, size_t path_len, uint8_t begin, uint8_t lref, |
| 217 | uint8_t prefix, uint8_t pred, struct lyxp_expr **expr) |
| 218 | { |
| 219 | struct lyxp_expr *exp; |
| 220 | uint16_t tok_idx; |
| 221 | |
| 222 | assert((begin == LY_PATH_BEGIN_ABSOLUTE) || (begin == LY_PATH_BEGIN_EITHER)); |
| 223 | assert((lref == LY_PATH_LREF_TRUE) || (lref == LY_PATH_LREF_FALSE)); |
| 224 | assert((prefix == LY_PATH_PREFIX_OPTIONAL) || (prefix == LY_PATH_PREFIX_MANDATORY)); |
| 225 | assert((pred == LY_PATH_PRED_KEYS) || (pred == LY_PATH_PRED_SIMPLE) || (pred == LY_PATH_PRED_LEAFREF)); |
| 226 | |
| 227 | /* parse as a generic XPath expression */ |
| 228 | exp = lyxp_expr_parse(ctx, str_path, path_len, 1); |
| 229 | LY_CHECK_GOTO(!exp, error); |
| 230 | tok_idx = 0; |
| 231 | |
| 232 | if (begin == LY_PATH_BEGIN_EITHER) { |
| 233 | /* is the path relative? */ |
| 234 | if (lyxp_next_token(NULL, exp, &tok_idx, LYXP_TOKEN_OPER_PATH)) { |
| 235 | /* '..' */ |
| 236 | while ((lref == LY_PATH_LREF_TRUE) && !lyxp_next_token(NULL, exp, &tok_idx, LYXP_TOKEN_DDOT)) { |
| 237 | /* '/' */ |
| 238 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, &tok_idx, LYXP_TOKEN_OPER_PATH), error); |
| 239 | } |
| 240 | } |
| 241 | } else { |
| 242 | /* '/' */ |
| 243 | LY_CHECK_GOTO(lyxp_next_token(ctx, exp, &tok_idx, LYXP_TOKEN_OPER_PATH), error); |
| 244 | } |
| 245 | |
| 246 | do { |
| 247 | /* NameTest */ |
| 248 | LY_CHECK_GOTO(lyxp_check_token(ctx, exp, tok_idx, LYXP_TOKEN_NAMETEST), error); |
| 249 | |
| 250 | /* check prefix based on the options */ |
| 251 | if ((prefix == LY_PATH_PREFIX_MANDATORY) && !strnstr(exp->expr + exp->tok_pos[tok_idx], ":", exp->tok_len[tok_idx])) { |
| 252 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Prefix missing for \"%.*s\" in path.", exp->tok_len[tok_idx], |
| 253 | exp->expr + exp->tok_pos[tok_idx]); |
| 254 | goto error; |
| 255 | } |
| 256 | |
| 257 | ++tok_idx; |
| 258 | |
| 259 | /* Predicate* */ |
| 260 | LY_CHECK_GOTO(ly_path_check_predicate(ctx, exp, &tok_idx, prefix, pred), error); |
| 261 | |
| 262 | /* '/' */ |
| 263 | } while (!lyxp_next_token(NULL, exp, &tok_idx, LYXP_TOKEN_OPER_PATH)); |
| 264 | |
| 265 | /* trailing token check */ |
| 266 | if (exp->used > tok_idx) { |
| 267 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of path.", |
| 268 | exp->expr + exp->tok_pos[tok_idx]); |
| 269 | goto error; |
| 270 | } |
| 271 | |
| 272 | *expr = exp; |
| 273 | return LY_SUCCESS; |
| 274 | |
| 275 | error: |
| 276 | lyxp_expr_free(ctx, exp); |
Radek Krejci | 8de005f | 2020-06-25 17:02:07 +0200 | [diff] [blame] | 277 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | LY_ERR |
| 281 | ly_path_parse_predicate(const struct ly_ctx *ctx, const char *str_path, size_t path_len, uint8_t prefix, uint8_t pred, |
| 282 | struct lyxp_expr **expr) |
| 283 | { |
| 284 | struct lyxp_expr *exp; |
| 285 | uint16_t tok_idx; |
| 286 | |
| 287 | assert((prefix == LY_PATH_PREFIX_OPTIONAL) || (prefix == LY_PATH_PREFIX_MANDATORY)); |
| 288 | assert((pred == LY_PATH_PRED_KEYS) || (pred == LY_PATH_PRED_SIMPLE) || (pred == LY_PATH_PRED_LEAFREF)); |
| 289 | |
| 290 | /* parse as a generic XPath expression */ |
| 291 | exp = lyxp_expr_parse(ctx, str_path, path_len, 0); |
| 292 | LY_CHECK_GOTO(!exp, error); |
| 293 | tok_idx = 0; |
| 294 | |
| 295 | LY_CHECK_GOTO(ly_path_check_predicate(ctx, exp, &tok_idx, prefix, pred), error); |
| 296 | |
| 297 | /* trailing token check */ |
| 298 | if (exp->used > tok_idx) { |
| 299 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of predicate.", |
| 300 | exp->expr + exp->tok_pos[tok_idx]); |
| 301 | goto error; |
| 302 | } |
| 303 | |
| 304 | *expr = exp; |
| 305 | return LY_SUCCESS; |
| 306 | |
| 307 | error: |
| 308 | lyxp_expr_free(ctx, exp); |
Radek Krejci | 8de005f | 2020-06-25 17:02:07 +0200 | [diff] [blame] | 309 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | /** |
| 313 | * @brief Parse prefix from a NameTest, if any, and node name, and return expected module of the node. |
| 314 | * |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 315 | * @param[in] ctx libyang context. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 316 | * @param[in] cur_mod Module of the current (original context) node. Needed for ::LYD_SCHEMA. |
| 317 | * @param[in] prev_ctx_node Previous context node. Needed for ::LYD_JSON. |
| 318 | * @param[in] expr Parsed path. |
| 319 | * @param[in] tok_idx Index in @p expr. |
| 320 | * @param[in] lref Lref option. |
| 321 | * @param[in] resolve_prefix Callback for prefix resolution. |
| 322 | * @param[in] prefix_data Data for @p resolve_prefix. |
| 323 | * @param[in] format Format of the path. |
| 324 | * @param[out] mod Resolved module. |
| 325 | * @param[out] name Parsed node name. |
| 326 | * @param[out] name_len Length of @p name. |
| 327 | * @return LY_ERR value. |
| 328 | */ |
| 329 | static LY_ERR |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 330 | ly_path_compile_prefix(const struct ly_ctx *ctx, const struct lys_module *cur_mod, const struct lysc_node *prev_ctx_node, |
| 331 | const struct lyxp_expr *expr, uint16_t tok_idx, uint8_t lref, ly_clb_resolve_prefix resolve_prefix, |
| 332 | void *prefix_data, LYD_FORMAT format, const struct lys_module **mod, const char **name, size_t *name_len) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 333 | { |
| 334 | const char *ptr; |
| 335 | size_t len; |
| 336 | |
| 337 | assert(expr->tokens[tok_idx] == LYXP_TOKEN_NAMETEST); |
| 338 | |
| 339 | /* get prefix */ |
| 340 | ptr = strnstr(expr->expr + expr->tok_pos[tok_idx], ":", expr->tok_len[tok_idx]); |
| 341 | len = ptr ? ptr - (expr->expr + expr->tok_pos[tok_idx]) : 0; |
| 342 | |
| 343 | /* find next node module */ |
| 344 | if (ptr) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 345 | *mod = resolve_prefix(ctx, expr->expr + expr->tok_pos[tok_idx], len, prefix_data); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 346 | if (!*mod) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 347 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Prefix \"%.*s\" not found of a module in path.", |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 348 | len, expr->expr + expr->tok_pos[tok_idx]); |
Radek Krejci | 8de005f | 2020-06-25 17:02:07 +0200 | [diff] [blame] | 349 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 350 | } else if (!(*mod)->implemented) { |
| 351 | if (lref == LY_PATH_LREF_FALSE) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 352 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Not implemented module \"%s\" in path.", (*mod)->name); |
Radek Krejci | 8de005f | 2020-06-25 17:02:07 +0200 | [diff] [blame] | 353 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 354 | } |
| 355 | lys_set_implemented_internal((struct lys_module *)*mod, 2); |
| 356 | } |
| 357 | } else { |
| 358 | switch (format) { |
| 359 | case LYD_SCHEMA: |
| 360 | *mod = cur_mod; |
| 361 | break; |
| 362 | case LYD_JSON: |
| 363 | if (!prev_ctx_node) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 364 | LOGINT_RET(ctx); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 365 | } |
| 366 | *mod = prev_ctx_node->module; |
| 367 | break; |
| 368 | case LYD_XML: |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 369 | case LYD_LYB: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 370 | /* not really defined */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 371 | LOGINT_RET(ctx); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
| 375 | /* set name */ |
| 376 | if (ptr) { |
| 377 | *name = ptr + 1; |
| 378 | *name_len = expr->tok_len[tok_idx] - len - 1; |
| 379 | } else { |
| 380 | *name = expr->expr + expr->tok_pos[tok_idx]; |
| 381 | *name_len = expr->tok_len[tok_idx]; |
| 382 | } |
| 383 | |
| 384 | return LY_SUCCESS; |
| 385 | } |
| 386 | |
| 387 | LY_ERR |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 388 | ly_path_compile_predicate(const struct ly_ctx *ctx, const struct lys_module *cur_mod, const struct lysc_node *ctx_node, |
| 389 | const struct lyxp_expr *expr, uint16_t *tok_idx, ly_clb_resolve_prefix resolve_prefix, |
| 390 | void *prefix_data, LYD_FORMAT format, struct ly_path_predicate **predicates, |
| 391 | enum ly_path_pred_type *pred_type) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 392 | { |
| 393 | struct ly_path_predicate *p; |
| 394 | const struct lysc_node *key; |
| 395 | const struct lys_module *mod; |
| 396 | const char *name; |
| 397 | size_t name_len, key_count; |
| 398 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 399 | assert(ctx && ctx_node); |
| 400 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 401 | *pred_type = 0; |
| 402 | |
| 403 | /* '[' */ |
| 404 | if (lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1)) { |
| 405 | /* no predicate */ |
| 406 | return LY_SUCCESS; |
| 407 | } |
| 408 | |
| 409 | if (expr->tokens[*tok_idx] == LYXP_TOKEN_NAMETEST) { |
| 410 | if (ctx_node->nodetype != LYS_LIST) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 411 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "List predicate defined for %s \"%s\" in path.", |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 412 | lys_nodetype2str(ctx_node->nodetype), ctx_node->name); |
Radek Krejci | 8de005f | 2020-06-25 17:02:07 +0200 | [diff] [blame] | 413 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 414 | } else if (ctx_node->flags & LYS_KEYLESS) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 415 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "List predicate defined for keyless %s \"%s\" in path.", |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 416 | lys_nodetype2str(ctx_node->nodetype), ctx_node->name); |
Radek Krejci | 8de005f | 2020-06-25 17:02:07 +0200 | [diff] [blame] | 417 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | do { |
| 421 | /* NameTest, find the key */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 422 | LY_CHECK_RET(ly_path_compile_prefix(ctx, cur_mod, ctx_node, expr, *tok_idx, LY_PATH_LREF_FALSE, resolve_prefix, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 423 | prefix_data, format, &mod, &name, &name_len)); |
| 424 | key = lys_find_child(ctx_node, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK); |
| 425 | if (!key) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 426 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Not found node \"%.*s\" in path.", name_len, name); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 427 | return LY_ENOTFOUND; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 428 | } else if ((key->nodetype != LYS_LEAF) || !(key->flags & LYS_KEY)) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 429 | LOGVAL(ctx, LY_VLOG_LYSC, key, LYVE_XPATH, "Key expected instead of %s \"%s\" in path.", |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 430 | lys_nodetype2str(key->nodetype), key->name); |
Radek Krejci | 8de005f | 2020-06-25 17:02:07 +0200 | [diff] [blame] | 431 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 432 | } |
| 433 | ++(*tok_idx); |
| 434 | |
| 435 | /* new predicate */ |
| 436 | if (!*pred_type) { |
| 437 | *pred_type = LY_PATH_PREDTYPE_LIST; |
| 438 | } |
| 439 | assert(*pred_type == LY_PATH_PREDTYPE_LIST); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 440 | LY_ARRAY_NEW_RET(ctx, *predicates, p, LY_EMEM); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 441 | p->key = key; |
| 442 | |
| 443 | /* '=' */ |
| 444 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL); |
| 445 | ++(*tok_idx); |
| 446 | |
| 447 | /* Literal */ |
| 448 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_LITERAL); |
| 449 | LY_CHECK_RET(lyd_value_store(&p->value, key, expr->expr + expr->tok_pos[*tok_idx] + 1, |
| 450 | expr->tok_len[*tok_idx] - 2, NULL, resolve_prefix, prefix_data, format)); |
| 451 | ++(*tok_idx); |
| 452 | |
| 453 | /* ']' */ |
| 454 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
| 455 | ++(*tok_idx); |
| 456 | |
| 457 | /* another predicate follows? */ |
| 458 | } while (!lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1)); |
| 459 | |
| 460 | /* check that all keys were set */ |
| 461 | key_count = 0; |
| 462 | for (key = lysc_node_children(ctx_node, 0); key && (key->flags & LYS_KEY); key = key->next) { |
| 463 | ++key_count; |
| 464 | } |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 465 | if (LY_ARRAY_COUNT(*predicates) != key_count) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 466 | /* names (keys) are unique - it was checked when parsing */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 467 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Predicate missing for a key of %s \"%s\" in path.", |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 468 | lys_nodetype2str(ctx_node->nodetype), ctx_node->name); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 469 | ly_path_predicates_free(ctx, LY_PATH_PREDTYPE_LIST, NULL, *predicates); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 470 | *predicates = NULL; |
Radek Krejci | 8de005f | 2020-06-25 17:02:07 +0200 | [diff] [blame] | 471 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | } else if (expr->tokens[*tok_idx] == LYXP_TOKEN_DOT) { |
| 475 | if (ctx_node->nodetype != LYS_LEAFLIST) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 476 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Leaf-list predicate defined for %s \"%s\" in path.", |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 477 | lys_nodetype2str(ctx_node->nodetype), ctx_node->name); |
Radek Krejci | 8de005f | 2020-06-25 17:02:07 +0200 | [diff] [blame] | 478 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 479 | } |
| 480 | ++(*tok_idx); |
| 481 | |
| 482 | /* new predicate */ |
| 483 | *pred_type = LY_PATH_PREDTYPE_LEAFLIST; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 484 | LY_ARRAY_NEW_RET(ctx, *predicates, p, LY_EMEM); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 485 | |
| 486 | /* '=' */ |
| 487 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL); |
| 488 | ++(*tok_idx); |
| 489 | |
| 490 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_LITERAL); |
| 491 | /* store the value */ |
| 492 | LY_CHECK_RET(lyd_value_store(&p->value, ctx_node, expr->expr + expr->tok_pos[*tok_idx] + 1, |
| 493 | expr->tok_len[*tok_idx] - 2, NULL, resolve_prefix, prefix_data, format)); |
| 494 | ++(*tok_idx); |
| 495 | |
| 496 | /* ']' */ |
| 497 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
| 498 | ++(*tok_idx); |
| 499 | } else { |
| 500 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_NUMBER); |
| 501 | if (!(ctx_node->nodetype & (LYS_LEAFLIST | LYS_LIST))) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 502 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Positional predicate defined for %s \"%s\" in path.", |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 503 | lys_nodetype2str(ctx_node->nodetype), ctx_node->name); |
Radek Krejci | 8de005f | 2020-06-25 17:02:07 +0200 | [diff] [blame] | 504 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 505 | } else if (ctx_node->flags & LYS_CONFIG_W) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 506 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Positional predicate defined for configuration" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 507 | " %s \"%s\" in path.", lys_nodetype2str(ctx_node->nodetype), ctx_node->name); |
Radek Krejci | 8de005f | 2020-06-25 17:02:07 +0200 | [diff] [blame] | 508 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 509 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 510 | |
| 511 | /* new predicate */ |
| 512 | *pred_type = LY_PATH_PREDTYPE_POSITION; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 513 | LY_ARRAY_NEW_RET(ctx, *predicates, p, LY_EMEM); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 514 | |
| 515 | /* syntax was already checked */ |
| 516 | p->position = strtoull(expr->expr + expr->tok_pos[*tok_idx], (char **)&name, 10); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 517 | ++(*tok_idx); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 518 | |
| 519 | /* ']' */ |
| 520 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
| 521 | ++(*tok_idx); |
| 522 | } |
| 523 | |
| 524 | return LY_SUCCESS; |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * @brief Compile leafref predicate. Actually, it is only checked. |
| 529 | * |
| 530 | * @param[in] ctx_node Context node, node for which the predicate is defined. |
| 531 | * @param[in] cur_node Current (original context) node. |
| 532 | * @param[in] expr Parsed path. |
| 533 | * @param[in,out] tok_idx Index in @p expr, is adjusted for parsed tokens. |
| 534 | * @param[in] resolve_prefix Callback for prefix resolution. |
| 535 | * @param[in] prefix_data Data for @p resolve_prefix. |
| 536 | * @param[in] format Format of the path. |
| 537 | * @return LY_ERR value. |
| 538 | */ |
| 539 | static LY_ERR |
| 540 | ly_path_compile_predicate_leafref(const struct lysc_node *ctx_node, const struct lysc_node *cur_node, |
| 541 | const struct lyxp_expr *expr, uint16_t *tok_idx, ly_clb_resolve_prefix resolve_prefix, |
| 542 | void *prefix_data, LYD_FORMAT format) |
| 543 | { |
| 544 | const struct lysc_node *key, *node, *node2; |
| 545 | const struct lys_module *mod; |
| 546 | const char *name; |
| 547 | size_t name_len; |
| 548 | |
| 549 | /* '[' */ |
| 550 | if (lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1)) { |
| 551 | /* no predicate */ |
| 552 | return LY_SUCCESS; |
| 553 | } |
| 554 | |
| 555 | if (ctx_node->nodetype != LYS_LIST) { |
| 556 | LOGVAL(cur_node->module->ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "List predicate defined for %s \"%s\" in path.", |
| 557 | lys_nodetype2str(ctx_node->nodetype), ctx_node->name); |
Radek Krejci | 8de005f | 2020-06-25 17:02:07 +0200 | [diff] [blame] | 558 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 559 | } else if (ctx_node->flags & LYS_KEYLESS) { |
| 560 | LOGVAL(cur_node->module->ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "List predicate defined for keyless %s \"%s\" in path.", |
| 561 | lys_nodetype2str(ctx_node->nodetype), ctx_node->name); |
Radek Krejci | 8de005f | 2020-06-25 17:02:07 +0200 | [diff] [blame] | 562 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | do { |
| 566 | /* NameTest, find the key */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 567 | LY_CHECK_RET(ly_path_compile_prefix(cur_node->module->ctx, cur_node->module, ctx_node, expr, *tok_idx, |
| 568 | LY_PATH_LREF_TRUE, resolve_prefix, prefix_data, format, &mod, &name, &name_len)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 569 | key = lys_find_child(ctx_node, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK); |
| 570 | if (!key) { |
| 571 | LOGVAL(cur_node->module->ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Not found node \"%.*s\" in path.", name_len, name); |
Radek Krejci | 8de005f | 2020-06-25 17:02:07 +0200 | [diff] [blame] | 572 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 573 | } else if ((key->nodetype != LYS_LEAF) || !(key->flags & LYS_KEY)) { |
| 574 | LOGVAL(cur_node->module->ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Key expected instead of %s \"%s\" in path.", |
| 575 | lys_nodetype2str(key->nodetype), key->name); |
Radek Krejci | 8de005f | 2020-06-25 17:02:07 +0200 | [diff] [blame] | 576 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 577 | } |
| 578 | ++(*tok_idx); |
| 579 | |
| 580 | /* we are not actually compiling, throw the key away */ |
| 581 | (void)key; |
| 582 | |
| 583 | /* '=' */ |
| 584 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL); |
| 585 | ++(*tok_idx); |
| 586 | |
| 587 | /* FuncName */ |
| 588 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_FUNCNAME); |
| 589 | ++(*tok_idx); |
| 590 | |
| 591 | /* evaluating from the "current()" node */ |
| 592 | node = cur_node; |
| 593 | |
| 594 | /* '(' */ |
| 595 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
| 596 | ++(*tok_idx); |
| 597 | |
| 598 | /* ')' */ |
| 599 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
| 600 | ++(*tok_idx); |
| 601 | |
| 602 | do { |
| 603 | /* '/' */ |
| 604 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH); |
| 605 | ++(*tok_idx); |
| 606 | |
| 607 | /* go to parent */ |
| 608 | if (!node) { |
| 609 | LOGVAL(cur_node->module->ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Too many parent references in path."); |
Radek Krejci | 8de005f | 2020-06-25 17:02:07 +0200 | [diff] [blame] | 610 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 611 | } |
| 612 | node = lysc_data_parent(node); |
| 613 | |
| 614 | /* '..' */ |
| 615 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_DDOT); |
| 616 | ++(*tok_idx); |
| 617 | } while (expr->tokens[*tok_idx + 1] == LYXP_TOKEN_DDOT); |
| 618 | |
| 619 | do { |
| 620 | /* '/' */ |
| 621 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH); |
| 622 | ++(*tok_idx); |
| 623 | |
| 624 | /* NameTest */ |
| 625 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_NAMETEST); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 626 | LY_CHECK_RET(ly_path_compile_prefix(cur_node->module->ctx, cur_node->module, node, expr, *tok_idx, |
| 627 | LY_PATH_LREF_TRUE, resolve_prefix, prefix_data, format, &mod, &name, &name_len)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 628 | node2 = lys_find_child(node, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK); |
| 629 | if (!node2) { |
| 630 | LOGVAL(cur_node->module->ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Not found node \"%.*s\" in path.", |
| 631 | name_len, name); |
Radek Krejci | 8de005f | 2020-06-25 17:02:07 +0200 | [diff] [blame] | 632 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 633 | } |
| 634 | node = node2; |
| 635 | ++(*tok_idx); |
| 636 | } while ((*tok_idx + 1 < expr->used) && (expr->tokens[*tok_idx + 1] == LYXP_TOKEN_NAMETEST)); |
| 637 | |
| 638 | /* check the last target node */ |
| 639 | if (node->nodetype != LYS_LEAF) { |
| 640 | LOGVAL(cur_node->module->ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Leaf expected instead of %s \"%s\" in" |
| 641 | " leafref predicate in path.", lys_nodetype2str(node->nodetype), node->name); |
Radek Krejci | 8de005f | 2020-06-25 17:02:07 +0200 | [diff] [blame] | 642 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | /* we are not actually compiling, throw the rightside node away */ |
| 646 | (void)node; |
| 647 | |
| 648 | /* ']' */ |
| 649 | assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
| 650 | ++(*tok_idx); |
| 651 | |
| 652 | /* another predicate follows? */ |
| 653 | } while (!lyxp_next_token(NULL, expr, tok_idx, LYXP_TOKEN_BRACK1)); |
| 654 | |
| 655 | return LY_SUCCESS; |
| 656 | } |
| 657 | |
| 658 | LY_ERR |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 659 | ly_path_compile(const struct ly_ctx *ctx, const struct lys_module *cur_mod, const struct lysc_node *ctx_node, |
| 660 | const struct lyxp_expr *expr, uint8_t lref, uint8_t oper, uint8_t target, |
| 661 | ly_clb_resolve_prefix resolve_prefix, void *prefix_data, LYD_FORMAT format, struct ly_path **path) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 662 | { |
| 663 | LY_ERR ret = LY_SUCCESS; |
| 664 | uint16_t tok_idx = 0; |
| 665 | const struct lys_module *mod; |
| 666 | const struct lysc_node *node2, *cur_node; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 667 | struct ly_path *p = NULL; |
| 668 | int getnext_opts; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 669 | const char *name; |
| 670 | size_t name_len; |
| 671 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 672 | assert(ctx); |
| 673 | assert((expr->tokens[tok_idx] == LYXP_TOKEN_OPER_PATH) || (lref == LY_PATH_LREF_FALSE) || ctx_node); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 674 | assert((lref == LY_PATH_LREF_TRUE) || (lref == LY_PATH_LREF_FALSE)); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 675 | assert((oper == LY_PATH_OPER_INPUT) || (oper == LY_PATH_OPER_OUTPUT)); |
| 676 | assert((target == LY_PATH_TARGET_SINGLE) || (target == LY_PATH_TARGET_MANY)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 677 | |
| 678 | if (lref == LY_PATH_LREF_TRUE) { |
| 679 | /* remember original context node */ |
| 680 | cur_node = ctx_node; |
| 681 | } |
| 682 | *path = NULL; |
| 683 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 684 | if (oper == LY_PATH_OPER_OUTPUT) { |
| 685 | getnext_opts = LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_OUTPUT; |
| 686 | } else { |
| 687 | getnext_opts = LYS_GETNEXT_NOSTATECHECK; |
| 688 | } |
| 689 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 690 | if (expr->tokens[tok_idx] == LYXP_TOKEN_OPER_PATH) { |
| 691 | /* absolute path */ |
| 692 | ctx_node = NULL; |
| 693 | |
| 694 | ++tok_idx; |
| 695 | } else { |
| 696 | /* relative path */ |
| 697 | while ((lref == LY_PATH_LREF_TRUE) && (expr->tokens[tok_idx] == LYXP_TOKEN_DDOT)) { |
| 698 | if (!ctx_node) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 699 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Too many parent references in path."); |
Radek Krejci | 8de005f | 2020-06-25 17:02:07 +0200 | [diff] [blame] | 700 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | /* get parent */ |
| 704 | ctx_node = lysc_data_parent(ctx_node); |
| 705 | |
| 706 | ++tok_idx; |
| 707 | |
| 708 | assert(expr->tokens[tok_idx] == LYXP_TOKEN_OPER_PATH); |
| 709 | ++tok_idx; |
| 710 | } |
| 711 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 712 | /* we are not storing the parent */ |
| 713 | (void)ctx_node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | do { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 717 | /* check last compiled inner node, whether it is uniquely identified (even key-less list) */ |
| 718 | if (p && (lref == LY_PATH_LREF_FALSE) && (p->node->nodetype == LYS_LIST) && !p->predicates) { |
| 719 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.", |
| 720 | lys_nodetype2str(p->node->nodetype), p->node->name); |
Radek Krejci | 8de005f | 2020-06-25 17:02:07 +0200 | [diff] [blame] | 721 | return LY_EVALID; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 722 | } |
| 723 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 724 | /* get module and node name */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 725 | LY_CHECK_GOTO(ret = ly_path_compile_prefix(ctx, cur_mod, ctx_node, expr, tok_idx, lref, resolve_prefix, |
| 726 | prefix_data, format, &mod, &name, &name_len), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 727 | ++tok_idx; |
| 728 | |
| 729 | /* find the next node */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 730 | node2 = lys_find_child(ctx_node, mod, name, name_len, 0, getnext_opts); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 731 | if (!node2) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 732 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Not found node \"%.*s\" in path.", name_len, name); |
Radek Krejci | 8de005f | 2020-06-25 17:02:07 +0200 | [diff] [blame] | 733 | ret = LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 734 | goto cleanup; |
| 735 | } |
| 736 | ctx_node = node2; |
| 737 | |
| 738 | /* new path segment */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 739 | LY_ARRAY_NEW_GOTO(ctx, *path, p, ret, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 740 | p->node = ctx_node; |
| 741 | |
| 742 | /* compile any predicates */ |
| 743 | if (lref == LY_PATH_LREF_TRUE) { |
| 744 | ret = ly_path_compile_predicate_leafref(ctx_node, cur_node, expr, &tok_idx, resolve_prefix, prefix_data, format); |
| 745 | } else { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 746 | ret = ly_path_compile_predicate(ctx, cur_mod, ctx_node, expr, &tok_idx, resolve_prefix, prefix_data, format, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 747 | &p->predicates, &p->pred_type); |
| 748 | } |
| 749 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 750 | } while (!lyxp_next_token(NULL, expr, &tok_idx, LYXP_TOKEN_OPER_PATH)); |
| 751 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 752 | /* check last compiled node */ |
| 753 | if ((lref == LY_PATH_LREF_FALSE) && (target == LY_PATH_TARGET_SINGLE) |
| 754 | && (p->node->nodetype & (LYS_LIST | LYS_LEAFLIST)) && !p->predicates) { |
| 755 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Predicate missing for %s \"%s\" in path.", |
| 756 | lys_nodetype2str(p->node->nodetype), p->node->name); |
Radek Krejci | 8de005f | 2020-06-25 17:02:07 +0200 | [diff] [blame] | 757 | return LY_EVALID; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 758 | } |
| 759 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 760 | cleanup: |
| 761 | if (ret) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 762 | ly_path_free(ctx, *path); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 763 | *path = NULL; |
| 764 | } |
| 765 | return ret; |
| 766 | } |
| 767 | |
| 768 | LY_ERR |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 769 | ly_path_eval_partial(const struct ly_path *path, const struct lyd_node *start, LY_ARRAY_COUNT_TYPE *path_idx, |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 770 | struct lyd_node **match) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 771 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 772 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 773 | struct lyd_node *prev_node = NULL, *node, *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 774 | uint64_t pos; |
| 775 | |
| 776 | assert(path && start); |
| 777 | |
| 778 | if (lysc_data_parent(path[0].node)) { |
| 779 | /* relative path, start from the parent children */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 780 | start = lyd_node_children(start, 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 781 | } else { |
| 782 | /* absolute path, start from the first top-level sibling */ |
| 783 | while (start->parent) { |
| 784 | start = (struct lyd_node *)start->parent; |
| 785 | } |
| 786 | while (start->prev->next) { |
| 787 | start = start->prev; |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | LY_ARRAY_FOR(path, u) { |
| 792 | switch (path[u].pred_type) { |
| 793 | case LY_PATH_PREDTYPE_POSITION: |
| 794 | /* we cannot use hashes and want an instance on a specific position */ |
| 795 | pos = 1; |
| 796 | node = (struct lyd_node *)start; |
| 797 | while (!lyd_find_sibling_next2(node, path[u].node, NULL, 0, &node)) { |
| 798 | if (pos == path[u].predicates[0].position) { |
| 799 | break; |
| 800 | } |
| 801 | ++pos; |
| 802 | } |
| 803 | break; |
| 804 | case LY_PATH_PREDTYPE_LEAFLIST: |
| 805 | /* we will use hashes to find one leaf-list instance */ |
| 806 | LY_CHECK_RET(lyd_create_term2(path[u].node, &path[u].predicates[0].value, &target)); |
| 807 | lyd_find_sibling_first(start, target, &node); |
| 808 | lyd_free_tree(target); |
| 809 | break; |
| 810 | case LY_PATH_PREDTYPE_LIST: |
| 811 | /* we will use hashes to find one list instance */ |
| 812 | LY_CHECK_RET(lyd_create_list(path[u].node, path[u].predicates, &target)); |
| 813 | lyd_find_sibling_first(start, target, &node); |
| 814 | lyd_free_tree(target); |
| 815 | break; |
| 816 | case LY_PATH_PREDTYPE_NONE: |
| 817 | /* we will use hashes to find one any/container/leaf instance */ |
| 818 | lyd_find_sibling_val(start, path[u].node, NULL, 0, &node); |
| 819 | break; |
| 820 | } |
| 821 | |
| 822 | if (!node) { |
| 823 | /* no matching nodes */ |
| 824 | break; |
| 825 | } |
| 826 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 827 | /* rememeber previous node */ |
| 828 | prev_node = node; |
| 829 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 830 | /* next path segment, if any */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 831 | start = lyd_node_children(node, 0); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 832 | } |
| 833 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 834 | if (node) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 835 | /* we have found the full path */ |
| 836 | if (path_idx) { |
| 837 | *path_idx = u; |
| 838 | } |
| 839 | if (match) { |
| 840 | *match = node; |
| 841 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 842 | return LY_SUCCESS; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 843 | |
| 844 | } else if (prev_node) { |
| 845 | /* we have found only some partial match */ |
| 846 | if (path_idx) { |
| 847 | *path_idx = u - 1; |
| 848 | } |
| 849 | if (match) { |
| 850 | *match = prev_node; |
| 851 | } |
| 852 | return LY_EINCOMPLETE; |
| 853 | } |
| 854 | |
| 855 | /* we have not found any nodes */ |
| 856 | if (path_idx) { |
| 857 | *path_idx = 0; |
| 858 | } |
| 859 | if (match) { |
| 860 | *match = NULL; |
| 861 | } |
| 862 | return LY_ENOTFOUND; |
| 863 | } |
| 864 | |
| 865 | LY_ERR |
| 866 | ly_path_eval(const struct ly_path *path, const struct lyd_node *start, struct lyd_node **match) |
| 867 | { |
| 868 | LY_ERR ret; |
| 869 | struct lyd_node *m; |
| 870 | |
| 871 | ret = ly_path_eval_partial(path, start, NULL, &m); |
| 872 | |
| 873 | if (ret == LY_SUCCESS) { |
| 874 | /* last node was found */ |
| 875 | if (match) { |
| 876 | *match = m; |
| 877 | } |
| 878 | return LY_SUCCESS; |
| 879 | } |
| 880 | |
| 881 | /* not a full match */ |
| 882 | if (match) { |
| 883 | *match = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 884 | } |
| 885 | return LY_ENOTFOUND; |
| 886 | } |
| 887 | |
| 888 | LY_ERR |
| 889 | ly_path_dup(const struct ly_ctx *ctx, const struct ly_path *path, struct ly_path **dup) |
| 890 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 891 | LY_ARRAY_COUNT_TYPE u, v; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 892 | |
| 893 | if (!path) { |
| 894 | return LY_SUCCESS; |
| 895 | } |
| 896 | |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 897 | LY_ARRAY_CREATE_RET(ctx, *dup, LY_ARRAY_COUNT(path), LY_EMEM); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 898 | LY_ARRAY_FOR(path, u) { |
| 899 | LY_ARRAY_INCREMENT(*dup); |
| 900 | (*dup)[u].node = path[u].node; |
| 901 | if (path[u].predicates) { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 902 | 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] | 903 | (*dup)[u].pred_type = path[u].pred_type; |
| 904 | LY_ARRAY_FOR(path[u].predicates, v) { |
| 905 | struct ly_path_predicate *pred = &path[u].predicates[v]; |
| 906 | |
| 907 | LY_ARRAY_INCREMENT((*dup)[u].predicates); |
| 908 | switch (path[u].pred_type) { |
| 909 | case LY_PATH_PREDTYPE_POSITION: |
| 910 | /* position-predicate */ |
| 911 | (*dup)[u].predicates[v].position = pred->position; |
| 912 | break; |
| 913 | case LY_PATH_PREDTYPE_LIST: |
| 914 | case LY_PATH_PREDTYPE_LEAFLIST: |
| 915 | /* key-predicate or leaf-list-predicate */ |
| 916 | (*dup)[u].predicates[v].key = pred->key; |
| 917 | (*dup)[u].predicates[v].value.realtype = pred->value.realtype; |
| 918 | pred->value.realtype->plugin->duplicate(ctx, &pred->value, &(*dup)[u].predicates[v].value); |
| 919 | break; |
| 920 | case LY_PATH_PREDTYPE_NONE: |
| 921 | break; |
| 922 | } |
| 923 | } |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | return LY_SUCCESS; |
| 928 | } |
| 929 | |
| 930 | void |
| 931 | ly_path_predicates_free(const struct ly_ctx *ctx, enum ly_path_pred_type pred_type, const struct lysc_node *llist, |
| 932 | struct ly_path_predicate *predicates) |
| 933 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 934 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 935 | |
| 936 | if (!predicates) { |
| 937 | return; |
| 938 | } |
| 939 | |
| 940 | LY_ARRAY_FOR(predicates, u) { |
| 941 | switch (pred_type) { |
| 942 | case LY_PATH_PREDTYPE_POSITION: |
| 943 | case LY_PATH_PREDTYPE_NONE: |
| 944 | /* nothing to free */ |
| 945 | break; |
| 946 | case LY_PATH_PREDTYPE_LIST: |
| 947 | ((struct lysc_node_leaf *)predicates[u].key)->type->plugin->free(ctx, &predicates[u].value); |
| 948 | break; |
| 949 | case LY_PATH_PREDTYPE_LEAFLIST: |
| 950 | ((struct lysc_node_leaflist *)llist)->type->plugin->free(ctx, &predicates[u].value); |
| 951 | break; |
| 952 | } |
| 953 | } |
| 954 | LY_ARRAY_FREE(predicates); |
| 955 | } |
| 956 | |
| 957 | void |
| 958 | ly_path_free(const struct ly_ctx *ctx, struct ly_path *path) |
| 959 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 960 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 961 | |
| 962 | LY_ARRAY_FOR(path, u) { |
| 963 | ly_path_predicates_free(ctx, path[u].pred_type, path[u].node, path[u].predicates); |
| 964 | } |
| 965 | LY_ARRAY_FREE(path); |
| 966 | } |