Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file resolve.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief libyang resolve functions |
| 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 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 |
Michal Vasko | 8de098c | 2016-02-26 10:00:25 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 15 | #define _GNU_SOURCE |
| 16 | |
| 17 | #include <stdlib.h> |
| 18 | #include <assert.h> |
| 19 | #include <string.h> |
| 20 | #include <ctype.h> |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 21 | #include <limits.h> |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 22 | |
| 23 | #include "libyang.h" |
| 24 | #include "resolve.h" |
| 25 | #include "common.h" |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 26 | #include "xpath.h" |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 27 | #include "parser.h" |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 28 | #include "parser_yang.h" |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 29 | #include "xml_internal.h" |
Radek Krejci | 41912fe | 2015-10-22 10:22:12 +0200 | [diff] [blame] | 30 | #include "dict_private.h" |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 31 | #include "tree_internal.h" |
| 32 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 33 | /** |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 34 | * @brief Parse an identifier. |
| 35 | * |
| 36 | * ;; An identifier MUST NOT start with (('X'|'x') ('M'|'m') ('L'|'l')) |
| 37 | * identifier = (ALPHA / "_") |
| 38 | * *(ALPHA / DIGIT / "_" / "-" / ".") |
| 39 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 40 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 41 | * |
| 42 | * @return Number of characters successfully parsed. |
| 43 | */ |
Michal Vasko | 249e6b5 | 2015-08-19 11:08:52 +0200 | [diff] [blame] | 44 | int |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 45 | parse_identifier(const char *id) |
| 46 | { |
| 47 | int parsed = 0; |
| 48 | |
Michal Vasko | 1ab90bc | 2016-03-15 10:40:22 +0100 | [diff] [blame] | 49 | assert(id); |
| 50 | |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 51 | if (((id[0] == 'x') || (id[0] == 'X')) |
Michal Vasko | 1ab90bc | 2016-03-15 10:40:22 +0100 | [diff] [blame] | 52 | && (id[0] && ((id[1] == 'm') || (id[0] == 'M'))) |
| 53 | && (id[1] && ((id[2] == 'l') || (id[2] == 'L')))) { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 54 | return -parsed; |
| 55 | } |
| 56 | |
| 57 | if (!isalpha(id[0]) && (id[0] != '_')) { |
| 58 | return -parsed; |
| 59 | } |
| 60 | |
| 61 | ++parsed; |
| 62 | ++id; |
| 63 | |
| 64 | while (isalnum(id[0]) || (id[0] == '_') || (id[0] == '-') || (id[0] == '.')) { |
| 65 | ++parsed; |
| 66 | ++id; |
| 67 | } |
| 68 | |
| 69 | return parsed; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @brief Parse a node-identifier. |
| 74 | * |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 75 | * node-identifier = [module-name ":"] identifier |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 76 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 77 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 78 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 79 | * @param[out] mod_name_len Length of the module name, 0 if there is not any. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 80 | * @param[out] name Points to the node name. |
| 81 | * @param[out] nam_len Length of the node name. |
| 82 | * |
| 83 | * @return Number of characters successfully parsed, |
| 84 | * positive on success, negative on failure. |
| 85 | */ |
| 86 | static int |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 87 | parse_node_identifier(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 88 | { |
| 89 | int parsed = 0, ret; |
| 90 | |
| 91 | assert(id); |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 92 | if (mod_name) { |
| 93 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 94 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 95 | if (mod_name_len) { |
| 96 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 97 | } |
| 98 | if (name) { |
| 99 | *name = NULL; |
| 100 | } |
| 101 | if (nam_len) { |
| 102 | *nam_len = 0; |
| 103 | } |
| 104 | |
| 105 | if ((ret = parse_identifier(id)) < 1) { |
| 106 | return ret; |
| 107 | } |
| 108 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 109 | if (mod_name) { |
| 110 | *mod_name = id; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 111 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 112 | if (mod_name_len) { |
| 113 | *mod_name_len = ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | parsed += ret; |
| 117 | id += ret; |
| 118 | |
| 119 | /* there is prefix */ |
| 120 | if (id[0] == ':') { |
| 121 | ++parsed; |
| 122 | ++id; |
| 123 | |
| 124 | /* there isn't */ |
| 125 | } else { |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 126 | if (name && mod_name) { |
| 127 | *name = *mod_name; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 128 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 129 | if (mod_name) { |
| 130 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 131 | } |
| 132 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 133 | if (nam_len && mod_name_len) { |
| 134 | *nam_len = *mod_name_len; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 135 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 136 | if (mod_name_len) { |
| 137 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | return parsed; |
| 141 | } |
| 142 | |
| 143 | /* identifier (node name) */ |
| 144 | if ((ret = parse_identifier(id)) < 1) { |
| 145 | return -parsed+ret; |
| 146 | } |
| 147 | |
| 148 | if (name) { |
| 149 | *name = id; |
| 150 | } |
| 151 | if (nam_len) { |
| 152 | *nam_len = ret; |
| 153 | } |
| 154 | |
| 155 | return parsed+ret; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * @brief Parse a path-predicate (leafref). |
| 160 | * |
| 161 | * path-predicate = "[" *WSP path-equality-expr *WSP "]" |
| 162 | * path-equality-expr = node-identifier *WSP "=" *WSP path-key-expr |
| 163 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 164 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 165 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 166 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 167 | * @param[out] name Points to the node name. |
| 168 | * @param[out] nam_len Length of the node name. |
| 169 | * @param[out] path_key_expr Points to the path-key-expr. |
| 170 | * @param[out] pke_len Length of the path-key-expr. |
| 171 | * @param[out] has_predicate Flag to mark whether there is another predicate following. |
| 172 | * |
| 173 | * @return Number of characters successfully parsed, |
| 174 | * positive on success, negative on failure. |
| 175 | */ |
| 176 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 177 | parse_path_predicate(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, |
| 178 | const char **path_key_expr, int *pke_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 179 | { |
| 180 | const char *ptr; |
| 181 | int parsed = 0, ret; |
| 182 | |
| 183 | assert(id); |
| 184 | if (prefix) { |
| 185 | *prefix = NULL; |
| 186 | } |
| 187 | if (pref_len) { |
| 188 | *pref_len = 0; |
| 189 | } |
| 190 | if (name) { |
| 191 | *name = NULL; |
| 192 | } |
| 193 | if (nam_len) { |
| 194 | *nam_len = 0; |
| 195 | } |
| 196 | if (path_key_expr) { |
| 197 | *path_key_expr = NULL; |
| 198 | } |
| 199 | if (pke_len) { |
| 200 | *pke_len = 0; |
| 201 | } |
| 202 | if (has_predicate) { |
| 203 | *has_predicate = 0; |
| 204 | } |
| 205 | |
| 206 | if (id[0] != '[') { |
| 207 | return -parsed; |
| 208 | } |
| 209 | |
| 210 | ++parsed; |
| 211 | ++id; |
| 212 | |
| 213 | while (isspace(id[0])) { |
| 214 | ++parsed; |
| 215 | ++id; |
| 216 | } |
| 217 | |
| 218 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) { |
| 219 | return -parsed+ret; |
| 220 | } |
| 221 | |
| 222 | parsed += ret; |
| 223 | id += ret; |
| 224 | |
| 225 | while (isspace(id[0])) { |
| 226 | ++parsed; |
| 227 | ++id; |
| 228 | } |
| 229 | |
| 230 | if (id[0] != '=') { |
| 231 | return -parsed; |
| 232 | } |
| 233 | |
| 234 | ++parsed; |
| 235 | ++id; |
| 236 | |
| 237 | while (isspace(id[0])) { |
| 238 | ++parsed; |
| 239 | ++id; |
| 240 | } |
| 241 | |
| 242 | if ((ptr = strchr(id, ']')) == NULL) { |
| 243 | return -parsed; |
| 244 | } |
| 245 | |
| 246 | --ptr; |
| 247 | while (isspace(ptr[0])) { |
| 248 | --ptr; |
| 249 | } |
| 250 | ++ptr; |
| 251 | |
| 252 | ret = ptr-id; |
| 253 | if (path_key_expr) { |
| 254 | *path_key_expr = id; |
| 255 | } |
| 256 | if (pke_len) { |
| 257 | *pke_len = ret; |
| 258 | } |
| 259 | |
| 260 | parsed += ret; |
| 261 | id += ret; |
| 262 | |
| 263 | while (isspace(id[0])) { |
| 264 | ++parsed; |
| 265 | ++id; |
| 266 | } |
| 267 | |
| 268 | assert(id[0] == ']'); |
| 269 | |
| 270 | if (id[1] == '[') { |
| 271 | *has_predicate = 1; |
| 272 | } |
| 273 | |
| 274 | return parsed+1; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * @brief Parse a path-key-expr (leafref). First call parses "current()", all |
| 279 | * the ".." and the first node-identifier, other calls parse a single |
| 280 | * node-identifier each. |
| 281 | * |
| 282 | * path-key-expr = current-function-invocation *WSP "/" *WSP |
| 283 | * rel-path-keyexpr |
| 284 | * rel-path-keyexpr = 1*(".." *WSP "/" *WSP) |
| 285 | * *(node-identifier *WSP "/" *WSP) |
| 286 | * node-identifier |
| 287 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 288 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 289 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 290 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 291 | * @param[out] name Points to the node name. |
| 292 | * @param[out] nam_len Length of the node name. |
| 293 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 294 | * must not be changed between consecutive calls. |
| 295 | * @return Number of characters successfully parsed, |
| 296 | * positive on success, negative on failure. |
| 297 | */ |
| 298 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 299 | parse_path_key_expr(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, |
| 300 | int *parent_times) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 301 | { |
| 302 | int parsed = 0, ret, par_times = 0; |
| 303 | |
| 304 | assert(id); |
| 305 | assert(parent_times); |
| 306 | if (prefix) { |
| 307 | *prefix = NULL; |
| 308 | } |
| 309 | if (pref_len) { |
| 310 | *pref_len = 0; |
| 311 | } |
| 312 | if (name) { |
| 313 | *name = NULL; |
| 314 | } |
| 315 | if (nam_len) { |
| 316 | *nam_len = 0; |
| 317 | } |
| 318 | |
| 319 | if (!*parent_times) { |
| 320 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
| 321 | if (strncmp(id, "current()", 9)) { |
| 322 | return -parsed; |
| 323 | } |
| 324 | |
| 325 | parsed += 9; |
| 326 | id += 9; |
| 327 | |
| 328 | while (isspace(id[0])) { |
| 329 | ++parsed; |
| 330 | ++id; |
| 331 | } |
| 332 | |
| 333 | if (id[0] != '/') { |
| 334 | return -parsed; |
| 335 | } |
| 336 | |
| 337 | ++parsed; |
| 338 | ++id; |
| 339 | |
| 340 | while (isspace(id[0])) { |
| 341 | ++parsed; |
| 342 | ++id; |
| 343 | } |
| 344 | |
| 345 | /* rel-path-keyexpr */ |
| 346 | if (strncmp(id, "..", 2)) { |
| 347 | return -parsed; |
| 348 | } |
| 349 | ++par_times; |
| 350 | |
| 351 | parsed += 2; |
| 352 | id += 2; |
| 353 | |
| 354 | while (isspace(id[0])) { |
| 355 | ++parsed; |
| 356 | ++id; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | /* 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier |
| 361 | * |
| 362 | * first parent reference with whitespaces already parsed |
| 363 | */ |
| 364 | if (id[0] != '/') { |
| 365 | return -parsed; |
| 366 | } |
| 367 | |
| 368 | ++parsed; |
| 369 | ++id; |
| 370 | |
| 371 | while (isspace(id[0])) { |
| 372 | ++parsed; |
| 373 | ++id; |
| 374 | } |
| 375 | |
| 376 | while (!strncmp(id, "..", 2) && !*parent_times) { |
| 377 | ++par_times; |
| 378 | |
| 379 | parsed += 2; |
| 380 | id += 2; |
| 381 | |
| 382 | while (isspace(id[0])) { |
| 383 | ++parsed; |
| 384 | ++id; |
| 385 | } |
| 386 | |
| 387 | if (id[0] != '/') { |
| 388 | return -parsed; |
| 389 | } |
| 390 | |
| 391 | ++parsed; |
| 392 | ++id; |
| 393 | |
| 394 | while (isspace(id[0])) { |
| 395 | ++parsed; |
| 396 | ++id; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | if (!*parent_times) { |
| 401 | *parent_times = par_times; |
| 402 | } |
| 403 | |
| 404 | /* all parent references must be parsed at this point */ |
| 405 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) { |
| 406 | return -parsed+ret; |
| 407 | } |
| 408 | |
| 409 | parsed += ret; |
| 410 | id += ret; |
| 411 | |
| 412 | return parsed; |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * @brief Parse path-arg (leafref). |
| 417 | * |
| 418 | * path-arg = absolute-path / relative-path |
| 419 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 420 | * relative-path = 1*(".." "/") descendant-path |
| 421 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 422 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 423 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 424 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 425 | * @param[out] name Points to the node name. |
| 426 | * @param[out] nam_len Length of the node name. |
| 427 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 428 | * must not be changed between consecutive calls. -1 if the |
| 429 | * path is relative. |
| 430 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 431 | * |
| 432 | * @return Number of characters successfully parsed, |
| 433 | * positive on success, negative on failure. |
| 434 | */ |
| 435 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 436 | parse_path_arg(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, int *parent_times, |
| 437 | int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 438 | { |
| 439 | int parsed = 0, ret, par_times = 0; |
| 440 | |
| 441 | assert(id); |
| 442 | assert(parent_times); |
| 443 | if (prefix) { |
| 444 | *prefix = NULL; |
| 445 | } |
| 446 | if (pref_len) { |
| 447 | *pref_len = 0; |
| 448 | } |
| 449 | if (name) { |
| 450 | *name = NULL; |
| 451 | } |
| 452 | if (nam_len) { |
| 453 | *nam_len = 0; |
| 454 | } |
| 455 | if (has_predicate) { |
| 456 | *has_predicate = 0; |
| 457 | } |
| 458 | |
| 459 | if (!*parent_times && !strncmp(id, "..", 2)) { |
| 460 | ++par_times; |
| 461 | |
| 462 | parsed += 2; |
| 463 | id += 2; |
| 464 | |
| 465 | while (!strncmp(id, "/..", 3)) { |
| 466 | ++par_times; |
| 467 | |
| 468 | parsed += 3; |
| 469 | id += 3; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | if (!*parent_times) { |
| 474 | if (par_times) { |
| 475 | *parent_times = par_times; |
| 476 | } else { |
| 477 | *parent_times = -1; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | if (id[0] != '/') { |
| 482 | return -parsed; |
| 483 | } |
| 484 | |
| 485 | /* skip '/' */ |
| 486 | ++parsed; |
| 487 | ++id; |
| 488 | |
| 489 | /* node-identifier ([prefix:]identifier) */ |
| 490 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) { |
| 491 | return -parsed-ret; |
| 492 | } |
| 493 | |
| 494 | parsed += ret; |
| 495 | id += ret; |
| 496 | |
| 497 | /* there is no predicate */ |
| 498 | if ((id[0] == '/') || !id[0]) { |
| 499 | return parsed; |
| 500 | } else if (id[0] != '[') { |
| 501 | return -parsed; |
| 502 | } |
| 503 | |
| 504 | if (has_predicate) { |
| 505 | *has_predicate = 1; |
| 506 | } |
| 507 | |
| 508 | return parsed; |
| 509 | } |
| 510 | |
| 511 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 512 | * @brief Parse instance-identifier in JSON data format. That means that prefixes |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 513 | * (which are mandatory) are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 514 | * |
| 515 | * instance-identifier = 1*("/" (node-identifier *predicate)) |
| 516 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 517 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 518 | * @param[out] model Points to the model name. |
| 519 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 520 | * @param[out] name Points to the node name. |
| 521 | * @param[out] nam_len Length of the node name. |
| 522 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 523 | * |
| 524 | * @return Number of characters successfully parsed, |
| 525 | * positive on success, negative on failure. |
| 526 | */ |
| 527 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 528 | parse_instance_identifier(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 529 | int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 530 | { |
| 531 | int parsed = 0, ret; |
| 532 | |
| 533 | assert(id); |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 534 | if (model) { |
| 535 | *model = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 536 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 537 | if (mod_len) { |
| 538 | *mod_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 539 | } |
| 540 | if (name) { |
| 541 | *name = NULL; |
| 542 | } |
| 543 | if (nam_len) { |
| 544 | *nam_len = 0; |
| 545 | } |
| 546 | if (has_predicate) { |
| 547 | *has_predicate = 0; |
| 548 | } |
| 549 | |
| 550 | if (id[0] != '/') { |
| 551 | return -parsed; |
| 552 | } |
| 553 | |
| 554 | ++parsed; |
| 555 | ++id; |
| 556 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 557 | if ((ret = parse_node_identifier(id, model, mod_len, name, nam_len)) < 1) { |
Michal Vasko | bea08e9 | 2016-05-18 13:28:08 +0200 | [diff] [blame] | 558 | return -parsed + ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | parsed += ret; |
| 562 | id += ret; |
| 563 | |
| 564 | if ((id[0] == '[') && has_predicate) { |
| 565 | *has_predicate = 1; |
| 566 | } |
| 567 | |
| 568 | return parsed; |
| 569 | } |
| 570 | |
| 571 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 572 | * @brief Parse predicate (instance-identifier) in JSON data format. That means that prefixes |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 573 | * (which are mandatory) are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 574 | * |
| 575 | * predicate = "[" *WSP (predicate-expr / pos) *WSP "]" |
| 576 | * predicate-expr = (node-identifier / ".") *WSP "=" *WSP |
| 577 | * ((DQUOTE string DQUOTE) / |
| 578 | * (SQUOTE string SQUOTE)) |
| 579 | * pos = non-negative-integer-value |
| 580 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 581 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 582 | * @param[out] model Points to the model name. |
| 583 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 584 | * @param[out] name Points to the node name. Can be identifier (from node-identifier), "." or pos. |
| 585 | * @param[out] nam_len Length of the node name. |
| 586 | * @param[out] value Value the node-identifier must have (string from the grammar), |
| 587 | * NULL if there is not any. |
| 588 | * @param[out] val_len Length of the value, 0 if there is not any. |
| 589 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 590 | * |
| 591 | * @return Number of characters successfully parsed, |
| 592 | * positive on success, negative on failure. |
| 593 | */ |
| 594 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 595 | parse_predicate(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 596 | const char **value, int *val_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 597 | { |
| 598 | const char *ptr; |
| 599 | int parsed = 0, ret; |
| 600 | char quote; |
| 601 | |
| 602 | assert(id); |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 603 | if (model) { |
| 604 | *model = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 605 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 606 | if (mod_len) { |
| 607 | *mod_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 608 | } |
| 609 | if (name) { |
| 610 | *name = NULL; |
| 611 | } |
| 612 | if (nam_len) { |
| 613 | *nam_len = 0; |
| 614 | } |
| 615 | if (value) { |
| 616 | *value = NULL; |
| 617 | } |
| 618 | if (val_len) { |
| 619 | *val_len = 0; |
| 620 | } |
| 621 | if (has_predicate) { |
| 622 | *has_predicate = 0; |
| 623 | } |
| 624 | |
| 625 | if (id[0] != '[') { |
| 626 | return -parsed; |
| 627 | } |
| 628 | |
| 629 | ++parsed; |
| 630 | ++id; |
| 631 | |
| 632 | while (isspace(id[0])) { |
| 633 | ++parsed; |
| 634 | ++id; |
| 635 | } |
| 636 | |
| 637 | /* pos */ |
| 638 | if (isdigit(id[0])) { |
| 639 | if (name) { |
| 640 | *name = id; |
| 641 | } |
| 642 | |
| 643 | if (id[0] == '0') { |
| 644 | ++parsed; |
| 645 | ++id; |
| 646 | |
| 647 | if (isdigit(id[0])) { |
| 648 | return -parsed; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | while (isdigit(id[0])) { |
| 653 | ++parsed; |
| 654 | ++id; |
| 655 | } |
| 656 | |
| 657 | if (nam_len) { |
| 658 | *nam_len = id-(*name); |
| 659 | } |
| 660 | |
| 661 | /* "." */ |
| 662 | } else if (id[0] == '.') { |
| 663 | if (name) { |
| 664 | *name = id; |
| 665 | } |
| 666 | if (nam_len) { |
| 667 | *nam_len = 1; |
| 668 | } |
| 669 | |
| 670 | ++parsed; |
| 671 | ++id; |
| 672 | |
| 673 | /* node-identifier */ |
| 674 | } else { |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 675 | if ((ret = parse_node_identifier(id, model, mod_len, name, nam_len)) < 1) { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 676 | return -parsed+ret; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 677 | } else if (model && !*model) { |
| 678 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 679 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 680 | |
| 681 | parsed += ret; |
| 682 | id += ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | while (isspace(id[0])) { |
| 686 | ++parsed; |
| 687 | ++id; |
| 688 | } |
| 689 | |
| 690 | if (id[0] != '=') { |
| 691 | return -parsed; |
| 692 | } |
| 693 | |
| 694 | ++parsed; |
| 695 | ++id; |
| 696 | |
| 697 | while (isspace(id[0])) { |
| 698 | ++parsed; |
| 699 | ++id; |
| 700 | } |
| 701 | |
| 702 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 703 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 704 | quote = id[0]; |
| 705 | |
| 706 | ++parsed; |
| 707 | ++id; |
| 708 | |
| 709 | if ((ptr = strchr(id, quote)) == NULL) { |
| 710 | return -parsed; |
| 711 | } |
| 712 | ret = ptr-id; |
| 713 | |
| 714 | if (value) { |
| 715 | *value = id; |
| 716 | } |
| 717 | if (val_len) { |
| 718 | *val_len = ret; |
| 719 | } |
| 720 | |
| 721 | parsed += ret+1; |
| 722 | id += ret+1; |
| 723 | } else { |
| 724 | return -parsed; |
| 725 | } |
| 726 | |
| 727 | while (isspace(id[0])) { |
| 728 | ++parsed; |
| 729 | ++id; |
| 730 | } |
| 731 | |
| 732 | if (id[0] != ']') { |
| 733 | return -parsed; |
| 734 | } |
| 735 | |
| 736 | ++parsed; |
| 737 | ++id; |
| 738 | |
| 739 | if ((id[0] == '[') && has_predicate) { |
| 740 | *has_predicate = 1; |
| 741 | } |
| 742 | |
| 743 | return parsed; |
| 744 | } |
| 745 | |
| 746 | /** |
| 747 | * @brief Parse schema-nodeid. |
| 748 | * |
| 749 | * schema-nodeid = absolute-schema-nodeid / |
| 750 | * descendant-schema-nodeid |
| 751 | * absolute-schema-nodeid = 1*("/" node-identifier) |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 752 | * descendant-schema-nodeid = ["." "/"] |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 753 | * node-identifier |
| 754 | * absolute-schema-nodeid |
| 755 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 756 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 757 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 758 | * @param[out] mod_name_len Length of the module name, 0 if there is not any. |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 759 | * @param[out] name Points to the node name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 760 | * @param[out] nam_len Length of the node name. |
| 761 | * @param[out] is_relative Flag to mark whether the nodeid is absolute or descendant. Must be -1 |
| 762 | * on the first call, must not be changed between consecutive calls. |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 763 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. It cannot be |
| 764 | * based on the grammar, in those cases use NULL. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 765 | * |
| 766 | * @return Number of characters successfully parsed, |
| 767 | * positive on success, negative on failure. |
| 768 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 769 | int |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 770 | parse_schema_nodeid(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len, |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 771 | int *is_relative, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 772 | { |
| 773 | int parsed = 0, ret; |
| 774 | |
| 775 | assert(id); |
| 776 | assert(is_relative); |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 777 | if (mod_name) { |
| 778 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 779 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 780 | if (mod_name_len) { |
| 781 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 782 | } |
| 783 | if (name) { |
| 784 | *name = NULL; |
| 785 | } |
| 786 | if (nam_len) { |
| 787 | *nam_len = 0; |
| 788 | } |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 789 | if (has_predicate) { |
| 790 | *has_predicate = 0; |
| 791 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 792 | |
| 793 | if (id[0] != '/') { |
| 794 | if (*is_relative != -1) { |
| 795 | return -parsed; |
| 796 | } else { |
| 797 | *is_relative = 1; |
| 798 | } |
Michal Vasko | 4893535 | 2016-03-29 11:52:36 +0200 | [diff] [blame] | 799 | if (!strncmp(id, "./", 2)) { |
| 800 | parsed += 2; |
| 801 | id += 2; |
| 802 | } |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 803 | } else { |
| 804 | if (*is_relative == -1) { |
| 805 | *is_relative = 0; |
| 806 | } |
| 807 | ++parsed; |
| 808 | ++id; |
| 809 | } |
| 810 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 811 | if ((ret = parse_node_identifier(id, mod_name, mod_name_len, name, nam_len)) < 1) { |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 812 | return -parsed+ret; |
| 813 | } |
| 814 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 815 | parsed += ret; |
| 816 | id += ret; |
| 817 | |
| 818 | if ((id[0] == '[') && has_predicate) { |
| 819 | *has_predicate = 1; |
| 820 | } |
| 821 | |
| 822 | return parsed; |
| 823 | } |
| 824 | |
| 825 | /** |
| 826 | * @brief Parse schema predicate (special format internally used). |
| 827 | * |
| 828 | * predicate = "[" *WSP predicate-expr *WSP "]" |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 829 | * predicate-expr = "." / identifier / key-with-value |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 830 | * key-with-value = identifier *WSP "=" *WSP |
| 831 | * ((DQUOTE string DQUOTE) / |
| 832 | * (SQUOTE string SQUOTE)) |
| 833 | * |
| 834 | * @param[in] id Identifier to use. |
| 835 | * @param[out] name Points to the list key name. |
| 836 | * @param[out] nam_len Length of \p name. |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 837 | * @param[out] value Points to the key value. If specified, key-with-value is expected. |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 838 | * @param[out] val_len Length of \p value. |
| 839 | * @param[out] has_predicate Flag to mark whether there is another predicate specified. |
| 840 | */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 841 | int |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 842 | parse_schema_json_predicate(const char *id, const char **name, int *nam_len, const char **value, int *val_len, |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 843 | int *has_predicate) |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 844 | { |
| 845 | const char *ptr; |
| 846 | int parsed = 0, ret; |
| 847 | char quote; |
| 848 | |
| 849 | assert(id); |
| 850 | if (name) { |
| 851 | *name = NULL; |
| 852 | } |
| 853 | if (nam_len) { |
| 854 | *nam_len = 0; |
| 855 | } |
| 856 | if (value) { |
| 857 | *value = NULL; |
| 858 | } |
| 859 | if (val_len) { |
| 860 | *val_len = 0; |
| 861 | } |
| 862 | if (has_predicate) { |
| 863 | *has_predicate = 0; |
| 864 | } |
| 865 | |
| 866 | if (id[0] != '[') { |
| 867 | return -parsed; |
| 868 | } |
| 869 | |
| 870 | ++parsed; |
| 871 | ++id; |
| 872 | |
| 873 | while (isspace(id[0])) { |
| 874 | ++parsed; |
| 875 | ++id; |
| 876 | } |
| 877 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 878 | /* identifier */ |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 879 | if (id[0] == '.') { |
| 880 | ret = 1; |
| 881 | } else if ((ret = parse_identifier(id)) < 1) { |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 882 | return -parsed + ret; |
| 883 | } |
| 884 | if (name) { |
| 885 | *name = id; |
| 886 | } |
| 887 | if (nam_len) { |
| 888 | *nam_len = ret; |
| 889 | } |
| 890 | |
| 891 | parsed += ret; |
| 892 | id += ret; |
| 893 | |
| 894 | while (isspace(id[0])) { |
| 895 | ++parsed; |
| 896 | ++id; |
| 897 | } |
| 898 | |
| 899 | /* there is value as well */ |
| 900 | if (id[0] == '=') { |
| 901 | ++parsed; |
| 902 | ++id; |
| 903 | |
| 904 | while (isspace(id[0])) { |
| 905 | ++parsed; |
| 906 | ++id; |
| 907 | } |
| 908 | |
| 909 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 910 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 911 | quote = id[0]; |
| 912 | |
| 913 | ++parsed; |
| 914 | ++id; |
| 915 | |
| 916 | if ((ptr = strchr(id, quote)) == NULL) { |
| 917 | return -parsed; |
| 918 | } |
| 919 | ret = ptr - id; |
| 920 | |
| 921 | if (value) { |
| 922 | *value = id; |
| 923 | } |
| 924 | if (val_len) { |
| 925 | *val_len = ret; |
| 926 | } |
| 927 | |
| 928 | parsed += ret + 1; |
| 929 | id += ret + 1; |
| 930 | } else { |
| 931 | return -parsed; |
| 932 | } |
| 933 | |
| 934 | while (isspace(id[0])) { |
| 935 | ++parsed; |
| 936 | ++id; |
| 937 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 938 | } else if (value) { |
| 939 | /* if value was expected, it's mandatory */ |
| 940 | return -parsed; |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | if (id[0] != ']') { |
| 944 | return -parsed; |
| 945 | } |
| 946 | |
| 947 | ++parsed; |
| 948 | ++id; |
| 949 | |
| 950 | if ((id[0] == '[') && has_predicate) { |
| 951 | *has_predicate = 1; |
| 952 | } |
| 953 | |
| 954 | return parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | /** |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 958 | * @brief Resolve (find) a data node based on a schema-nodeid. |
| 959 | * |
| 960 | * Used for resolving unique statements - so id is expected to be relative and local (without reference to a different |
| 961 | * module). |
| 962 | * |
| 963 | */ |
| 964 | struct lyd_node * |
| 965 | resolve_data_descendant_schema_nodeid(const char *nodeid, struct lyd_node *start) |
| 966 | { |
| 967 | char *str, *token, *p; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 968 | struct lyd_node *result = NULL, *iter; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 969 | const struct lys_node *schema = NULL; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 970 | int shorthand = 0; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 971 | |
| 972 | assert(nodeid && start); |
| 973 | |
| 974 | if (nodeid[0] == '/') { |
| 975 | return NULL; |
| 976 | } |
| 977 | |
| 978 | str = p = strdup(nodeid); |
| 979 | if (!str) { |
| 980 | LOGMEM; |
| 981 | return NULL; |
| 982 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 983 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 984 | while (p) { |
| 985 | token = p; |
| 986 | p = strchr(p, '/'); |
| 987 | if (p) { |
| 988 | *p = '\0'; |
| 989 | p++; |
| 990 | } |
| 991 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 992 | if (p) { |
| 993 | /* inner node */ |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 994 | if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 995 | LYS_CONTAINER | LYS_CHOICE | LYS_CASE | LYS_LEAF, 0, 0, &schema) |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 996 | || !schema) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 997 | result = NULL; |
| 998 | break; |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 999 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1000 | |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1001 | if (schema->nodetype & (LYS_CHOICE | LYS_CASE)) { |
| 1002 | continue; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 1003 | } else if (lys_parent(schema)->nodetype == LYS_CHOICE) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1004 | /* shorthand case */ |
| 1005 | if (!shorthand) { |
| 1006 | shorthand = 1; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 1007 | schema = lys_parent(schema); |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1008 | continue; |
| 1009 | } else { |
| 1010 | shorthand = 0; |
| 1011 | if (schema->nodetype == LYS_LEAF) { |
| 1012 | /* should not be here, since we have leaf, which is not a shorthand nor final node */ |
| 1013 | result = NULL; |
| 1014 | break; |
| 1015 | } |
| 1016 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1017 | } |
| 1018 | } else { |
| 1019 | /* final node */ |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1020 | if (resolve_descendant_schema_nodeid(token, schema ? schema->child : start->schema, LYS_LEAF, |
| 1021 | shorthand ? 0 : 1, 0, &schema) |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1022 | || !schema) { |
| 1023 | result = NULL; |
| 1024 | break; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1025 | } |
| 1026 | } |
Radek Krejci | 5da4eb6 | 2016-04-08 14:45:51 +0200 | [diff] [blame] | 1027 | LY_TREE_FOR(result ? result->child : start, iter) { |
| 1028 | if (iter->schema == schema) { |
| 1029 | /* move in data tree according to returned schema */ |
| 1030 | result = iter; |
| 1031 | break; |
| 1032 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1033 | } |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1034 | if (!iter) { |
| 1035 | /* instance not found */ |
| 1036 | result = NULL; |
| 1037 | break; |
| 1038 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1039 | } |
| 1040 | free(str); |
| 1041 | |
| 1042 | return result; |
| 1043 | } |
| 1044 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1045 | /* |
| 1046 | * 0 - ok (done) |
| 1047 | * 1 - continue |
| 1048 | * 2 - break |
| 1049 | * -1 - error |
| 1050 | */ |
| 1051 | static int |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1052 | schema_nodeid_siblingcheck(const struct lys_node *sibling, int8_t *shorthand, const char *id, |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1053 | const struct lys_module *module, const char *mod_name, int mod_name_len, |
| 1054 | const struct lys_node **start) |
| 1055 | { |
| 1056 | const struct lys_module *prefix_mod; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1057 | int sh = 0; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1058 | |
| 1059 | /* module check */ |
| 1060 | prefix_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len); |
| 1061 | if (!prefix_mod) { |
| 1062 | return -1; |
| 1063 | } |
| 1064 | if (prefix_mod != lys_node_module(sibling)) { |
| 1065 | return 1; |
| 1066 | } |
| 1067 | |
| 1068 | /* check for shorthand cases - then 'start' does not change */ |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 1069 | if (lys_parent(sibling) && (lys_parent(sibling)->nodetype == LYS_CHOICE) && (sibling->nodetype != LYS_CASE)) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1070 | if (*shorthand != -1) { |
| 1071 | *shorthand = *shorthand ? 0 : 1; |
| 1072 | } |
| 1073 | sh = 1; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | /* the result node? */ |
| 1077 | if (!id[0]) { |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1078 | if (*shorthand == 1) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1079 | return 1; |
| 1080 | } |
| 1081 | return 0; |
| 1082 | } |
| 1083 | |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1084 | if (!sh) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1085 | /* move down the tree, if possible */ |
| 1086 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) { |
| 1087 | return -1; |
| 1088 | } |
| 1089 | *start = sibling->child; |
| 1090 | } |
| 1091 | |
| 1092 | return 2; |
| 1093 | } |
| 1094 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1095 | /* start - relative, module - absolute, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */ |
| 1096 | int |
| 1097 | resolve_augment_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_module *module, |
| 1098 | const struct lys_node **ret) |
| 1099 | { |
| 1100 | const char *name, *mod_name, *id; |
| 1101 | const struct lys_node *sibling; |
| 1102 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1103 | int8_t shorthand = 0; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1104 | /* resolved import module from the start module, it must match the next node-name-match sibling */ |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1105 | const struct lys_module *start_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1106 | |
| 1107 | assert(nodeid && (start || module) && !(start && module) && ret); |
| 1108 | |
| 1109 | id = nodeid; |
| 1110 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1111 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL)) < 1) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1112 | return ((id - nodeid) - r) + 1; |
| 1113 | } |
| 1114 | id += r; |
| 1115 | |
| 1116 | if ((is_relative && !start) || (!is_relative && !module)) { |
| 1117 | return -1; |
| 1118 | } |
| 1119 | |
| 1120 | /* descendant-schema-nodeid */ |
| 1121 | if (is_relative) { |
Michal Vasko | 4c06fd8 | 2016-02-17 13:43:38 +0100 | [diff] [blame] | 1122 | module = start_mod = start->module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1123 | |
| 1124 | /* absolute-schema-nodeid */ |
| 1125 | } else { |
| 1126 | start_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len); |
Michal Vasko | e290563 | 2016-02-11 15:42:24 +0100 | [diff] [blame] | 1127 | if (!start_mod) { |
| 1128 | return -1; |
| 1129 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1130 | start = start_mod->data; |
| 1131 | } |
| 1132 | |
| 1133 | while (1) { |
| 1134 | sibling = NULL; |
| 1135 | while ((sibling = lys_getnext(sibling, lys_parent(start), start_mod, |
| 1136 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT))) { |
| 1137 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 1138 | if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1139 | r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len, &start); |
| 1140 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1141 | *ret = sibling; |
| 1142 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1143 | } else if (r == 1) { |
| 1144 | continue; |
| 1145 | } else if (r == 2) { |
| 1146 | break; |
| 1147 | } else { |
| 1148 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1149 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | /* no match */ |
| 1154 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1155 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1156 | return EXIT_SUCCESS; |
| 1157 | } |
| 1158 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1159 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL)) < 1) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1160 | return ((id - nodeid) - r) + 1; |
| 1161 | } |
| 1162 | id += r; |
| 1163 | } |
| 1164 | |
| 1165 | /* cannot get here */ |
| 1166 | LOGINT; |
| 1167 | return -1; |
| 1168 | } |
| 1169 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1170 | /* unique, refine, |
| 1171 | * >0 - unexpected char on position (ret - 1), |
| 1172 | * 0 - ok (but ret can still be NULL), |
| 1173 | * -1 - error, |
| 1174 | * -2 - violated no_innerlist */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1175 | int |
| 1176 | resolve_descendant_schema_nodeid(const char *nodeid, const struct lys_node *start, int ret_nodetype, |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1177 | int check_shorthand, int no_innerlist, const struct lys_node **ret) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1178 | { |
| 1179 | const char *name, *mod_name, *id; |
| 1180 | const struct lys_node *sibling; |
| 1181 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1182 | int8_t shorthand = check_shorthand ? 0 : -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1183 | /* resolved import module from the start module, it must match the next node-name-match sibling */ |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1184 | const struct lys_module *module; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1185 | |
| 1186 | assert(nodeid && start && ret); |
| 1187 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING))); |
| 1188 | |
| 1189 | id = nodeid; |
| 1190 | module = start->module; |
| 1191 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1192 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL)) < 1) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1193 | return ((id - nodeid) - r) + 1; |
| 1194 | } |
| 1195 | id += r; |
| 1196 | |
| 1197 | if (!is_relative) { |
| 1198 | return -1; |
| 1199 | } |
| 1200 | |
| 1201 | while (1) { |
| 1202 | sibling = NULL; |
| 1203 | while ((sibling = lys_getnext(sibling, lys_parent(start), module, |
| 1204 | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE))) { |
| 1205 | /* name match */ |
| 1206 | if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1207 | r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len, &start); |
| 1208 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1209 | if (!(sibling->nodetype & ret_nodetype)) { |
| 1210 | /* wrong node type, too bad */ |
| 1211 | continue; |
| 1212 | } |
| 1213 | *ret = sibling; |
| 1214 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1215 | } else if (r == 1) { |
| 1216 | continue; |
| 1217 | } else if (r == 2) { |
| 1218 | break; |
| 1219 | } else { |
| 1220 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1221 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | /* no match */ |
| 1226 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1227 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1228 | return EXIT_SUCCESS; |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1229 | } else if (no_innerlist && sibling->nodetype == LYS_LIST) { |
| 1230 | *ret = NULL; |
| 1231 | return -2; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1232 | } |
| 1233 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1234 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL)) < 1) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1235 | return ((id - nodeid) - r) + 1; |
| 1236 | } |
| 1237 | id += r; |
| 1238 | } |
| 1239 | |
| 1240 | /* cannot get here */ |
| 1241 | LOGINT; |
| 1242 | return -1; |
| 1243 | } |
| 1244 | |
| 1245 | /* choice default */ |
| 1246 | int |
| 1247 | resolve_choice_default_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node **ret) |
| 1248 | { |
| 1249 | /* cannot actually be a path */ |
| 1250 | if (strchr(nodeid, '/')) { |
| 1251 | return -1; |
| 1252 | } |
| 1253 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 1254 | return resolve_descendant_schema_nodeid(nodeid, start, LYS_NO_RPC_NOTIF_NODE, 1, 0, ret); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | /* uses, -1 error, EXIT_SUCCESS ok (but ret can still be NULL), >0 unexpected char on ret - 1 */ |
| 1258 | static int |
| 1259 | resolve_uses_schema_nodeid(const char *nodeid, const struct lys_node *start, const struct lys_node_grp **ret) |
| 1260 | { |
| 1261 | const struct lys_module *module; |
| 1262 | const char *mod_prefix, *name; |
| 1263 | int i, mod_prefix_len, nam_len; |
| 1264 | |
| 1265 | /* parse the identifier, it must be parsed on one call */ |
| 1266 | if (((i = parse_node_identifier(nodeid, &mod_prefix, &mod_prefix_len, &name, &nam_len)) < 1) || nodeid[i]) { |
| 1267 | return -i + 1; |
| 1268 | } |
| 1269 | |
| 1270 | module = lys_get_import_module(start->module, mod_prefix, mod_prefix_len, NULL, 0); |
| 1271 | if (!module) { |
| 1272 | return -1; |
| 1273 | } |
| 1274 | if (module != start->module) { |
| 1275 | start = module->data; |
| 1276 | } |
| 1277 | |
| 1278 | *ret = lys_find_grouping_up(name, (struct lys_node *)start); |
| 1279 | |
| 1280 | return EXIT_SUCCESS; |
| 1281 | } |
| 1282 | |
| 1283 | int |
| 1284 | resolve_absolute_schema_nodeid(const char *nodeid, const struct lys_module *module, int ret_nodetype, |
| 1285 | const struct lys_node **ret) |
| 1286 | { |
| 1287 | const char *name, *mod_name, *id; |
| 1288 | const struct lys_node *sibling, *start; |
| 1289 | int r, nam_len, mod_name_len, is_relative = -1; |
Radek Krejci | cc217a6 | 2016-04-08 16:58:11 +0200 | [diff] [blame] | 1290 | int8_t shorthand = 0; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1291 | const struct lys_module *abs_start_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1292 | |
| 1293 | assert(nodeid && module && ret); |
| 1294 | assert(!(ret_nodetype & (LYS_USES | LYS_AUGMENT)) && ((ret_nodetype == LYS_GROUPING) || !(ret_nodetype & LYS_GROUPING))); |
| 1295 | |
| 1296 | id = nodeid; |
| 1297 | start = module->data; |
| 1298 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1299 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL)) < 1) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1300 | return ((id - nodeid) - r) + 1; |
| 1301 | } |
| 1302 | id += r; |
| 1303 | |
| 1304 | if (is_relative) { |
| 1305 | return -1; |
| 1306 | } |
| 1307 | |
| 1308 | abs_start_mod = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len); |
Michal Vasko | e290563 | 2016-02-11 15:42:24 +0100 | [diff] [blame] | 1309 | if (!abs_start_mod) { |
| 1310 | return -1; |
| 1311 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1312 | |
| 1313 | while (1) { |
| 1314 | sibling = NULL; |
| 1315 | while ((sibling = lys_getnext(sibling, lys_parent(start), abs_start_mod, LYS_GETNEXT_WITHCHOICE |
| 1316 | | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT | LYS_GETNEXT_WITHGROUPING))) { |
| 1317 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 1318 | if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1319 | r = schema_nodeid_siblingcheck(sibling, &shorthand, id, module, mod_name, mod_name_len, &start); |
| 1320 | if (r == 0) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1321 | if (!(sibling->nodetype & ret_nodetype)) { |
| 1322 | /* wrong node type, too bad */ |
| 1323 | continue; |
| 1324 | } |
| 1325 | *ret = sibling; |
| 1326 | return EXIT_SUCCESS; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1327 | } else if (r == 1) { |
| 1328 | continue; |
| 1329 | } else if (r == 2) { |
| 1330 | break; |
| 1331 | } else { |
| 1332 | return -1; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1333 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | /* no match */ |
| 1338 | if (!sibling) { |
Michal Vasko | a426fef | 2016-03-07 10:47:31 +0100 | [diff] [blame] | 1339 | *ret = NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1340 | return EXIT_SUCCESS; |
| 1341 | } |
| 1342 | |
Michal Vasko | 83e8e5b | 2016-03-11 14:29:10 +0100 | [diff] [blame] | 1343 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, NULL)) < 1) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1344 | return ((id - nodeid) - r) + 1; |
| 1345 | } |
| 1346 | id += r; |
| 1347 | } |
| 1348 | |
| 1349 | /* cannot get here */ |
| 1350 | LOGINT; |
| 1351 | return -1; |
| 1352 | } |
| 1353 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1354 | static int |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1355 | resolve_json_schema_list_predicate(const char *predicate, const struct lys_node_list *list, int *parsed) |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1356 | { |
| 1357 | const char *name; |
| 1358 | int nam_len, has_predicate, i; |
| 1359 | |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 1360 | if (((i = parse_schema_json_predicate(predicate, &name, &nam_len, NULL, NULL, &has_predicate)) < 1) |
| 1361 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1362 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-i], &predicate[-i]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1363 | return -1; |
| 1364 | } |
| 1365 | |
| 1366 | predicate += i; |
| 1367 | *parsed += i; |
| 1368 | |
| 1369 | for (i = 0; i < list->keys_size; ++i) { |
| 1370 | if (!strncmp(list->keys[i]->name, name, nam_len) && !list->keys[i]->name[nam_len]) { |
| 1371 | break; |
| 1372 | } |
| 1373 | } |
| 1374 | |
| 1375 | if (i == list->keys_size) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1376 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1377 | return -1; |
| 1378 | } |
| 1379 | |
| 1380 | /* more predicates? */ |
| 1381 | if (has_predicate) { |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1382 | return resolve_json_schema_list_predicate(predicate, list, parsed); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | return 0; |
| 1386 | } |
| 1387 | |
Michal Vasko | 9fd98e2 | 2016-04-07 15:44:19 +0200 | [diff] [blame] | 1388 | /* cannot return LYS_GROUPING, LYS_AUGMENT, LYS_USES, logs directly |
| 1389 | * data_nodeid - 0 schema nodeid, 1 - data nodeid with RPC input, 2 - data nodeid with RPC output */ |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1390 | const struct lys_node * |
Michal Vasko | 9fd98e2 | 2016-04-07 15:44:19 +0200 | [diff] [blame] | 1391 | resolve_json_schema_nodeid(const char *nodeid, struct ly_ctx *ctx, const struct lys_node *start, int data_nodeid) |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1392 | { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1393 | char *module_name = ly_buf(), *buf_backup = NULL, *str; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1394 | const char *name, *mod_name, *id; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1395 | const struct lys_node *sibling; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1396 | int r, nam_len, mod_name_len, is_relative = -1, has_predicate, shorthand = 0; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1397 | /* resolved import module from the start module, it must match the next node-name-match sibling */ |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1398 | const struct lys_module *prefix_mod, *module, *prev_mod; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1399 | |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1400 | assert(nodeid && (ctx || start)); |
| 1401 | if (!ctx) { |
| 1402 | ctx = start->module->ctx; |
| 1403 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1404 | |
| 1405 | id = nodeid; |
| 1406 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1407 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate)) < 1) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1408 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1409 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1410 | } |
| 1411 | id += r; |
| 1412 | |
| 1413 | if (is_relative) { |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1414 | assert(start); |
| 1415 | start = start->child; |
| 1416 | if (!start) { |
| 1417 | /* no descendants, fail for sure */ |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1418 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 1419 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 1420 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1421 | return NULL; |
| 1422 | } |
| 1423 | module = start->module; |
| 1424 | } else { |
| 1425 | if (!mod_name) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1426 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 1427 | LOGVAL(LYE_PATH_MISSMOD, LY_VLOG_STR, nodeid); |
| 1428 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1429 | return NULL; |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1430 | } else if (mod_name_len > LY_BUF_SIZE - 1) { |
| 1431 | LOGINT; |
| 1432 | return NULL; |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1433 | } |
| 1434 | |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1435 | if (ly_buf_used && module_name[0]) { |
| 1436 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 1437 | } |
| 1438 | ly_buf_used++; |
| 1439 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 1440 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1441 | module_name[mod_name_len] = '\0'; |
| 1442 | module = ly_ctx_get_module(ctx, module_name, NULL); |
| 1443 | |
| 1444 | if (buf_backup) { |
| 1445 | /* return previous internal buffer content */ |
| 1446 | strcpy(module_name, buf_backup); |
| 1447 | free(buf_backup); |
| 1448 | buf_backup = NULL; |
| 1449 | } |
| 1450 | ly_buf_used--; |
| 1451 | |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1452 | if (!module) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1453 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 1454 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 1455 | free(str); |
Michal Vasko | 3547c53 | 2016-03-14 09:40:50 +0100 | [diff] [blame] | 1456 | return NULL; |
| 1457 | } |
| 1458 | start = module->data; |
| 1459 | |
| 1460 | /* now it's as if there was no module name */ |
| 1461 | mod_name = NULL; |
| 1462 | mod_name_len = 0; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1463 | } |
| 1464 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1465 | prev_mod = module; |
| 1466 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1467 | while (1) { |
| 1468 | sibling = NULL; |
Michal Vasko | 9fd98e2 | 2016-04-07 15:44:19 +0200 | [diff] [blame] | 1469 | while ((sibling = lys_getnext(sibling, lys_parent(start), module, (data_nodeid ? |
| 1470 | 0 : LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT)))) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1471 | /* name match */ |
Michal Vasko | 0a1aaa4 | 2016-04-19 09:48:25 +0200 | [diff] [blame] | 1472 | if (sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) { |
Michal Vasko | 9fd98e2 | 2016-04-07 15:44:19 +0200 | [diff] [blame] | 1473 | |
| 1474 | /* data RPC input/output check */ |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 1475 | if ((data_nodeid == 1) && lys_parent(sibling) && (lys_parent(sibling)->nodetype == LYS_OUTPUT)) { |
Michal Vasko | 9fd98e2 | 2016-04-07 15:44:19 +0200 | [diff] [blame] | 1476 | continue; |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 1477 | } else if ((data_nodeid == 2) && lys_parent(sibling) && (lys_parent(sibling)->nodetype == LYS_INPUT)) { |
Michal Vasko | 9fd98e2 | 2016-04-07 15:44:19 +0200 | [diff] [blame] | 1478 | continue; |
| 1479 | } |
| 1480 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1481 | /* module check */ |
| 1482 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1483 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 1484 | LOGINT; |
| 1485 | return NULL; |
| 1486 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1487 | |
| 1488 | if (ly_buf_used && module_name[0]) { |
| 1489 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 1490 | } |
| 1491 | ly_buf_used++; |
| 1492 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 1493 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 8757e7c | 2016-03-15 10:41:30 +0100 | [diff] [blame] | 1494 | module_name[mod_name_len] = '\0'; |
| 1495 | /* will also find an augment module */ |
| 1496 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1497 | |
| 1498 | if (buf_backup) { |
| 1499 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 1500 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1501 | free(buf_backup); |
| 1502 | buf_backup = NULL; |
| 1503 | } |
| 1504 | ly_buf_used--; |
| 1505 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1506 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1507 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 1508 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 1509 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1510 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1511 | } |
| 1512 | } else { |
| 1513 | prefix_mod = prev_mod; |
| 1514 | } |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 1515 | if (prefix_mod != lys_node_module(sibling)) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1516 | continue; |
| 1517 | } |
| 1518 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1519 | /* do we have some predicates on it? */ |
| 1520 | if (has_predicate) { |
| 1521 | r = 0; |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 1522 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 1523 | if ((r = parse_schema_json_predicate(id, NULL, NULL, NULL, NULL, &has_predicate)) < 1) { |
| 1524 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
| 1525 | return NULL; |
| 1526 | } |
| 1527 | } else if (sibling->nodetype == LYS_LIST) { |
| 1528 | if (resolve_json_schema_list_predicate(id, (const struct lys_node_list *)sibling, &r)) { |
| 1529 | return NULL; |
| 1530 | } |
| 1531 | } else { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1532 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1533 | return NULL; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1534 | } |
| 1535 | id += r; |
| 1536 | } |
| 1537 | |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1538 | /* check for shorthand cases - then 'start' does not change */ |
Michal Vasko | 3c0f9f5 | 2016-05-17 16:38:10 +0200 | [diff] [blame] | 1539 | if (!data_nodeid && lys_parent(sibling) && (lys_parent(sibling)->nodetype == LYS_CHOICE) && (sibling->nodetype != LYS_CASE)) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1540 | shorthand = ~shorthand; |
| 1541 | } |
| 1542 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1543 | /* the result node? */ |
| 1544 | if (!id[0]) { |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1545 | if (shorthand) { |
| 1546 | /* wrong path for shorthand */ |
Michal Vasko | 025e045 | 2016-05-17 16:14:20 +0200 | [diff] [blame] | 1547 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 1548 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
Michal Vasko | 3c0f9f5 | 2016-05-17 16:38:10 +0200 | [diff] [blame] | 1549 | LOGVAL(LYE_SPEC, LY_VLOG_STR, str, "Schema shorthand case path must include the virtual case statement."); |
Radek Krejci | 9a5fccc | 2016-05-18 15:44:58 +0200 | [diff] [blame] | 1550 | free(str); |
Michal Vasko | 025e045 | 2016-05-17 16:14:20 +0200 | [diff] [blame] | 1551 | return NULL; |
Radek Krejci | bdf9236 | 2016-04-08 14:43:34 +0200 | [diff] [blame] | 1552 | } |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1553 | return sibling; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1554 | } |
| 1555 | |
Michal Vasko | 3c0f9f5 | 2016-05-17 16:38:10 +0200 | [diff] [blame] | 1556 | if (data_nodeid || !shorthand) { |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 1557 | /* move down the tree, if possible */ |
| 1558 | if (sibling->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1559 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 1560 | return NULL; |
| 1561 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1562 | start = sibling->child; |
| 1563 | } |
| 1564 | |
| 1565 | /* update prev mod */ |
| 1566 | prev_mod = start->module; |
| 1567 | break; |
| 1568 | } |
| 1569 | } |
| 1570 | |
| 1571 | /* no match */ |
| 1572 | if (!sibling) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1573 | str = strndup(nodeid, (name + nam_len) - nodeid); |
| 1574 | LOGVAL(LYE_PATH_INNODE, LY_VLOG_STR, str); |
| 1575 | free(str); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1576 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1577 | } |
| 1578 | |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1579 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate)) < 1) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1580 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1581 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1582 | } |
| 1583 | id += r; |
| 1584 | } |
| 1585 | |
| 1586 | /* cannot get here */ |
| 1587 | LOGINT; |
Michal Vasko | e733d68 | 2016-03-14 09:08:27 +0100 | [diff] [blame] | 1588 | return NULL; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1589 | } |
| 1590 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1591 | static int |
| 1592 | resolve_partial_json_data_list_predicate(const char *predicate, const char *node_name, struct lyd_node *node, int *parsed) |
| 1593 | { |
| 1594 | const char *name, *value; |
| 1595 | int nam_len, val_len, has_predicate = 1, r; |
| 1596 | uint16_t i; |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 1597 | struct lyd_node_leaf_list *key; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1598 | |
Radek Krejci | 61a86c6 | 2016-03-24 11:06:44 +0100 | [diff] [blame] | 1599 | assert(node); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1600 | assert(node->schema->nodetype == LYS_LIST); |
| 1601 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 1602 | key = (struct lyd_node_leaf_list *)node->child; |
| 1603 | for (i = 0; i < ((struct lys_node_list *)node->schema)->keys_size; ++i) { |
| 1604 | if (!key) { |
| 1605 | /* invalid data */ |
| 1606 | LOGINT; |
| 1607 | return -1; |
| 1608 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1609 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1610 | if (!has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1611 | LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, node_name); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 1612 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1613 | } |
| 1614 | |
Michal Vasko | 7b54f7e | 2016-05-03 15:07:31 +0200 | [diff] [blame] | 1615 | if (((r = parse_schema_json_predicate(predicate, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) |
| 1616 | || !strncmp(name, ".", nam_len)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1617 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, predicate[-r], &predicate[-r]); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 1618 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1619 | } |
| 1620 | |
| 1621 | predicate += r; |
| 1622 | *parsed += r; |
| 1623 | |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 1624 | if (strncmp(key->schema->name, name, nam_len) || key->schema->name[nam_len]) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1625 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 1626 | return -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | /* value does not match */ |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 1630 | if (strncmp(key->value_str, value, val_len) || key->value_str[val_len]) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1631 | return 1; |
| 1632 | } |
Michal Vasko | f29903d | 2016-04-18 13:13:10 +0200 | [diff] [blame] | 1633 | |
| 1634 | key = (struct lyd_node_leaf_list *)key->next; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1635 | } |
| 1636 | |
| 1637 | if (has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1638 | LOGVAL(LYE_PATH_INKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1639 | return -1; |
| 1640 | } |
| 1641 | |
| 1642 | return 0; |
| 1643 | } |
| 1644 | |
| 1645 | struct lyd_node * |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 1646 | resolve_partial_json_data_nodeid(const char *nodeid, const char *llist_value, struct lyd_node *start, int options, |
| 1647 | int *parsed) |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1648 | { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1649 | char *module_name = ly_buf(), *buf_backup = NULL, *str; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1650 | const char *id, *mod_name, *name; |
| 1651 | int r, ret, mod_name_len, nam_len, is_relative = -1, has_predicate, last_parsed; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 1652 | struct lyd_node *sibling, *last_match = NULL; |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 1653 | struct lyd_node_leaf_list *llist; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1654 | const struct lys_module *prefix_mod, *prev_mod; |
| 1655 | struct ly_ctx *ctx; |
| 1656 | |
| 1657 | assert(nodeid && start && parsed); |
| 1658 | |
| 1659 | ctx = start->schema->module->ctx; |
| 1660 | id = nodeid; |
| 1661 | |
| 1662 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate)) < 1) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1663 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 1664 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1665 | return NULL; |
| 1666 | } |
| 1667 | id += r; |
| 1668 | /* add it to parsed only after the data node was actually found */ |
| 1669 | last_parsed = r; |
| 1670 | |
| 1671 | if (is_relative) { |
| 1672 | prev_mod = start->schema->module; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1673 | start = start->child; |
| 1674 | } else { |
| 1675 | for (; start->parent; start = start->parent); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1676 | prev_mod = start->schema->module; |
| 1677 | } |
| 1678 | |
| 1679 | while (1) { |
| 1680 | LY_TREE_FOR(start, sibling) { |
Michal Vasko | 2411b94 | 2016-03-23 13:50:03 +0100 | [diff] [blame] | 1681 | /* RPC data check, return simply invalid argument, because the data tree is invalid */ |
| 1682 | if (lys_parent(sibling->schema)) { |
| 1683 | if (options & LYD_PATH_OPT_OUTPUT) { |
| 1684 | if (lys_parent(sibling->schema)->nodetype == LYS_INPUT) { |
Michal Vasko | b724689 | 2016-04-19 10:37:35 +0200 | [diff] [blame] | 1685 | LOGERR(LY_EINVAL, "Provided data tree includes some RPC input nodes (%s).", sibling->schema->name); |
Michal Vasko | 2411b94 | 2016-03-23 13:50:03 +0100 | [diff] [blame] | 1686 | *parsed = -1; |
| 1687 | return NULL; |
| 1688 | } |
| 1689 | } else { |
| 1690 | if (lys_parent(sibling->schema)->nodetype == LYS_OUTPUT) { |
Michal Vasko | b724689 | 2016-04-19 10:37:35 +0200 | [diff] [blame] | 1691 | LOGERR(LY_EINVAL, "Provided data tree includes some RPC output nodes (%s).", sibling->schema->name); |
Michal Vasko | 2411b94 | 2016-03-23 13:50:03 +0100 | [diff] [blame] | 1692 | *parsed = -1; |
| 1693 | return NULL; |
| 1694 | } |
| 1695 | } |
| 1696 | } |
| 1697 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1698 | /* name match */ |
| 1699 | if (!strncmp(name, sibling->schema->name, nam_len) && !sibling->schema->name[nam_len]) { |
| 1700 | |
| 1701 | /* module check */ |
| 1702 | if (mod_name) { |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1703 | if (mod_name_len > LY_BUF_SIZE - 1) { |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1704 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 1705 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1706 | return NULL; |
| 1707 | } |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1708 | |
| 1709 | if (ly_buf_used && module_name[0]) { |
| 1710 | buf_backup = strndup(module_name, LY_BUF_SIZE - 1); |
| 1711 | } |
| 1712 | ly_buf_used++; |
| 1713 | |
Michal Vasko | df3f1ab | 2016-04-01 15:07:22 +0200 | [diff] [blame] | 1714 | memmove(module_name, mod_name, mod_name_len); |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1715 | module_name[mod_name_len] = '\0'; |
| 1716 | /* will also find an augment module */ |
| 1717 | prefix_mod = ly_ctx_get_module(ctx, module_name, NULL); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1718 | |
| 1719 | if (buf_backup) { |
| 1720 | /* return previous internal buffer content */ |
Michal Vasko | 888a129 | 2016-04-05 12:08:54 +0200 | [diff] [blame] | 1721 | strncpy(module_name, buf_backup, LY_BUF_SIZE - 1); |
Michal Vasko | 971a3ca | 2016-04-01 13:09:29 +0200 | [diff] [blame] | 1722 | free(buf_backup); |
| 1723 | buf_backup = NULL; |
| 1724 | } |
| 1725 | ly_buf_used--; |
| 1726 | |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1727 | if (!prefix_mod) { |
Michal Vasko | 10728b5 | 2016-04-07 14:26:29 +0200 | [diff] [blame] | 1728 | str = strndup(nodeid, (mod_name + mod_name_len) - nodeid); |
| 1729 | LOGVAL(LYE_PATH_INMOD, LY_VLOG_STR, str); |
| 1730 | free(str); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 1731 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1732 | return NULL; |
| 1733 | } |
| 1734 | } else { |
| 1735 | prefix_mod = prev_mod; |
| 1736 | } |
| 1737 | if (prefix_mod != lys_node_module(sibling->schema)) { |
| 1738 | continue; |
| 1739 | } |
| 1740 | |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 1741 | /* leaf-list, did we find it with the correct value or not? */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1742 | if (sibling->schema->nodetype == LYS_LEAFLIST) { |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 1743 | llist = (struct lyd_node_leaf_list *)sibling; |
| 1744 | if ((!llist_value && llist->value_str && llist->value_str[0]) |
| 1745 | || (llist_value && strcmp(llist_value, llist->value_str))) { |
| 1746 | continue; |
| 1747 | } |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1748 | } |
| 1749 | |
Michal Vasko | 13eb4ac | 2016-04-06 12:19:37 +0200 | [diff] [blame] | 1750 | /* list, we need predicates'n'stuff then */ |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1751 | if (sibling->schema->nodetype == LYS_LIST) { |
| 1752 | r = 0; |
| 1753 | if (!has_predicate) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1754 | LOGVAL(LYE_PATH_MISSKEY, LY_VLOG_NONE, NULL, name); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 1755 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1756 | return NULL; |
| 1757 | } |
| 1758 | ret = resolve_partial_json_data_list_predicate(id, name, sibling, &r); |
| 1759 | if (ret == -1) { |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 1760 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1761 | return NULL; |
| 1762 | } else if (ret == 1) { |
| 1763 | /* this list instance does not match */ |
| 1764 | continue; |
| 1765 | } |
| 1766 | id += r; |
| 1767 | last_parsed += r; |
| 1768 | } |
| 1769 | |
| 1770 | *parsed += last_parsed; |
| 1771 | |
| 1772 | /* the result node? */ |
| 1773 | if (!id[0]) { |
| 1774 | return sibling; |
| 1775 | } |
| 1776 | |
| 1777 | /* move down the tree, if possible */ |
Michal Vasko | c6c52cf | 2016-03-29 11:53:04 +0200 | [diff] [blame] | 1778 | if (sibling->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1779 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[0], id); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 1780 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1781 | return NULL; |
| 1782 | } |
Michal Vasko | c6c52cf | 2016-03-29 11:53:04 +0200 | [diff] [blame] | 1783 | last_match = sibling; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1784 | start = sibling->child; |
| 1785 | if (start) { |
| 1786 | prev_mod = start->schema->module; |
| 1787 | } |
| 1788 | break; |
| 1789 | } |
| 1790 | } |
| 1791 | |
| 1792 | /* no match, return last match */ |
| 1793 | if (!sibling) { |
| 1794 | return last_match; |
| 1795 | } |
| 1796 | |
| 1797 | if ((r = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative, &has_predicate)) < 1) { |
Michal Vasko | 43c300e | 2016-03-22 12:54:27 +0100 | [diff] [blame] | 1798 | LOGVAL(LYE_PATH_INCHAR, LY_VLOG_NONE, NULL, id[-r], &id[-r]); |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 1799 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1800 | return NULL; |
| 1801 | } |
| 1802 | id += r; |
| 1803 | last_parsed = r; |
| 1804 | } |
| 1805 | |
| 1806 | /* cannot get here */ |
| 1807 | LOGINT; |
Michal Vasko | 238bd2f | 2016-03-23 09:39:01 +0100 | [diff] [blame] | 1808 | *parsed = -1; |
Michal Vasko | 22448d3 | 2016-03-16 13:17:29 +0100 | [diff] [blame] | 1809 | return NULL; |
| 1810 | } |
| 1811 | |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 1812 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1813 | * @brief Resolves length or range intervals. Does not log. |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 1814 | * Syntax is assumed to be correct, *ret MUST be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1815 | * |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 1816 | * @param[in] str_restr Restriction as a string. |
| 1817 | * @param[in] type Type of the restriction. |
| 1818 | * @param[out] ret Final interval structure that starts with |
| 1819 | * the interval of the initial type, continues with intervals |
| 1820 | * of any superior types derived from the initial one, and |
| 1821 | * finishes with intervals from our \p type. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1822 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1823 | * @return EXIT_SUCCESS on succes, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1824 | */ |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1825 | int |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 1826 | resolve_len_ran_interval(const char *str_restr, struct lys_type *type, struct len_ran_intv **ret) |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1827 | { |
| 1828 | /* 0 - unsigned, 1 - signed, 2 - floating point */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 1829 | int kind; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1830 | int64_t local_smin, local_smax; |
| 1831 | uint64_t local_umin, local_umax; |
| 1832 | long double local_fmin, local_fmax; |
| 1833 | const char *seg_ptr, *ptr; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 1834 | struct len_ran_intv *local_intv = NULL, *tmp_local_intv = NULL, *tmp_intv, *intv = NULL; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1835 | |
| 1836 | switch (type->base) { |
| 1837 | case LY_TYPE_BINARY: |
| 1838 | kind = 0; |
| 1839 | local_umin = 0; |
| 1840 | local_umax = 18446744073709551615UL; |
| 1841 | |
| 1842 | if (!str_restr && type->info.binary.length) { |
| 1843 | str_restr = type->info.binary.length->expr; |
| 1844 | } |
| 1845 | break; |
| 1846 | case LY_TYPE_DEC64: |
| 1847 | kind = 2; |
| 1848 | local_fmin = -9223372036854775808.0; |
| 1849 | local_fmin /= 1 << type->info.dec64.dig; |
| 1850 | local_fmax = 9223372036854775807.0; |
| 1851 | local_fmax /= 1 << type->info.dec64.dig; |
| 1852 | |
| 1853 | if (!str_restr && type->info.dec64.range) { |
| 1854 | str_restr = type->info.dec64.range->expr; |
| 1855 | } |
| 1856 | break; |
| 1857 | case LY_TYPE_INT8: |
| 1858 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 1859 | local_smin = __INT64_C(-128); |
| 1860 | local_smax = __INT64_C(127); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1861 | |
| 1862 | if (!str_restr && type->info.num.range) { |
| 1863 | str_restr = type->info.num.range->expr; |
| 1864 | } |
| 1865 | break; |
| 1866 | case LY_TYPE_INT16: |
| 1867 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 1868 | local_smin = __INT64_C(-32768); |
| 1869 | local_smax = __INT64_C(32767); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1870 | |
| 1871 | if (!str_restr && type->info.num.range) { |
| 1872 | str_restr = type->info.num.range->expr; |
| 1873 | } |
| 1874 | break; |
| 1875 | case LY_TYPE_INT32: |
| 1876 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 1877 | local_smin = __INT64_C(-2147483648); |
| 1878 | local_smax = __INT64_C(2147483647); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1879 | |
| 1880 | if (!str_restr && type->info.num.range) { |
| 1881 | str_restr = type->info.num.range->expr; |
| 1882 | } |
| 1883 | break; |
| 1884 | case LY_TYPE_INT64: |
| 1885 | kind = 1; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 1886 | local_smin = __INT64_C(-9223372036854775807) - __INT64_C(1); |
| 1887 | local_smax = __INT64_C(9223372036854775807); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1888 | |
| 1889 | if (!str_restr && type->info.num.range) { |
| 1890 | str_restr = type->info.num.range->expr; |
| 1891 | } |
| 1892 | break; |
| 1893 | case LY_TYPE_UINT8: |
| 1894 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 1895 | local_umin = __UINT64_C(0); |
| 1896 | local_umax = __UINT64_C(255); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1897 | |
| 1898 | if (!str_restr && type->info.num.range) { |
| 1899 | str_restr = type->info.num.range->expr; |
| 1900 | } |
| 1901 | break; |
| 1902 | case LY_TYPE_UINT16: |
| 1903 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 1904 | local_umin = __UINT64_C(0); |
| 1905 | local_umax = __UINT64_C(65535); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1906 | |
| 1907 | if (!str_restr && type->info.num.range) { |
| 1908 | str_restr = type->info.num.range->expr; |
| 1909 | } |
| 1910 | break; |
| 1911 | case LY_TYPE_UINT32: |
| 1912 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 1913 | local_umin = __UINT64_C(0); |
| 1914 | local_umax = __UINT64_C(4294967295); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1915 | |
| 1916 | if (!str_restr && type->info.num.range) { |
| 1917 | str_restr = type->info.num.range->expr; |
| 1918 | } |
| 1919 | break; |
| 1920 | case LY_TYPE_UINT64: |
| 1921 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 1922 | local_umin = __UINT64_C(0); |
| 1923 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1924 | |
| 1925 | if (!str_restr && type->info.num.range) { |
| 1926 | str_restr = type->info.num.range->expr; |
| 1927 | } |
| 1928 | break; |
| 1929 | case LY_TYPE_STRING: |
| 1930 | kind = 0; |
Radek Krejci | f56577b | 2015-10-29 14:05:25 +0100 | [diff] [blame] | 1931 | local_umin = __UINT64_C(0); |
| 1932 | local_umax = __UINT64_C(18446744073709551615); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1933 | |
| 1934 | if (!str_restr && type->info.str.length) { |
| 1935 | str_restr = type->info.str.length->expr; |
| 1936 | } |
| 1937 | break; |
| 1938 | default: |
| 1939 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1940 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1941 | } |
| 1942 | |
| 1943 | /* process superior types */ |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 1944 | if (type->der) { |
| 1945 | if (resolve_len_ran_interval(NULL, &type->der->type, &intv)) { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 1946 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1947 | return -1; |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 1948 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1949 | assert(!intv || (intv->kind == kind)); |
| 1950 | } |
| 1951 | |
| 1952 | if (!str_restr) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 1953 | /* we do not have any restriction, return superior ones */ |
| 1954 | *ret = intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1955 | return EXIT_SUCCESS; |
| 1956 | } |
| 1957 | |
| 1958 | /* adjust local min and max */ |
| 1959 | if (intv) { |
| 1960 | tmp_intv = intv; |
| 1961 | |
| 1962 | if (kind == 0) { |
| 1963 | local_umin = tmp_intv->value.uval.min; |
| 1964 | } else if (kind == 1) { |
| 1965 | local_smin = tmp_intv->value.sval.min; |
| 1966 | } else if (kind == 2) { |
| 1967 | local_fmin = tmp_intv->value.fval.min; |
| 1968 | } |
| 1969 | |
| 1970 | while (tmp_intv->next) { |
| 1971 | tmp_intv = tmp_intv->next; |
| 1972 | } |
| 1973 | |
| 1974 | if (kind == 0) { |
| 1975 | local_umax = tmp_intv->value.uval.max; |
| 1976 | } else if (kind == 1) { |
| 1977 | local_smax = tmp_intv->value.sval.max; |
| 1978 | } else if (kind == 2) { |
| 1979 | local_fmax = tmp_intv->value.fval.max; |
| 1980 | } |
| 1981 | } |
| 1982 | |
| 1983 | /* finally parse our restriction */ |
| 1984 | seg_ptr = str_restr; |
| 1985 | while (1) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 1986 | if (!tmp_local_intv) { |
| 1987 | assert(!local_intv); |
| 1988 | local_intv = malloc(sizeof *local_intv); |
| 1989 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1990 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 1991 | tmp_local_intv->next = malloc(sizeof *tmp_local_intv); |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1992 | tmp_local_intv = tmp_local_intv->next; |
| 1993 | } |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1994 | if (!tmp_local_intv) { |
| 1995 | LOGMEM; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 1996 | goto error; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 1997 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1998 | |
| 1999 | tmp_local_intv->kind = kind; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2000 | tmp_local_intv->type = type; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2001 | tmp_local_intv->next = NULL; |
| 2002 | |
| 2003 | /* min */ |
| 2004 | ptr = seg_ptr; |
| 2005 | while (isspace(ptr[0])) { |
| 2006 | ++ptr; |
| 2007 | } |
| 2008 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2009 | if (kind == 0) { |
| 2010 | tmp_local_intv->value.uval.min = atoll(ptr); |
| 2011 | } else if (kind == 1) { |
| 2012 | tmp_local_intv->value.sval.min = atoll(ptr); |
| 2013 | } else if (kind == 2) { |
| 2014 | tmp_local_intv->value.fval.min = atoll(ptr); |
| 2015 | } |
| 2016 | |
| 2017 | if ((ptr[0] == '+') || (ptr[0] == '-')) { |
| 2018 | ++ptr; |
| 2019 | } |
| 2020 | while (isdigit(ptr[0])) { |
| 2021 | ++ptr; |
| 2022 | } |
| 2023 | } else if (!strncmp(ptr, "min", 3)) { |
| 2024 | if (kind == 0) { |
| 2025 | tmp_local_intv->value.uval.min = local_umin; |
| 2026 | } else if (kind == 1) { |
| 2027 | tmp_local_intv->value.sval.min = local_smin; |
| 2028 | } else if (kind == 2) { |
| 2029 | tmp_local_intv->value.fval.min = local_fmin; |
| 2030 | } |
| 2031 | |
| 2032 | ptr += 3; |
| 2033 | } else if (!strncmp(ptr, "max", 3)) { |
| 2034 | if (kind == 0) { |
| 2035 | tmp_local_intv->value.uval.min = local_umax; |
| 2036 | } else if (kind == 1) { |
| 2037 | tmp_local_intv->value.sval.min = local_smax; |
| 2038 | } else if (kind == 2) { |
| 2039 | tmp_local_intv->value.fval.min = local_fmax; |
| 2040 | } |
| 2041 | |
| 2042 | ptr += 3; |
| 2043 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2044 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2045 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2046 | } |
| 2047 | |
| 2048 | while (isspace(ptr[0])) { |
| 2049 | ptr++; |
| 2050 | } |
| 2051 | |
| 2052 | /* no interval or interval */ |
| 2053 | if ((ptr[0] == '|') || !ptr[0]) { |
| 2054 | if (kind == 0) { |
| 2055 | tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min; |
| 2056 | } else if (kind == 1) { |
| 2057 | tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min; |
| 2058 | } else if (kind == 2) { |
| 2059 | tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min; |
| 2060 | } |
| 2061 | } else if (!strncmp(ptr, "..", 2)) { |
| 2062 | /* skip ".." */ |
| 2063 | ptr += 2; |
| 2064 | while (isspace(ptr[0])) { |
| 2065 | ++ptr; |
| 2066 | } |
| 2067 | |
| 2068 | /* max */ |
| 2069 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 2070 | if (kind == 0) { |
| 2071 | tmp_local_intv->value.uval.max = atoll(ptr); |
| 2072 | } else if (kind == 1) { |
| 2073 | tmp_local_intv->value.sval.max = atoll(ptr); |
| 2074 | } else if (kind == 2) { |
| 2075 | tmp_local_intv->value.fval.max = atoll(ptr); |
| 2076 | } |
| 2077 | } else if (!strncmp(ptr, "max", 3)) { |
| 2078 | if (kind == 0) { |
| 2079 | tmp_local_intv->value.uval.max = local_umax; |
| 2080 | } else if (kind == 1) { |
| 2081 | tmp_local_intv->value.sval.max = local_smax; |
| 2082 | } else if (kind == 2) { |
| 2083 | tmp_local_intv->value.fval.max = local_fmax; |
| 2084 | } |
| 2085 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2086 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2087 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2088 | } |
| 2089 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 2090 | LOGINT; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2091 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | /* next segment (next OR) */ |
| 2095 | seg_ptr = strchr(seg_ptr, '|'); |
| 2096 | if (!seg_ptr) { |
| 2097 | break; |
| 2098 | } |
| 2099 | seg_ptr++; |
| 2100 | } |
| 2101 | |
| 2102 | /* check local restrictions against superior ones */ |
| 2103 | if (intv) { |
| 2104 | tmp_intv = intv; |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2105 | tmp_local_intv = local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2106 | |
| 2107 | while (tmp_local_intv && tmp_intv) { |
| 2108 | /* reuse local variables */ |
| 2109 | if (kind == 0) { |
| 2110 | local_umin = tmp_local_intv->value.uval.min; |
| 2111 | local_umax = tmp_local_intv->value.uval.max; |
| 2112 | |
| 2113 | /* it must be in this interval */ |
| 2114 | if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) { |
| 2115 | /* this interval is covered, next one */ |
| 2116 | if (local_umax <= tmp_intv->value.uval.max) { |
| 2117 | tmp_local_intv = tmp_local_intv->next; |
| 2118 | continue; |
| 2119 | /* ascending order of restrictions -> fail */ |
| 2120 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2121 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2122 | } |
| 2123 | } |
| 2124 | } else if (kind == 1) { |
| 2125 | local_smin = tmp_local_intv->value.sval.min; |
| 2126 | local_smax = tmp_local_intv->value.sval.max; |
| 2127 | |
| 2128 | if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) { |
| 2129 | if (local_smax <= tmp_intv->value.sval.max) { |
| 2130 | tmp_local_intv = tmp_local_intv->next; |
| 2131 | continue; |
| 2132 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2133 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2134 | } |
| 2135 | } |
| 2136 | } else if (kind == 2) { |
| 2137 | local_fmin = tmp_local_intv->value.fval.min; |
| 2138 | local_fmax = tmp_local_intv->value.fval.max; |
| 2139 | |
| 2140 | if ((local_fmin >= tmp_intv->value.fval.min) && (local_fmin <= tmp_intv->value.fval.max)) { |
| 2141 | if (local_fmax <= tmp_intv->value.fval.max) { |
| 2142 | tmp_local_intv = tmp_local_intv->next; |
| 2143 | continue; |
| 2144 | } else { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2145 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2146 | } |
| 2147 | } |
| 2148 | } |
| 2149 | |
| 2150 | tmp_intv = tmp_intv->next; |
| 2151 | } |
| 2152 | |
| 2153 | /* some interval left uncovered -> fail */ |
| 2154 | if (tmp_local_intv) { |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2155 | goto error; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2156 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2157 | } |
| 2158 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2159 | /* append the local intervals to all the intervals of the superior types, return it all */ |
| 2160 | if (intv) { |
| 2161 | for (tmp_intv = intv; tmp_intv->next; tmp_intv = tmp_intv->next); |
| 2162 | tmp_intv->next = local_intv; |
| 2163 | } else { |
| 2164 | intv = local_intv; |
| 2165 | } |
| 2166 | *ret = intv; |
| 2167 | |
| 2168 | return EXIT_SUCCESS; |
| 2169 | |
| 2170 | error: |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2171 | while (intv) { |
| 2172 | tmp_intv = intv->next; |
| 2173 | free(intv); |
| 2174 | intv = tmp_intv; |
| 2175 | } |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2176 | while (local_intv) { |
| 2177 | tmp_local_intv = local_intv->next; |
| 2178 | free(local_intv); |
| 2179 | local_intv = tmp_local_intv; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2180 | } |
| 2181 | |
Michal Vasko | aeb5180 | 2016-04-11 10:58:47 +0200 | [diff] [blame] | 2182 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 2183 | } |
| 2184 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2185 | /** |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 2186 | * @brief Resolve a typedef, return only resolved typedefs if derived. Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2187 | * |
| 2188 | * @param[in] name Typedef name. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2189 | * @param[in] mod_name Typedef name module name. |
| 2190 | * @param[in] module Main module. |
| 2191 | * @param[in] parent Parent of the resolved type definition. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2192 | * @param[out] ret Pointer to the resolved typedef. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2193 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2194 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2195 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2196 | int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2197 | resolve_superior_type(const char *name, const char *mod_name, const struct lys_module *module, |
| 2198 | const struct lys_node *parent, struct lys_tpdf **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2199 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2200 | int i, j; |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2201 | struct lys_tpdf *tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2202 | int tpdf_size; |
| 2203 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2204 | if (!mod_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2205 | /* no prefix, try built-in types */ |
| 2206 | for (i = 1; i < LY_DATA_TYPE_COUNT; i++) { |
| 2207 | if (!strcmp(ly_types[i].def->name, name)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2208 | if (ret) { |
| 2209 | *ret = ly_types[i].def; |
| 2210 | } |
| 2211 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2212 | } |
| 2213 | } |
| 2214 | } else { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2215 | if (!strcmp(mod_name, module->name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2216 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2217 | mod_name = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2218 | } |
| 2219 | } |
| 2220 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2221 | if (!mod_name && parent) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2222 | /* search in local typedefs */ |
| 2223 | while (parent) { |
| 2224 | switch (parent->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2225 | case LYS_CONTAINER: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2226 | tpdf_size = ((struct lys_node_container *)parent)->tpdf_size; |
| 2227 | tpdf = ((struct lys_node_container *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2228 | break; |
| 2229 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2230 | case LYS_LIST: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2231 | tpdf_size = ((struct lys_node_list *)parent)->tpdf_size; |
| 2232 | tpdf = ((struct lys_node_list *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2233 | break; |
| 2234 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2235 | case LYS_GROUPING: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2236 | tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size; |
| 2237 | tpdf = ((struct lys_node_grp *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2238 | break; |
| 2239 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2240 | case LYS_RPC: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2241 | tpdf_size = ((struct lys_node_rpc *)parent)->tpdf_size; |
| 2242 | tpdf = ((struct lys_node_rpc *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2243 | break; |
| 2244 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2245 | case LYS_NOTIF: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2246 | tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size; |
| 2247 | tpdf = ((struct lys_node_notif *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2248 | break; |
| 2249 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2250 | case LYS_INPUT: |
| 2251 | case LYS_OUTPUT: |
Radek Krejci | 4608ada | 2015-08-05 16:04:37 +0200 | [diff] [blame] | 2252 | tpdf_size = ((struct lys_node_rpc_inout *)parent)->tpdf_size; |
| 2253 | tpdf = ((struct lys_node_rpc_inout *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2254 | break; |
| 2255 | |
| 2256 | default: |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2257 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2258 | continue; |
| 2259 | } |
| 2260 | |
| 2261 | for (i = 0; i < tpdf_size; i++) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 2262 | if (!strcmp(tpdf[i].name, name) && tpdf[i].type.base) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2263 | if (ret) { |
| 2264 | *ret = &tpdf[i]; |
| 2265 | } |
| 2266 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2267 | } |
| 2268 | } |
| 2269 | |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 2270 | parent = lys_parent(parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2271 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2272 | } else { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2273 | /* get module where to search */ |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 2274 | module = lys_get_import_module(module, NULL, 0, mod_name, 0); |
Michal Vasko | c935fff | 2015-08-17 14:02:06 +0200 | [diff] [blame] | 2275 | if (!module) { |
| 2276 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2277 | } |
| 2278 | } |
| 2279 | |
| 2280 | /* search in top level typedefs */ |
| 2281 | for (i = 0; i < module->tpdf_size; i++) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 2282 | if (!strcmp(module->tpdf[i].name, name) && module->tpdf[i].type.base) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2283 | if (ret) { |
| 2284 | *ret = &module->tpdf[i]; |
| 2285 | } |
| 2286 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2287 | } |
| 2288 | } |
| 2289 | |
| 2290 | /* search in submodules */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2291 | for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2292 | for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 2293 | if (!strcmp(module->inc[i].submodule->tpdf[j].name, name) && module->inc[i].submodule->tpdf[j].type.base) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2294 | if (ret) { |
| 2295 | *ret = &module->inc[i].submodule->tpdf[j]; |
| 2296 | } |
| 2297 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2298 | } |
| 2299 | } |
| 2300 | } |
| 2301 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2302 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2303 | } |
| 2304 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2305 | /** |
| 2306 | * @brief Check the default \p value of the \p type. Logs directly. |
| 2307 | * |
| 2308 | * @param[in] type Type definition to use. |
| 2309 | * @param[in] value Default value to check. |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 2310 | * @param[in] module Type module. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2311 | * |
| 2312 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 2313 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2314 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2315 | check_default(struct lys_type *type, const char *value, struct lys_module *module) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2316 | { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2317 | struct lyd_node_leaf_list node; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 2318 | int ret = EXIT_SUCCESS; |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2319 | |
| 2320 | /* dummy leaf */ |
Radek Krejci | 4fe36bd | 2016-03-17 16:47:16 +0100 | [diff] [blame] | 2321 | memset(&node, 0, sizeof node); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2322 | node.value_str = value; |
| 2323 | node.value_type = type->base; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 2324 | node.schema = calloc(1, sizeof (struct lys_node_leaf)); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2325 | if (!node.schema) { |
| 2326 | LOGMEM; |
| 2327 | return -1; |
| 2328 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2329 | node.schema->name = strdup("default"); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2330 | if (!node.schema->name) { |
| 2331 | LOGMEM; |
| 2332 | return -1; |
| 2333 | } |
Michal Vasko | 5682640 | 2016-03-02 11:11:37 +0100 | [diff] [blame] | 2334 | node.schema->module = module; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 2335 | memcpy(&((struct lys_node_leaf *)node.schema)->type, type, sizeof *type); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2336 | |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 2337 | if (type->base == LY_TYPE_LEAFREF) { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2338 | if (!type->info.lref.target) { |
| 2339 | ret = EXIT_FAILURE; |
| 2340 | goto finish; |
| 2341 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2342 | ret = check_default(&type->info.lref.target->type, value, module); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2343 | |
| 2344 | } else if ((type->base == LY_TYPE_INST) || (type->base == LY_TYPE_IDENT)) { |
| 2345 | /* it was converted to JSON format before, nothing else sensible we can do */ |
| 2346 | |
| 2347 | } else { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 2348 | ret = lyp_parse_value(&node, NULL, 1); |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2349 | } |
| 2350 | |
| 2351 | finish: |
| 2352 | if (node.value_type == LY_TYPE_BITS) { |
| 2353 | free(node.value.bit); |
| 2354 | } |
| 2355 | free((char *)node.schema->name); |
| 2356 | free(node.schema); |
| 2357 | |
| 2358 | return ret; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2359 | } |
| 2360 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2361 | /** |
| 2362 | * @brief Check a key for mandatory attributes. Logs directly. |
| 2363 | * |
| 2364 | * @param[in] key The key to check. |
| 2365 | * @param[in] flags What flags to check. |
| 2366 | * @param[in] list The list of all the keys. |
| 2367 | * @param[in] index Index of the key in the key list. |
| 2368 | * @param[in] name The name of the keys. |
| 2369 | * @param[in] len The name length. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2370 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2371 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2372 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2373 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2374 | check_key(struct lys_node_list *list, int index, const char *name, int len) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2375 | { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 2376 | struct lys_node_leaf *key = list->keys[index]; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2377 | char *dup = NULL; |
| 2378 | int j; |
| 2379 | |
| 2380 | /* existence */ |
| 2381 | if (!key) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2382 | if (name[len] != '\0') { |
| 2383 | dup = strdup(name); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2384 | if (!dup) { |
| 2385 | LOGMEM; |
| 2386 | return -1; |
| 2387 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2388 | dup[len] = '\0'; |
| 2389 | name = dup; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2390 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2391 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, list, name); |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 2392 | free(dup); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2393 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2394 | } |
| 2395 | |
| 2396 | /* uniqueness */ |
| 2397 | for (j = index - 1; j >= 0; j--) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 2398 | if (key == list->keys[j]) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2399 | LOGVAL(LYE_KEY_DUP, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2400 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2401 | } |
| 2402 | } |
| 2403 | |
| 2404 | /* key is a leaf */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2405 | if (key->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2406 | LOGVAL(LYE_KEY_NLEAF, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2407 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2408 | } |
| 2409 | |
| 2410 | /* type of the leaf is not built-in empty */ |
| 2411 | if (key->type.base == LY_TYPE_EMPTY) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2412 | LOGVAL(LYE_KEY_TYPE, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2413 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2414 | } |
| 2415 | |
| 2416 | /* config attribute is the same as of the list */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 2417 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2418 | LOGVAL(LYE_KEY_CONFIG, LY_VLOG_LYS, list, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2419 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2420 | } |
| 2421 | |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 2422 | /* key is not placed from augment */ |
| 2423 | if (key->parent->nodetype == LYS_AUGMENT) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2424 | LOGVAL(LYE_KEY_MISS, LY_VLOG_LYS, key, key->name); |
| 2425 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, key, "Key inserted from augment."); |
Radek Krejci | 55e2cdc | 2016-03-11 13:51:09 +0100 | [diff] [blame] | 2426 | return -1; |
| 2427 | } |
| 2428 | |
Michal Vasko | cca4784 | 2016-03-17 10:31:07 +0100 | [diff] [blame] | 2429 | /* key is not when-conditional */ |
| 2430 | if (key->when) { |
Radek Krejci | 02a0499 | 2016-03-17 16:06:37 +0100 | [diff] [blame] | 2431 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, key, "when", "leaf"); |
| 2432 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, key, "Key definition cannot depend on a \"when\" condition."); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2433 | return -1; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2434 | } |
| 2435 | |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 2436 | return EXIT_SUCCESS; |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2437 | } |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2438 | |
| 2439 | /** |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2440 | * @brief Resolve (test the target exists) unique. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2441 | * |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2442 | * @param[in] parent The parent node of the unique structure. |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 2443 | * @param[in] uniq_str_path One path from the unique string. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2444 | * |
| 2445 | * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error. |
| 2446 | */ |
| 2447 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2448 | resolve_unique(struct lys_node *parent, const char *uniq_str_path) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2449 | { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2450 | int rc; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2451 | const struct lys_node *leaf = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2452 | |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 2453 | rc = resolve_descendant_schema_nodeid(uniq_str_path, parent->child, LYS_LEAF, 1, 1, &leaf); |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 2454 | if (rc || !leaf) { |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 2455 | if (rc) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2456 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 2457 | if (rc > 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2458 | LOGVAL(LYE_INCHAR, LY_VLOG_LYS, parent, uniq_str_path[rc - 1], &uniq_str_path[rc - 1]); |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 2459 | } else if (rc == -2) { |
Michal Vasko | c66c6d8 | 2016-04-12 11:37:31 +0200 | [diff] [blame] | 2460 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Unique argument references list."); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2461 | } |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 2462 | rc = -1; |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 2463 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2464 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 2465 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Target leaf not found."); |
Michal Vasko | 0b85aa8 | 2016-03-07 14:37:43 +0100 | [diff] [blame] | 2466 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2467 | } |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2468 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2469 | } |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 2470 | if (leaf->nodetype != LYS_LEAF) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2471 | LOGVAL(LYE_INARG, LY_VLOG_LYS, parent, uniq_str_path, "unique"); |
| 2472 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, parent, "Target is not a leaf."); |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2473 | rc = -1; |
| 2474 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2475 | } |
| 2476 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 2477 | /* check status */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2478 | if (lyp_check_status(parent->flags, parent->module, parent->name, leaf->flags, leaf->module, leaf->name, leaf)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 2479 | return -1; |
| 2480 | } |
| 2481 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 2482 | /* set leaf's unique flag */ |
| 2483 | ((struct lys_node_leaf *)leaf)->flags |= LYS_UNIQUE; |
| 2484 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2485 | return EXIT_SUCCESS; |
| 2486 | |
| 2487 | error: |
| 2488 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2489 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2490 | } |
| 2491 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2492 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2493 | * @brief Resolve (find) a feature definition. Logs directly. |
| 2494 | * |
| 2495 | * @param[in] name Feature name. |
| 2496 | * @param[in] module Module to search in. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2497 | * @param[out] ret Pointer to the resolved feature. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2498 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2499 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2500 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2501 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2502 | resolve_feature(const char *id, const struct lys_module *module, struct lys_feature **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2503 | { |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 2504 | const char *mod_name, *name; |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 2505 | int mod_name_len, nam_len, i, j; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 2506 | struct lys_node *node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2507 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2508 | assert(id); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2509 | assert(module); |
| 2510 | |
| 2511 | /* check prefix */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 2512 | if ((i = parse_node_identifier(id, &mod_name, &mod_name_len, &name, &nam_len)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2513 | LOGVAL(LYE_INCHAR, LY_VLOG_NONE, NULL, id[-i], &id[-i]); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2514 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2515 | } |
| 2516 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2517 | module = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len); |
| 2518 | if (!module) { |
| 2519 | /* identity refers unknown data model */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2520 | LOGVAL(LYE_INMOD_LEN, LY_VLOG_NONE, NULL, mod_name_len, mod_name); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2521 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2522 | } |
| 2523 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2524 | /* search in the identified module ... */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2525 | for (j = 0; j < module->features_size; j++) { |
| 2526 | if (!strcmp(name, module->features[j].name)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2527 | if (ret) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 2528 | /* check status */ |
| 2529 | node = (struct lys_node *)*ret; |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 2530 | if (lyp_check_status(node->flags, node->module, node->name, module->features[j].flags, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2531 | module->features[j].module, module->features[j].name, node)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 2532 | return -1; |
| 2533 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2534 | *ret = &module->features[j]; |
| 2535 | } |
| 2536 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2537 | } |
| 2538 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2539 | /* ... and all its submodules */ |
Michal Vasko | 27ab822 | 2016-02-12 09:33:52 +0100 | [diff] [blame] | 2540 | for (i = 0; i < module->inc_size; i++) { |
| 2541 | if (!module->inc[i].submodule) { |
| 2542 | /* not yet resolved */ |
| 2543 | continue; |
| 2544 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2545 | for (j = 0; j < module->inc[i].submodule->features_size; j++) { |
| 2546 | if (!strcmp(name, module->inc[i].submodule->features[j].name)) { |
| 2547 | if (ret) { |
| 2548 | /* check status */ |
| 2549 | node = (struct lys_node *)*ret; |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 2550 | if (lyp_check_status(node->flags, node->module, node->name, |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2551 | module->inc[i].submodule->features[j].flags, |
| 2552 | module->inc[i].submodule->features[j].module, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2553 | module->inc[i].submodule->features[j].name, node)) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 2554 | return -1; |
| 2555 | } |
| 2556 | *ret = &(module->inc[i].submodule->features[j]); |
| 2557 | } |
| 2558 | return EXIT_SUCCESS; |
| 2559 | } |
| 2560 | } |
| 2561 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2562 | |
| 2563 | /* not found */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2564 | LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, "feature", id); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2565 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2566 | } |
| 2567 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 2568 | void |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2569 | unres_data_del(struct unres_data *unres, uint32_t i) |
| 2570 | { |
| 2571 | /* there are items after the one deleted */ |
| 2572 | if (i+1 < unres->count) { |
| 2573 | /* we only move the data, memory is left allocated, why bother */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2574 | memmove(&unres->node[i], &unres->node[i+1], (unres->count-(i+1)) * sizeof *unres->node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2575 | |
| 2576 | /* deleting the last item */ |
| 2577 | } else if (i == 0) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2578 | free(unres->node); |
| 2579 | unres->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2580 | } |
| 2581 | |
| 2582 | /* if there are no items after and it is not the last one, just move the counter */ |
| 2583 | --unres->count; |
| 2584 | } |
| 2585 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2586 | /** |
| 2587 | * @brief Resolve (find) a data node from a specific module. Does not log. |
| 2588 | * |
| 2589 | * @param[in] mod Module to search in. |
| 2590 | * @param[in] name Name of the data node. |
| 2591 | * @param[in] nam_len Length of the name. |
| 2592 | * @param[in] start Data node to start the search from. |
| 2593 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 2594 | * they are replaced (!!) with the resolvents. |
| 2595 | * |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 2596 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2597 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2598 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2599 | resolve_data(const struct lys_module *mod, const char *name, int nam_len, struct lyd_node *start, struct unres_data *parents) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2600 | { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2601 | struct lyd_node *node; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2602 | int flag; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2603 | uint32_t i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2604 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2605 | if (!parents->count) { |
| 2606 | parents->count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2607 | parents->node = malloc(sizeof *parents->node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2608 | if (!parents->node) { |
| 2609 | LOGMEM; |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 2610 | return -1; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2611 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2612 | parents->node[0] = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2613 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2614 | for (i = 0; i < parents->count;) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2615 | if (parents->node[i] && (parents->node[i]->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML))) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2616 | /* skip */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2617 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2618 | continue; |
| 2619 | } |
| 2620 | flag = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2621 | LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2622 | if (node->schema->module == mod && !strncmp(node->schema->name, name, nam_len) |
| 2623 | && node->schema->name[nam_len] == '\0') { |
| 2624 | /* matching target */ |
| 2625 | if (!flag) { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 2626 | /* put node instead of the current parent */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2627 | parents->node[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2628 | flag = 1; |
| 2629 | } else { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 2630 | /* multiple matching, so create a new node */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2631 | ++parents->count; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2632 | parents->node = ly_realloc(parents->node, parents->count * sizeof *parents->node); |
| 2633 | if (!parents->node) { |
| 2634 | return EXIT_FAILURE; |
| 2635 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2636 | parents->node[parents->count-1] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2637 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2638 | } |
| 2639 | } |
| 2640 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2641 | |
| 2642 | if (!flag) { |
| 2643 | /* remove item from the parents list */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2644 | unres_data_del(parents, i); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2645 | } else { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2646 | ++i; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2647 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2648 | } |
| 2649 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2650 | return parents->count ? EXIT_SUCCESS : EXIT_FAILURE; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2651 | } |
| 2652 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2653 | /** |
| 2654 | * @brief Resolve (find) a data node. Does not log. |
| 2655 | * |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2656 | * @param[in] mod_name Module name of the data node. |
| 2657 | * @param[in] mod_name_len Length of the module name. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2658 | * @param[in] name Name of the data node. |
| 2659 | * @param[in] nam_len Length of the name. |
| 2660 | * @param[in] start Data node to start the search from. |
| 2661 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 2662 | * they are replaced (!!) with the resolvents. |
| 2663 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2664 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2665 | */ |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2666 | static int |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2667 | resolve_data_node(const char *mod_name, int mod_name_len, const char *name, int name_len, struct lyd_node *start, |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2668 | struct unres_data *parents) |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2669 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2670 | const struct lys_module *mod; |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 2671 | char *str; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2672 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2673 | assert(start); |
| 2674 | |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 2675 | if (mod_name) { |
| 2676 | /* we have mod_name, find appropriate module */ |
| 2677 | str = strndup(mod_name, mod_name_len); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2678 | if (!str) { |
| 2679 | LOGMEM; |
| 2680 | return -1; |
| 2681 | } |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 2682 | mod = ly_ctx_get_module(start->schema->module->ctx, str, NULL); |
| 2683 | free(str); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2684 | if (!mod) { |
| 2685 | /* invalid prefix */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2686 | return -1; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2687 | } |
| 2688 | } else { |
| 2689 | /* no prefix, module is the same as of current node */ |
| 2690 | mod = start->schema->module; |
| 2691 | } |
| 2692 | |
| 2693 | return resolve_data(mod, name, name_len, start, parents); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2694 | } |
| 2695 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2696 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 2697 | * @brief Resolve a path predicate (leafref) in JSON data context. Logs directly |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2698 | * only specific errors, general no-resolvent error is left to the caller. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2699 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 2700 | * @param[in] pred Predicate to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 2701 | * @param[in] node Node from which the predicate is being resolved |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2702 | * @param[in,out] node_match Nodes satisfying the restriction |
| 2703 | * without the predicate. Nodes not |
| 2704 | * satisfying the predicate are removed. |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2705 | * @param[out] parsed Number of characters parsed, negative on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2706 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2707 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2708 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2709 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2710 | resolve_path_predicate_data(const char *pred, struct lyd_node *node, struct unres_data *node_match, |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 2711 | int *parsed) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2712 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2713 | /* ... /node[source = destination] ... */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2714 | struct unres_data source_match, dest_match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2715 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2716 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, parsed_loc = 0, pke_parsed = 0; |
| 2717 | int has_predicate, dest_parent_times, i, rc; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2718 | uint32_t j; |
| 2719 | |
| 2720 | source_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2721 | source_match.node = malloc(sizeof *source_match.node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2722 | if (!source_match.node) { |
| 2723 | LOGMEM; |
| 2724 | return -1; |
| 2725 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2726 | dest_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2727 | dest_match.node = malloc(sizeof *dest_match.node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2728 | if (!dest_match.node) { |
| 2729 | LOGMEM; |
| 2730 | return -1; |
| 2731 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2732 | |
| 2733 | do { |
| 2734 | if ((i = parse_path_predicate(pred, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr, |
| 2735 | &pke_len, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2736 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, pred[-i], &pred[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2737 | rc = -1; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2738 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2739 | } |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2740 | parsed_loc += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2741 | pred += i; |
| 2742 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2743 | for (j = 0; j < node_match->count;) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2744 | /* source */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2745 | source_match.node[0] = node_match->node[j]; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2746 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2747 | /* must be leaf (key of a list) */ |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2748 | if ((rc = resolve_data_node(sour_pref, sour_pref_len, source, sour_len, node_match->node[j], |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2749 | &source_match)) || (source_match.count != 1) || (source_match.node[0]->schema->nodetype != LYS_LEAF)) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2750 | i = 0; |
| 2751 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2752 | } |
| 2753 | |
| 2754 | /* destination */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2755 | dest_match.node[0] = node_match->node[j]; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2756 | dest_parent_times = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2757 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 2758 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2759 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path_key_expr[-i], &path_key_expr[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2760 | rc = -1; |
| 2761 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2762 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2763 | pke_parsed = i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2764 | for (i = 0; i < dest_parent_times; ++i) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2765 | dest_match.node[0] = dest_match.node[0]->parent; |
| 2766 | if (!dest_match.node[0]) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2767 | i = 0; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2768 | rc = EXIT_FAILURE; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2769 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2770 | } |
| 2771 | } |
| 2772 | while (1) { |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2773 | if ((rc = resolve_data_node(dest_pref, dest_pref_len, dest, dest_len, dest_match.node[0], |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2774 | &dest_match)) || (dest_match.count != 1)) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2775 | i = 0; |
| 2776 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2777 | } |
| 2778 | |
| 2779 | if (pke_len == pke_parsed) { |
| 2780 | break; |
| 2781 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2782 | if ((i = parse_path_key_expr(path_key_expr+pke_parsed, &dest_pref, &dest_pref_len, &dest, &dest_len, |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2783 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2784 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path_key_expr[-i], &path_key_expr[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2785 | rc = -1; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2786 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2787 | } |
| 2788 | pke_parsed += i; |
| 2789 | } |
| 2790 | |
| 2791 | /* check match between source and destination nodes */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2792 | if (((struct lys_node_leaf *)source_match.node[0]->schema)->type.base |
| 2793 | != ((struct lys_node_leaf *)dest_match.node[0]->schema)->type.base) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2794 | goto remove_leafref; |
| 2795 | } |
| 2796 | |
Radek Krejci | c1ffa4d | 2016-02-17 13:11:11 +0100 | [diff] [blame] | 2797 | if (!ly_strequal(((struct lyd_node_leaf_list *)source_match.node[0])->value_str, |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 2798 | ((struct lyd_node_leaf_list *)dest_match.node[0])->value_str, 1)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2799 | goto remove_leafref; |
| 2800 | } |
| 2801 | |
| 2802 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2803 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2804 | continue; |
| 2805 | |
| 2806 | remove_leafref: |
| 2807 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2808 | unres_data_del(node_match, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2809 | } |
| 2810 | } while (has_predicate); |
| 2811 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2812 | free(source_match.node); |
| 2813 | free(dest_match.node); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2814 | if (parsed) { |
| 2815 | *parsed = parsed_loc; |
| 2816 | } |
| 2817 | return EXIT_SUCCESS; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2818 | |
| 2819 | error: |
| 2820 | |
| 2821 | if (source_match.count) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2822 | free(source_match.node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2823 | } |
| 2824 | if (dest_match.count) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2825 | free(dest_match.node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2826 | } |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2827 | if (parsed) { |
| 2828 | *parsed = -parsed_loc+i; |
| 2829 | } |
| 2830 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2831 | } |
| 2832 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2833 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 2834 | * @brief Resolve a path (leafref) in JSON data context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2835 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2836 | * @param[in] node Leafref data node. |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2837 | * @param[in] path Path of the leafref. |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2838 | * @param[out] ret Matching nodes. Expects an empty, but allocated structure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2839 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2840 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2841 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2842 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2843 | resolve_path_arg_data(struct lyd_node *node, const char *path, struct unres_data *ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2844 | { |
Radek Krejci | 71b795b | 2015-08-10 16:20:39 +0200 | [diff] [blame] | 2845 | struct lyd_node *data = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2846 | const char *prefix, *name; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2847 | int pref_len, nam_len, has_predicate, parent_times, i, parsed, rc; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2848 | uint32_t j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2849 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2850 | assert(node && path && ret && !ret->count); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2851 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2852 | parent_times = 0; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 2853 | parsed = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2854 | |
| 2855 | /* searching for nodeset */ |
| 2856 | do { |
| 2857 | if ((i = parse_path_arg(path, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2858 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, path[-i], &path[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2859 | rc = -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2860 | goto error; |
| 2861 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2862 | path += i; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 2863 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2864 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2865 | if (!ret->count) { |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 2866 | if (parent_times != -1) { |
| 2867 | ret->count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2868 | ret->node = calloc(1, sizeof *ret->node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2869 | if (!ret->node) { |
| 2870 | LOGMEM; |
Radek Krejci | 5050173 | 2016-01-07 13:06:39 +0100 | [diff] [blame] | 2871 | rc = -1; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 2872 | goto error; |
| 2873 | } |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 2874 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2875 | for (i = 0; i < parent_times; ++i) { |
| 2876 | /* relative path */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2877 | if (!ret->count) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2878 | /* error, too many .. */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2879 | LOGVAL(LYE_INVAL, LY_VLOG_LYD, node, path, node->schema->name); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2880 | rc = -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2881 | goto error; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2882 | } else if (!ret->node[0]) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2883 | /* first .. */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2884 | data = ret->node[0] = node->parent; |
| 2885 | } else if (!ret->node[0]->parent) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2886 | /* we are in root */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2887 | ret->count = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2888 | free(ret->node); |
| 2889 | ret->node = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2890 | } else { |
| 2891 | /* multiple .. */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2892 | data = ret->node[0] = ret->node[0]->parent; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2893 | } |
| 2894 | } |
| 2895 | |
| 2896 | /* absolute path */ |
| 2897 | if (parent_times == -1) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2898 | for (data = node; data->parent; data = data->parent); |
Michal Vasko | db9323c | 2015-10-16 10:32:44 +0200 | [diff] [blame] | 2899 | /* we're still parsing it and the pointer is not correct yet */ |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 2900 | if (data->prev) { |
| 2901 | for (; data->prev->next; data = data->prev); |
| 2902 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2903 | } |
| 2904 | } |
| 2905 | |
| 2906 | /* node identifier */ |
Radek Krejci | 581ce77 | 2015-11-10 17:22:40 +0100 | [diff] [blame] | 2907 | if ((rc = resolve_data_node(prefix, pref_len, name, nam_len, data, ret))) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 2908 | if (rc == -1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2909 | LOGVAL(LYE_INELEM_LEN, LY_VLOG_LYD, node, nam_len, name); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2910 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2911 | goto error; |
| 2912 | } |
| 2913 | |
| 2914 | if (has_predicate) { |
| 2915 | /* we have predicate, so the current results must be lists */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2916 | for (j = 0; j < ret->count;) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2917 | if (ret->node[j]->schema->nodetype == LYS_LIST && |
| 2918 | ((struct lys_node_list *)ret->node[0]->schema)->keys) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2919 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2920 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2921 | continue; |
| 2922 | } |
| 2923 | |
| 2924 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2925 | unres_data_del(ret, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2926 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2927 | if ((rc = resolve_path_predicate_data(path, node, ret, &i))) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 2928 | if (rc == -1) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame^] | 2929 | LOGVAL(LYE_NORESOLV, LY_VLOG_LYD, node, "leafref", path); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2930 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2931 | goto error; |
| 2932 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2933 | path += i; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 2934 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2935 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2936 | if (!ret->count) { |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2937 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2938 | goto error; |
| 2939 | } |
| 2940 | } |
| 2941 | } while (path[0] != '\0'); |
| 2942 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2943 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2944 | |
| 2945 | error: |
| 2946 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2947 | free(ret->node); |
| 2948 | ret->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2949 | ret->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2950 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2951 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2952 | } |
| 2953 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2954 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 2955 | * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2956 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 2957 | * @param[in] path Path to use. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 2958 | * @param[in] context_node Predicate context node (where the predicate is placed). |
| 2959 | * @param[in] parent Path context node (where the path begins/is placed). |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2960 | * |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2961 | * @return 0 on forward reference, otherwise the number |
| 2962 | * of characters successfully parsed, |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2963 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2964 | */ |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2965 | static int |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 2966 | resolve_path_predicate_schema(const char *path, const struct lys_node *context_node, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2967 | struct lys_node *parent) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2968 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 2969 | const struct lys_node *src_node, *dst_node; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2970 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
| 2971 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, parsed = 0, pke_parsed = 0; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2972 | int has_predicate, dest_parent_times = 0, i, rc; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2973 | |
| 2974 | do { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2975 | if ((i = parse_path_predicate(path, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr, |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2976 | &pke_len, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2977 | LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, path[-i], path-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2978 | return -parsed+i; |
| 2979 | } |
| 2980 | parsed += i; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2981 | path += i; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2982 | |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 2983 | /* source (must be leaf) */ |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 2984 | if (!sour_pref) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 2985 | sour_pref = context_node->module->name; |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 2986 | } |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 2987 | rc = lys_get_sibling(context_node->child, sour_pref, sour_pref_len, source, sour_len, |
Michal Vasko | 165dc4a | 2015-10-23 09:44:27 +0200 | [diff] [blame] | 2988 | LYS_LEAF | LYS_AUGMENT, &src_node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2989 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame^] | 2990 | LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path-parsed); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2991 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2992 | } |
| 2993 | |
| 2994 | /* destination */ |
| 2995 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 2996 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 2997 | LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, path_key_expr[-i], path_key_expr-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2998 | return -parsed; |
| 2999 | } |
| 3000 | pke_parsed += i; |
| 3001 | |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3002 | /* parent is actually the parent of this leaf, so skip the first ".." */ |
| 3003 | for (i = 0, dst_node = parent; i < dest_parent_times; ++i) { |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3004 | if (!dst_node) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame^] | 3005 | LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path_key_expr); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3006 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3007 | } |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 3008 | dst_node = lys_parent(dst_node); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3009 | } |
| 3010 | while (1) { |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3011 | if (!dest_pref) { |
| 3012 | dest_pref = dst_node->module->name; |
| 3013 | } |
| 3014 | rc = lys_get_sibling(dst_node->child, dest_pref, dest_pref_len, dest, dest_len, |
Michal Vasko | 165dc4a | 2015-10-23 09:44:27 +0200 | [diff] [blame] | 3015 | LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_AUGMENT, &dst_node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3016 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame^] | 3017 | LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path_key_expr); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3018 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3019 | } |
| 3020 | |
| 3021 | if (pke_len == pke_parsed) { |
| 3022 | break; |
| 3023 | } |
| 3024 | |
| 3025 | if ((i = parse_path_key_expr(path_key_expr+pke_parsed, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 3026 | &dest_parent_times)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3027 | LOGVAL(LYE_INCHAR, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3028 | (path_key_expr+pke_parsed)[-i], (path_key_expr+pke_parsed)-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3029 | return -parsed; |
| 3030 | } |
| 3031 | pke_parsed += i; |
| 3032 | } |
| 3033 | |
| 3034 | /* check source - dest match */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3035 | if (dst_node->nodetype != LYS_LEAF) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame^] | 3036 | LOGVAL(LYE_NORESOLV, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, "leafref predicate", path-parsed); |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3037 | LOGVAL(LYE_SPEC, parent ? LY_VLOG_LYS : LY_VLOG_NONE, parent, |
| 3038 | "Destination node is not a leaf, but %s.", strnodetype(dst_node->nodetype)); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3039 | return -parsed; |
| 3040 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3041 | } while (has_predicate); |
| 3042 | |
| 3043 | return parsed; |
| 3044 | } |
| 3045 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3046 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3047 | * @brief Resolve a path (leafref) in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3048 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3049 | * @param[in] path Path to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3050 | * @param[in] parent_node Parent of the leafref. |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3051 | * @param[in] parent_tpdf Flag if the parent node is actually typedef, in that case the path |
| 3052 | * has to contain absolute path |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3053 | * @param[out] ret Pointer to the resolved schema node. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3054 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3055 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3056 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3057 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3058 | resolve_path_arg_schema(const char *path, struct lys_node *parent, int parent_tpdf, |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3059 | const struct lys_node **ret) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3060 | { |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3061 | const struct lys_node *node; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3062 | const struct lys_module *mod; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3063 | const char *id, *prefix, *name; |
| 3064 | int pref_len, nam_len, parent_times, has_predicate; |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3065 | int i, first_iter, rc; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3066 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3067 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3068 | parent_times = 0; |
| 3069 | id = path; |
| 3070 | |
| 3071 | do { |
| 3072 | if ((i = parse_path_arg(id, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3073 | LOGVAL(LYE_INCHAR, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, id[-i], &id[-i]); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3074 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3075 | } |
| 3076 | id += i; |
| 3077 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3078 | if (first_iter) { |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3079 | if (parent_times == -1) { |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3080 | /* resolve prefix of the module */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3081 | mod = lys_get_import_module(parent->module, NULL, 0, prefix, pref_len); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3082 | /* get start node */ |
| 3083 | node = mod ? mod->data : NULL; |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3084 | if (!node) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame^] | 3085 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, |
| 3086 | "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3087 | return EXIT_FAILURE; |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3088 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3089 | } else if (parent_times > 0) { |
Michal Vasko | 73ae256 | 2015-08-06 11:58:13 +0200 | [diff] [blame] | 3090 | /* node is the parent already, skip one ".." */ |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3091 | if (parent_tpdf) { |
| 3092 | /* the path is not allowed to contain relative path since we are in top level typedef */ |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame^] | 3093 | LOGVAL(LYE_NORESOLV, 0, NULL, "leafref", path); |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 3094 | return -1; |
| 3095 | } |
| 3096 | |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3097 | node = parent; |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3098 | i = 0; |
| 3099 | while (1) { |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3100 | if (!node) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame^] | 3101 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, |
| 3102 | "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3103 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3104 | } |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 3105 | |
| 3106 | /* this node is a wrong node, we actually need the augment target */ |
| 3107 | if (node->nodetype == LYS_AUGMENT) { |
| 3108 | node = ((struct lys_node_augment *)node)->target; |
| 3109 | if (!node) { |
| 3110 | continue; |
| 3111 | } |
| 3112 | } |
| 3113 | |
| 3114 | ++i; |
| 3115 | if (i == parent_times) { |
| 3116 | break; |
| 3117 | } |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 3118 | node = lys_parent(node); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3119 | } |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3120 | |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3121 | node = node->child; |
Michal Vasko | e01eca5 | 2015-08-13 14:42:02 +0200 | [diff] [blame] | 3122 | } else { |
| 3123 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3124 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3125 | } |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3126 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3127 | first_iter = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3128 | } else { |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 3129 | /* move down the tree, if possible */ |
| 3130 | if (node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) { |
Michal Vasko | 2f5aceb | 2016-03-22 10:24:14 +0100 | [diff] [blame] | 3131 | LOGVAL(LYE_INCHAR, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, name[0], name); |
Michal Vasko | 7dc71d0 | 2016-03-15 10:42:28 +0100 | [diff] [blame] | 3132 | return -1; |
| 3133 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3134 | node = node->child; |
| 3135 | } |
| 3136 | |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 3137 | if (!prefix) { |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3138 | prefix = lys_node_module(parent)->name; |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 3139 | } |
| 3140 | |
Michal Vasko | 36cbaa4 | 2015-12-14 13:15:48 +0100 | [diff] [blame] | 3141 | rc = lys_get_sibling(node, prefix, pref_len, name, nam_len, LYS_ANY & ~(LYS_USES | LYS_GROUPING), &node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3142 | if (rc) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame^] | 3143 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, "leafref", path); |
Michal Vasko | 7a55bea | 2016-05-02 14:51:20 +0200 | [diff] [blame] | 3144 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3145 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3146 | |
| 3147 | if (has_predicate) { |
| 3148 | /* we have predicate, so the current result must be list */ |
| 3149 | if (node->nodetype != LYS_LIST) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame^] | 3150 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3151 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3152 | } |
| 3153 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3154 | i = resolve_path_predicate_schema(id, node, parent); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3155 | if (!i) { |
Michal Vasko | f9664da | 2015-08-24 15:03:30 +0200 | [diff] [blame] | 3156 | return EXIT_FAILURE; |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3157 | } else if (i < 0) { |
| 3158 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3159 | } |
| 3160 | id += i; |
| 3161 | } |
| 3162 | } while (id[0]); |
| 3163 | |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 3164 | /* the target must be leaf or leaf-list */ |
| 3165 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 3e9f41e | 2016-05-20 11:41:39 +0200 | [diff] [blame^] | 3166 | LOGVAL(LYE_NORESOLV, parent_tpdf ? LY_VLOG_NONE : LY_VLOG_LYS, parent_tpdf ? NULL : parent, "leafref", path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3167 | return -1; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 3168 | } |
| 3169 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3170 | /* check status */ |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3171 | if (lyp_check_status(parent->flags, parent->module, parent->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3172 | node->flags, node->module, node->name, node)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3173 | return -1; |
| 3174 | } |
| 3175 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3176 | if (ret) { |
| 3177 | *ret = node; |
| 3178 | } |
| 3179 | return EXIT_SUCCESS; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 3180 | } |
| 3181 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3182 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3183 | * @brief Resolve instance-identifier predicate in JSON data format. |
| 3184 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3185 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3186 | * @param[in] pred Predicate to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3187 | * @param[in,out] node_match Nodes matching the restriction without |
| 3188 | * the predicate. Nodes not satisfying |
| 3189 | * the predicate are removed. |
| 3190 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3191 | * @return Number of characters successfully parsed, |
| 3192 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3193 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3194 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3195 | resolve_predicate(const char *pred, struct unres_data *node_match) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3196 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3197 | /* ... /node[target = value] ... */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3198 | struct unres_data target_match; |
| 3199 | struct ly_ctx *ctx; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3200 | const struct lys_module *mod; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3201 | const char *model, *name, *value; |
| 3202 | char *str; |
| 3203 | int mod_len, nam_len, val_len, i, has_predicate, cur_idx, idx, parsed; |
| 3204 | uint32_t j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3205 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3206 | assert(pred && node_match->count); |
| 3207 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3208 | ctx = node_match->node[0]->schema->module->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3209 | idx = -1; |
| 3210 | parsed = 0; |
| 3211 | |
| 3212 | do { |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3213 | if ((i = parse_predicate(pred, &model, &mod_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3214 | return -parsed+i; |
| 3215 | } |
| 3216 | parsed += i; |
| 3217 | pred += i; |
| 3218 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3219 | /* pos */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3220 | if (isdigit(name[0])) { |
| 3221 | idx = atoi(name); |
| 3222 | } |
| 3223 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3224 | for (cur_idx = 0, j = 0; j < node_match->count; ++cur_idx) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3225 | /* target */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3226 | memset(&target_match, 0, sizeof target_match); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3227 | if ((name[0] == '.') || !value) { |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3228 | target_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3229 | target_match.node = malloc(sizeof *target_match.node); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3230 | if (!target_match.node) { |
| 3231 | LOGMEM; |
| 3232 | return -1; |
| 3233 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3234 | target_match.node[0] = node_match->node[j]; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3235 | } else { |
| 3236 | str = strndup(model, mod_len); |
| 3237 | mod = ly_ctx_get_module(ctx, str, NULL); |
| 3238 | free(str); |
| 3239 | |
Radek Krejci | 804836a | 2016-02-03 10:39:55 +0100 | [diff] [blame] | 3240 | if (resolve_data(mod, name, nam_len, node_match->node[j]->child, &target_match)) { |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3241 | goto remove_instid; |
| 3242 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3243 | } |
| 3244 | |
| 3245 | /* check that we have the correct type */ |
| 3246 | if (name[0] == '.') { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3247 | if (node_match->node[j]->schema->nodetype != LYS_LEAFLIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3248 | goto remove_instid; |
| 3249 | } |
| 3250 | } else if (value) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3251 | if (node_match->node[j]->schema->nodetype != LYS_LIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3252 | goto remove_instid; |
| 3253 | } |
| 3254 | } |
| 3255 | |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 3256 | if ((value && (strncmp(((struct lyd_node_leaf_list *)target_match.node[0])->value_str, value, val_len) |
| 3257 | || ((struct lyd_node_leaf_list *)target_match.node[0])->value_str[val_len])) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3258 | || (!value && (idx != cur_idx))) { |
| 3259 | goto remove_instid; |
| 3260 | } |
| 3261 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3262 | free(target_match.node); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3263 | |
| 3264 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3265 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3266 | continue; |
| 3267 | |
| 3268 | remove_instid: |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3269 | free(target_match.node); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3270 | |
| 3271 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3272 | unres_data_del(node_match, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3273 | } |
| 3274 | } while (has_predicate); |
| 3275 | |
| 3276 | return parsed; |
| 3277 | } |
| 3278 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3279 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3280 | * @brief Resolve instance-identifier in JSON data format. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3281 | * |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3282 | * @param[in] data Data node where the path is used |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3283 | * @param[in] path Instance-identifier node value. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3284 | * |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3285 | * @return Matching node or NULL if no such a node exists. If error occurs, NULL is returned and ly_errno is set. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3286 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3287 | static struct lyd_node * |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3288 | resolve_instid(struct lyd_node *data, const char *path) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3289 | { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3290 | int i = 0, j; |
| 3291 | struct lyd_node *result = NULL; |
Michal Vasko | bea08e9 | 2016-05-18 13:28:08 +0200 | [diff] [blame] | 3292 | const struct lys_module *mod, *prev_mod; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3293 | struct ly_ctx *ctx = data->schema->module->ctx; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3294 | const char *model, *name; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3295 | char *str; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3296 | int mod_len, name_len, has_predicate; |
| 3297 | struct unres_data node_match; |
| 3298 | uint32_t k; |
| 3299 | |
| 3300 | memset(&node_match, 0, sizeof node_match); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3301 | |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3302 | /* we need root to resolve absolute path */ |
| 3303 | for (; data->parent; data = data->parent); |
Michal Vasko | db9323c | 2015-10-16 10:32:44 +0200 | [diff] [blame] | 3304 | /* we're still parsing it and the pointer is not correct yet */ |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3305 | if (data->prev) { |
| 3306 | for (; data->prev->next; data = data->prev); |
| 3307 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3308 | |
Michal Vasko | bea08e9 | 2016-05-18 13:28:08 +0200 | [diff] [blame] | 3309 | prev_mod = lyd_node_module(data); |
| 3310 | |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3311 | /* search for the instance node */ |
| 3312 | while (path[i]) { |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3313 | j = parse_instance_identifier(&path[i], &model, &mod_len, &name, &name_len, &has_predicate); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3314 | if (j <= 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3315 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, data, path[i-j], &path[i-j]); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3316 | goto error; |
| 3317 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3318 | i += j; |
Michal Vasko | b387c48 | 2015-08-12 09:32:59 +0200 | [diff] [blame] | 3319 | |
Michal Vasko | bea08e9 | 2016-05-18 13:28:08 +0200 | [diff] [blame] | 3320 | if (model) { |
| 3321 | str = strndup(model, mod_len); |
| 3322 | if (!str) { |
| 3323 | LOGMEM; |
| 3324 | goto error; |
| 3325 | } |
| 3326 | mod = ly_ctx_get_module(ctx, str, NULL); |
| 3327 | free(str); |
| 3328 | } else { |
| 3329 | mod = prev_mod; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3330 | } |
| 3331 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3332 | if (resolve_data(mod, name, name_len, data, &node_match)) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3333 | /* no instance exists */ |
| 3334 | return NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3335 | } |
| 3336 | |
| 3337 | if (has_predicate) { |
| 3338 | /* we have predicate, so the current results must be list or leaf-list */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3339 | for (k = 0; k < node_match.count;) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3340 | if ((node_match.node[k]->schema->nodetype == LYS_LIST && |
| 3341 | ((struct lys_node_list *)node_match.node[k]->schema)->keys) |
| 3342 | || (node_match.node[k]->schema->nodetype == LYS_LEAFLIST)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3343 | /* instid is ok, continue check with next instid */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3344 | ++k; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3345 | continue; |
| 3346 | } |
| 3347 | |
| 3348 | /* does not fulfill conditions, remove inst record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3349 | unres_data_del(&node_match, k); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3350 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3351 | |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3352 | j = resolve_predicate(&path[i], &node_match); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3353 | if (j < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3354 | LOGVAL(LYE_INPRED, LY_VLOG_LYD, data, &path[i-j]); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3355 | goto error; |
| 3356 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3357 | i += j; |
Michal Vasko | b387c48 | 2015-08-12 09:32:59 +0200 | [diff] [blame] | 3358 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3359 | if (!node_match.count) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3360 | /* no instance exists */ |
| 3361 | return NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3362 | } |
| 3363 | } |
Michal Vasko | bea08e9 | 2016-05-18 13:28:08 +0200 | [diff] [blame] | 3364 | |
| 3365 | prev_mod = mod; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3366 | } |
| 3367 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 3368 | if (!node_match.count) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3369 | /* no instance exists */ |
| 3370 | return NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 3371 | } else if (node_match.count > 1) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3372 | /* instance identifier must resolve to a single node */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3373 | LOGVAL(LYE_TOOMANY, LY_VLOG_LYD, data, path, "data tree"); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3374 | |
Michal Vasko | d6adbaa | 2016-04-11 11:01:09 +0200 | [diff] [blame] | 3375 | goto error; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3376 | } else { |
| 3377 | /* we have required result, remember it and cleanup */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3378 | result = node_match.node[0]; |
| 3379 | free(node_match.node); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3380 | |
| 3381 | return result; |
| 3382 | } |
| 3383 | |
| 3384 | error: |
| 3385 | |
| 3386 | /* cleanup */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3387 | free(node_match.node); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 3388 | |
| 3389 | return NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3390 | } |
| 3391 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3392 | /** |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 3393 | * @brief Passes config flag down to children, skips nodes without config flags. |
| 3394 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3395 | * |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 3396 | * @param[in] node Siblings and their children to have flags changed. |
| 3397 | * @param[in] flags Flags to assign to all the nodes. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3398 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3399 | static void |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 3400 | inherit_config_flag(struct lys_node *node, int flags) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3401 | { |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 3402 | assert(!(flags ^ (flags & LYS_CONFIG_MASK))); |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3403 | LY_TREE_FOR(node, node) { |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 3404 | if (node->flags & LYS_CONFIG_SET) { |
| 3405 | /* skip nodes with an explicit config value */ |
| 3406 | continue; |
| 3407 | } |
| 3408 | if (!(node->nodetype & (LYS_USES | LYS_GROUPING))) { |
| 3409 | node->flags |= flags; |
| 3410 | } |
| 3411 | inherit_config_flag(node->child, flags); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3412 | } |
| 3413 | } |
| 3414 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3415 | /** |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 3416 | * @brief Resolve augment target. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3417 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3418 | * @param[in] aug Augment to use. |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 3419 | * @param[in] siblings Nodes where to start the search in. If set, uses augment, if not, standalone augment. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3420 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3421 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3422 | */ |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 3423 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3424 | resolve_augment(struct lys_node_augment *aug, struct lys_node *siblings) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3425 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3426 | int rc; |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 3427 | struct lys_node *sub; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3428 | |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 3429 | assert(aug); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3430 | |
| 3431 | /* resolve target node */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 3432 | rc = resolve_augment_schema_nodeid(aug->target_name, siblings, (siblings ? NULL : aug->module), (const struct lys_node **)&aug->target); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 3433 | if (rc == -1) { |
| 3434 | return -1; |
| 3435 | } |
| 3436 | if (rc > 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3437 | LOGVAL(LYE_INCHAR, LY_VLOG_LYS, aug, aug->target_name[rc - 1], &aug->target_name[rc - 1]); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 3438 | return -1; |
| 3439 | } |
| 3440 | if (!aug->target) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3441 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, aug, "augment", aug->target_name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 3442 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3443 | } |
| 3444 | |
| 3445 | if (!aug->child) { |
| 3446 | /* nothing to do */ |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 3447 | LOGWRN("Augment \"%s\" without children.", aug->target_name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3448 | return EXIT_SUCCESS; |
| 3449 | } |
| 3450 | |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 3451 | /* check for mandatory nodes - if the target node is in another module |
| 3452 | * the added nodes cannot be mandatory |
| 3453 | */ |
| 3454 | if (!aug->parent && (lys_node_module((struct lys_node *)aug) != lys_node_module(aug->target)) |
| 3455 | && lyp_check_mandatory((struct lys_node *)aug)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3456 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, "mandatory", "augment node"); |
| 3457 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, aug, "When augmenting data in another module, mandatory nodes are not allowed."); |
Michal Vasko | d58d596 | 2016-03-02 14:29:41 +0100 | [diff] [blame] | 3458 | return -1; |
| 3459 | } |
| 3460 | |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 3461 | /* check augment target type and then augment nodes type */ |
| 3462 | if (aug->target->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_CASE | LYS_INPUT | LYS_OUTPUT | LYS_NOTIF)) { |
| 3463 | LY_TREE_FOR(aug->child, sub) { |
| 3464 | if (!(sub->nodetype & (LYS_ANYXML | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST | LYS_USES | LYS_CHOICE))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3465 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 3466 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, aug, "Cannot augment \"%s\" with a \"%s\".", |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 3467 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
| 3468 | return -1; |
| 3469 | } |
| 3470 | } |
| 3471 | } else if (aug->target->nodetype == LYS_CHOICE) { |
| 3472 | LY_TREE_FOR(aug->child, sub) { |
| 3473 | if (!(sub->nodetype & (LYS_CASE | LYS_ANYXML | LYS_CONTAINER | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3474 | LOGVAL(LYE_INCHILDSTMT, LY_VLOG_LYS, aug, strnodetype(sub->nodetype), "augment"); |
| 3475 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, aug, "Cannot augment \"%s\" with a \"%s\".", |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 3476 | strnodetype(aug->target->nodetype), strnodetype(sub->nodetype)); |
| 3477 | return -1; |
| 3478 | } |
| 3479 | } |
| 3480 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3481 | LOGVAL(LYE_INARG, LY_VLOG_LYS, aug, aug->target_name, "target-node"); |
| 3482 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, aug, "Invalid augment target node type \"%s\".", strnodetype(aug->target->nodetype)); |
Michal Vasko | 07e89ef | 2016-03-03 13:28:57 +0100 | [diff] [blame] | 3483 | return -1; |
| 3484 | } |
| 3485 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 3486 | /* inherit config information from actual parent */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3487 | LY_TREE_FOR(aug->child, sub) { |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 3488 | inherit_config_flag(sub, aug->target->flags & LYS_CONFIG_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3489 | } |
| 3490 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3491 | /* check identifier uniqueness as in lys_node_addchild() */ |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 3492 | LY_TREE_FOR(aug->child, sub) { |
Michal Vasko | 2afc1ca | 2016-05-03 11:38:53 +0200 | [diff] [blame] | 3493 | if (lys_check_id(sub, aug->target, NULL)) { |
Michal Vasko | 3e6665f | 2015-08-17 14:00:38 +0200 | [diff] [blame] | 3494 | return -1; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 3495 | } |
| 3496 | } |
Radek Krejci | 0acbe1b | 2015-08-04 09:33:49 +0200 | [diff] [blame] | 3497 | /* reconnect augmenting data into the target - add them to the target child list */ |
| 3498 | if (aug->target->child) { |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 3499 | sub = aug->target->child->prev; /* remember current target's last node */ |
| 3500 | sub->next = aug->child; /* connect augmenting data after target's last node */ |
Radek Krejci | 0acbe1b | 2015-08-04 09:33:49 +0200 | [diff] [blame] | 3501 | aug->target->child->prev = aug->child->prev; /* new target's last node is last augmenting node */ |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 3502 | aug->child->prev = sub; /* finish connecting of both child lists */ |
Radek Krejci | 0acbe1b | 2015-08-04 09:33:49 +0200 | [diff] [blame] | 3503 | } else { |
| 3504 | aug->target->child = aug->child; |
| 3505 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3506 | |
| 3507 | return EXIT_SUCCESS; |
| 3508 | } |
| 3509 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3510 | /** |
| 3511 | * @brief Resolve uses, apply augments, refines. Logs directly. |
| 3512 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3513 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3514 | * @param[in,out] unres List of unresolved items. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3515 | * |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 3516 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3517 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3518 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3519 | resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3520 | { |
| 3521 | struct ly_ctx *ctx; |
Radek Krejci | 989790c | 2016-04-14 17:49:41 +0200 | [diff] [blame] | 3522 | struct lys_node *node = NULL, *next, *iter; |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 3523 | struct lys_node *node_aux; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 3524 | struct lys_refine *rfn; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 3525 | struct lys_restr *must, **old_must; |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 3526 | int i, j, rc, parent_flags; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 3527 | uint8_t size, *old_size; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3528 | |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 3529 | assert(uses->grp); |
Michal Vasko | e770851 | 2016-03-11 10:26:55 +0100 | [diff] [blame] | 3530 | /* HACK just check that the grouping is resolved */ |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 3531 | assert(!uses->grp->nacm); |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 3532 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 3533 | if (!uses->grp->child) { |
| 3534 | /* grouping without children, warning was already displayed */ |
| 3535 | return EXIT_SUCCESS; |
| 3536 | } |
| 3537 | |
| 3538 | /* get proper parent (config) flags */ |
| 3539 | for (node_aux = lys_parent((struct lys_node *)uses); node_aux && (node_aux->nodetype == LYS_USES); node_aux = lys_parent(node_aux)); |
| 3540 | if (node_aux) { |
| 3541 | parent_flags = node_aux->flags & LYS_CONFIG_MASK; |
| 3542 | } else { |
| 3543 | /* default */ |
| 3544 | parent_flags = LYS_CONFIG_W; |
| 3545 | } |
| 3546 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3547 | /* copy the data nodes from grouping into the uses context */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3548 | LY_TREE_FOR(uses->grp->child, node_aux) { |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 3549 | node = lys_node_dup(uses->module, (struct lys_node *)uses, node_aux, 0, uses->nacm, unres, 0); |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3550 | if (!node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3551 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, uses->grp->name, "uses"); |
| 3552 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Copying data from grouping failed."); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3553 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3554 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3555 | } |
| 3556 | ctx = uses->module->ctx; |
| 3557 | |
Michal Vasko | aec34ea | 2016-05-19 15:21:40 +0200 | [diff] [blame] | 3558 | if (parent_flags) { |
| 3559 | assert(uses->child); |
| 3560 | inherit_config_flag(uses->child, parent_flags); |
| 3561 | } |
| 3562 | |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 3563 | /* we managed to copy the grouping, the rest must be possible to resolve */ |
| 3564 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3565 | /* apply refines */ |
| 3566 | for (i = 0; i < uses->refine_size; i++) { |
| 3567 | rfn = &uses->refine[i]; |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 3568 | rc = resolve_descendant_schema_nodeid(rfn->target_name, uses->child, LYS_NO_RPC_NOTIF_NODE, |
Radek Krejci | f3c71de | 2016-04-11 12:45:46 +0200 | [diff] [blame] | 3569 | 1, 0, (const struct lys_node **)&node); |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 3570 | if (rc || !node) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3571 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 3572 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3573 | } |
| 3574 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3575 | if (rfn->target_type && !(node->nodetype & rfn->target_type)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3576 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->target_name, "refine"); |
| 3577 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, "Refine substatements not applicable to the target-node."); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3578 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3579 | } |
| 3580 | |
| 3581 | /* description on any nodetype */ |
| 3582 | if (rfn->dsc) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3583 | lydict_remove(ctx, node->dsc); |
| 3584 | node->dsc = lydict_insert(ctx, rfn->dsc, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3585 | } |
| 3586 | |
| 3587 | /* reference on any nodetype */ |
| 3588 | if (rfn->ref) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3589 | lydict_remove(ctx, node->ref); |
| 3590 | node->ref = lydict_insert(ctx, rfn->ref, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3591 | } |
| 3592 | |
| 3593 | /* config on any nodetype */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 3594 | if (rfn->flags & LYS_CONFIG_MASK) { |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 3595 | if (lys_parent(node) && |
| 3596 | ((lys_parent(node)->flags & LYS_CONFIG_MASK) != (rfn->flags & LYS_CONFIG_MASK)) && |
Radek Krejci | 989790c | 2016-04-14 17:49:41 +0200 | [diff] [blame] | 3597 | (rfn->flags & LYS_CONFIG_W)) { |
| 3598 | /* setting config true under config false is prohibited */ |
| 3599 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
| 3600 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, |
| 3601 | "changing config from 'false' to 'true' is prohibited while " |
| 3602 | "the target's parent is still config 'false'."); |
| 3603 | return -1; |
| 3604 | } |
| 3605 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3606 | node->flags &= ~LYS_CONFIG_MASK; |
| 3607 | node->flags |= (rfn->flags & LYS_CONFIG_MASK); |
Radek Krejci | 989790c | 2016-04-14 17:49:41 +0200 | [diff] [blame] | 3608 | |
| 3609 | /* inherit config change to the target children */ |
| 3610 | LY_TREE_DFS_BEGIN(node->child, next, iter) { |
| 3611 | if (rfn->flags & LYS_CONFIG_W) { |
| 3612 | if (iter->flags & LYS_CONFIG_SET) { |
| 3613 | /* config is set explicitely, go to next sibling */ |
| 3614 | next = NULL; |
| 3615 | goto nextsibling; |
| 3616 | } |
| 3617 | } else { /* LYS_CONFIG_R */ |
| 3618 | if ((iter->flags & LYS_CONFIG_SET) && (iter->flags & LYS_CONFIG_W)) { |
| 3619 | /* error - we would have config data under status data */ |
| 3620 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, "config", "refine"); |
| 3621 | LOGVAL(LYE_SPEC, LY_VLOG_LYS, uses, |
| 3622 | "changing config from 'true' to 'false' is prohibited while the target " |
| 3623 | "has still a children with explicit config 'true'."); |
| 3624 | return -1; |
| 3625 | } |
| 3626 | } |
| 3627 | /* change config */ |
| 3628 | iter->flags &= ~LYS_CONFIG_MASK; |
| 3629 | iter->flags |= (rfn->flags & LYS_CONFIG_MASK); |
| 3630 | |
| 3631 | /* select next iter - modified LY_TREE_DFS_END */ |
| 3632 | if (iter->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYXML)) { |
| 3633 | next = NULL; |
| 3634 | } else { |
| 3635 | next = iter->child; |
| 3636 | } |
| 3637 | nextsibling: |
| 3638 | if (!next) { |
| 3639 | /* no children */ |
| 3640 | if (iter == node->child) { |
| 3641 | /* we are done, (START) has no children */ |
| 3642 | break; |
| 3643 | } |
| 3644 | /* try siblings */ |
| 3645 | next = iter->next; |
| 3646 | } |
| 3647 | while (!next) { |
| 3648 | /* parent is already processed, go to its sibling */ |
| 3649 | iter = lys_parent(iter); |
| 3650 | |
| 3651 | /* no siblings, go back through parents */ |
| 3652 | if (iter == node) { |
| 3653 | /* we are done, no next element to process */ |
| 3654 | break; |
| 3655 | } |
| 3656 | next = iter->next; |
| 3657 | } |
| 3658 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3659 | } |
| 3660 | |
| 3661 | /* default value ... */ |
| 3662 | if (rfn->mod.dflt) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3663 | if (node->nodetype == LYS_LEAF) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3664 | /* leaf */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3665 | lydict_remove(ctx, ((struct lys_node_leaf *)node)->dflt); |
| 3666 | ((struct lys_node_leaf *)node)->dflt = lydict_insert(ctx, rfn->mod.dflt, 0); |
| 3667 | } else if (node->nodetype == LYS_CHOICE) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3668 | /* choice */ |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 3669 | rc = resolve_choice_default_schema_nodeid(rfn->mod.dflt, node->child, |
| 3670 | (const struct lys_node **)&((struct lys_node_choice *)node)->dflt); |
Michal Vasko | 9bb061b | 2016-02-12 11:00:19 +0100 | [diff] [blame] | 3671 | if (rc || !((struct lys_node_choice *)node)->dflt) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3672 | LOGVAL(LYE_INARG, LY_VLOG_LYS, uses, rfn->mod.dflt, "default"); |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 3673 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3674 | } |
| 3675 | } |
| 3676 | } |
| 3677 | |
| 3678 | /* mandatory on leaf, anyxml or choice */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 3679 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3680 | if (node->nodetype & (LYS_LEAF | LYS_ANYXML | LYS_CHOICE)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3681 | /* remove current value */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3682 | node->flags &= ~LYS_MAND_MASK; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3683 | |
| 3684 | /* set new value */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3685 | node->flags |= (rfn->flags & LYS_MAND_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3686 | } |
| 3687 | } |
| 3688 | |
| 3689 | /* presence on container */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3690 | if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) { |
| 3691 | lydict_remove(ctx, ((struct lys_node_container *)node)->presence); |
| 3692 | ((struct lys_node_container *)node)->presence = lydict_insert(ctx, rfn->mod.presence, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3693 | } |
| 3694 | |
| 3695 | /* min/max-elements on list or leaf-list */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3696 | if (node->nodetype == LYS_LIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 3697 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3698 | ((struct lys_node_list *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3699 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 3700 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3701 | ((struct lys_node_list *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3702 | } |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3703 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 3704 | if (rfn->flags & LYS_RFN_MINSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3705 | ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3706 | } |
Radek Krejci | 0f04a6c | 2016-04-14 16:16:36 +0200 | [diff] [blame] | 3707 | if (rfn->flags & LYS_RFN_MAXSET) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3708 | ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3709 | } |
| 3710 | } |
| 3711 | |
| 3712 | /* must in leaf, leaf-list, list, container or anyxml */ |
| 3713 | if (rfn->must_size) { |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 3714 | switch (node->nodetype) { |
| 3715 | case LYS_LEAF: |
| 3716 | old_size = &((struct lys_node_leaf *)node)->must_size; |
| 3717 | old_must = &((struct lys_node_leaf *)node)->must; |
| 3718 | break; |
| 3719 | case LYS_LEAFLIST: |
| 3720 | old_size = &((struct lys_node_leaflist *)node)->must_size; |
| 3721 | old_must = &((struct lys_node_leaflist *)node)->must; |
| 3722 | break; |
| 3723 | case LYS_LIST: |
| 3724 | old_size = &((struct lys_node_list *)node)->must_size; |
| 3725 | old_must = &((struct lys_node_list *)node)->must; |
| 3726 | break; |
| 3727 | case LYS_CONTAINER: |
| 3728 | old_size = &((struct lys_node_container *)node)->must_size; |
| 3729 | old_must = &((struct lys_node_container *)node)->must; |
| 3730 | break; |
| 3731 | case LYS_ANYXML: |
| 3732 | old_size = &((struct lys_node_anyxml *)node)->must_size; |
| 3733 | old_must = &((struct lys_node_anyxml *)node)->must; |
| 3734 | break; |
| 3735 | default: |
| 3736 | LOGINT; |
Radek Krejci | e4e4d72 | 2015-10-05 16:53:50 +0200 | [diff] [blame] | 3737 | return -1; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 3738 | } |
| 3739 | |
| 3740 | size = *old_size + rfn->must_size; |
| 3741 | must = realloc(*old_must, size * sizeof *rfn->must); |
| 3742 | if (!must) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3743 | LOGMEM; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3744 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3745 | } |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 3746 | for (i = 0, j = *old_size; i < rfn->must_size; i++, j++) { |
| 3747 | must[j].expr = lydict_insert(ctx, rfn->must[i].expr, 0); |
| 3748 | must[j].dsc = lydict_insert(ctx, rfn->must[i].dsc, 0); |
| 3749 | must[j].ref = lydict_insert(ctx, rfn->must[i].ref, 0); |
| 3750 | must[j].eapptag = lydict_insert(ctx, rfn->must[i].eapptag, 0); |
| 3751 | must[j].emsg = lydict_insert(ctx, rfn->must[i].emsg, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3752 | } |
| 3753 | |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 3754 | *old_must = must; |
| 3755 | *old_size = size; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3756 | } |
| 3757 | } |
| 3758 | |
| 3759 | /* apply augments */ |
| 3760 | for (i = 0; i < uses->augment_size; i++) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3761 | rc = resolve_augment(&uses->augment[i], uses->child); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3762 | if (rc) { |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 3763 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3764 | } |
| 3765 | } |
| 3766 | |
| 3767 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3768 | } |
| 3769 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3770 | /** |
| 3771 | * @brief Resolve base identity recursively. Does not log. |
| 3772 | * |
| 3773 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3774 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3775 | * @param[in] basename Base name of the identity. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3776 | * @param[out] ret Pointer to the resolved identity. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3777 | * |
Michal Vasko | f200600 | 2016-04-21 16:28:15 +0200 | [diff] [blame] | 3778 | * @return EXIT_SUCCESS on success (but ret can still be NULL), EXIT_FAILURE on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3779 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3780 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3781 | resolve_base_ident_sub(const struct lys_module *module, struct lys_ident *ident, const char *basename, |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3782 | struct lys_ident **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3783 | { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3784 | uint32_t i, j; |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 3785 | struct lys_ident *base = NULL, *base_iter; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3786 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3787 | assert(ret); |
| 3788 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3789 | /* search module */ |
| 3790 | for (i = 0; i < module->ident_size; i++) { |
| 3791 | if (!strcmp(basename, module->ident[i].name)) { |
| 3792 | |
| 3793 | if (!ident) { |
| 3794 | /* just search for type, so do not modify anything, just return |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 3795 | * the base identity pointer */ |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3796 | *ret = &module->ident[i]; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3797 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3798 | } |
| 3799 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 3800 | base = &module->ident[i]; |
| 3801 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3802 | } |
| 3803 | } |
| 3804 | |
| 3805 | /* search submodules */ |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 3806 | for (j = 0; j < module->inc_size && module->inc[j].submodule; j++) { |
| 3807 | for (i = 0; i < module->inc[j].submodule->ident_size; i++) { |
| 3808 | if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3809 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 3810 | if (!ident) { |
| 3811 | *ret = &module->inc[j].submodule->ident[i]; |
| 3812 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3813 | } |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 3814 | |
| 3815 | base = &module->inc[j].submodule->ident[i]; |
| 3816 | goto matchfound; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3817 | } |
| 3818 | } |
| 3819 | } |
| 3820 | |
Radek Krejci | f2ac7ae | 2016-02-19 13:38:07 +0100 | [diff] [blame] | 3821 | matchfound: |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3822 | /* we found it somewhere */ |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 3823 | if (base) { |
| 3824 | /* check for circular reference */ |
| 3825 | for (base_iter = base; base_iter; base_iter = base_iter->base) { |
| 3826 | if (ident == base_iter) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3827 | LOGVAL(LYE_INARG, LY_VLOG_NONE, NULL, base_iter->name, "base"); |
| 3828 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Circular reference of \"%s\" identity.", basename); |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 3829 | return EXIT_FAILURE; |
| 3830 | } |
| 3831 | } |
| 3832 | /* checks done, store the result */ |
| 3833 | ident->base = base; |
| 3834 | |
| 3835 | /* maintain backlinks to the derived identitise */ |
| 3836 | while (base) { |
Radek Krejci | 1b61d0e | 2016-04-15 13:55:44 +0200 | [diff] [blame] | 3837 | /* 1. get current number of backlinks */ |
| 3838 | if (base->der) { |
| 3839 | for (i = 0; base->der[i]; i++); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3840 | } else { |
Radek Krejci | 1b61d0e | 2016-04-15 13:55:44 +0200 | [diff] [blame] | 3841 | i = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3842 | } |
Radek Krejci | 1b61d0e | 2016-04-15 13:55:44 +0200 | [diff] [blame] | 3843 | base->der = ly_realloc(base->der, (i + 2) * sizeof *(base->der)); |
| 3844 | if (!base->der) { |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 3845 | LOGMEM; |
| 3846 | return EXIT_FAILURE; |
| 3847 | } |
Radek Krejci | 1b61d0e | 2016-04-15 13:55:44 +0200 | [diff] [blame] | 3848 | base->der[i] = ident; |
| 3849 | base->der[i + 1] = NULL; /* array termination */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3850 | |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 3851 | base = base->base; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3852 | } |
Radek Krejci | 1b61d0e | 2016-04-15 13:55:44 +0200 | [diff] [blame] | 3853 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3854 | *ret = ident->base; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3855 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3856 | } |
| 3857 | |
Michal Vasko | f200600 | 2016-04-21 16:28:15 +0200 | [diff] [blame] | 3858 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3859 | } |
| 3860 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3861 | /** |
| 3862 | * @brief Resolve base identity. Logs directly. |
| 3863 | * |
| 3864 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3865 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3866 | * @param[in] basename Base name of the identity. |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 3867 | * @param[in] parent Either "type" or "identity". |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3868 | * @param[in,out] type Type structure where we want to resolve identity. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3869 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3870 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3871 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3872 | static int |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 3873 | resolve_base_ident(const struct lys_module *module, struct lys_ident *ident, const char *basename, const char* parent, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3874 | struct lys_type *type) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3875 | { |
| 3876 | const char *name; |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 3877 | int i, mod_name_len = 0; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3878 | struct lys_ident *target, **ret; |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 3879 | uint16_t flags; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3880 | struct lys_module *mod; |
| 3881 | |
| 3882 | assert((ident && !type) || (!ident && type)); |
| 3883 | |
| 3884 | if (!type) { |
| 3885 | /* have ident to resolve */ |
| 3886 | ret = ⌖ |
| 3887 | flags = ident->flags; |
| 3888 | mod = ident->module; |
| 3889 | } else { |
| 3890 | /* have type to fill */ |
| 3891 | ret = &type->info.ident.ref; |
| 3892 | flags = type->parent->flags; |
| 3893 | mod = type->parent->module; |
| 3894 | } |
Michal Vasko | f200600 | 2016-04-21 16:28:15 +0200 | [diff] [blame] | 3895 | *ret = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3896 | |
| 3897 | /* search for the base identity */ |
| 3898 | name = strchr(basename, ':'); |
| 3899 | if (name) { |
| 3900 | /* set name to correct position after colon */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 3901 | mod_name_len = name - basename; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3902 | name++; |
| 3903 | |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 3904 | if (!strncmp(basename, module->name, mod_name_len) && !module->name[mod_name_len]) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3905 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 3906 | mod_name_len = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3907 | } |
| 3908 | } else { |
| 3909 | name = basename; |
| 3910 | } |
| 3911 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3912 | /* get module where to search */ |
| 3913 | module = lys_get_import_module(module, NULL, 0, mod_name_len ? basename : NULL, mod_name_len); |
| 3914 | if (!module) { |
| 3915 | /* identity refers unknown data model */ |
Radek Krejci | 02a0499 | 2016-03-17 16:06:37 +0100 | [diff] [blame] | 3916 | LOGVAL(LYE_INMOD, LY_VLOG_NONE, NULL, basename); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3917 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3918 | } |
| 3919 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3920 | /* search in the identified module ... */ |
Michal Vasko | f200600 | 2016-04-21 16:28:15 +0200 | [diff] [blame] | 3921 | if (resolve_base_ident_sub(module, ident, name, ret)) { |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 3922 | return EXIT_FAILURE; |
Michal Vasko | f200600 | 2016-04-21 16:28:15 +0200 | [diff] [blame] | 3923 | } else if (*ret) { |
| 3924 | goto success; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3925 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3926 | /* and all its submodules */ |
| 3927 | for (i = 0; i < module->inc_size && module->inc[i].submodule; i++) { |
Michal Vasko | f200600 | 2016-04-21 16:28:15 +0200 | [diff] [blame] | 3928 | if (resolve_base_ident_sub((struct lys_module *)module->inc[i].submodule, ident, name, ret)) { |
Radek Krejci | babbff8 | 2016-02-19 13:31:37 +0100 | [diff] [blame] | 3929 | return EXIT_FAILURE; |
Michal Vasko | f200600 | 2016-04-21 16:28:15 +0200 | [diff] [blame] | 3930 | } else if (*ret) { |
| 3931 | goto success; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 3932 | } |
| 3933 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3934 | |
Michal Vasko | f200600 | 2016-04-21 16:28:15 +0200 | [diff] [blame] | 3935 | LOGVAL(LYE_INRESOLV, LY_VLOG_NONE, NULL, parent, basename); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3936 | return EXIT_FAILURE; |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3937 | |
| 3938 | success: |
| 3939 | /* check status */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3940 | if (lyp_check_status(flags, mod, ident ? ident->name : "of type", |
| 3941 | (*ret)->flags, (*ret)->module, (*ret)->name, NULL)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 3942 | return -1; |
| 3943 | } |
| 3944 | |
| 3945 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3946 | } |
| 3947 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3948 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3949 | * @brief Resolve JSON data format identityref. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3950 | * |
| 3951 | * @param[in] base Base identity. |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 3952 | * @param[in] ident_name Identityref name. |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 3953 | * @param[in] node Node where the identityref is being resolved |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3954 | * |
| 3955 | * @return Pointer to the identity resolvent, NULL on error. |
| 3956 | */ |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 3957 | struct lys_ident * |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3958 | resolve_identref(struct lys_ident *base, const char *ident_name, struct lyd_node *node) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3959 | { |
Michal Vasko | c633ca0 | 2015-08-21 14:03:51 +0200 | [diff] [blame] | 3960 | const char *mod_name, *name; |
| 3961 | int mod_name_len, rc; |
Radek Krejci | 1b61d0e | 2016-04-15 13:55:44 +0200 | [diff] [blame] | 3962 | int i; |
| 3963 | struct lys_ident *der; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3964 | |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 3965 | if (!base || !ident_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3966 | return NULL; |
| 3967 | } |
| 3968 | |
Michal Vasko | c633ca0 | 2015-08-21 14:03:51 +0200 | [diff] [blame] | 3969 | rc = parse_node_identifier(ident_name, &mod_name, &mod_name_len, &name, NULL); |
Michal Vasko | b43bb3c | 2016-03-17 15:00:27 +0100 | [diff] [blame] | 3970 | if (rc < 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3971 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, ident_name[-rc], &ident_name[-rc]); |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 3972 | return NULL; |
Michal Vasko | b43bb3c | 2016-03-17 15:00:27 +0100 | [diff] [blame] | 3973 | } else if (rc < (signed)strlen(ident_name)) { |
Radek Krejci | 02a0499 | 2016-03-17 16:06:37 +0100 | [diff] [blame] | 3974 | LOGVAL(LYE_INCHAR, LY_VLOG_LYD, node, ident_name[rc], &ident_name[rc]); |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 3975 | return NULL; |
| 3976 | } |
| 3977 | |
Michal Vasko | c633ca0 | 2015-08-21 14:03:51 +0200 | [diff] [blame] | 3978 | if (!strcmp(base->name, name) && (!mod_name |
| 3979 | || (!strncmp(base->module->name, mod_name, mod_name_len) && !base->module->name[mod_name_len]))) { |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 3980 | return base; |
| 3981 | } |
| 3982 | |
Radek Krejci | 1b61d0e | 2016-04-15 13:55:44 +0200 | [diff] [blame] | 3983 | if (base->der) { |
| 3984 | for (der = base->der[i = 0]; base->der[i]; der = base->der[++i]) { |
| 3985 | if (!strcmp(der->name, name) && |
| 3986 | (!mod_name || (!strncmp(der->module->name, mod_name, mod_name_len) && !der->module->name[mod_name_len]))) { |
| 3987 | /* we have match */ |
| 3988 | return der; |
| 3989 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3990 | } |
| 3991 | } |
| 3992 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 3993 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYD, node, "identityref", ident_name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3994 | return NULL; |
| 3995 | } |
| 3996 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3997 | /** |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 3998 | * @brief Resolve (find) choice default case. Does not log. |
| 3999 | * |
| 4000 | * @param[in] choic Choice to use. |
| 4001 | * @param[in] dflt Name of the default case. |
| 4002 | * |
| 4003 | * @return Pointer to the default node or NULL. |
| 4004 | */ |
| 4005 | static struct lys_node * |
| 4006 | resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt) |
| 4007 | { |
| 4008 | struct lys_node *child, *ret; |
| 4009 | |
| 4010 | LY_TREE_FOR(choic->child, child) { |
| 4011 | if (child->nodetype == LYS_USES) { |
| 4012 | ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt); |
| 4013 | if (ret) { |
| 4014 | return ret; |
| 4015 | } |
| 4016 | } |
| 4017 | |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 4018 | if (ly_strequal(child->name, dflt, 1) && (child->nodetype & (LYS_ANYXML | LYS_CASE |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 4019 | | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) { |
| 4020 | return child; |
| 4021 | } |
| 4022 | } |
| 4023 | |
| 4024 | return NULL; |
| 4025 | } |
| 4026 | |
| 4027 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4028 | * @brief Resolve unresolved uses. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4029 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4030 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4031 | * @param[in] unres Specific unres item. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4032 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4033 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4034 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4035 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4036 | resolve_unres_schema_uses(struct lys_node_uses *uses, struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4037 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4038 | int rc; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4039 | struct lys_node *par_grp; |
Michal Vasko | e91afce | 2015-08-12 12:21:00 +0200 | [diff] [blame] | 4040 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4041 | /* HACK: when a grouping has uses inside, all such uses have to be resolved before the grouping itself |
| 4042 | * is used in some uses. When we see such a uses, the grouping's nacm member (not used in grouping) |
| 4043 | * is used to store number of so far unresolved uses. The grouping cannot be used unless the nacm |
| 4044 | * value is decreased back to 0. To remember that the uses already increased grouping's nacm, the |
| 4045 | * LYS_USESGRP flag is used. */ |
Michal Vasko | dcf98e6 | 2016-05-05 17:53:53 +0200 | [diff] [blame] | 4046 | for (par_grp = lys_parent((struct lys_node *)uses); par_grp && (par_grp->nodetype != LYS_GROUPING); par_grp = lys_parent(par_grp)); |
Michal Vasko | e91afce | 2015-08-12 12:21:00 +0200 | [diff] [blame] | 4047 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4048 | if (!uses->grp) { |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4049 | rc = resolve_uses_schema_nodeid(uses->name, (const struct lys_node *)uses, (const struct lys_node_grp **)&uses->grp); |
| 4050 | if (rc == -1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4051 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, uses, "grouping", uses->name); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4052 | return -1; |
| 4053 | } else if (rc > 0) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4054 | LOGVAL(LYE_INCHAR, LY_VLOG_LYS, uses, uses->name[rc - 1], &uses->name[rc - 1]); |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4055 | return -1; |
| 4056 | } else if (!uses->grp) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4057 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 4058 | /* hack - in contrast to lys_node, lys_node_grp has bigger nacm field |
| 4059 | * (and smaller flags - it uses only a limited set of flags) |
| 4060 | */ |
| 4061 | ((struct lys_node_grp *)par_grp)->nacm++; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4062 | uses->flags |= LYS_USESGRP; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 4063 | } |
Michal Vasko | 3edeaf7 | 2016-02-11 13:17:43 +0100 | [diff] [blame] | 4064 | return EXIT_FAILURE; |
Michal Vasko | 12e3084 | 2015-08-04 11:54:00 +0200 | [diff] [blame] | 4065 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4066 | } |
| 4067 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4068 | if (uses->grp->nacm) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4069 | if (par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 4070 | ((struct lys_node_grp *)par_grp)->nacm++; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4071 | uses->flags |= LYS_USESGRP; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 4072 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4073 | return EXIT_FAILURE; |
| 4074 | } |
| 4075 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4076 | rc = resolve_uses(uses, unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4077 | if (!rc) { |
| 4078 | /* decrease unres count only if not first try */ |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4079 | if (par_grp && (uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 4080 | if (!((struct lys_node_grp *)par_grp)->nacm) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4081 | LOGINT; |
| 4082 | return -1; |
| 4083 | } |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 4084 | ((struct lys_node_grp *)par_grp)->nacm--; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4085 | uses->flags &= ~LYS_USESGRP; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4086 | } |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4087 | |
| 4088 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 4089 | if (lyp_check_status(uses->flags, uses->module, "of uses", |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 4090 | uses->grp->flags, uses->grp->module, uses->grp->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4091 | (struct lys_node *)uses)) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4092 | return -1; |
| 4093 | } |
| 4094 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4095 | return EXIT_SUCCESS; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4096 | } else if ((rc == EXIT_FAILURE) && par_grp && !(uses->flags & LYS_USESGRP)) { |
Radek Krejci | 4372b4e | 2016-04-14 17:42:16 +0200 | [diff] [blame] | 4097 | ((struct lys_node_grp *)par_grp)->nacm++; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4098 | uses->flags |= LYS_USESGRP; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4099 | } |
| 4100 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4101 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4102 | } |
| 4103 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4104 | /** |
Michal Vasko | 9957e59 | 2015-08-17 15:04:09 +0200 | [diff] [blame] | 4105 | * @brief Resolve list keys. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4106 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4107 | * @param[in] list List to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4108 | * @param[in] keys_str Keys node value. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4109 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4110 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 4111 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4112 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4113 | resolve_list_keys(struct lys_node_list *list, const char *keys_str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4114 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4115 | int i, len, rc; |
Michal Vasko | 4f0dad0 | 2016-02-15 14:08:23 +0100 | [diff] [blame] | 4116 | const char *value; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4117 | |
| 4118 | for (i = 0; i < list->keys_size; ++i) { |
| 4119 | /* get the key name */ |
| 4120 | if ((value = strpbrk(keys_str, " \t\n"))) { |
| 4121 | len = value - keys_str; |
| 4122 | while (isspace(value[0])) { |
| 4123 | value++; |
| 4124 | } |
| 4125 | } else { |
| 4126 | len = strlen(keys_str); |
| 4127 | } |
| 4128 | |
Radek Krejci | c428344 | 2016-04-22 09:19:27 +0200 | [diff] [blame] | 4129 | rc = lys_get_sibling(list->child, lys_main_module(list->module)->name, 0, keys_str, len, LYS_LEAF, (const struct lys_node **)&list->keys[i]); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4130 | if (rc) { |
Michal Vasko | 7a55bea | 2016-05-02 14:51:20 +0200 | [diff] [blame] | 4131 | LOGVAL(LYE_INRESOLV, LY_VLOG_LYS, list, "list keys", keys_str); |
| 4132 | return EXIT_FAILURE; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4133 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4134 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4135 | if (check_key(list, i, keys_str, len)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4136 | /* check_key logs */ |
| 4137 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4138 | } |
| 4139 | |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4140 | /* check status */ |
Radek Krejci | c655602 | 2016-01-27 15:16:45 +0100 | [diff] [blame] | 4141 | if (lyp_check_status(list->flags, list->module, list->name, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4142 | list->keys[i]->flags, list->keys[i]->module, list->keys[i]->name, |
| 4143 | (struct lys_node *)list->keys[i])) { |
Radek Krejci | cf50998 | 2015-12-15 09:22:44 +0100 | [diff] [blame] | 4144 | return -1; |
| 4145 | } |
| 4146 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4147 | /* prepare for next iteration */ |
| 4148 | while (value && isspace(value[0])) { |
| 4149 | value++; |
| 4150 | } |
| 4151 | keys_str = value; |
| 4152 | } |
| 4153 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4154 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4155 | } |
| 4156 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4157 | /** |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4158 | * @brief Resolve (check) all must conditions of \p node. |
| 4159 | * Logs directly. |
| 4160 | * |
| 4161 | * @param[in] node Data node with optional must statements. |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4162 | * |
| 4163 | * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error. |
| 4164 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4165 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4166 | resolve_must(struct lyd_node *node) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4167 | { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4168 | uint8_t i, must_size; |
| 4169 | struct lys_restr *must; |
| 4170 | struct lyxp_set set; |
| 4171 | |
| 4172 | assert(node); |
| 4173 | memset(&set, 0, sizeof set); |
| 4174 | |
| 4175 | switch (node->schema->nodetype) { |
| 4176 | case LYS_CONTAINER: |
| 4177 | must_size = ((struct lys_node_container *)node->schema)->must_size; |
| 4178 | must = ((struct lys_node_container *)node->schema)->must; |
| 4179 | break; |
| 4180 | case LYS_LEAF: |
| 4181 | must_size = ((struct lys_node_leaf *)node->schema)->must_size; |
| 4182 | must = ((struct lys_node_leaf *)node->schema)->must; |
| 4183 | break; |
| 4184 | case LYS_LEAFLIST: |
| 4185 | must_size = ((struct lys_node_leaflist *)node->schema)->must_size; |
| 4186 | must = ((struct lys_node_leaflist *)node->schema)->must; |
| 4187 | break; |
| 4188 | case LYS_LIST: |
| 4189 | must_size = ((struct lys_node_list *)node->schema)->must_size; |
| 4190 | must = ((struct lys_node_list *)node->schema)->must; |
| 4191 | break; |
| 4192 | case LYS_ANYXML: |
| 4193 | must_size = ((struct lys_node_anyxml *)node->schema)->must_size; |
| 4194 | must = ((struct lys_node_anyxml *)node->schema)->must; |
| 4195 | break; |
| 4196 | default: |
| 4197 | must_size = 0; |
| 4198 | break; |
| 4199 | } |
| 4200 | |
| 4201 | for (i = 0; i < must_size; ++i) { |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 4202 | if (lyxp_eval(must[i].expr, node, &set, LYXP_MUST)) { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4203 | return -1; |
| 4204 | } |
| 4205 | |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 4206 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, LYXP_MUST); |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4207 | |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 4208 | if (!set.val.bool) { |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 4209 | LOGVAL(LYE_NOMUST, LY_VLOG_LYD, node, must[i].expr); |
| 4210 | if (must[i].emsg) { |
| 4211 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, node, must[i].emsg); |
| 4212 | } |
| 4213 | if (must[i].eapptag) { |
| 4214 | strncpy(((struct ly_err *)&ly_errno)->apptag, must[i].eapptag, LY_APPTAG_LEN - 1); |
| 4215 | } |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4216 | return 1; |
| 4217 | } |
| 4218 | } |
| 4219 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4220 | return EXIT_SUCCESS; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4221 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4222 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4223 | /** |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4224 | * @brief Resolve (find) when condition context node. Does not log. |
| 4225 | * |
| 4226 | * @param[in] node Data node, whose conditional definition is being decided. |
| 4227 | * @param[in] schema Schema node with a when condition. |
| 4228 | * |
| 4229 | * @return Context node. |
| 4230 | */ |
| 4231 | static struct lyd_node * |
| 4232 | resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4233 | { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4234 | struct lyd_node *parent; |
| 4235 | struct lys_node *sparent; |
| 4236 | uint16_t i, data_depth, schema_depth; |
| 4237 | |
| 4238 | /* find a not schema-only node */ |
| 4239 | while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) { |
| 4240 | schema = lys_parent(schema); |
| 4241 | if (!schema) { |
| 4242 | return NULL; |
| 4243 | } |
| 4244 | } |
| 4245 | |
| 4246 | /* get node depths */ |
| 4247 | for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth); |
| 4248 | for (sparent = lys_parent(schema), schema_depth = 1; sparent; sparent = lys_parent(sparent)) { |
| 4249 | if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYXML | LYS_NOTIF | LYS_RPC)) { |
| 4250 | ++schema_depth; |
| 4251 | } |
| 4252 | } |
| 4253 | if (data_depth < schema_depth) { |
| 4254 | return NULL; |
| 4255 | } |
| 4256 | |
| 4257 | /* find the corresponding data node */ |
| 4258 | for (i = 0; i < data_depth - schema_depth; ++i) { |
| 4259 | node = node->parent; |
| 4260 | } |
| 4261 | if (node->schema != schema) { |
| 4262 | return NULL; |
| 4263 | } |
| 4264 | |
| 4265 | return node; |
| 4266 | } |
| 4267 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4268 | int |
Radek Krejci | 01696bf | 2016-03-18 13:19:36 +0100 | [diff] [blame] | 4269 | resolve_applies_must(const struct lyd_node *node) |
| 4270 | { |
| 4271 | switch (node->schema->nodetype) { |
| 4272 | case LYS_CONTAINER: |
| 4273 | return ((struct lys_node_container *)node->schema)->must_size; |
| 4274 | case LYS_LEAF: |
| 4275 | return ((struct lys_node_leaf *)node->schema)->must_size; |
| 4276 | case LYS_LEAFLIST: |
| 4277 | return ((struct lys_node_leaflist *)node->schema)->must_size; |
| 4278 | case LYS_LIST: |
| 4279 | return ((struct lys_node_list *)node->schema)->must_size; |
| 4280 | case LYS_ANYXML: |
| 4281 | return ((struct lys_node_anyxml *)node->schema)->must_size; |
| 4282 | default: |
| 4283 | return 0; |
| 4284 | } |
| 4285 | } |
| 4286 | |
| 4287 | int |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4288 | resolve_applies_when(const struct lyd_node *node) |
| 4289 | { |
| 4290 | struct lys_node *parent; |
| 4291 | |
| 4292 | assert(node); |
| 4293 | |
| 4294 | if (!(node->schema->nodetype & (LYS_NOTIF | LYS_RPC)) && (((struct lys_node_container *)node->schema)->when)) { |
| 4295 | return 1; |
| 4296 | } |
| 4297 | |
| 4298 | parent = node->schema; |
| 4299 | goto check_augment; |
| 4300 | |
| 4301 | while (parent && (parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 4302 | if (((struct lys_node_uses *)parent)->when) { |
| 4303 | return 1; |
| 4304 | } |
| 4305 | check_augment: |
| 4306 | |
| 4307 | if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) && |
| 4308 | (((struct lys_node_augment *)parent->parent)->when))) { |
| 4309 | |
| 4310 | } |
| 4311 | parent = lys_parent(parent); |
| 4312 | } |
| 4313 | |
| 4314 | return 0; |
| 4315 | } |
| 4316 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4317 | /** |
| 4318 | * @brief Resolve (check) all when conditions relevant for \p node. |
| 4319 | * Logs directly. |
| 4320 | * |
| 4321 | * @param[in] node Data node, whose conditional reference, if such, is being decided. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4322 | * |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4323 | * @return |
| 4324 | * -1 - error, ly_errno is set |
| 4325 | * 0 - true "when" statement |
| 4326 | * 0, ly_vecode = LYVE_NOCOND - false "when" statement |
| 4327 | * 1, ly_vecode = LYVE_INWHEN - nodes needed to resolve are conditional and not yet resolved (under another "when") |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4328 | */ |
| 4329 | static int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4330 | resolve_when(struct lyd_node *node) |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4331 | { |
| 4332 | struct lyd_node *ctx_node = NULL; |
| 4333 | struct lys_node *parent; |
| 4334 | struct lyxp_set set; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4335 | int rc = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4336 | |
| 4337 | assert(node); |
| 4338 | memset(&set, 0, sizeof set); |
| 4339 | |
| 4340 | if (!(node->schema->nodetype & (LYS_NOTIF | LYS_RPC)) && (((struct lys_node_container *)node->schema)->when)) { |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 4341 | rc = lyxp_eval(((struct lys_node_container *)node->schema)->when->cond, node, &set, LYXP_WHEN); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4342 | if (rc) { |
| 4343 | if (rc == 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4344 | LOGVAL(LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4345 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4346 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4347 | } |
| 4348 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4349 | /* set boolean result of the condition */ |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 4350 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node, LYXP_WHEN); |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 4351 | if (!set.val.bool) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4352 | ly_vlog_hide(1); |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 4353 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_container *)node->schema)->when->cond); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4354 | ly_vlog_hide(0); |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 4355 | node->when_status |= LYD_WHEN_FALSE; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4356 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4357 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4358 | |
| 4359 | /* free xpath set content */ |
| 4360 | lyxp_set_cast(&set, LYXP_SET_EMPTY, node, 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4361 | } |
| 4362 | |
| 4363 | parent = node->schema; |
| 4364 | goto check_augment; |
| 4365 | |
| 4366 | /* check when in every schema node that affects node */ |
| 4367 | while (parent && (parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 4368 | if (((struct lys_node_uses *)parent)->when) { |
| 4369 | if (!ctx_node) { |
| 4370 | ctx_node = resolve_when_ctx_node(node, parent); |
| 4371 | if (!ctx_node) { |
| 4372 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4373 | rc = -1; |
| 4374 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4375 | } |
| 4376 | } |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 4377 | rc = lyxp_eval(((struct lys_node_uses *)parent)->when->cond, ctx_node, &set, LYXP_WHEN); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4378 | if (rc) { |
| 4379 | if (rc == 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4380 | LOGVAL(LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)parent)->when->cond); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4381 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4382 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4383 | } |
| 4384 | |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 4385 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, LYXP_WHEN); |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 4386 | if (!set.val.bool) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4387 | ly_vlog_hide(1); |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 4388 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_uses *)parent)->when->cond); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4389 | ly_vlog_hide(0); |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 4390 | node->when_status |= LYD_WHEN_FALSE; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4391 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4392 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4393 | |
| 4394 | /* free xpath set content */ |
| 4395 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4396 | } |
| 4397 | |
| 4398 | check_augment: |
| 4399 | if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) && (((struct lys_node_augment *)parent->parent)->when))) { |
| 4400 | if (!ctx_node) { |
| 4401 | ctx_node = resolve_when_ctx_node(node, parent->parent); |
| 4402 | if (!ctx_node) { |
| 4403 | LOGINT; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4404 | rc = -1; |
| 4405 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4406 | } |
| 4407 | } |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 4408 | rc = lyxp_eval(((struct lys_node_augment *)parent->parent)->when->cond, ctx_node, &set, LYXP_WHEN); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4409 | if (rc) { |
| 4410 | if (rc == 1) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4411 | LOGVAL(LYE_INWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)parent->parent)->when->cond); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4412 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4413 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4414 | } |
| 4415 | |
Michal Vasko | 944a564 | 2016-03-21 11:48:58 +0100 | [diff] [blame] | 4416 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node, LYXP_WHEN); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4417 | |
Michal Vasko | 8146d4c | 2016-05-09 15:50:29 +0200 | [diff] [blame] | 4418 | if (!set.val.bool) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4419 | ly_vlog_hide(1); |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 4420 | LOGVAL(LYE_NOWHEN, LY_VLOG_LYD, node, ((struct lys_node_augment *)parent->parent)->when->cond); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4421 | ly_vlog_hide(0); |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 4422 | node->when_status |= LYD_WHEN_FALSE; |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4423 | goto cleanup; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4424 | } |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4425 | |
| 4426 | /* free xpath set content */ |
| 4427 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node, 0); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4428 | } |
| 4429 | |
| 4430 | parent = lys_parent(parent); |
| 4431 | } |
| 4432 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 4433 | node->when_status |= LYD_WHEN_TRUE; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 4434 | |
Radek Krejci | 5109364 | 2016-03-29 10:14:59 +0200 | [diff] [blame] | 4435 | cleanup: |
| 4436 | |
| 4437 | /* free xpath set content */ |
| 4438 | lyxp_set_cast(&set, LYXP_SET_EMPTY, ctx_node ? ctx_node : node, 0); |
| 4439 | |
| 4440 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4441 | } |
| 4442 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4443 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4444 | * @brief Resolve a single unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4445 | * |
| 4446 | * @param[in] mod Main module. |
| 4447 | * @param[in] item Item to resolve. Type determined by \p type. |
| 4448 | * @param[in] type Type of the unresolved item. |
| 4449 | * @param[in] str_snode String, a schema node, or NULL. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4450 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4451 | * |
| 4452 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 4453 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4454 | static int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 4455 | resolve_unres_schema_item(struct lys_module *mod, void *item, enum UNRES_ITEM type, void *str_snode, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4456 | struct unres_schema *unres) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4457 | { |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 4458 | int rc = -1, has_str = 0, tpdf_flag = 0; |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 4459 | struct lys_node *node; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 4460 | const char *base_name; |
| 4461 | |
| 4462 | struct lys_ident *ident; |
| 4463 | struct lys_type *stype; |
| 4464 | struct lys_feature **feat_ptr; |
| 4465 | struct lys_node_choice *choic; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4466 | struct lyxml_elem *yin; |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 4467 | struct yang_type *yang; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4468 | |
| 4469 | switch (type) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4470 | case UNRES_IDENT: |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 4471 | base_name = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 4472 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 4473 | ident = item; |
| 4474 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4475 | rc = resolve_base_ident(mod, ident, base_name, "identity", NULL); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4476 | break; |
| 4477 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 4478 | base_name = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 4479 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 4480 | stype = item; |
| 4481 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4482 | rc = resolve_base_ident(mod, NULL, base_name, "type", stype); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4483 | break; |
| 4484 | case UNRES_TYPE_LEAFREF: |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 4485 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 4486 | stype = item; |
| 4487 | |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 4488 | /* HACK - when there is no parent, we are in top level typedef and in that |
| 4489 | * case, the path has to contain absolute path, so we let the resolve_path_arg_schema() |
| 4490 | * know it via tpdf_flag */ |
| 4491 | if (!node) { |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 4492 | tpdf_flag = 1; |
Radek Krejci | 2f12f85 | 2016-01-08 12:59:57 +0100 | [diff] [blame] | 4493 | node = (struct lys_node *)stype->parent; |
| 4494 | } |
| 4495 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4496 | rc = resolve_path_arg_schema(stype->info.lref.path, node, tpdf_flag, |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 4497 | (const struct lys_node **)&stype->info.lref.target); |
Radek Krejci | 46c4cd7 | 2016-01-21 15:13:52 +0100 | [diff] [blame] | 4498 | if (stype->info.lref.target) { |
| 4499 | /* store the backlink from leafref target */ |
| 4500 | if (!stype->info.lref.target->child) { |
| 4501 | stype->info.lref.target->child = (void*)ly_set_new(); |
| 4502 | if (!stype->info.lref.target->child) { |
| 4503 | LOGMEM; |
| 4504 | return -1; |
| 4505 | } |
| 4506 | } |
| 4507 | ly_set_add((struct ly_set *)stype->info.lref.target->child, stype->parent); |
| 4508 | } |
| 4509 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4510 | break; |
| 4511 | case UNRES_TYPE_DER: |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4512 | /* parent */ |
| 4513 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 4514 | stype = item; |
| 4515 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4516 | /* HACK type->der is temporarily unparsed type statement */ |
| 4517 | yin = (struct lyxml_elem *)stype->der; |
| 4518 | stype->der = NULL; |
| 4519 | |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 4520 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 4521 | yang = (struct yang_type *)yin; |
| 4522 | rc = yang_check_type(mod, node, yang, unres); |
| 4523 | |
| 4524 | if (rc) { |
Pavol Vican | 933aa5a | 2016-04-09 21:05:46 +0200 | [diff] [blame] | 4525 | if (rc == -1) { |
| 4526 | yang->type->base = yang->base; |
| 4527 | lydict_remove(mod->ctx, yang->name); |
| 4528 | free(yang); |
| 4529 | stype->der = NULL; |
| 4530 | } else { |
| 4531 | /* may try again later */ |
| 4532 | stype->der = (struct lys_tpdf *)yang; |
| 4533 | } |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 4534 | } else { |
| 4535 | /* we need to always be able to free this, it's safe only in this case */ |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 4536 | lydict_remove(mod->ctx, yang->name); |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 4537 | free(yang); |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 4538 | } |
| 4539 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4540 | } else { |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 4541 | rc = fill_yin_type(mod, node, yin, stype, unres); |
| 4542 | if (!rc) { |
| 4543 | /* we need to always be able to free this, it's safe only in this case */ |
| 4544 | lyxml_free(mod->ctx, yin); |
| 4545 | } else { |
| 4546 | /* may try again later, put all back how it was */ |
| 4547 | stype->der = (struct lys_tpdf *)yin; |
| 4548 | } |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 4549 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4550 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4551 | case UNRES_IFFEAT: |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 4552 | base_name = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 4553 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 4554 | feat_ptr = item; |
| 4555 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4556 | rc = resolve_feature(base_name, mod, feat_ptr); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4557 | break; |
| 4558 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4559 | rc = resolve_unres_schema_uses(item, unres); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4560 | break; |
| 4561 | case UNRES_TYPE_DFLT: |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 4562 | base_name = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 4563 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 4564 | stype = item; |
| 4565 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4566 | rc = check_default(stype, base_name, mod); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4567 | break; |
| 4568 | case UNRES_CHOICE_DFLT: |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 4569 | base_name = str_snode; |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 4570 | has_str = 1; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 4571 | choic = item; |
| 4572 | |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 4573 | choic->dflt = resolve_choice_dflt(choic, base_name); |
| 4574 | if (choic->dflt) { |
| 4575 | rc = EXIT_SUCCESS; |
| 4576 | } else { |
| 4577 | rc = EXIT_FAILURE; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 4578 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4579 | break; |
| 4580 | case UNRES_LIST_KEYS: |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 4581 | has_str = 1; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4582 | rc = resolve_list_keys(item, str_snode); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4583 | break; |
| 4584 | case UNRES_LIST_UNIQ: |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 4585 | has_str = 1; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4586 | rc = resolve_unique(item, str_snode); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4587 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4588 | case UNRES_AUGMENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4589 | rc = resolve_augment(item, NULL); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4590 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4591 | default: |
| 4592 | LOGINT; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4593 | break; |
| 4594 | } |
| 4595 | |
Radek Krejci | 4f78b53 | 2016-02-17 13:43:00 +0100 | [diff] [blame] | 4596 | if (has_str && !rc) { |
| 4597 | lydict_remove(mod->ctx, str_snode); |
| 4598 | } |
| 4599 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4600 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4601 | } |
| 4602 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4603 | /* logs directly */ |
| 4604 | static void |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4605 | print_unres_schema_item_fail(void *item, enum UNRES_ITEM type, void *str_node) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4606 | { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4607 | switch (type) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4608 | case UNRES_IDENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4609 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "identity", (char *)str_node); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4610 | break; |
| 4611 | case UNRES_TYPE_IDENTREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4612 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "identityref", (char *)str_node); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4613 | break; |
| 4614 | case UNRES_TYPE_LEAFREF: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4615 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "leafref", |
| 4616 | ((struct lys_type *)item)->info.lref.path); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4617 | break; |
| 4618 | case UNRES_TYPE_DER: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4619 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "derived type", |
| 4620 | ((struct lyxml_elem *)((struct lys_type *)item)->der)->attr->value); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4621 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4622 | case UNRES_IFFEAT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4623 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "if-feature", (char *)str_node); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4624 | break; |
| 4625 | case UNRES_USES: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4626 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "uses", ((struct lys_node_uses *)item)->name); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4627 | break; |
| 4628 | case UNRES_TYPE_DFLT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4629 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "type default", (char *)str_node); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4630 | break; |
| 4631 | case UNRES_CHOICE_DFLT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4632 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "choice default", (char *)str_node); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4633 | break; |
| 4634 | case UNRES_LIST_KEYS: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4635 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "list keys", (char *)str_node); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4636 | break; |
| 4637 | case UNRES_LIST_UNIQ: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4638 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "list unique", (char *)str_node); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4639 | break; |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4640 | case UNRES_AUGMENT: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4641 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "augment target", |
| 4642 | ((struct lys_node_augment *)item)->target_name); |
Michal Vasko | 7178e69 | 2016-02-12 15:58:05 +0100 | [diff] [blame] | 4643 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4644 | default: |
| 4645 | LOGINT; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4646 | break; |
| 4647 | } |
| 4648 | } |
| 4649 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4650 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4651 | * @brief Resolve every unres schema item in the structure. Logs directly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4652 | * |
| 4653 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4654 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4655 | * |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 4656 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4657 | */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4658 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 4659 | resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4660 | { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4661 | uint32_t i, resolved = 0, unres_count, res_count; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4662 | int rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4663 | |
| 4664 | assert(unres); |
| 4665 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 4666 | LOGVRB("Resolving unresolved schema nodes and their constraints..."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4667 | ly_vlog_hide(1); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 4668 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4669 | /* uses */ |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 4670 | do { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4671 | unres_count = 0; |
| 4672 | res_count = 0; |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 4673 | |
| 4674 | for (i = 0; i < unres->count; ++i) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4675 | /* we do not need to have UNRES_TYPE_IDENTREF or UNRES_TYPE_LEAFREF resolved, |
| 4676 | * we need every type's base only */ |
| 4677 | if ((unres->type[i] != UNRES_USES) && (unres->type[i] != UNRES_TYPE_DER)) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 4678 | continue; |
| 4679 | } |
| 4680 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4681 | ++unres_count; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4682 | rc = resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4683 | if (!rc) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 4684 | unres->type[i] = UNRES_RESOLVED; |
| 4685 | ++resolved; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4686 | ++res_count; |
Michal Vasko | 89e1532 | 2015-08-17 15:46:55 +0200 | [diff] [blame] | 4687 | } else if (rc == -1) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4688 | ly_vlog_hide(0); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4689 | return -1; |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 4690 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4691 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4692 | } while (res_count && (res_count < unres_count)); |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 4693 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4694 | if (res_count < unres_count) { |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 4695 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4696 | } |
| 4697 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4698 | /* the rest */ |
| 4699 | for (i = 0; i < unres->count; ++i) { |
| 4700 | if (unres->type[i] == UNRES_RESOLVED) { |
| 4701 | continue; |
| 4702 | } |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 4703 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4704 | rc = resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4705 | if (rc == 0) { |
| 4706 | unres->type[i] = UNRES_RESOLVED; |
| 4707 | ++resolved; |
| 4708 | } else if (rc == -1) { |
| 4709 | ly_vlog_hide(0); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 4710 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4711 | } |
| 4712 | } |
| 4713 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4714 | ly_vlog_hide(0); |
| 4715 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4716 | if (resolved < unres->count) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4717 | /* try to resolve the unresolved nodes again, it will not resolve anything, but it will print |
| 4718 | * all the validation errors |
| 4719 | */ |
| 4720 | for (i = 0; i < unres->count; ++i) { |
| 4721 | if (unres->type[i] == UNRES_RESOLVED) { |
| 4722 | continue; |
| 4723 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4724 | resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4725 | } |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 4726 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4727 | } |
| 4728 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 4729 | LOGVRB("All schema nodes and constraints resolved."); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4730 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4731 | return EXIT_SUCCESS; |
| 4732 | } |
| 4733 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4734 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4735 | * @brief Try to resolve an unres schema item with a string argument. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4736 | * |
| 4737 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4738 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4739 | * @param[in] item Item to resolve. Type determined by \p type. |
| 4740 | * @param[in] type Type of the unresolved item. |
| 4741 | * @param[in] str String argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4742 | * |
| 4743 | * @return EXIT_SUCCESS on success or storing the item in unres, -1 on error. |
| 4744 | */ |
| 4745 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4746 | unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type, |
| 4747 | const char *str) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4748 | { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4749 | return unres_schema_add_node(mod, unres, item, type, (struct lys_node *)lydict_insert(mod->ctx, str, 0)); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4750 | } |
| 4751 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4752 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4753 | * @brief Try to resolve an unres schema item with a schema node argument. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4754 | * |
| 4755 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4756 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4757 | * @param[in] item Item to resolve. Type determined by \p type. |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4758 | * @param[in] type Type of the unresolved item. UNRES_TYPE_DER is handled specially! |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4759 | * @param[in] snode Schema node argument. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4760 | * |
| 4761 | * @return EXIT_SUCCESS on success or storing the item in unres, -1 on error. |
| 4762 | */ |
| 4763 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 4764 | unres_schema_add_node(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type, |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4765 | struct lys_node *snode) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4766 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4767 | int rc; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4768 | struct lyxml_elem *yin; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4769 | char *path, *msg; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4770 | |
Michal Vasko | 9bf425b | 2015-10-22 11:42:03 +0200 | [diff] [blame] | 4771 | assert(unres && item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN) |
| 4772 | && (type != UNRES_MUST))); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4773 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4774 | ly_vlog_hide(1); |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4775 | rc = resolve_unres_schema_item(mod, item, type, snode, unres); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4776 | ly_vlog_hide(0); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4777 | if (rc != EXIT_FAILURE) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 4778 | if (rc == -1 && ly_errno == LY_EVALID) { |
| 4779 | path = strdup(ly_errpath()); |
| 4780 | LOGERR(LY_EVALID, "%s%s%s%s", msg = strdup(ly_errmsg()), |
| 4781 | path[0] ? " (path: " : "", path[0] ? path : "", path[0] ? ")" : ""); |
| 4782 | free(path); |
| 4783 | free(msg); |
| 4784 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4785 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4786 | } |
| 4787 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4788 | print_unres_schema_item_fail(item, type, snode); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4789 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4790 | /* HACK unlinking is performed here so that we do not do any (NS) copying in vain */ |
| 4791 | if (type == UNRES_TYPE_DER) { |
| 4792 | yin = (struct lyxml_elem *)((struct lys_type *)item)->der; |
Pavol Vican | a0e4e67 | 2016-02-24 12:20:04 +0100 | [diff] [blame] | 4793 | if (!(yin->flags & LY_YANG_STRUCTURE_FLAG)) { |
| 4794 | lyxml_unlink_elem(mod->ctx, yin, 1); |
| 4795 | ((struct lys_type *)item)->der = (struct lys_tpdf *)yin; |
| 4796 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4797 | } |
| 4798 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4799 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 4800 | unres->item = ly_realloc(unres->item, unres->count*sizeof *unres->item); |
| 4801 | if (!unres->item) { |
| 4802 | LOGMEM; |
| 4803 | return -1; |
| 4804 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4805 | unres->item[unres->count-1] = item; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 4806 | unres->type = ly_realloc(unres->type, unres->count*sizeof *unres->type); |
| 4807 | if (!unres->type) { |
| 4808 | LOGMEM; |
| 4809 | return -1; |
| 4810 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4811 | unres->type[unres->count-1] = type; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 4812 | unres->str_snode = ly_realloc(unres->str_snode, unres->count*sizeof *unres->str_snode); |
| 4813 | if (!unres->str_snode) { |
| 4814 | LOGMEM; |
| 4815 | return -1; |
| 4816 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4817 | unres->str_snode[unres->count-1] = snode; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4818 | unres->module = ly_realloc(unres->module, unres->count*sizeof *unres->module); |
| 4819 | if (!unres->module) { |
| 4820 | LOGMEM; |
| 4821 | return -1; |
| 4822 | } |
| 4823 | unres->module[unres->count-1] = mod; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4824 | |
| 4825 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4826 | } |
| 4827 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4828 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4829 | * @brief Duplicate an unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4830 | * |
| 4831 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 4832 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4833 | * @param[in] item Old item to be resolved. |
| 4834 | * @param[in] type Type of the old unresolved item. |
| 4835 | * @param[in] new_item New item to use in the duplicate. |
| 4836 | * |
| 4837 | * @return EXIT_SUCCESS on success, -1 on error. |
| 4838 | */ |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 4839 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 4840 | unres_schema_dup(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type, void *new_item) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4841 | { |
| 4842 | int i; |
| 4843 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4844 | assert(item && new_item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN))); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4845 | |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 4846 | i = unres_schema_find(unres, item, type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4847 | |
| 4848 | if (i == -1) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4849 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4850 | } |
| 4851 | |
Michal Vasko | 0d20459 | 2015-10-07 09:50:04 +0200 | [diff] [blame] | 4852 | if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT)) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4853 | if (unres_schema_add_node(mod, unres, new_item, type, unres->str_snode[i]) == -1) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4854 | LOGINT; |
| 4855 | return -1; |
| 4856 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4857 | } else { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4858 | if (unres_schema_add_str(mod, unres, new_item, type, unres->str_snode[i]) == -1) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 4859 | LOGINT; |
| 4860 | return -1; |
| 4861 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4862 | } |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 4863 | |
| 4864 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4865 | } |
| 4866 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 4867 | /* does not log */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4868 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 4869 | unres_schema_find(struct unres_schema *unres, void *item, enum UNRES_ITEM type) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 4870 | { |
| 4871 | uint32_t ret = -1, i; |
| 4872 | |
| 4873 | for (i = 0; i < unres->count; ++i) { |
| 4874 | if ((unres->item[i] == item) && (unres->type[i] == type)) { |
| 4875 | ret = i; |
| 4876 | break; |
| 4877 | } |
| 4878 | } |
| 4879 | |
| 4880 | return ret; |
| 4881 | } |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4882 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4883 | void |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4884 | unres_schema_free(struct lys_module *module, struct unres_schema **unres) |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4885 | { |
| 4886 | uint32_t i; |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4887 | unsigned int unresolved = 0; |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 4888 | struct lyxml_elem *yin; |
| 4889 | struct yang_type *yang; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4890 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4891 | if (!unres || !(*unres)) { |
| 4892 | return; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4893 | } |
| 4894 | |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4895 | assert(module || (*unres)->count == 0); |
| 4896 | |
| 4897 | for (i = 0; i < (*unres)->count; ++i) { |
| 4898 | if ((*unres)->module[i] != module) { |
| 4899 | if ((*unres)->type[i] != UNRES_RESOLVED) { |
| 4900 | unresolved++; |
| 4901 | } |
| 4902 | continue; |
| 4903 | } |
| 4904 | if ((*unres)->type[i] == UNRES_TYPE_DER) { |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 4905 | yin = (struct lyxml_elem *)((struct lys_type *)(*unres)->item[i])->der; |
| 4906 | if (yin->flags & LY_YANG_STRUCTURE_FLAG) { |
| 4907 | yang =(struct yang_type *)yin; |
Pavol Vican | 6b07251 | 2016-04-04 10:50:21 +0200 | [diff] [blame] | 4908 | yang->type->base = yang->base; |
Pavol Vican | 5f0316a | 2016-04-05 21:21:11 +0200 | [diff] [blame] | 4909 | lydict_remove(module->ctx, yang->name); |
Pavol Vican | d01d8ae | 2016-03-01 10:45:59 +0100 | [diff] [blame] | 4910 | free(yang); |
| 4911 | } else { |
| 4912 | lyxml_free(module->ctx, yin); |
| 4913 | } |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4914 | } |
| 4915 | (*unres)->type[i] = UNRES_RESOLVED; |
| 4916 | } |
| 4917 | |
| 4918 | if (!module || (!unresolved && !module->type)) { |
| 4919 | free((*unres)->item); |
| 4920 | free((*unres)->type); |
| 4921 | free((*unres)->str_snode); |
| 4922 | free((*unres)->module); |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 4923 | free((*unres)); |
| 4924 | (*unres) = NULL; |
| 4925 | } |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 4926 | } |
| 4927 | |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4928 | /** |
| 4929 | * @brief Resolve a single unres data item. Logs directly. |
| 4930 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4931 | * @param[in] node Data node to resolve. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4932 | * @param[in] type Type of the unresolved item. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4933 | * |
| 4934 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 4935 | */ |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 4936 | int |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4937 | resolve_unres_data_item(struct lyd_node *node, enum UNRES_ITEM type) |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4938 | { |
| 4939 | uint32_t i; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 4940 | int rc; |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 4941 | struct lyd_node_leaf_list *leaf; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4942 | struct lys_node_leaf *sleaf; |
Michal Vasko | c428084 | 2016-04-19 16:10:42 +0200 | [diff] [blame] | 4943 | struct lyd_node *parent; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4944 | struct unres_data matches; |
| 4945 | |
| 4946 | memset(&matches, 0, sizeof matches); |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 4947 | leaf = (struct lyd_node_leaf_list *)node; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4948 | sleaf = (struct lys_node_leaf *)leaf->schema; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4949 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4950 | switch (type) { |
| 4951 | case UNRES_LEAFREF: |
| 4952 | assert(sleaf->type.base == LY_TYPE_LEAFREF); |
Michal Vasko | 2471e7f | 2016-04-11 11:00:15 +0200 | [diff] [blame] | 4953 | /* EXIT_FAILURE return keeps leaf->value.lefref NULL, handled later */ |
| 4954 | if (resolve_path_arg_data(node, sleaf->type.info.lref.path, &matches) == -1) { |
| 4955 | return -1; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4956 | } |
| 4957 | |
| 4958 | /* check that value matches */ |
| 4959 | for (i = 0; i < matches.count; ++i) { |
Radek Krejci | 749190d | 2016-02-18 16:26:25 +0100 | [diff] [blame] | 4960 | if (ly_strequal(leaf->value_str, ((struct lyd_node_leaf_list *)matches.node[i])->value_str, 1)) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4961 | leaf->value.leafref = matches.node[i]; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4962 | break; |
| 4963 | } |
| 4964 | } |
| 4965 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4966 | free(matches.node); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4967 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4968 | if (!leaf->value.leafref) { |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4969 | /* reference not found */ |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 4970 | LOGVAL(LYE_NOLEAFREF, LY_VLOG_LYD, leaf, sleaf->type.info.lref.path, leaf->value_str); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4971 | return EXIT_FAILURE; |
| 4972 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4973 | break; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4974 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4975 | case UNRES_INSTID: |
Michal Vasko | 976a5f2 | 2016-05-18 13:28:42 +0200 | [diff] [blame] | 4976 | assert((sleaf->type.base == LY_TYPE_INST) || (sleaf->type.base == LY_TYPE_UNION)); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4977 | ly_errno = 0; |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4978 | leaf->value.instance = resolve_instid(node, leaf->value_str); |
Radek Krejci | 40f17b9 | 2016-02-03 14:30:43 +0100 | [diff] [blame] | 4979 | if (!leaf->value.instance) { |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4980 | if (ly_errno) { |
| 4981 | return -1; |
| 4982 | } else if (sleaf->type.info.inst.req > -1) { |
Michal Vasko | 6ac6828 | 2016-04-11 10:56:47 +0200 | [diff] [blame] | 4983 | LOGVAL(LYE_NOREQINS, LY_VLOG_LYD, leaf, leaf->value_str); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4984 | return EXIT_FAILURE; |
| 4985 | } else { |
Radek Krejci | 4ce42be | 2016-02-03 13:04:41 +0100 | [diff] [blame] | 4986 | LOGVRB("There is no instance of \"%s\", but it is not required.", leaf->value_str); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 4987 | } |
| 4988 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4989 | break; |
| 4990 | |
| 4991 | case UNRES_WHEN: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4992 | if ((rc = resolve_when(node))) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 4993 | return rc; |
| 4994 | } |
| 4995 | break; |
| 4996 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4997 | case UNRES_MUST: |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 4998 | if ((rc = resolve_must(node))) { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 4999 | return rc; |
| 5000 | } |
| 5001 | break; |
| 5002 | |
Michal Vasko | c428084 | 2016-04-19 16:10:42 +0200 | [diff] [blame] | 5003 | case UNRES_EMPTYCONT: |
| 5004 | do { |
| 5005 | parent = node->parent; |
| 5006 | lyd_free(node); |
| 5007 | node = parent; |
| 5008 | } while (node && (node->schema->nodetype == LYS_CONTAINER) && !node->child |
| 5009 | && !((struct lys_node_container *)node->schema)->presence); |
| 5010 | break; |
| 5011 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5012 | default: |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 5013 | LOGINT; |
| 5014 | return -1; |
| 5015 | } |
| 5016 | |
| 5017 | return EXIT_SUCCESS; |
| 5018 | } |
| 5019 | |
| 5020 | /** |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5021 | * @brief add data unres item |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 5022 | * |
| 5023 | * @param[in] unres Unres data structure to use. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5024 | * @param[in] node Data node to use. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 5025 | * |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5026 | * @return 0 on success, -1 on error. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 5027 | */ |
| 5028 | int |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5029 | unres_data_add(struct unres_data *unres, struct lyd_node *node, enum UNRES_ITEM type) |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 5030 | { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5031 | assert(unres && node); |
Michal Vasko | c428084 | 2016-04-19 16:10:42 +0200 | [diff] [blame] | 5032 | assert((type == UNRES_LEAFREF) || (type == UNRES_INSTID) || (type == UNRES_WHEN) || (type == UNRES_MUST) |
| 5033 | || (type == UNRES_EMPTYCONT)); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 5034 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5035 | unres->count++; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 5036 | unres->node = ly_realloc(unres->node, unres->count * sizeof *unres->node); |
| 5037 | if (!unres->node) { |
| 5038 | LOGMEM; |
| 5039 | return -1; |
| 5040 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5041 | unres->node[unres->count - 1] = node; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 5042 | unres->type = ly_realloc(unres->type, unres->count * sizeof *unres->type); |
| 5043 | if (!unres->type) { |
| 5044 | LOGMEM; |
| 5045 | return -1; |
| 5046 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 5047 | unres->type[unres->count - 1] = type; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 5048 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5049 | if (type == UNRES_WHEN) { |
| 5050 | /* remove previous result */ |
| 5051 | node->when_status = LYD_WHEN; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5052 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5053 | |
| 5054 | return EXIT_SUCCESS; |
| 5055 | } |
| 5056 | |
| 5057 | /** |
| 5058 | * @brief Resolve every unres data item in the structure. Logs directly. |
| 5059 | * |
| 5060 | * @param[in] unres Unres data structure to use. |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5061 | * @param[in,out] root Root node of the data tree. If not NULL, auto-delete is performed on false when condition. If |
| 5062 | * NULL and when condition is false the error is raised. |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 5063 | * @param[in] options Parer options |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5064 | * |
| 5065 | * @return EXIT_SUCCESS on success, -1 on error. |
| 5066 | */ |
| 5067 | int |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 5068 | resolve_unres_data(struct unres_data *unres, struct lyd_node **root, int options) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5069 | { |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 5070 | uint32_t i, j, first = 1, resolved = 0, del_items = 0, when_stmt = 0; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5071 | int rc, progress; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5072 | char *msg, *path; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5073 | struct lyd_node *parent; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5074 | |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5075 | assert(unres); |
Radek Krejci | 8ee1b56 | 2016-03-31 10:58:31 +0200 | [diff] [blame] | 5076 | assert((root && (*root)) || (options & LYD_OPT_NOAUTODEL)); |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5077 | |
| 5078 | if (!unres->count) { |
| 5079 | return EXIT_SUCCESS; |
| 5080 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5081 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 5082 | LOGVRB("Resolving unresolved data nodes and their constraints..."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5083 | ly_vlog_hide(1); |
| 5084 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5085 | /* when-stmt first */ |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5086 | ly_errno = LY_SUCCESS; |
| 5087 | ly_vecode = LYVE_SUCCESS; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5088 | do { |
| 5089 | progress = 0; |
| 5090 | for(i = 0; i < unres->count; i++) { |
| 5091 | if (unres->type[i] != UNRES_WHEN) { |
| 5092 | continue; |
| 5093 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 5094 | if (first) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5095 | /* count when-stmt nodes in unres list */ |
| 5096 | when_stmt++; |
| 5097 | } |
| 5098 | |
| 5099 | /* resolve when condition only when all parent when conditions are already resolved */ |
| 5100 | for (parent = unres->node[i]->parent; |
| 5101 | parent && LYD_WHEN_DONE(parent->when_status); |
| 5102 | parent = parent->parent) { |
| 5103 | if (!parent->parent && (parent->when_status & LYD_WHEN_FALSE)) { |
| 5104 | /* the parent node was already unlinked, do not resolve this node, |
| 5105 | * it will be removed anyway, so just mark it as resolved |
| 5106 | */ |
| 5107 | unres->node[i]->when_status |= LYD_WHEN_FALSE; |
| 5108 | unres->type[i] = UNRES_RESOLVED; |
| 5109 | resolved++; |
| 5110 | break; |
| 5111 | } |
| 5112 | } |
| 5113 | if (parent) { |
| 5114 | continue; |
| 5115 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5116 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5117 | rc = resolve_unres_data_item(unres->node[i], unres->type[i]); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5118 | if (!rc) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5119 | if (unres->node[i]->when_status & LYD_WHEN_FALSE) { |
| 5120 | if (!root) { |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5121 | /* false when condition */ |
| 5122 | ly_vlog_hide(0); |
| 5123 | path = strdup(ly_errpath()); |
| 5124 | LOGERR(LY_EVALID, "%s%s%s%s", msg = strdup(ly_errmsg()), path[0] ? " (path: " : "", |
| 5125 | path[0] ? path : "", path[0] ? ")" : ""); |
| 5126 | free(path); |
| 5127 | free(msg); |
| 5128 | return -1; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5129 | } /* follows else */ |
| 5130 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 5131 | /* only unlink now, the subtree can contain another nodes stored in the unres list */ |
| 5132 | /* if it has parent non-presence containers that would be empty, we should actually |
| 5133 | * remove the container |
| 5134 | */ |
| 5135 | if (!(options & LYD_OPT_KEEPEMPTYCONT)) { |
| 5136 | for (parent = unres->node[i]; |
| 5137 | parent->parent && parent->parent->schema->nodetype == LYS_CONTAINER; |
| 5138 | parent = parent->parent) { |
| 5139 | if (((struct lys_node_container *)parent->parent->schema)->presence) { |
| 5140 | /* presence container */ |
| 5141 | break; |
| 5142 | } |
| 5143 | if (parent->next || parent->prev != parent) { |
| 5144 | /* non empty (the child we are in and we are going to remove is not the only child) */ |
| 5145 | break; |
| 5146 | } |
| 5147 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 5148 | unres->node[i] = parent; |
| 5149 | } |
| 5150 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5151 | /* auto-delete */ |
| 5152 | LOGVRB("auto-delete node \"%s\" due to when condition (%s)", ly_errpath(), |
| 5153 | ((struct lys_node_leaf *)unres->node[i]->schema)->when->cond); |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 5154 | if (*root && *root == unres->node[i]) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5155 | *root = (*root)->next; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5156 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5157 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5158 | lyd_unlink(unres->node[i]); |
| 5159 | unres->type[i] = UNRES_DELETE; |
| 5160 | del_items++; |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 5161 | |
| 5162 | /* update the rest of unres items */ |
| 5163 | for (j = 0; j < unres->count; j++) { |
Radek Krejci | 3db819b | 2016-03-24 16:29:48 +0100 | [diff] [blame] | 5164 | if (unres->type[j] == UNRES_RESOLVED || unres->type[j] == UNRES_DELETE) { |
Radek Krejci | 51fd816 | 2016-03-24 15:49:51 +0100 | [diff] [blame] | 5165 | continue; |
| 5166 | } |
| 5167 | |
| 5168 | /* test if the node is in subtree to be deleted */ |
| 5169 | for (parent = unres->node[j]; parent; parent = parent->parent) { |
| 5170 | if (parent == unres->node[i]) { |
| 5171 | /* yes, it is */ |
| 5172 | unres->type[j] = UNRES_RESOLVED; |
| 5173 | resolved++; |
| 5174 | break; |
| 5175 | } |
| 5176 | } |
| 5177 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5178 | } else { |
| 5179 | unres->type[i] = UNRES_RESOLVED; |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 5180 | } |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5181 | ly_errno = LY_SUCCESS; |
| 5182 | ly_vecode = LYVE_SUCCESS; |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5183 | resolved++; |
| 5184 | progress = 1; |
| 5185 | } else if (rc == -1) { |
| 5186 | ly_vlog_hide(0); |
| 5187 | return -1; |
| 5188 | } |
| 5189 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 5190 | first = 0; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5191 | } while (progress && resolved < when_stmt); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5192 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5193 | /* do we have some unresolved when-stmt? */ |
Radek Krejci | d940d73 | 2016-03-24 16:02:28 +0100 | [diff] [blame] | 5194 | if (when_stmt > resolved) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5195 | ly_vlog_hide(0); |
| 5196 | path = strdup(ly_errpath()); |
| 5197 | LOGERR(LY_EVALID, "%s%s%s%s", msg = strdup(ly_errmsg()), path[0] ? " (path: " : "", |
| 5198 | path[0] ? path : "", path[0] ? ")" : ""); |
| 5199 | free(path); |
| 5200 | free(msg); |
| 5201 | return -1; |
| 5202 | } |
| 5203 | |
| 5204 | for (i = 0; del_items && i < unres->count; i++) { |
| 5205 | /* we had some when-stmt resulted to false, so now we have to sanitize the unres list */ |
| 5206 | if (unres->type[i] != UNRES_DELETE) { |
| 5207 | continue; |
| 5208 | } |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 5209 | if (!unres->node[i]) { |
| 5210 | unres->type[i] = UNRES_RESOLVED; |
| 5211 | del_items--; |
| 5212 | continue; |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 5213 | } |
| 5214 | |
| 5215 | /* really remove the complete subtree */ |
| 5216 | lyd_free(unres->node[i]); |
| 5217 | unres->type[i] = UNRES_RESOLVED; |
| 5218 | del_items--; |
| 5219 | } |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5220 | |
| 5221 | /* rest */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5222 | for (i = 0; i < unres->count; ++i) { |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5223 | if (unres->type[i] == UNRES_RESOLVED) { |
| 5224 | continue; |
| 5225 | } |
| 5226 | |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5227 | rc = resolve_unres_data_item(unres->node[i], unres->type[i]); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5228 | if (rc == 0) { |
| 5229 | unres->type[i] = UNRES_RESOLVED; |
| 5230 | resolved++; |
| 5231 | } else if (rc == -1) { |
| 5232 | ly_vlog_hide(0); |
Michal Vasko | 96b846c | 2016-05-18 13:28:58 +0200 | [diff] [blame] | 5233 | /* print only this last error */ |
| 5234 | resolve_unres_data_item(unres->node[i], unres->type[i]); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5235 | return -1; |
| 5236 | } |
| 5237 | } |
| 5238 | |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5239 | ly_vlog_hide(0); |
| 5240 | if (resolved < unres->count) { |
| 5241 | /* try to resolve the unresolved data again, it will not resolve anything, but it will print |
| 5242 | * all the validation errors |
| 5243 | */ |
| 5244 | for (i = 0; i < unres->count; ++i) { |
| 5245 | if (unres->type[i] == UNRES_RESOLVED) { |
| 5246 | continue; |
| 5247 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 5248 | resolve_unres_data_item(unres->node[i], unres->type[i]); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5249 | } |
| 5250 | return -1; |
| 5251 | } |
| 5252 | |
Michal Vasko | a0ffcab | 2016-05-02 14:52:08 +0200 | [diff] [blame] | 5253 | LOGVRB("All data nodes and constraints resolved."); |
Radek Krejci | 010e54b | 2016-03-15 09:40:34 +0100 | [diff] [blame] | 5254 | unres->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 5255 | return EXIT_SUCCESS; |
| 5256 | } |