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 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in |
| 15 | * the documentation and/or other materials provided with the |
| 16 | * distribution. |
| 17 | * 3. Neither the name of the Company nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this |
| 19 | * software without specific prior written permission. |
| 20 | */ |
| 21 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 22 | #define _GNU_SOURCE |
| 23 | |
| 24 | #include <stdlib.h> |
| 25 | #include <assert.h> |
| 26 | #include <string.h> |
| 27 | #include <ctype.h> |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 28 | #include <limits.h> |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 29 | |
| 30 | #include "libyang.h" |
| 31 | #include "resolve.h" |
| 32 | #include "common.h" |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 33 | #include "xpath.h" |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 34 | #include "parser.h" |
Radek Krejci | 41912fe | 2015-10-22 10:22:12 +0200 | [diff] [blame] | 35 | #include "dict_private.h" |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 36 | #include "tree_internal.h" |
| 37 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 38 | /** |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 39 | * @brief Parse an identifier. |
| 40 | * |
| 41 | * ;; An identifier MUST NOT start with (('X'|'x') ('M'|'m') ('L'|'l')) |
| 42 | * identifier = (ALPHA / "_") |
| 43 | * *(ALPHA / DIGIT / "_" / "-" / ".") |
| 44 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 45 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 46 | * |
| 47 | * @return Number of characters successfully parsed. |
| 48 | */ |
Michal Vasko | 249e6b5 | 2015-08-19 11:08:52 +0200 | [diff] [blame] | 49 | int |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 50 | parse_identifier(const char *id) |
| 51 | { |
| 52 | int parsed = 0; |
| 53 | |
| 54 | if (((id[0] == 'x') || (id[0] == 'X')) |
| 55 | && ((id[1] == 'm') || (id[0] == 'M')) |
| 56 | && ((id[2] == 'l') || (id[2] == 'L'))) { |
| 57 | return -parsed; |
| 58 | } |
| 59 | |
| 60 | if (!isalpha(id[0]) && (id[0] != '_')) { |
| 61 | return -parsed; |
| 62 | } |
| 63 | |
| 64 | ++parsed; |
| 65 | ++id; |
| 66 | |
| 67 | while (isalnum(id[0]) || (id[0] == '_') || (id[0] == '-') || (id[0] == '.')) { |
| 68 | ++parsed; |
| 69 | ++id; |
| 70 | } |
| 71 | |
| 72 | return parsed; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @brief Parse a node-identifier. |
| 77 | * |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 78 | * node-identifier = [module-name ":"] identifier |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 79 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 80 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 81 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 82 | * @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] | 83 | * @param[out] name Points to the node name. |
| 84 | * @param[out] nam_len Length of the node name. |
| 85 | * |
| 86 | * @return Number of characters successfully parsed, |
| 87 | * positive on success, negative on failure. |
| 88 | */ |
| 89 | static int |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 90 | 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] | 91 | { |
| 92 | int parsed = 0, ret; |
| 93 | |
| 94 | assert(id); |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 95 | if (mod_name) { |
| 96 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 97 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 98 | if (mod_name_len) { |
| 99 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 100 | } |
| 101 | if (name) { |
| 102 | *name = NULL; |
| 103 | } |
| 104 | if (nam_len) { |
| 105 | *nam_len = 0; |
| 106 | } |
| 107 | |
| 108 | if ((ret = parse_identifier(id)) < 1) { |
| 109 | return ret; |
| 110 | } |
| 111 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 112 | if (mod_name) { |
| 113 | *mod_name = id; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 114 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 115 | if (mod_name_len) { |
| 116 | *mod_name_len = ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | parsed += ret; |
| 120 | id += ret; |
| 121 | |
| 122 | /* there is prefix */ |
| 123 | if (id[0] == ':') { |
| 124 | ++parsed; |
| 125 | ++id; |
| 126 | |
| 127 | /* there isn't */ |
| 128 | } else { |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 129 | if (name && mod_name) { |
| 130 | *name = *mod_name; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 131 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 132 | if (mod_name) { |
| 133 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 136 | if (nam_len && mod_name_len) { |
| 137 | *nam_len = *mod_name_len; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 138 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 139 | if (mod_name_len) { |
| 140 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | return parsed; |
| 144 | } |
| 145 | |
| 146 | /* identifier (node name) */ |
| 147 | if ((ret = parse_identifier(id)) < 1) { |
| 148 | return -parsed+ret; |
| 149 | } |
| 150 | |
| 151 | if (name) { |
| 152 | *name = id; |
| 153 | } |
| 154 | if (nam_len) { |
| 155 | *nam_len = ret; |
| 156 | } |
| 157 | |
| 158 | return parsed+ret; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * @brief Parse a path-predicate (leafref). |
| 163 | * |
| 164 | * path-predicate = "[" *WSP path-equality-expr *WSP "]" |
| 165 | * path-equality-expr = node-identifier *WSP "=" *WSP path-key-expr |
| 166 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 167 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 168 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 169 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 170 | * @param[out] name Points to the node name. |
| 171 | * @param[out] nam_len Length of the node name. |
| 172 | * @param[out] path_key_expr Points to the path-key-expr. |
| 173 | * @param[out] pke_len Length of the path-key-expr. |
| 174 | * @param[out] has_predicate Flag to mark whether there is another predicate following. |
| 175 | * |
| 176 | * @return Number of characters successfully parsed, |
| 177 | * positive on success, negative on failure. |
| 178 | */ |
| 179 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 180 | parse_path_predicate(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, |
| 181 | const char **path_key_expr, int *pke_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 182 | { |
| 183 | const char *ptr; |
| 184 | int parsed = 0, ret; |
| 185 | |
| 186 | assert(id); |
| 187 | if (prefix) { |
| 188 | *prefix = NULL; |
| 189 | } |
| 190 | if (pref_len) { |
| 191 | *pref_len = 0; |
| 192 | } |
| 193 | if (name) { |
| 194 | *name = NULL; |
| 195 | } |
| 196 | if (nam_len) { |
| 197 | *nam_len = 0; |
| 198 | } |
| 199 | if (path_key_expr) { |
| 200 | *path_key_expr = NULL; |
| 201 | } |
| 202 | if (pke_len) { |
| 203 | *pke_len = 0; |
| 204 | } |
| 205 | if (has_predicate) { |
| 206 | *has_predicate = 0; |
| 207 | } |
| 208 | |
| 209 | if (id[0] != '[') { |
| 210 | return -parsed; |
| 211 | } |
| 212 | |
| 213 | ++parsed; |
| 214 | ++id; |
| 215 | |
| 216 | while (isspace(id[0])) { |
| 217 | ++parsed; |
| 218 | ++id; |
| 219 | } |
| 220 | |
| 221 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) { |
| 222 | return -parsed+ret; |
| 223 | } |
| 224 | |
| 225 | parsed += ret; |
| 226 | id += ret; |
| 227 | |
| 228 | while (isspace(id[0])) { |
| 229 | ++parsed; |
| 230 | ++id; |
| 231 | } |
| 232 | |
| 233 | if (id[0] != '=') { |
| 234 | return -parsed; |
| 235 | } |
| 236 | |
| 237 | ++parsed; |
| 238 | ++id; |
| 239 | |
| 240 | while (isspace(id[0])) { |
| 241 | ++parsed; |
| 242 | ++id; |
| 243 | } |
| 244 | |
| 245 | if ((ptr = strchr(id, ']')) == NULL) { |
| 246 | return -parsed; |
| 247 | } |
| 248 | |
| 249 | --ptr; |
| 250 | while (isspace(ptr[0])) { |
| 251 | --ptr; |
| 252 | } |
| 253 | ++ptr; |
| 254 | |
| 255 | ret = ptr-id; |
| 256 | if (path_key_expr) { |
| 257 | *path_key_expr = id; |
| 258 | } |
| 259 | if (pke_len) { |
| 260 | *pke_len = ret; |
| 261 | } |
| 262 | |
| 263 | parsed += ret; |
| 264 | id += ret; |
| 265 | |
| 266 | while (isspace(id[0])) { |
| 267 | ++parsed; |
| 268 | ++id; |
| 269 | } |
| 270 | |
| 271 | assert(id[0] == ']'); |
| 272 | |
| 273 | if (id[1] == '[') { |
| 274 | *has_predicate = 1; |
| 275 | } |
| 276 | |
| 277 | return parsed+1; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * @brief Parse a path-key-expr (leafref). First call parses "current()", all |
| 282 | * the ".." and the first node-identifier, other calls parse a single |
| 283 | * node-identifier each. |
| 284 | * |
| 285 | * path-key-expr = current-function-invocation *WSP "/" *WSP |
| 286 | * rel-path-keyexpr |
| 287 | * rel-path-keyexpr = 1*(".." *WSP "/" *WSP) |
| 288 | * *(node-identifier *WSP "/" *WSP) |
| 289 | * node-identifier |
| 290 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 291 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 292 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 293 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 294 | * @param[out] name Points to the node name. |
| 295 | * @param[out] nam_len Length of the node name. |
| 296 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 297 | * must not be changed between consecutive calls. |
| 298 | * @return Number of characters successfully parsed, |
| 299 | * positive on success, negative on failure. |
| 300 | */ |
| 301 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 302 | parse_path_key_expr(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, |
| 303 | int *parent_times) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 304 | { |
| 305 | int parsed = 0, ret, par_times = 0; |
| 306 | |
| 307 | assert(id); |
| 308 | assert(parent_times); |
| 309 | if (prefix) { |
| 310 | *prefix = NULL; |
| 311 | } |
| 312 | if (pref_len) { |
| 313 | *pref_len = 0; |
| 314 | } |
| 315 | if (name) { |
| 316 | *name = NULL; |
| 317 | } |
| 318 | if (nam_len) { |
| 319 | *nam_len = 0; |
| 320 | } |
| 321 | |
| 322 | if (!*parent_times) { |
| 323 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
| 324 | if (strncmp(id, "current()", 9)) { |
| 325 | return -parsed; |
| 326 | } |
| 327 | |
| 328 | parsed += 9; |
| 329 | id += 9; |
| 330 | |
| 331 | while (isspace(id[0])) { |
| 332 | ++parsed; |
| 333 | ++id; |
| 334 | } |
| 335 | |
| 336 | if (id[0] != '/') { |
| 337 | return -parsed; |
| 338 | } |
| 339 | |
| 340 | ++parsed; |
| 341 | ++id; |
| 342 | |
| 343 | while (isspace(id[0])) { |
| 344 | ++parsed; |
| 345 | ++id; |
| 346 | } |
| 347 | |
| 348 | /* rel-path-keyexpr */ |
| 349 | if (strncmp(id, "..", 2)) { |
| 350 | return -parsed; |
| 351 | } |
| 352 | ++par_times; |
| 353 | |
| 354 | parsed += 2; |
| 355 | id += 2; |
| 356 | |
| 357 | while (isspace(id[0])) { |
| 358 | ++parsed; |
| 359 | ++id; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | /* 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier |
| 364 | * |
| 365 | * first parent reference with whitespaces already parsed |
| 366 | */ |
| 367 | if (id[0] != '/') { |
| 368 | return -parsed; |
| 369 | } |
| 370 | |
| 371 | ++parsed; |
| 372 | ++id; |
| 373 | |
| 374 | while (isspace(id[0])) { |
| 375 | ++parsed; |
| 376 | ++id; |
| 377 | } |
| 378 | |
| 379 | while (!strncmp(id, "..", 2) && !*parent_times) { |
| 380 | ++par_times; |
| 381 | |
| 382 | parsed += 2; |
| 383 | id += 2; |
| 384 | |
| 385 | while (isspace(id[0])) { |
| 386 | ++parsed; |
| 387 | ++id; |
| 388 | } |
| 389 | |
| 390 | if (id[0] != '/') { |
| 391 | return -parsed; |
| 392 | } |
| 393 | |
| 394 | ++parsed; |
| 395 | ++id; |
| 396 | |
| 397 | while (isspace(id[0])) { |
| 398 | ++parsed; |
| 399 | ++id; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | if (!*parent_times) { |
| 404 | *parent_times = par_times; |
| 405 | } |
| 406 | |
| 407 | /* all parent references must be parsed at this point */ |
| 408 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) { |
| 409 | return -parsed+ret; |
| 410 | } |
| 411 | |
| 412 | parsed += ret; |
| 413 | id += ret; |
| 414 | |
| 415 | return parsed; |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * @brief Parse path-arg (leafref). |
| 420 | * |
| 421 | * path-arg = absolute-path / relative-path |
| 422 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 423 | * relative-path = 1*(".." "/") descendant-path |
| 424 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 425 | * @param[in] id Identifier to use. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 426 | * @param[out] prefix Points to the prefix, NULL if there is not any. |
| 427 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 428 | * @param[out] name Points to the node name. |
| 429 | * @param[out] nam_len Length of the node name. |
| 430 | * @param[out] parent_times Number of ".." in the path. Must be 0 on the first call, |
| 431 | * must not be changed between consecutive calls. -1 if the |
| 432 | * path is relative. |
| 433 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 434 | * |
| 435 | * @return Number of characters successfully parsed, |
| 436 | * positive on success, negative on failure. |
| 437 | */ |
| 438 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 439 | parse_path_arg(const char *id, const char **prefix, int *pref_len, const char **name, int *nam_len, int *parent_times, |
| 440 | int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 441 | { |
| 442 | int parsed = 0, ret, par_times = 0; |
| 443 | |
| 444 | assert(id); |
| 445 | assert(parent_times); |
| 446 | if (prefix) { |
| 447 | *prefix = NULL; |
| 448 | } |
| 449 | if (pref_len) { |
| 450 | *pref_len = 0; |
| 451 | } |
| 452 | if (name) { |
| 453 | *name = NULL; |
| 454 | } |
| 455 | if (nam_len) { |
| 456 | *nam_len = 0; |
| 457 | } |
| 458 | if (has_predicate) { |
| 459 | *has_predicate = 0; |
| 460 | } |
| 461 | |
| 462 | if (!*parent_times && !strncmp(id, "..", 2)) { |
| 463 | ++par_times; |
| 464 | |
| 465 | parsed += 2; |
| 466 | id += 2; |
| 467 | |
| 468 | while (!strncmp(id, "/..", 3)) { |
| 469 | ++par_times; |
| 470 | |
| 471 | parsed += 3; |
| 472 | id += 3; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | if (!*parent_times) { |
| 477 | if (par_times) { |
| 478 | *parent_times = par_times; |
| 479 | } else { |
| 480 | *parent_times = -1; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | if (id[0] != '/') { |
| 485 | return -parsed; |
| 486 | } |
| 487 | |
| 488 | /* skip '/' */ |
| 489 | ++parsed; |
| 490 | ++id; |
| 491 | |
| 492 | /* node-identifier ([prefix:]identifier) */ |
| 493 | if ((ret = parse_node_identifier(id, prefix, pref_len, name, nam_len)) < 1) { |
| 494 | return -parsed-ret; |
| 495 | } |
| 496 | |
| 497 | parsed += ret; |
| 498 | id += ret; |
| 499 | |
| 500 | /* there is no predicate */ |
| 501 | if ((id[0] == '/') || !id[0]) { |
| 502 | return parsed; |
| 503 | } else if (id[0] != '[') { |
| 504 | return -parsed; |
| 505 | } |
| 506 | |
| 507 | if (has_predicate) { |
| 508 | *has_predicate = 1; |
| 509 | } |
| 510 | |
| 511 | return parsed; |
| 512 | } |
| 513 | |
| 514 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 515 | * @brief Parse instance-identifier in JSON data format. That means that prefixes |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 516 | * (which are mandatory) are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 517 | * |
| 518 | * instance-identifier = 1*("/" (node-identifier *predicate)) |
| 519 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 520 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 521 | * @param[out] model Points to the model name. |
| 522 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 523 | * @param[out] name Points to the node name. |
| 524 | * @param[out] nam_len Length of the node name. |
| 525 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 526 | * |
| 527 | * @return Number of characters successfully parsed, |
| 528 | * positive on success, negative on failure. |
| 529 | */ |
| 530 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 531 | parse_instance_identifier(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 532 | int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 533 | { |
| 534 | int parsed = 0, ret; |
| 535 | |
| 536 | assert(id); |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 537 | if (model) { |
| 538 | *model = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 539 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 540 | if (mod_len) { |
| 541 | *mod_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 542 | } |
| 543 | if (name) { |
| 544 | *name = NULL; |
| 545 | } |
| 546 | if (nam_len) { |
| 547 | *nam_len = 0; |
| 548 | } |
| 549 | if (has_predicate) { |
| 550 | *has_predicate = 0; |
| 551 | } |
| 552 | |
| 553 | if (id[0] != '/') { |
| 554 | return -parsed; |
| 555 | } |
| 556 | |
| 557 | ++parsed; |
| 558 | ++id; |
| 559 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 560 | 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] | 561 | return -parsed+ret; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 562 | } else if (model && !*model) { |
| 563 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | parsed += ret; |
| 567 | id += ret; |
| 568 | |
| 569 | if ((id[0] == '[') && has_predicate) { |
| 570 | *has_predicate = 1; |
| 571 | } |
| 572 | |
| 573 | return parsed; |
| 574 | } |
| 575 | |
| 576 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 577 | * @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] | 578 | * (which are mandatory) are actually model names. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 579 | * |
| 580 | * predicate = "[" *WSP (predicate-expr / pos) *WSP "]" |
| 581 | * predicate-expr = (node-identifier / ".") *WSP "=" *WSP |
| 582 | * ((DQUOTE string DQUOTE) / |
| 583 | * (SQUOTE string SQUOTE)) |
| 584 | * pos = non-negative-integer-value |
| 585 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 586 | * @param[in] id Identifier to use. |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 587 | * @param[out] model Points to the model name. |
| 588 | * @param[out] mod_len Length of the model name. |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 589 | * @param[out] name Points to the node name. Can be identifier (from node-identifier), "." or pos. |
| 590 | * @param[out] nam_len Length of the node name. |
| 591 | * @param[out] value Value the node-identifier must have (string from the grammar), |
| 592 | * NULL if there is not any. |
| 593 | * @param[out] val_len Length of the value, 0 if there is not any. |
| 594 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 595 | * |
| 596 | * @return Number of characters successfully parsed, |
| 597 | * positive on success, negative on failure. |
| 598 | */ |
| 599 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 600 | parse_predicate(const char *id, const char **model, int *mod_len, const char **name, int *nam_len, |
| 601 | const char **value, int *val_len, int *has_predicate) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 602 | { |
| 603 | const char *ptr; |
| 604 | int parsed = 0, ret; |
| 605 | char quote; |
| 606 | |
| 607 | assert(id); |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 608 | if (model) { |
| 609 | *model = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 610 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 611 | if (mod_len) { |
| 612 | *mod_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 613 | } |
| 614 | if (name) { |
| 615 | *name = NULL; |
| 616 | } |
| 617 | if (nam_len) { |
| 618 | *nam_len = 0; |
| 619 | } |
| 620 | if (value) { |
| 621 | *value = NULL; |
| 622 | } |
| 623 | if (val_len) { |
| 624 | *val_len = 0; |
| 625 | } |
| 626 | if (has_predicate) { |
| 627 | *has_predicate = 0; |
| 628 | } |
| 629 | |
| 630 | if (id[0] != '[') { |
| 631 | return -parsed; |
| 632 | } |
| 633 | |
| 634 | ++parsed; |
| 635 | ++id; |
| 636 | |
| 637 | while (isspace(id[0])) { |
| 638 | ++parsed; |
| 639 | ++id; |
| 640 | } |
| 641 | |
| 642 | /* pos */ |
| 643 | if (isdigit(id[0])) { |
| 644 | if (name) { |
| 645 | *name = id; |
| 646 | } |
| 647 | |
| 648 | if (id[0] == '0') { |
| 649 | ++parsed; |
| 650 | ++id; |
| 651 | |
| 652 | if (isdigit(id[0])) { |
| 653 | return -parsed; |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | while (isdigit(id[0])) { |
| 658 | ++parsed; |
| 659 | ++id; |
| 660 | } |
| 661 | |
| 662 | if (nam_len) { |
| 663 | *nam_len = id-(*name); |
| 664 | } |
| 665 | |
| 666 | /* "." */ |
| 667 | } else if (id[0] == '.') { |
| 668 | if (name) { |
| 669 | *name = id; |
| 670 | } |
| 671 | if (nam_len) { |
| 672 | *nam_len = 1; |
| 673 | } |
| 674 | |
| 675 | ++parsed; |
| 676 | ++id; |
| 677 | |
| 678 | /* node-identifier */ |
| 679 | } else { |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 680 | 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] | 681 | return -parsed+ret; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 682 | } else if (model && !*model) { |
| 683 | return -parsed; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 684 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 685 | |
| 686 | parsed += ret; |
| 687 | id += ret; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | while (isspace(id[0])) { |
| 691 | ++parsed; |
| 692 | ++id; |
| 693 | } |
| 694 | |
| 695 | if (id[0] != '=') { |
| 696 | return -parsed; |
| 697 | } |
| 698 | |
| 699 | ++parsed; |
| 700 | ++id; |
| 701 | |
| 702 | while (isspace(id[0])) { |
| 703 | ++parsed; |
| 704 | ++id; |
| 705 | } |
| 706 | |
| 707 | /* ((DQUOTE string DQUOTE) / (SQUOTE string SQUOTE)) */ |
| 708 | if ((id[0] == '\"') || (id[0] == '\'')) { |
| 709 | quote = id[0]; |
| 710 | |
| 711 | ++parsed; |
| 712 | ++id; |
| 713 | |
| 714 | if ((ptr = strchr(id, quote)) == NULL) { |
| 715 | return -parsed; |
| 716 | } |
| 717 | ret = ptr-id; |
| 718 | |
| 719 | if (value) { |
| 720 | *value = id; |
| 721 | } |
| 722 | if (val_len) { |
| 723 | *val_len = ret; |
| 724 | } |
| 725 | |
| 726 | parsed += ret+1; |
| 727 | id += ret+1; |
| 728 | } else { |
| 729 | return -parsed; |
| 730 | } |
| 731 | |
| 732 | while (isspace(id[0])) { |
| 733 | ++parsed; |
| 734 | ++id; |
| 735 | } |
| 736 | |
| 737 | if (id[0] != ']') { |
| 738 | return -parsed; |
| 739 | } |
| 740 | |
| 741 | ++parsed; |
| 742 | ++id; |
| 743 | |
| 744 | if ((id[0] == '[') && has_predicate) { |
| 745 | *has_predicate = 1; |
| 746 | } |
| 747 | |
| 748 | return parsed; |
| 749 | } |
| 750 | |
| 751 | /** |
| 752 | * @brief Parse schema-nodeid. |
| 753 | * |
| 754 | * schema-nodeid = absolute-schema-nodeid / |
| 755 | * descendant-schema-nodeid |
| 756 | * absolute-schema-nodeid = 1*("/" node-identifier) |
| 757 | * descendant-schema-nodeid = |
| 758 | * node-identifier |
| 759 | * absolute-schema-nodeid |
| 760 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 761 | * @param[in] id Identifier to use. |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 762 | * @param[out] mod_name Points to the module name, NULL if there is not any. |
| 763 | * @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] | 764 | * @param[out] name Points to the node name. Can be identifier (from node-identifier), "." or pos. |
| 765 | * @param[out] nam_len Length of the node name. |
| 766 | * @param[out] is_relative Flag to mark whether the nodeid is absolute or descendant. Must be -1 |
| 767 | * on the first call, must not be changed between consecutive calls. |
| 768 | * |
| 769 | * @return Number of characters successfully parsed, |
| 770 | * positive on success, negative on failure. |
| 771 | */ |
| 772 | static int |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 773 | parse_schema_nodeid(const char *id, const char **mod_name, int *mod_name_len, const char **name, int *nam_len, |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 774 | int *is_relative) |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 775 | { |
| 776 | int parsed = 0, ret; |
| 777 | |
| 778 | assert(id); |
| 779 | assert(is_relative); |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 780 | if (mod_name) { |
| 781 | *mod_name = NULL; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 782 | } |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 783 | if (mod_name_len) { |
| 784 | *mod_name_len = 0; |
Radek Krejci | 6dc53a2 | 2015-08-17 13:27:59 +0200 | [diff] [blame] | 785 | } |
| 786 | if (name) { |
| 787 | *name = NULL; |
| 788 | } |
| 789 | if (nam_len) { |
| 790 | *nam_len = 0; |
| 791 | } |
| 792 | |
| 793 | if (id[0] != '/') { |
| 794 | if (*is_relative != -1) { |
| 795 | return -parsed; |
| 796 | } else { |
| 797 | *is_relative = 1; |
| 798 | } |
| 799 | } else { |
| 800 | if (*is_relative == -1) { |
| 801 | *is_relative = 0; |
| 802 | } |
| 803 | ++parsed; |
| 804 | ++id; |
| 805 | } |
| 806 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 807 | 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] | 808 | return -parsed+ret; |
| 809 | } |
| 810 | |
| 811 | return parsed+ret; |
| 812 | } |
| 813 | |
| 814 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 815 | * @brief Resolves length or range intervals. Does not log. |
| 816 | * Syntax is assumed to be correct, *local_intv MUST be NULL. |
| 817 | * |
| 818 | * @param[in] str_restr The restriction as a string. |
| 819 | * @param[in] type The type of the restriction. |
| 820 | * @param[in] superior_restr Flag whether to check superior |
| 821 | * types. |
| 822 | * @param[out] local_intv The final interval structure. |
| 823 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 824 | * @return EXIT_SUCCESS on succes, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 825 | */ |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 826 | int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 827 | resolve_len_ran_interval(const char *str_restr, struct lys_type *type, int superior_restr, |
| 828 | struct len_ran_intv** local_intv) |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 829 | { |
| 830 | /* 0 - unsigned, 1 - signed, 2 - floating point */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 831 | int kind, rc = EXIT_SUCCESS; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 832 | int64_t local_smin, local_smax; |
| 833 | uint64_t local_umin, local_umax; |
| 834 | long double local_fmin, local_fmax; |
| 835 | const char *seg_ptr, *ptr; |
Michal Vasko | e01eca5 | 2015-08-13 14:42:02 +0200 | [diff] [blame] | 836 | struct len_ran_intv *tmp_local_intv = NULL, *tmp_intv, *intv = NULL; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 837 | |
| 838 | switch (type->base) { |
| 839 | case LY_TYPE_BINARY: |
| 840 | kind = 0; |
| 841 | local_umin = 0; |
| 842 | local_umax = 18446744073709551615UL; |
| 843 | |
| 844 | if (!str_restr && type->info.binary.length) { |
| 845 | str_restr = type->info.binary.length->expr; |
| 846 | } |
| 847 | break; |
| 848 | case LY_TYPE_DEC64: |
| 849 | kind = 2; |
| 850 | local_fmin = -9223372036854775808.0; |
| 851 | local_fmin /= 1 << type->info.dec64.dig; |
| 852 | local_fmax = 9223372036854775807.0; |
| 853 | local_fmax /= 1 << type->info.dec64.dig; |
| 854 | |
| 855 | if (!str_restr && type->info.dec64.range) { |
| 856 | str_restr = type->info.dec64.range->expr; |
| 857 | } |
| 858 | break; |
| 859 | case LY_TYPE_INT8: |
| 860 | kind = 1; |
| 861 | local_smin = -128; |
| 862 | local_smax = 127; |
| 863 | |
| 864 | if (!str_restr && type->info.num.range) { |
| 865 | str_restr = type->info.num.range->expr; |
| 866 | } |
| 867 | break; |
| 868 | case LY_TYPE_INT16: |
| 869 | kind = 1; |
| 870 | local_smin = -32768; |
| 871 | local_smax = 32767; |
| 872 | |
| 873 | if (!str_restr && type->info.num.range) { |
| 874 | str_restr = type->info.num.range->expr; |
| 875 | } |
| 876 | break; |
| 877 | case LY_TYPE_INT32: |
| 878 | kind = 1; |
| 879 | local_smin = -2147483648; |
| 880 | local_smax = 2147483647; |
| 881 | |
| 882 | if (!str_restr && type->info.num.range) { |
| 883 | str_restr = type->info.num.range->expr; |
| 884 | } |
| 885 | break; |
| 886 | case LY_TYPE_INT64: |
| 887 | kind = 1; |
| 888 | local_smin = -9223372036854775807L - 1L; |
| 889 | local_smax = 9223372036854775807L; |
| 890 | |
| 891 | if (!str_restr && type->info.num.range) { |
| 892 | str_restr = type->info.num.range->expr; |
| 893 | } |
| 894 | break; |
| 895 | case LY_TYPE_UINT8: |
| 896 | kind = 0; |
| 897 | local_umin = 0; |
| 898 | local_umax = 255; |
| 899 | |
| 900 | if (!str_restr && type->info.num.range) { |
| 901 | str_restr = type->info.num.range->expr; |
| 902 | } |
| 903 | break; |
| 904 | case LY_TYPE_UINT16: |
| 905 | kind = 0; |
| 906 | local_umin = 0; |
| 907 | local_umax = 65535; |
| 908 | |
| 909 | if (!str_restr && type->info.num.range) { |
| 910 | str_restr = type->info.num.range->expr; |
| 911 | } |
| 912 | break; |
| 913 | case LY_TYPE_UINT32: |
| 914 | kind = 0; |
| 915 | local_umin = 0; |
| 916 | local_umax = 4294967295; |
| 917 | |
| 918 | if (!str_restr && type->info.num.range) { |
| 919 | str_restr = type->info.num.range->expr; |
| 920 | } |
| 921 | break; |
| 922 | case LY_TYPE_UINT64: |
| 923 | kind = 0; |
| 924 | local_umin = 0; |
| 925 | local_umax = 18446744073709551615UL; |
| 926 | |
| 927 | if (!str_restr && type->info.num.range) { |
| 928 | str_restr = type->info.num.range->expr; |
| 929 | } |
| 930 | break; |
| 931 | case LY_TYPE_STRING: |
| 932 | kind = 0; |
| 933 | local_umin = 0; |
| 934 | local_umax = 18446744073709551615UL; |
| 935 | |
| 936 | if (!str_restr && type->info.str.length) { |
| 937 | str_restr = type->info.str.length->expr; |
| 938 | } |
| 939 | break; |
| 940 | default: |
| 941 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 942 | return -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | /* process superior types */ |
| 946 | if (type->der && superior_restr) { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 947 | if (resolve_len_ran_interval(NULL, &type->der->type, superior_restr, &intv)) { |
| 948 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 949 | return -1; |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 950 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 951 | assert(!intv || (intv->kind == kind)); |
| 952 | } |
| 953 | |
| 954 | if (!str_restr) { |
| 955 | /* we are validating data and not have any restriction, but a superior type might have */ |
| 956 | if (type->der && !superior_restr && !intv) { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 957 | if (resolve_len_ran_interval(NULL, &type->der->type, superior_restr, &intv)) { |
| 958 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 959 | return -1; |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 960 | } |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 961 | assert(!intv || (intv->kind == kind)); |
| 962 | } |
| 963 | *local_intv = intv; |
| 964 | return EXIT_SUCCESS; |
| 965 | } |
| 966 | |
| 967 | /* adjust local min and max */ |
| 968 | if (intv) { |
| 969 | tmp_intv = intv; |
| 970 | |
| 971 | if (kind == 0) { |
| 972 | local_umin = tmp_intv->value.uval.min; |
| 973 | } else if (kind == 1) { |
| 974 | local_smin = tmp_intv->value.sval.min; |
| 975 | } else if (kind == 2) { |
| 976 | local_fmin = tmp_intv->value.fval.min; |
| 977 | } |
| 978 | |
| 979 | while (tmp_intv->next) { |
| 980 | tmp_intv = tmp_intv->next; |
| 981 | } |
| 982 | |
| 983 | if (kind == 0) { |
| 984 | local_umax = tmp_intv->value.uval.max; |
| 985 | } else if (kind == 1) { |
| 986 | local_smax = tmp_intv->value.sval.max; |
| 987 | } else if (kind == 2) { |
| 988 | local_fmax = tmp_intv->value.fval.max; |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | /* finally parse our restriction */ |
| 993 | seg_ptr = str_restr; |
| 994 | while (1) { |
Michal Vasko | e01eca5 | 2015-08-13 14:42:02 +0200 | [diff] [blame] | 995 | if (!*local_intv && !tmp_local_intv) { |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 996 | *local_intv = malloc(sizeof **local_intv); |
| 997 | tmp_local_intv = *local_intv; |
| 998 | } else { |
| 999 | tmp_local_intv->next = malloc(sizeof **local_intv); |
| 1000 | tmp_local_intv = tmp_local_intv->next; |
| 1001 | } |
| 1002 | |
| 1003 | tmp_local_intv->kind = kind; |
| 1004 | tmp_local_intv->next = NULL; |
| 1005 | |
| 1006 | /* min */ |
| 1007 | ptr = seg_ptr; |
| 1008 | while (isspace(ptr[0])) { |
| 1009 | ++ptr; |
| 1010 | } |
| 1011 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 1012 | if (kind == 0) { |
| 1013 | tmp_local_intv->value.uval.min = atoll(ptr); |
| 1014 | } else if (kind == 1) { |
| 1015 | tmp_local_intv->value.sval.min = atoll(ptr); |
| 1016 | } else if (kind == 2) { |
| 1017 | tmp_local_intv->value.fval.min = atoll(ptr); |
| 1018 | } |
| 1019 | |
| 1020 | if ((ptr[0] == '+') || (ptr[0] == '-')) { |
| 1021 | ++ptr; |
| 1022 | } |
| 1023 | while (isdigit(ptr[0])) { |
| 1024 | ++ptr; |
| 1025 | } |
| 1026 | } else if (!strncmp(ptr, "min", 3)) { |
| 1027 | if (kind == 0) { |
| 1028 | tmp_local_intv->value.uval.min = local_umin; |
| 1029 | } else if (kind == 1) { |
| 1030 | tmp_local_intv->value.sval.min = local_smin; |
| 1031 | } else if (kind == 2) { |
| 1032 | tmp_local_intv->value.fval.min = local_fmin; |
| 1033 | } |
| 1034 | |
| 1035 | ptr += 3; |
| 1036 | } else if (!strncmp(ptr, "max", 3)) { |
| 1037 | if (kind == 0) { |
| 1038 | tmp_local_intv->value.uval.min = local_umax; |
| 1039 | } else if (kind == 1) { |
| 1040 | tmp_local_intv->value.sval.min = local_smax; |
| 1041 | } else if (kind == 2) { |
| 1042 | tmp_local_intv->value.fval.min = local_fmax; |
| 1043 | } |
| 1044 | |
| 1045 | ptr += 3; |
| 1046 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 1047 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1048 | rc = -1; |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 1049 | goto cleanup; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | while (isspace(ptr[0])) { |
| 1053 | ptr++; |
| 1054 | } |
| 1055 | |
| 1056 | /* no interval or interval */ |
| 1057 | if ((ptr[0] == '|') || !ptr[0]) { |
| 1058 | if (kind == 0) { |
| 1059 | tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min; |
| 1060 | } else if (kind == 1) { |
| 1061 | tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min; |
| 1062 | } else if (kind == 2) { |
| 1063 | tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min; |
| 1064 | } |
| 1065 | } else if (!strncmp(ptr, "..", 2)) { |
| 1066 | /* skip ".." */ |
| 1067 | ptr += 2; |
| 1068 | while (isspace(ptr[0])) { |
| 1069 | ++ptr; |
| 1070 | } |
| 1071 | |
| 1072 | /* max */ |
| 1073 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 1074 | if (kind == 0) { |
| 1075 | tmp_local_intv->value.uval.max = atoll(ptr); |
| 1076 | } else if (kind == 1) { |
| 1077 | tmp_local_intv->value.sval.max = atoll(ptr); |
| 1078 | } else if (kind == 2) { |
| 1079 | tmp_local_intv->value.fval.max = atoll(ptr); |
| 1080 | } |
| 1081 | } else if (!strncmp(ptr, "max", 3)) { |
| 1082 | if (kind == 0) { |
| 1083 | tmp_local_intv->value.uval.max = local_umax; |
| 1084 | } else if (kind == 1) { |
| 1085 | tmp_local_intv->value.sval.max = local_smax; |
| 1086 | } else if (kind == 2) { |
| 1087 | tmp_local_intv->value.fval.max = local_fmax; |
| 1088 | } |
| 1089 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 1090 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1091 | rc = -1; |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 1092 | goto cleanup; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1093 | } |
| 1094 | } else { |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 1095 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1096 | rc = -1; |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 1097 | goto cleanup; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | /* next segment (next OR) */ |
| 1101 | seg_ptr = strchr(seg_ptr, '|'); |
| 1102 | if (!seg_ptr) { |
| 1103 | break; |
| 1104 | } |
| 1105 | seg_ptr++; |
| 1106 | } |
| 1107 | |
| 1108 | /* check local restrictions against superior ones */ |
| 1109 | if (intv) { |
| 1110 | tmp_intv = intv; |
| 1111 | tmp_local_intv = *local_intv; |
| 1112 | |
| 1113 | while (tmp_local_intv && tmp_intv) { |
| 1114 | /* reuse local variables */ |
| 1115 | if (kind == 0) { |
| 1116 | local_umin = tmp_local_intv->value.uval.min; |
| 1117 | local_umax = tmp_local_intv->value.uval.max; |
| 1118 | |
| 1119 | /* it must be in this interval */ |
| 1120 | if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) { |
| 1121 | /* this interval is covered, next one */ |
| 1122 | if (local_umax <= tmp_intv->value.uval.max) { |
| 1123 | tmp_local_intv = tmp_local_intv->next; |
| 1124 | continue; |
| 1125 | /* ascending order of restrictions -> fail */ |
| 1126 | } else { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1127 | rc = -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1128 | goto cleanup; |
| 1129 | } |
| 1130 | } |
| 1131 | } else if (kind == 1) { |
| 1132 | local_smin = tmp_local_intv->value.sval.min; |
| 1133 | local_smax = tmp_local_intv->value.sval.max; |
| 1134 | |
| 1135 | if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) { |
| 1136 | if (local_smax <= tmp_intv->value.sval.max) { |
| 1137 | tmp_local_intv = tmp_local_intv->next; |
| 1138 | continue; |
| 1139 | } else { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1140 | rc = -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1141 | goto cleanup; |
| 1142 | } |
| 1143 | } |
| 1144 | } else if (kind == 2) { |
| 1145 | local_fmin = tmp_local_intv->value.fval.min; |
| 1146 | local_fmax = tmp_local_intv->value.fval.max; |
| 1147 | |
| 1148 | if ((local_fmin >= tmp_intv->value.fval.min) && (local_fmin <= tmp_intv->value.fval.max)) { |
| 1149 | if (local_fmax <= tmp_intv->value.fval.max) { |
| 1150 | tmp_local_intv = tmp_local_intv->next; |
| 1151 | continue; |
| 1152 | } else { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1153 | rc = -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1154 | goto cleanup; |
| 1155 | } |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | tmp_intv = tmp_intv->next; |
| 1160 | } |
| 1161 | |
| 1162 | /* some interval left uncovered -> fail */ |
| 1163 | if (tmp_local_intv) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1164 | rc = -1; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | } |
| 1168 | |
| 1169 | cleanup: |
| 1170 | while (intv) { |
| 1171 | tmp_intv = intv->next; |
| 1172 | free(intv); |
| 1173 | intv = tmp_intv; |
| 1174 | } |
| 1175 | |
| 1176 | /* fail */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1177 | if (rc) { |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1178 | while (*local_intv) { |
| 1179 | tmp_local_intv = (*local_intv)->next; |
| 1180 | free(*local_intv); |
| 1181 | *local_intv = tmp_local_intv; |
| 1182 | } |
| 1183 | } |
| 1184 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1185 | return rc; |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 1186 | } |
| 1187 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1188 | /** |
| 1189 | * @brief Resolve a typedef. Does not log. |
| 1190 | * |
| 1191 | * @param[in] name Typedef name. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 1192 | * @param[in] mod_name Typedef name module name. |
| 1193 | * @param[in] module Main module. |
| 1194 | * @param[in] parent Parent of the resolved type definition. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1195 | * @param[out] ret Pointer to the resolved typedef. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1196 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1197 | * @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] | 1198 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1199 | int |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 1200 | resolve_superior_type(const char *name, const char *mod_name, struct lys_module *module, struct lys_node *parent, |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1201 | struct lys_tpdf **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1202 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1203 | int i, j; |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1204 | struct lys_tpdf *tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1205 | int tpdf_size; |
| 1206 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 1207 | if (!mod_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1208 | /* no prefix, try built-in types */ |
| 1209 | for (i = 1; i < LY_DATA_TYPE_COUNT; i++) { |
| 1210 | if (!strcmp(ly_types[i].def->name, name)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1211 | if (ret) { |
| 1212 | *ret = ly_types[i].def; |
| 1213 | } |
| 1214 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1215 | } |
| 1216 | } |
| 1217 | } else { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 1218 | if (!strcmp(mod_name, module->name)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1219 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 1220 | mod_name = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1221 | } |
| 1222 | } |
| 1223 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 1224 | if (!mod_name && parent) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1225 | /* search in local typedefs */ |
| 1226 | while (parent) { |
| 1227 | switch (parent->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1228 | case LYS_CONTAINER: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 1229 | tpdf_size = ((struct lys_node_container *)parent)->tpdf_size; |
| 1230 | tpdf = ((struct lys_node_container *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1231 | break; |
| 1232 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1233 | case LYS_LIST: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 1234 | tpdf_size = ((struct lys_node_list *)parent)->tpdf_size; |
| 1235 | tpdf = ((struct lys_node_list *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1236 | break; |
| 1237 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1238 | case LYS_GROUPING: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 1239 | tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size; |
| 1240 | tpdf = ((struct lys_node_grp *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1241 | break; |
| 1242 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1243 | case LYS_RPC: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 1244 | tpdf_size = ((struct lys_node_rpc *)parent)->tpdf_size; |
| 1245 | tpdf = ((struct lys_node_rpc *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1246 | break; |
| 1247 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1248 | case LYS_NOTIF: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 1249 | tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size; |
| 1250 | tpdf = ((struct lys_node_notif *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1251 | break; |
| 1252 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1253 | case LYS_INPUT: |
| 1254 | case LYS_OUTPUT: |
Radek Krejci | 4608ada | 2015-08-05 16:04:37 +0200 | [diff] [blame] | 1255 | tpdf_size = ((struct lys_node_rpc_inout *)parent)->tpdf_size; |
| 1256 | tpdf = ((struct lys_node_rpc_inout *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1257 | break; |
| 1258 | |
| 1259 | default: |
| 1260 | parent = parent->parent; |
| 1261 | continue; |
| 1262 | } |
| 1263 | |
| 1264 | for (i = 0; i < tpdf_size; i++) { |
| 1265 | if (!strcmp(tpdf[i].name, name)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1266 | if (ret) { |
| 1267 | *ret = &tpdf[i]; |
| 1268 | } |
| 1269 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | parent = parent->parent; |
| 1274 | } |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 1275 | } else if (mod_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1276 | /* get module where to search */ |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 1277 | module = lys_get_import_module(module, NULL, 0, mod_name, 0); |
Michal Vasko | c935fff | 2015-08-17 14:02:06 +0200 | [diff] [blame] | 1278 | if (!module) { |
| 1279 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | /* search in top level typedefs */ |
| 1284 | for (i = 0; i < module->tpdf_size; i++) { |
| 1285 | if (!strcmp(module->tpdf[i].name, name)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1286 | if (ret) { |
| 1287 | *ret = &module->tpdf[i]; |
| 1288 | } |
| 1289 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | /* search in submodules */ |
| 1294 | for (i = 0; i < module->inc_size; i++) { |
| 1295 | for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) { |
| 1296 | if (!strcmp(module->inc[i].submodule->tpdf[j].name, name)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1297 | if (ret) { |
| 1298 | *ret = &module->inc[i].submodule->tpdf[j]; |
| 1299 | } |
| 1300 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1301 | } |
| 1302 | } |
| 1303 | } |
| 1304 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1305 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1306 | } |
| 1307 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 1308 | /** |
| 1309 | * @brief Check the default \p value of the \p type. Logs directly. |
| 1310 | * |
| 1311 | * @param[in] type Type definition to use. |
| 1312 | * @param[in] value Default value to check. |
| 1313 | * @param[in] first Whether this is the first resolution try. Affects logging. |
| 1314 | * @param[in] line Line in the input file. |
| 1315 | * |
| 1316 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 1317 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1318 | static int |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 1319 | check_default(struct lys_type *type, const char *value, int first, uint32_t line) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1320 | { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 1321 | struct lyd_node_leaf_list node; |
| 1322 | struct lys_type *iter; |
| 1323 | int ret = EXIT_SUCCESS, found; |
| 1324 | |
| 1325 | /* dummy leaf */ |
| 1326 | node.value_str = value; |
| 1327 | node.value_type = type->base; |
| 1328 | node.schema = calloc(1, sizeof *node.schema); |
| 1329 | node.schema->name = strdup("default"); |
| 1330 | |
| 1331 | if (type->base == LY_TYPE_UNION) { |
| 1332 | found = 0; |
| 1333 | iter = lyp_get_next_union_type(type, NULL, &found); |
| 1334 | while (iter) { |
| 1335 | node.value_type = iter->base; |
| 1336 | |
| 1337 | /* special case with too much effort required and almost no added value */ |
| 1338 | if ((iter->base == LY_TYPE_IDENT) || (iter->base == LY_TYPE_INST)) { |
| 1339 | LOGVAL(LYE_SPEC, line, |
| 1340 | "Union with \"instance-identifier\" or \"identityref\" and a default value is not supported!"); |
| 1341 | ret = -1; |
| 1342 | goto finish; |
| 1343 | } |
| 1344 | |
| 1345 | if (!lyp_parse_value(&node, iter, 1, NULL, UINT_MAX)) { |
| 1346 | break; |
| 1347 | } |
| 1348 | |
| 1349 | found = 0; |
| 1350 | iter = lyp_get_next_union_type(type, iter, &found); |
| 1351 | } |
| 1352 | |
| 1353 | if (!iter) { |
| 1354 | if (!first) { |
| 1355 | LOGVAL(LYE_INVAL, line, node.value_str, "default"); |
| 1356 | } |
| 1357 | ret = EXIT_FAILURE; |
| 1358 | goto finish; |
| 1359 | } |
| 1360 | |
| 1361 | } else if (type->base == LY_TYPE_LEAFREF) { |
| 1362 | if (!type->info.lref.target) { |
| 1363 | ret = EXIT_FAILURE; |
| 1364 | goto finish; |
| 1365 | } |
| 1366 | ret = check_default(&type->info.lref.target->type, value, first, line); |
| 1367 | |
| 1368 | } else if ((type->base == LY_TYPE_INST) || (type->base == LY_TYPE_IDENT)) { |
| 1369 | /* it was converted to JSON format before, nothing else sensible we can do */ |
| 1370 | |
| 1371 | } else { |
| 1372 | ret = lyp_parse_value(&node, type, 1, NULL, (first ? UINT_MAX : line)); |
| 1373 | } |
| 1374 | |
| 1375 | finish: |
| 1376 | if (node.value_type == LY_TYPE_BITS) { |
| 1377 | free(node.value.bit); |
| 1378 | } |
| 1379 | free((char *)node.schema->name); |
| 1380 | free(node.schema); |
| 1381 | |
| 1382 | return ret; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1383 | } |
| 1384 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1385 | /** |
| 1386 | * @brief Check a key for mandatory attributes. Logs directly. |
| 1387 | * |
| 1388 | * @param[in] key The key to check. |
| 1389 | * @param[in] flags What flags to check. |
| 1390 | * @param[in] list The list of all the keys. |
| 1391 | * @param[in] index Index of the key in the key list. |
| 1392 | * @param[in] name The name of the keys. |
| 1393 | * @param[in] len The name length. |
| 1394 | * @param[in] line The line in the input file. |
| 1395 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1396 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1397 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1398 | static int |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 1399 | check_key(struct lys_node_leaf *key, uint8_t flags, struct lys_node_leaf **list, int index, const char *name, int len, |
| 1400 | uint32_t line) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1401 | { |
| 1402 | char *dup = NULL; |
| 1403 | int j; |
| 1404 | |
| 1405 | /* existence */ |
| 1406 | if (!key) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 1407 | if (name[len] != '\0') { |
| 1408 | dup = strdup(name); |
| 1409 | dup[len] = '\0'; |
| 1410 | name = dup; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1411 | } |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1412 | LOGVAL(LYE_KEY_MISS, line, name); |
| 1413 | free(dup); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1414 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | /* uniqueness */ |
| 1418 | for (j = index - 1; j >= 0; j--) { |
| 1419 | if (list[index] == list[j]) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1420 | LOGVAL(LYE_KEY_DUP, line, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1421 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1422 | } |
| 1423 | } |
| 1424 | |
| 1425 | /* key is a leaf */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1426 | if (key->nodetype != LYS_LEAF) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1427 | LOGVAL(LYE_KEY_NLEAF, line, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1428 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | /* type of the leaf is not built-in empty */ |
| 1432 | if (key->type.base == LY_TYPE_EMPTY) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1433 | LOGVAL(LYE_KEY_TYPE, line, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1434 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | /* config attribute is the same as of the list */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1438 | if ((flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1439 | LOGVAL(LYE_KEY_CONFIG, line, key->name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1440 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1441 | } |
| 1442 | |
| 1443 | return EXIT_SUCCESS; |
| 1444 | } |
| 1445 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1446 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1447 | * @brief Resolve (fill) a unique. Logs directly. |
| 1448 | * |
| 1449 | * @param[in] parent The parent node of the unique structure. |
| 1450 | * @param[in] uniq_str The value of the unique node. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 1451 | * @param[in] uniq_s The unique structure to use. |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 1452 | * @param[in] first Whether this is the first resolution try. Affects logging. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1453 | * @param[in] line The line in the input file. |
| 1454 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1455 | * @return EXIT_SUCCESS on succes, EXIT_FAILURE on forward reference, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1456 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1457 | int |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 1458 | resolve_unique(struct lys_node *parent, const char *uniq_str, struct lys_unique *uniq_s, int first, uint32_t line) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1459 | { |
| 1460 | char *uniq_val, *uniq_begin, *start; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1461 | int i, j, rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1462 | |
| 1463 | /* count the number of unique values */ |
| 1464 | uniq_val = uniq_begin = strdup(uniq_str); |
| 1465 | uniq_s->leafs_size = 0; |
| 1466 | while ((uniq_val = strpbrk(uniq_val, " \t\n"))) { |
| 1467 | uniq_s->leafs_size++; |
| 1468 | while (isspace(*uniq_val)) { |
| 1469 | uniq_val++; |
| 1470 | } |
| 1471 | } |
| 1472 | uniq_s->leafs_size++; |
| 1473 | uniq_s->leafs = calloc(uniq_s->leafs_size, sizeof *uniq_s->leafs); |
| 1474 | |
| 1475 | /* interconnect unique values with the leafs */ |
| 1476 | uniq_val = uniq_begin; |
| 1477 | for (i = 0; uniq_val && i < uniq_s->leafs_size; i++) { |
| 1478 | start = uniq_val; |
| 1479 | if ((uniq_val = strpbrk(start, " \t\n"))) { |
| 1480 | *uniq_val = '\0'; /* add terminating NULL byte */ |
| 1481 | uniq_val++; |
| 1482 | while (isspace(*uniq_val)) { |
| 1483 | uniq_val++; |
| 1484 | } |
| 1485 | } /* else only one nodeid present/left already NULL byte terminated */ |
| 1486 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1487 | rc = resolve_schema_nodeid(start, parent->child, parent->module, LYS_LEAF, |
| 1488 | (struct lys_node **)&uniq_s->leafs[i]); |
| 1489 | if (rc) { |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 1490 | if ((rc == -1) || !first) { |
| 1491 | LOGVAL(LYE_INARG, line, start, "unique"); |
| 1492 | if (rc == EXIT_FAILURE) { |
| 1493 | LOGVAL(LYE_SPEC, 0, "Target leaf not found."); |
| 1494 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1495 | } |
| 1496 | goto error; |
| 1497 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1498 | if (uniq_s->leafs[i]->nodetype != LYS_LEAF) { |
| 1499 | LOGVAL(LYE_INARG, line, start, "unique"); |
| 1500 | LOGVAL(LYE_SPEC, 0, "Target is not a leaf."); |
| 1501 | rc = -1; |
| 1502 | goto error; |
| 1503 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1504 | |
| 1505 | for (j = 0; j < i; j++) { |
| 1506 | if (uniq_s->leafs[j] == uniq_s->leafs[i]) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1507 | LOGVAL(LYE_INARG, line, start, "unique"); |
| 1508 | LOGVAL(LYE_SPEC, 0, "The identifier is not unique"); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1509 | rc = -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1510 | goto error; |
| 1511 | } |
| 1512 | } |
| 1513 | } |
| 1514 | |
| 1515 | free(uniq_begin); |
| 1516 | return EXIT_SUCCESS; |
| 1517 | |
| 1518 | error: |
| 1519 | |
| 1520 | free(uniq_s->leafs); |
| 1521 | free(uniq_begin); |
| 1522 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1523 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1524 | } |
| 1525 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1526 | /** |
| 1527 | * @brief Resolve (fill) a grouping in an uses. Logs directly. |
| 1528 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 1529 | * @param[in] uses The uses to use. |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 1530 | * @param[in] first Whether this is the first resolution try. Affects logging. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1531 | * @param[in] line The line in the input file. |
| 1532 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1533 | * @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] | 1534 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1535 | static int |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 1536 | resolve_grouping(struct lys_node_uses *uses, int first, uint32_t line) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1537 | { |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 1538 | struct lys_module *module; |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 1539 | const char *mod_name, *name; |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 1540 | int i, mod_name_len, nam_len; |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 1541 | struct lys_node *start; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1542 | |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 1543 | /* parse the identifier, it must be parsed on one call */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 1544 | if ((i = parse_node_identifier(uses->name, &mod_name, &mod_name_len, &name, &nam_len)) < 1) { |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 1545 | LOGVAL(LYE_INCHAR, line, uses->name[-i], &uses->name[-i]); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1546 | return -1; |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 1547 | } else if (uses->name[i]) { |
| 1548 | LOGVAL(LYE_INCHAR, line, uses->name[i], &uses->name[i]); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1549 | return -1; |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 1550 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1551 | |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 1552 | if (mod_name) { |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 1553 | module = lys_get_import_module(uses->module, NULL, 0, mod_name, mod_name_len); |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 1554 | if (!module) { |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 1555 | LOGVAL(LYE_INMOD_LEN, line, mod_name_len, mod_name); |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 1556 | return -1; |
| 1557 | } |
Michal Vasko | 2bdcca9 | 2015-08-17 16:00:45 +0200 | [diff] [blame] | 1558 | start = module->data; |
| 1559 | } else { |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 1560 | start = (struct lys_node *)uses; |
Michal Vasko | 2bdcca9 | 2015-08-17 16:00:45 +0200 | [diff] [blame] | 1561 | } |
| 1562 | |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 1563 | uses->grp = lys_find_grouping_up(name, start, 1); |
| 1564 | if (uses->grp) { |
| 1565 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1566 | } |
| 1567 | |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 1568 | if (mod_name || !first) { |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 1569 | LOGVAL(LYE_INRESOLV, line, "grouping", uses->name); |
| 1570 | } |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 1571 | /* import must now be fully resolved */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 1572 | if (mod_name) { |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 1573 | return -1; |
| 1574 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1575 | return EXIT_FAILURE; |
| 1576 | } |
| 1577 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1578 | /** |
| 1579 | * @brief Resolve (find) a feature definition. Logs directly. |
| 1580 | * |
| 1581 | * @param[in] name Feature name. |
| 1582 | * @param[in] module Module to search in. |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 1583 | * @param[in] first Whether this is the first resolution try. Affects logging. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1584 | * @param[in] line The line in the input file. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1585 | * @param[out] ret Pointer to the resolved feature. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1586 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1587 | * @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] | 1588 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1589 | static int |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 1590 | resolve_feature(const char *id, struct lys_module *module, int first, uint32_t line, struct lys_feature **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1591 | { |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 1592 | const char *mod_name, *name; |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 1593 | int mod_name_len, nam_len, i, j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1594 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1595 | assert(id); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1596 | assert(module); |
| 1597 | |
| 1598 | /* check prefix */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 1599 | if ((i = parse_node_identifier(id, &mod_name, &mod_name_len, &name, &nam_len)) < 1) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1600 | LOGVAL(LYE_INCHAR, line, id[-i], &id[-i]); |
| 1601 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1602 | } |
| 1603 | |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 1604 | if (mod_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1605 | /* search in imported modules */ |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 1606 | module = lys_get_import_module(module, NULL, 0, mod_name, mod_name_len); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 1607 | if (!module) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1608 | /* identity refers unknown data model */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 1609 | LOGVAL(LYE_INMOD_LEN, line, mod_name_len, mod_name); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1610 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1611 | } |
| 1612 | } else { |
| 1613 | /* search in submodules */ |
| 1614 | for (i = 0; i < module->inc_size; i++) { |
| 1615 | for (j = 0; j < module->inc[i].submodule->features_size; j++) { |
| 1616 | if (!strcmp(name, module->inc[i].submodule->features[j].name)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1617 | if (ret) { |
| 1618 | *ret = &(module->inc[i].submodule->features[j]); |
| 1619 | } |
| 1620 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1621 | } |
| 1622 | } |
| 1623 | } |
| 1624 | } |
| 1625 | |
| 1626 | /* search in the identified module */ |
| 1627 | for (j = 0; j < module->features_size; j++) { |
| 1628 | if (!strcmp(name, module->features[j].name)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1629 | if (ret) { |
| 1630 | *ret = &module->features[j]; |
| 1631 | } |
| 1632 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1633 | } |
| 1634 | } |
| 1635 | |
| 1636 | /* not found */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 1637 | if (!first) { |
| 1638 | LOGVAL(LYE_INRESOLV, line, "feature", id); |
| 1639 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1640 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1641 | } |
| 1642 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1643 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1644 | * @brief Resolve (find) a schema node based on a schema-nodeid. Does not log. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1645 | * |
Michal Vasko | 1be8830 | 2015-10-22 16:07:47 +0200 | [diff] [blame^] | 1646 | * node_type - LYS_AUGMENT (searches also RPCs and notifications, augmented nodes are fine, too) |
Michal Vasko | 2e1a7e4 | 2015-08-06 15:08:32 +0200 | [diff] [blame] | 1647 | * - LYS_USES (only descendant-schema-nodeid allowed, ".." not allowed, always return a grouping) |
Michal Vasko | cc9e12e | 2015-08-04 16:14:37 +0200 | [diff] [blame] | 1648 | * - LYS_CHOICE (search only start->child, only descendant-schema-nodeid allowed) |
Michal Vasko | 2e1a7e4 | 2015-08-06 15:08:32 +0200 | [diff] [blame] | 1649 | * - LYS_LEAF (like LYS_USES, but always returns a data node) |
| 1650 | * |
Michal Vasko | 1be8830 | 2015-10-22 16:07:47 +0200 | [diff] [blame^] | 1651 | * If \p id is absolute, \p start is ignored. If \p id is relative, \p start must be the first child to be searched |
| 1652 | * continuing with its siblings. Normally skips augments except \p node_type LYS_AUGMENT (augmenting an augment node). |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1653 | * |
| 1654 | * @param[in] id Schema-nodeid string. |
| 1655 | * @param[in] start Start of the relative search. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 1656 | * @param[in] mod Module to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1657 | * @param[in] node_type Decides how to modify the search. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1658 | * @param[out] ret Pointer to the matching node. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1659 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1660 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1661 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1662 | int |
| 1663 | resolve_schema_nodeid(const char *id, struct lys_node *start, struct lys_module *mod, LYS_NODE node_type, |
| 1664 | struct lys_node **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1665 | { |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 1666 | const char *name, *mod_name; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1667 | struct lys_node *sibling; |
Michal Vasko | 1be8830 | 2015-10-22 16:07:47 +0200 | [diff] [blame^] | 1668 | int i, opts, nam_len, mod_name_len, is_relative = -1; |
| 1669 | struct lys_module *prev_mod, *prefix_mod, *start_mod; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1670 | /* 0 - in module, 1 - in 1st submodule, 2 - in 2nd submodule, ... */ |
| 1671 | uint8_t in_submod = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1672 | |
| 1673 | assert(mod); |
| 1674 | assert(id); |
| 1675 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 1676 | if ((i = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative)) < 1) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1677 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1678 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1679 | id += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1680 | |
Michal Vasko | 2e1a7e4 | 2015-08-06 15:08:32 +0200 | [diff] [blame] | 1681 | if (!is_relative && (node_type & (LYS_USES | LYS_CHOICE | LYS_LEAF))) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1682 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1683 | } |
| 1684 | |
Michal Vasko | 1be8830 | 2015-10-22 16:07:47 +0200 | [diff] [blame^] | 1685 | /* set options for lys_getnext() */ |
| 1686 | opts = LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE | LYS_GETNEXT_WITHINOUT; |
| 1687 | if (node_type == LYS_USES) { |
| 1688 | opts |= LYS_GETNEXT_WITHGROUPING; |
| 1689 | } |
| 1690 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1691 | /* absolute-schema-nodeid */ |
| 1692 | if (!is_relative) { |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 1693 | if (mod_name) { |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 1694 | start_mod = lys_get_import_module(mod, NULL, 0, mod_name, mod_name_len); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1695 | if (!start_mod) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1696 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1697 | } |
| 1698 | start = start_mod->data; |
| 1699 | } else { |
| 1700 | start = mod->data; |
| 1701 | start_mod = mod; |
| 1702 | } |
| 1703 | /* descendant-schema-nodeid */ |
| 1704 | } else { |
Michal Vasko | 2e1a7e4 | 2015-08-06 15:08:32 +0200 | [diff] [blame] | 1705 | if (start) { |
| 1706 | start_mod = start->module; |
| 1707 | } else { |
| 1708 | start_mod = mod; |
| 1709 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1710 | } |
| 1711 | |
Michal Vasko | 1be8830 | 2015-10-22 16:07:47 +0200 | [diff] [blame^] | 1712 | prev_mod = start_mod; |
| 1713 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1714 | while (1) { |
Michal Vasko | 1be8830 | 2015-10-22 16:07:47 +0200 | [diff] [blame^] | 1715 | sibling = lys_getnext(NULL, start->parent, start_mod, opts); |
| 1716 | while (sibling) { |
Michal Vasko | 1e989c0 | 2015-08-04 12:33:00 +0200 | [diff] [blame] | 1717 | /* name match */ |
Michal Vasko | 2e1a7e4 | 2015-08-06 15:08:32 +0200 | [diff] [blame] | 1718 | if (((sibling->nodetype != LYS_GROUPING) || (node_type == LYS_USES)) |
Michal Vasko | a4b5d32 | 2015-10-09 12:12:37 +0200 | [diff] [blame] | 1719 | && (!(sibling->nodetype & (LYS_RPC | LYS_NOTIF)) || (node_type == LYS_AUGMENT)) |
Michal Vasko | 2e1a7e4 | 2015-08-06 15:08:32 +0200 | [diff] [blame] | 1720 | && ((sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) |
Michal Vasko | 1e989c0 | 2015-08-04 12:33:00 +0200 | [diff] [blame] | 1721 | || (!strncmp(name, "input", 5) && (nam_len == 5) && (sibling->nodetype == LYS_INPUT)) |
Michal Vasko | dcc7a80 | 2015-08-06 11:59:47 +0200 | [diff] [blame] | 1722 | || (!strncmp(name, "output", 6) && (nam_len == 6) && (sibling->nodetype == LYS_OUTPUT)))) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1723 | |
Michal Vasko | 1be8830 | 2015-10-22 16:07:47 +0200 | [diff] [blame^] | 1724 | /* module name match check */ |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 1725 | if (mod_name) { |
Michal Vasko | 1be8830 | 2015-10-22 16:07:47 +0200 | [diff] [blame^] | 1726 | prefix_mod = lys_get_import_module(prev_mod, NULL, 0, mod_name, mod_name_len); |
| 1727 | if (!prefix_mod && (node_type == LYS_AUGMENT)) { |
| 1728 | /* we want augment nodes in this case */ |
| 1729 | prefix_mod = sibling->module; |
| 1730 | if (prefix_mod->type) { |
| 1731 | prefix_mod = ((struct lys_submodule *)prefix_mod)->belongsto; |
| 1732 | } |
| 1733 | if (strncmp(prefix_mod->name, mod_name, mod_name_len) || prefix_mod->name[mod_name_len]) { |
| 1734 | prefix_mod = NULL; |
| 1735 | } |
| 1736 | } |
Michal Vasko | 1e989c0 | 2015-08-04 12:33:00 +0200 | [diff] [blame] | 1737 | if (!prefix_mod) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1738 | return -1; |
Michal Vasko | 1e989c0 | 2015-08-04 12:33:00 +0200 | [diff] [blame] | 1739 | } |
Michal Vasko | 2e1a7e4 | 2015-08-06 15:08:32 +0200 | [diff] [blame] | 1740 | } else { |
Michal Vasko | 1be8830 | 2015-10-22 16:07:47 +0200 | [diff] [blame^] | 1741 | prefix_mod = prev_mod; |
Michal Vasko | 2e1a7e4 | 2015-08-06 15:08:32 +0200 | [diff] [blame] | 1742 | } |
Michal Vasko | 1e989c0 | 2015-08-04 12:33:00 +0200 | [diff] [blame] | 1743 | |
Michal Vasko | 2e1a7e4 | 2015-08-06 15:08:32 +0200 | [diff] [blame] | 1744 | /* modules need to always be checked, we want to skip augments */ |
| 1745 | if (!sibling->module->type) { |
| 1746 | if (prefix_mod != sibling->module) { |
Michal Vasko | 1be8830 | 2015-10-22 16:07:47 +0200 | [diff] [blame^] | 1747 | goto next; |
Michal Vasko | 2e1a7e4 | 2015-08-06 15:08:32 +0200 | [diff] [blame] | 1748 | } |
| 1749 | } else { |
| 1750 | if (prefix_mod != ((struct lys_submodule *)sibling->module)->belongsto) { |
Michal Vasko | 1be8830 | 2015-10-22 16:07:47 +0200 | [diff] [blame^] | 1751 | goto next; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1752 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1753 | } |
Michal Vasko | 1e989c0 | 2015-08-04 12:33:00 +0200 | [diff] [blame] | 1754 | |
Michal Vasko | 1e989c0 | 2015-08-04 12:33:00 +0200 | [diff] [blame] | 1755 | /* the result node? */ |
| 1756 | if (!id[0]) { |
Michal Vasko | 2e1a7e4 | 2015-08-06 15:08:32 +0200 | [diff] [blame] | 1757 | /* we're looking only for groupings, this is a data node */ |
| 1758 | if ((node_type == LYS_USES) && (sibling->nodetype != LYS_GROUPING)) { |
Michal Vasko | 1be8830 | 2015-10-22 16:07:47 +0200 | [diff] [blame^] | 1759 | goto next; |
Michal Vasko | 2e1a7e4 | 2015-08-06 15:08:32 +0200 | [diff] [blame] | 1760 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1761 | if (ret) { |
| 1762 | *ret = sibling; |
| 1763 | } |
| 1764 | return EXIT_SUCCESS; |
Michal Vasko | 1e989c0 | 2015-08-04 12:33:00 +0200 | [diff] [blame] | 1765 | } |
| 1766 | |
Michal Vasko | dcc7a80 | 2015-08-06 11:59:47 +0200 | [diff] [blame] | 1767 | /* we're looking for a grouping (node_type == LYS_USES), |
| 1768 | * but this isn't it, we cannot search inside |
| 1769 | */ |
| 1770 | if (sibling->nodetype == LYS_GROUPING) { |
Michal Vasko | 1be8830 | 2015-10-22 16:07:47 +0200 | [diff] [blame^] | 1771 | goto next; |
Michal Vasko | dcc7a80 | 2015-08-06 11:59:47 +0200 | [diff] [blame] | 1772 | } |
| 1773 | |
Michal Vasko | 1be8830 | 2015-10-22 16:07:47 +0200 | [diff] [blame^] | 1774 | /* remember the module */ |
| 1775 | prev_mod = prefix_mod; |
| 1776 | |
Michal Vasko | 1e989c0 | 2015-08-04 12:33:00 +0200 | [diff] [blame] | 1777 | /* check for shorthand cases - then 'start' does not change */ |
| 1778 | if (!sibling->parent || (sibling->parent->nodetype != LYS_CHOICE) |
| 1779 | || (sibling->nodetype == LYS_CASE)) { |
| 1780 | start = sibling->child; |
| 1781 | } |
| 1782 | break; |
| 1783 | } |
Michal Vasko | 1be8830 | 2015-10-22 16:07:47 +0200 | [diff] [blame^] | 1784 | |
| 1785 | next: |
| 1786 | sibling = lys_getnext(sibling, start->parent, NULL, opts); |
Michal Vasko | 1e989c0 | 2015-08-04 12:33:00 +0200 | [diff] [blame] | 1787 | } |
| 1788 | |
| 1789 | /* we did not find the case in direct siblings */ |
| 1790 | if (node_type == LYS_CHOICE) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1791 | return -1; |
Michal Vasko | 1e989c0 | 2015-08-04 12:33:00 +0200 | [diff] [blame] | 1792 | } |
| 1793 | |
| 1794 | /* no match */ |
| 1795 | if (!sibling) { |
Michal Vasko | 1e989c0 | 2015-08-04 12:33:00 +0200 | [diff] [blame] | 1796 | /* are we done with the included submodules as well? */ |
| 1797 | if (in_submod == start_mod->inc_size) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1798 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1799 | } |
| 1800 | |
Michal Vasko | 1e989c0 | 2015-08-04 12:33:00 +0200 | [diff] [blame] | 1801 | /* we aren't, check the next one */ |
| 1802 | ++in_submod; |
Michal Vasko | 1e989c0 | 2015-08-04 12:33:00 +0200 | [diff] [blame] | 1803 | start = start_mod->inc[in_submod-1].submodule->data; |
| 1804 | continue; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1805 | } |
| 1806 | |
| 1807 | /* we found our submodule */ |
| 1808 | if (in_submod) { |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 1809 | start_mod = (struct lys_module *)start_mod->inc[in_submod-1].submodule; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1810 | in_submod = 0; |
| 1811 | } |
| 1812 | |
Michal Vasko | 723e50c | 2015-10-20 15:20:29 +0200 | [diff] [blame] | 1813 | if ((i = parse_schema_nodeid(id, &mod_name, &mod_name_len, &name, &nam_len, &is_relative)) < 1) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1814 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1815 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1816 | id += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | /* cannot get here */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1820 | LOGINT; |
| 1821 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1822 | } |
| 1823 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 1824 | /* ignores line */ |
| 1825 | static void |
| 1826 | unres_data_del(struct unres_data *unres, uint32_t i) |
| 1827 | { |
| 1828 | /* there are items after the one deleted */ |
| 1829 | if (i+1 < unres->count) { |
| 1830 | /* we only move the data, memory is left allocated, why bother */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 1831 | 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] | 1832 | |
| 1833 | /* deleting the last item */ |
| 1834 | } else if (i == 0) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 1835 | free(unres->node); |
| 1836 | unres->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 1837 | } |
| 1838 | |
| 1839 | /* if there are no items after and it is not the last one, just move the counter */ |
| 1840 | --unres->count; |
| 1841 | } |
| 1842 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 1843 | /** |
| 1844 | * @brief Resolve (find) a data node from a specific module. Does not log. |
| 1845 | * |
| 1846 | * @param[in] mod Module to search in. |
| 1847 | * @param[in] name Name of the data node. |
| 1848 | * @param[in] nam_len Length of the name. |
| 1849 | * @param[in] start Data node to start the search from. |
| 1850 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 1851 | * they are replaced (!!) with the resolvents. |
| 1852 | * |
| 1853 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference. |
| 1854 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1855 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 1856 | resolve_data(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] | 1857 | { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1858 | struct lyd_node *node; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 1859 | int flag; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 1860 | uint32_t i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1861 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 1862 | if (!parents->count) { |
| 1863 | parents->count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 1864 | parents->node = malloc(sizeof *parents->node); |
| 1865 | parents->node[0] = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1866 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 1867 | for (i = 0; i < parents->count;) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 1868 | 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] | 1869 | /* skip */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 1870 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1871 | continue; |
| 1872 | } |
| 1873 | flag = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 1874 | LY_TREE_FOR(parents->node[i] ? parents->node[i]->child : start, node) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1875 | if (node->schema->module == mod && !strncmp(node->schema->name, name, nam_len) |
| 1876 | && node->schema->name[nam_len] == '\0') { |
| 1877 | /* matching target */ |
| 1878 | if (!flag) { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 1879 | /* put node instead of the current parent */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 1880 | parents->node[i] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1881 | flag = 1; |
| 1882 | } else { |
Michal Vasko | 9a47e12 | 2015-09-03 14:26:32 +0200 | [diff] [blame] | 1883 | /* multiple matching, so create a new node */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 1884 | ++parents->count; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 1885 | parents->node = realloc(parents->node, parents->count * sizeof *parents->node); |
| 1886 | parents->node[parents->count-1] = node; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 1887 | ++i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1888 | } |
| 1889 | } |
| 1890 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 1891 | |
| 1892 | if (!flag) { |
| 1893 | /* remove item from the parents list */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 1894 | unres_data_del(parents, i); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 1895 | } else { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 1896 | ++i; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 1897 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1898 | } |
| 1899 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 1900 | return parents->count ? EXIT_SUCCESS : EXIT_FAILURE; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 1901 | } |
| 1902 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1903 | /** |
| 1904 | * @brief Resolve (find) a data node. Does not log. |
| 1905 | * |
| 1906 | * @param[in] prefix Prefix of the data node. |
| 1907 | * @param[in] pref_len Length of the prefix. |
| 1908 | * @param[in] name Name of the data node. |
| 1909 | * @param[in] nam_len Length of the name. |
| 1910 | * @param[in] start Data node to start the search from. |
| 1911 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 1912 | * they are replaced (!!) with the resolvents. |
| 1913 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 1914 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1915 | */ |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 1916 | static int |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 1917 | resolve_data_nodeid(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] | 1918 | struct unres_data *parents) |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 1919 | { |
| 1920 | struct lys_module *mod; |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 1921 | char *str; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 1922 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 1923 | assert(start); |
| 1924 | |
Michal Vasko | 31fc367 | 2015-10-21 12:08:13 +0200 | [diff] [blame] | 1925 | if (mod_name) { |
| 1926 | /* we have mod_name, find appropriate module */ |
| 1927 | str = strndup(mod_name, mod_name_len); |
| 1928 | mod = ly_ctx_get_module(start->schema->module->ctx, str, NULL); |
| 1929 | free(str); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 1930 | if (!mod) { |
| 1931 | /* invalid prefix */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 1932 | return -1; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 1933 | } |
| 1934 | } else { |
| 1935 | /* no prefix, module is the same as of current node */ |
| 1936 | mod = start->schema->module; |
| 1937 | } |
| 1938 | |
| 1939 | return resolve_data(mod, name, name_len, start, parents); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1940 | } |
| 1941 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1942 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 1943 | * @brief Resolve a path predicate (leafref) in JSON data context. Logs directly |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 1944 | * only specific errors, general no-resolvent error is left to the caller, |
| 1945 | * but line fail is always displayed. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1946 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 1947 | * @param[in] pred Predicate to use. |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 1948 | * @param[in] first Whether this is the first resolution try. Affects logging. |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 1949 | * @param[in] line Line in the input file. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1950 | * @param[in,out] node_match Nodes satisfying the restriction |
| 1951 | * without the predicate. Nodes not |
| 1952 | * satisfying the predicate are removed. |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 1953 | * @param[out] parsed Number of characters parsed, negative on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1954 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 1955 | * @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] | 1956 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1957 | static int |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 1958 | resolve_path_predicate_data(const char *pred, int first, uint32_t line, struct unres_data *node_match, int *parsed) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1959 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1960 | /* ... /node[source = destination] ... */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 1961 | struct unres_data source_match, dest_match; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1962 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 1963 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, parsed_loc = 0, pke_parsed = 0; |
| 1964 | int has_predicate, dest_parent_times, i, rc; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 1965 | uint32_t j; |
| 1966 | |
| 1967 | source_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 1968 | source_match.node = malloc(sizeof *source_match.node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 1969 | dest_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 1970 | dest_match.node = malloc(sizeof *dest_match.node); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1971 | |
| 1972 | do { |
| 1973 | if ((i = parse_path_predicate(pred, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr, |
| 1974 | &pke_len, &has_predicate)) < 1) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 1975 | LOGVAL(LYE_INCHAR, line, pred[-i], &pred[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 1976 | rc = -1; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 1977 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1978 | } |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 1979 | parsed_loc += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1980 | pred += i; |
| 1981 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 1982 | for (j = 0; j < node_match->count;) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1983 | /* source */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 1984 | source_match.node[0] = node_match->node[j]; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 1985 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1986 | /* must be leaf (key of a list) */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 1987 | if ((rc = resolve_data_nodeid(sour_pref, sour_pref_len, source, sour_len, node_match->node[j], |
| 1988 | &source_match)) || (source_match.count != 1) || (source_match.node[0]->schema->nodetype != LYS_LEAF)) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1989 | /* general error, the one written later will suffice */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 1990 | if ((rc == -1) || !first) { |
| 1991 | LOGVAL(LYE_LINE, line); |
| 1992 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 1993 | i = 0; |
| 1994 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1995 | } |
| 1996 | |
| 1997 | /* destination */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 1998 | dest_match.node[0] = node_match->node[j]; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 1999 | dest_parent_times = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2000 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 2001 | &dest_parent_times)) < 1) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2002 | LOGVAL(LYE_INCHAR, line, path_key_expr[-i], &path_key_expr[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2003 | rc = -1; |
| 2004 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2005 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2006 | pke_parsed = i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2007 | for (i = 0; i < dest_parent_times; ++i) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2008 | dest_match.node[0] = dest_match.node[0]->parent; |
| 2009 | if (!dest_match.node[0]) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 2010 | /* general error, the one written later will suffice */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2011 | if (!first) { |
| 2012 | LOGVAL(LYE_LINE, line); |
| 2013 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2014 | i = 0; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2015 | rc = EXIT_FAILURE; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2016 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2017 | } |
| 2018 | } |
| 2019 | while (1) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2020 | if ((rc = resolve_data_nodeid(dest_pref, dest_pref_len, dest, dest_len, dest_match.node[0], |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2021 | &dest_match)) || (dest_match.count != 1)) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 2022 | /* general error, the one written later will suffice */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2023 | if ((rc == -1) || !first) { |
| 2024 | LOGVAL(LYE_LINE, line); |
| 2025 | } |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2026 | i = 0; |
| 2027 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2028 | } |
| 2029 | |
| 2030 | if (pke_len == pke_parsed) { |
| 2031 | break; |
| 2032 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2033 | 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] | 2034 | &dest_parent_times)) < 1) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2035 | LOGVAL(LYE_INCHAR, line, path_key_expr[-i], &path_key_expr[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2036 | rc = -1; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2037 | goto error; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2038 | } |
| 2039 | pke_parsed += i; |
| 2040 | } |
| 2041 | |
| 2042 | /* check match between source and destination nodes */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2043 | if (((struct lys_node_leaf *)source_match.node[0]->schema)->type.base |
| 2044 | != ((struct lys_node_leaf *)dest_match.node[0]->schema)->type.base) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2045 | goto remove_leafref; |
| 2046 | } |
| 2047 | |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 2048 | if (((struct lyd_node_leaf_list *)source_match.node[0])->value_str |
| 2049 | != ((struct lyd_node_leaf_list *)dest_match.node[0])->value_str) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2050 | goto remove_leafref; |
| 2051 | } |
| 2052 | |
| 2053 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2054 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2055 | continue; |
| 2056 | |
| 2057 | remove_leafref: |
| 2058 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2059 | unres_data_del(node_match, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2060 | } |
| 2061 | } while (has_predicate); |
| 2062 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2063 | free(source_match.node); |
| 2064 | free(dest_match.node); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2065 | if (parsed) { |
| 2066 | *parsed = parsed_loc; |
| 2067 | } |
| 2068 | return EXIT_SUCCESS; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2069 | |
| 2070 | error: |
| 2071 | |
| 2072 | if (source_match.count) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2073 | free(source_match.node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2074 | } |
| 2075 | if (dest_match.count) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2076 | free(dest_match.node); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2077 | } |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2078 | if (parsed) { |
| 2079 | *parsed = -parsed_loc+i; |
| 2080 | } |
| 2081 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2082 | } |
| 2083 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2084 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 2085 | * @brief Resolve a path (leafref) in JSON data context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2086 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2087 | * @param[in] node Leafref data node. |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2088 | * @param[in] path Path of the leafref. |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2089 | * @param[in] first Whether this is the first resolution try. Affects logging. |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2090 | * @param[in] line Line in the input file. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 2091 | * @param[out] ret Matching nodes. Expects an empty, but allocated structure. Lines left untouched. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2092 | * |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2093 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 otherwise. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2094 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2095 | static int |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2096 | resolve_path_arg_data(struct lyd_node *node, const char *path, int first, uint32_t line, struct unres_data *ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2097 | { |
Radek Krejci | 71b795b | 2015-08-10 16:20:39 +0200 | [diff] [blame] | 2098 | struct lyd_node *data = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2099 | const char *prefix, *name; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2100 | int pref_len, nam_len, has_predicate, parent_times, i, parsed, rc; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2101 | uint32_t j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2102 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2103 | assert(node && path && ret && !ret->count); |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2104 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2105 | parent_times = 0; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 2106 | parsed = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2107 | |
| 2108 | /* searching for nodeset */ |
| 2109 | do { |
| 2110 | if ((i = parse_path_arg(path, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) { |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2111 | LOGVAL(LYE_INCHAR, line, path[-i], &path[-i]); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2112 | rc = -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2113 | goto error; |
| 2114 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2115 | path += i; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 2116 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2117 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2118 | if (!ret->count) { |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 2119 | if (parent_times != -1) { |
| 2120 | ret->count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2121 | ret->node = calloc(1, sizeof *ret->node); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 2122 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2123 | for (i = 0; i < parent_times; ++i) { |
| 2124 | /* relative path */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2125 | if (!ret->count) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2126 | /* error, too many .. */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2127 | LOGVAL(LYE_INVAL, line, path, node->schema->name); |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2128 | rc = -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2129 | goto error; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2130 | } else if (!ret->node[0]) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2131 | /* first .. */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2132 | data = ret->node[0] = node->parent; |
| 2133 | } else if (!ret->node[0]->parent) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2134 | /* we are in root */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2135 | ret->count = 0; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2136 | free(ret->node); |
| 2137 | ret->node = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2138 | } else { |
| 2139 | /* multiple .. */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2140 | data = ret->node[0] = ret->node[0]->parent; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2141 | } |
| 2142 | } |
| 2143 | |
| 2144 | /* absolute path */ |
| 2145 | if (parent_times == -1) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2146 | for (data = node; data->parent; data = data->parent); |
Michal Vasko | db9323c | 2015-10-16 10:32:44 +0200 | [diff] [blame] | 2147 | /* we're still parsing it and the pointer is not correct yet */ |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 2148 | if (data->prev) { |
| 2149 | for (; data->prev->next; data = data->prev); |
| 2150 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2151 | } |
| 2152 | } |
| 2153 | |
| 2154 | /* node identifier */ |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2155 | if ((rc = resolve_data_nodeid(prefix, pref_len, name, nam_len, data, ret))) { |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2156 | if ((rc == -1) || !first) { |
| 2157 | LOGVAL(LYE_INELEM_LEN, line, nam_len, name); |
| 2158 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2159 | goto error; |
| 2160 | } |
| 2161 | |
| 2162 | if (has_predicate) { |
| 2163 | /* we have predicate, so the current results must be lists */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2164 | for (j = 0; j < ret->count;) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2165 | if (ret->node[j]->schema->nodetype == LYS_LIST && |
| 2166 | ((struct lys_node_list *)ret->node[0]->schema)->keys) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2167 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2168 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2169 | continue; |
| 2170 | } |
| 2171 | |
| 2172 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2173 | unres_data_del(ret, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2174 | } |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2175 | if ((rc = resolve_path_predicate_data(path, first, line, ret, &i))) { |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 2176 | /* line was already displayed */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2177 | if ((rc == -1) || !first) { |
| 2178 | LOGVAL(LYE_NORESOLV, 0, path); |
| 2179 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2180 | goto error; |
| 2181 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2182 | path += i; |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 2183 | parsed += i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2184 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2185 | if (!ret->count) { |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2186 | if (!first) { |
| 2187 | LOGVAL(LYE_NORESOLV, line, path-parsed); |
| 2188 | } |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2189 | rc = EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2190 | goto error; |
| 2191 | } |
| 2192 | } |
| 2193 | } while (path[0] != '\0'); |
| 2194 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2195 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2196 | |
| 2197 | error: |
| 2198 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2199 | free(ret->node); |
| 2200 | ret->node = NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2201 | ret->count = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2202 | |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2203 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2204 | } |
| 2205 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2206 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 2207 | * @brief Resolve a path (leafref) predicate in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2208 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 2209 | * @param[in] path Path to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2210 | * @param[in] mod Schema module. |
| 2211 | * @param[in] source_node Left operand node. |
| 2212 | * @param[in] dest_node Right ooperand node. |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2213 | * @param[in] first Whether this is the first resolution try. Affects logging. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2214 | * @param[in] line Line in the input file. |
| 2215 | * |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2216 | * @return 0 on forward reference, otherwise the number |
| 2217 | * of characters successfully parsed, |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2218 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2219 | */ |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2220 | static int |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2221 | resolve_path_predicate_schema(const char *path, struct lys_module *mod, struct lys_node *source_node, |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2222 | struct lys_node *dest_node, int first, uint32_t line) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2223 | { |
| 2224 | struct lys_node *src_node, *dst_node; |
| 2225 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
| 2226 | 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] | 2227 | int has_predicate, dest_parent_times = 0, i, rc; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2228 | |
| 2229 | do { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2230 | 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] | 2231 | &pke_len, &has_predicate)) < 1) { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2232 | LOGVAL(LYE_INCHAR, line, path[-i], path-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2233 | return -parsed+i; |
| 2234 | } |
| 2235 | parsed += i; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2236 | path += i; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2237 | |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 2238 | /* source (must be leaf) */ |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2239 | rc = lys_getsibling(mod, source_node->child, sour_pref, sour_pref_len, source, sour_len, LYS_LEAF, &src_node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2240 | if (rc) { |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2241 | if ((rc == -1) || !first) { |
| 2242 | LOGVAL(LYE_NORESOLV, line, path-parsed); |
| 2243 | } |
| 2244 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2245 | } |
| 2246 | |
| 2247 | /* destination */ |
| 2248 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 2249 | &dest_parent_times)) < 1) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 2250 | LOGVAL(LYE_INCHAR, line, path_key_expr[-i], path_key_expr-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2251 | return -parsed; |
| 2252 | } |
| 2253 | pke_parsed += i; |
| 2254 | |
| 2255 | /* dest_node is actually the parent of this leaf, so skip the first ".." */ |
| 2256 | dst_node = dest_node; |
| 2257 | for (i = 1; i < dest_parent_times; ++i) { |
| 2258 | dst_node = dst_node->parent; |
| 2259 | if (!dst_node) { |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2260 | if (!first) { |
| 2261 | LOGVAL(LYE_NORESOLV, line, path_key_expr); |
| 2262 | } |
| 2263 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2264 | } |
| 2265 | } |
| 2266 | while (1) { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2267 | rc = lys_getsibling(mod, dst_node->child, dest_pref, dest_pref_len, dest, dest_len, |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2268 | LYS_CONTAINER | LYS_LIST | LYS_LEAF, &dst_node); |
| 2269 | if (rc) { |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2270 | if ((rc == -1) || !first) { |
| 2271 | LOGVAL(LYE_NORESOLV, line, path_key_expr); |
| 2272 | } |
| 2273 | return 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2274 | } |
| 2275 | |
| 2276 | if (pke_len == pke_parsed) { |
| 2277 | break; |
| 2278 | } |
| 2279 | |
| 2280 | if ((i = parse_path_key_expr(path_key_expr+pke_parsed, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 2281 | &dest_parent_times)) < 1) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 2282 | LOGVAL(LYE_INCHAR, line, (path_key_expr+pke_parsed)[-i], (path_key_expr+pke_parsed)-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2283 | return -parsed; |
| 2284 | } |
| 2285 | pke_parsed += i; |
| 2286 | } |
| 2287 | |
| 2288 | /* check source - dest match */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2289 | if (dst_node->nodetype != LYS_LEAF) { |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 2290 | LOGVAL(LYE_NORESOLV, line, path-parsed); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2291 | LOGVAL(LYE_SPEC, 0, "Destination node not a leaf, but %s.", strnodetype(dst_node->nodetype)); |
| 2292 | return -parsed; |
| 2293 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2294 | } while (has_predicate); |
| 2295 | |
| 2296 | return parsed; |
| 2297 | } |
| 2298 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2299 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 2300 | * @brief Resolve a path (leafref) in JSON schema context. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2301 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 2302 | * @param[in] mod Module to use. |
| 2303 | * @param[in] path Path to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2304 | * @param[in] parent_node Parent of the leafref. |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2305 | * @param[in] first Whether this is the first resolution try. Affects logging. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2306 | * @param[in] line Line in the input file. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2307 | * @param[out] ret Pointer to the resolved schema node. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2308 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2309 | * @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] | 2310 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2311 | static int |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2312 | resolve_path_arg_schema(struct lys_module *mod, const char *path, struct lys_node *parent_node, int first, |
| 2313 | uint32_t line, struct lys_node **ret) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2314 | { |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 2315 | struct lys_node *node; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2316 | const char *id, *prefix, *name; |
| 2317 | int pref_len, nam_len, parent_times, has_predicate; |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2318 | int i, first_iter, rc; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2319 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2320 | first_iter = 1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2321 | parent_times = 0; |
| 2322 | id = path; |
| 2323 | |
| 2324 | do { |
| 2325 | if ((i = parse_path_arg(id, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) { |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 2326 | LOGVAL(LYE_INCHAR, line, id[-i], &id[-i]); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2327 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2328 | } |
| 2329 | id += i; |
| 2330 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2331 | if (first_iter) { |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2332 | if (parent_times == -1) { |
| 2333 | node = mod->data; |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 2334 | if (!node) { |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2335 | if (!first) { |
| 2336 | LOGVAL(LYE_NORESOLV, line, path); |
| 2337 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2338 | return EXIT_FAILURE; |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 2339 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2340 | } else if (parent_times > 0) { |
Michal Vasko | 73ae256 | 2015-08-06 11:58:13 +0200 | [diff] [blame] | 2341 | /* node is the parent already, skip one ".." */ |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 2342 | node = parent_node; |
| 2343 | i = 0; |
| 2344 | while (1) { |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2345 | if (!node) { |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2346 | if (!first) { |
| 2347 | LOGVAL(LYE_NORESOLV, line, path); |
| 2348 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2349 | return EXIT_FAILURE; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2350 | } |
Michal Vasko | 5809090 | 2015-08-13 14:04:15 +0200 | [diff] [blame] | 2351 | |
| 2352 | /* this node is a wrong node, we actually need the augment target */ |
| 2353 | if (node->nodetype == LYS_AUGMENT) { |
| 2354 | node = ((struct lys_node_augment *)node)->target; |
| 2355 | if (!node) { |
| 2356 | continue; |
| 2357 | } |
| 2358 | } |
| 2359 | |
| 2360 | ++i; |
| 2361 | if (i == parent_times) { |
| 2362 | break; |
| 2363 | } |
| 2364 | node = node->parent; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2365 | } |
| 2366 | node = node->child; |
Michal Vasko | e01eca5 | 2015-08-13 14:42:02 +0200 | [diff] [blame] | 2367 | } else { |
| 2368 | LOGINT; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2369 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2370 | } |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2371 | first_iter = 0; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2372 | } else { |
| 2373 | node = node->child; |
| 2374 | } |
| 2375 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2376 | rc = lys_getsibling(mod, node, prefix, pref_len, name, nam_len, 0, &node); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2377 | if (rc) { |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2378 | if ((rc == -1) || !first) { |
| 2379 | LOGVAL(LYE_NORESOLV, line, path); |
| 2380 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2381 | return rc; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2382 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2383 | |
| 2384 | if (has_predicate) { |
| 2385 | /* we have predicate, so the current result must be list */ |
| 2386 | if (node->nodetype != LYS_LIST) { |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 2387 | LOGVAL(LYE_NORESOLV, line, path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2388 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2389 | } |
| 2390 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2391 | i = resolve_path_predicate_schema(id, mod, node, parent_node, first, line); |
| 2392 | if (!i) { |
Michal Vasko | f9664da | 2015-08-24 15:03:30 +0200 | [diff] [blame] | 2393 | return EXIT_FAILURE; |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2394 | } else if (i < 0) { |
| 2395 | return -1; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2396 | } |
| 2397 | id += i; |
| 2398 | } |
| 2399 | } while (id[0]); |
| 2400 | |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 2401 | /* the target must be leaf or leaf-list */ |
| 2402 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 2403 | LOGVAL(LYE_NORESOLV, line, path); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2404 | return -1; |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 2405 | } |
| 2406 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2407 | if (ret) { |
| 2408 | *ret = node; |
| 2409 | } |
| 2410 | return EXIT_SUCCESS; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2411 | } |
| 2412 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2413 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 2414 | * @brief Resolve instance-identifier predicate in JSON data format. |
| 2415 | * Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2416 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 2417 | * @param[in] pred Predicate to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2418 | * @param[in,out] node_match Nodes matching the restriction without |
| 2419 | * the predicate. Nodes not satisfying |
| 2420 | * the predicate are removed. |
| 2421 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2422 | * @return Number of characters successfully parsed, |
| 2423 | * positive on success, negative on failure. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2424 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2425 | static int |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 2426 | resolve_predicate(const char *pred, struct unres_data *node_match) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2427 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2428 | /* ... /node[target = value] ... */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 2429 | struct unres_data target_match; |
| 2430 | struct ly_ctx *ctx; |
| 2431 | struct lys_module *mod; |
| 2432 | const char *model, *name, *value; |
| 2433 | char *str; |
| 2434 | int mod_len, nam_len, val_len, i, has_predicate, cur_idx, idx, parsed; |
| 2435 | uint32_t j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2436 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 2437 | assert(pred && node_match->count); |
| 2438 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2439 | ctx = node_match->node[0]->schema->module->ctx; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2440 | idx = -1; |
| 2441 | parsed = 0; |
| 2442 | |
| 2443 | do { |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 2444 | 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] | 2445 | return -parsed+i; |
| 2446 | } |
| 2447 | parsed += i; |
| 2448 | pred += i; |
| 2449 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 2450 | /* pos */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2451 | if (isdigit(name[0])) { |
| 2452 | idx = atoi(name); |
| 2453 | } |
| 2454 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 2455 | for (cur_idx = 0, j = 0; j < node_match->count; ++cur_idx) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2456 | /* target */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 2457 | memset(&target_match, 0, sizeof target_match); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2458 | if ((name[0] == '.') || !value) { |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 2459 | target_match.count = 1; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2460 | target_match.node = malloc(sizeof *target_match.node); |
| 2461 | target_match.node[0] = node_match->node[j]; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 2462 | } else { |
| 2463 | str = strndup(model, mod_len); |
| 2464 | mod = ly_ctx_get_module(ctx, str, NULL); |
| 2465 | free(str); |
| 2466 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2467 | if (resolve_data(mod, name, nam_len, node_match->node[j], &target_match)) { |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 2468 | goto remove_instid; |
| 2469 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2470 | } |
| 2471 | |
| 2472 | /* check that we have the correct type */ |
| 2473 | if (name[0] == '.') { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2474 | if (node_match->node[j]->schema->nodetype != LYS_LEAFLIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2475 | goto remove_instid; |
| 2476 | } |
| 2477 | } else if (value) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2478 | if (node_match->node[j]->schema->nodetype != LYS_LIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2479 | goto remove_instid; |
| 2480 | } |
| 2481 | } |
| 2482 | |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 2483 | if ((value && (strncmp(((struct lyd_node_leaf_list *)target_match.node[0])->value_str, value, val_len) |
| 2484 | || ((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] | 2485 | || (!value && (idx != cur_idx))) { |
| 2486 | goto remove_instid; |
| 2487 | } |
| 2488 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2489 | free(target_match.node); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2490 | |
| 2491 | /* leafref is ok, continue check with next leafref */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 2492 | ++j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2493 | continue; |
| 2494 | |
| 2495 | remove_instid: |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2496 | free(target_match.node); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2497 | |
| 2498 | /* does not fulfill conditions, remove leafref record */ |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 2499 | unres_data_del(node_match, j); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2500 | } |
| 2501 | } while (has_predicate); |
| 2502 | |
| 2503 | return parsed; |
| 2504 | } |
| 2505 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2506 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 2507 | * @brief Resolve instance-identifier in JSON data format. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2508 | * |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2509 | * @param[in] data Any node in the data tree, used to get a data tree root and context |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2510 | * @param[in] path Instance-identifier node value. |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2511 | * @param[in] line Source line for error messages. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2512 | * |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2513 | * @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] | 2514 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2515 | static struct lyd_node * |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 2516 | resolve_instid(struct lyd_node *data, const char *path, int line) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2517 | { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2518 | int i = 0, j; |
| 2519 | struct lyd_node *result = NULL; |
| 2520 | struct lys_module *mod = NULL; |
| 2521 | struct ly_ctx *ctx = data->schema->module->ctx; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 2522 | const char *model, *name; |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2523 | char *str; |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 2524 | int mod_len, name_len, has_predicate; |
| 2525 | struct unres_data node_match; |
| 2526 | uint32_t k; |
| 2527 | |
| 2528 | memset(&node_match, 0, sizeof node_match); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2529 | |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2530 | /* we need root to resolve absolute path */ |
| 2531 | for (; data->parent; data = data->parent); |
Michal Vasko | db9323c | 2015-10-16 10:32:44 +0200 | [diff] [blame] | 2532 | /* we're still parsing it and the pointer is not correct yet */ |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 2533 | if (data->prev) { |
| 2534 | for (; data->prev->next; data = data->prev); |
| 2535 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2536 | |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2537 | /* search for the instance node */ |
| 2538 | while (path[i]) { |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 2539 | 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] | 2540 | if (j <= 0) { |
| 2541 | LOGVAL(LYE_INCHAR, line, path[i-j], &path[i-j]); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2542 | goto error; |
| 2543 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2544 | i += j; |
Michal Vasko | b387c48 | 2015-08-12 09:32:59 +0200 | [diff] [blame] | 2545 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 2546 | str = strndup(model, mod_len); |
| 2547 | mod = ly_ctx_get_module(ctx, str, NULL); |
| 2548 | free(str); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2549 | |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2550 | if (!mod) { |
| 2551 | /* no instance exists */ |
| 2552 | return NULL; |
| 2553 | } |
| 2554 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 2555 | if (resolve_data(mod, name, name_len, data, &node_match)) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2556 | /* no instance exists */ |
| 2557 | return NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2558 | } |
| 2559 | |
| 2560 | if (has_predicate) { |
| 2561 | /* 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] | 2562 | for (k = 0; k < node_match.count;) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2563 | if ((node_match.node[k]->schema->nodetype == LYS_LIST && |
| 2564 | ((struct lys_node_list *)node_match.node[k]->schema)->keys) |
| 2565 | || (node_match.node[k]->schema->nodetype == LYS_LEAFLIST)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2566 | /* instid is ok, continue check with next instid */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2567 | ++k; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2568 | continue; |
| 2569 | } |
| 2570 | |
| 2571 | /* does not fulfill conditions, remove inst record */ |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2572 | unres_data_del(&node_match, k); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2573 | } |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2574 | |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 2575 | j = resolve_predicate(&path[i], &node_match); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2576 | if (j < 1) { |
| 2577 | LOGVAL(LYE_INPRED, line, &path[i-j]); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2578 | goto error; |
| 2579 | } |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 2580 | i += j; |
Michal Vasko | b387c48 | 2015-08-12 09:32:59 +0200 | [diff] [blame] | 2581 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 2582 | if (!node_match.count) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2583 | /* no instance exists */ |
| 2584 | return NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2585 | } |
| 2586 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2587 | } |
| 2588 | |
Michal Vasko | 1f2cc33 | 2015-08-19 11:18:32 +0200 | [diff] [blame] | 2589 | if (!node_match.count) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2590 | /* no instance exists */ |
| 2591 | return NULL; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 2592 | } else if (node_match.count > 1) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2593 | /* instance identifier must resolve to a single node */ |
| 2594 | LOGVAL(LYE_TOOMANY, line, path, "data tree"); |
| 2595 | |
| 2596 | /* cleanup */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2597 | free(node_match.node); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2598 | |
| 2599 | return NULL; |
| 2600 | } else { |
| 2601 | /* we have required result, remember it and cleanup */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2602 | result = node_match.node[0]; |
| 2603 | free(node_match.node); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2604 | |
| 2605 | return result; |
| 2606 | } |
| 2607 | |
| 2608 | error: |
| 2609 | |
| 2610 | /* cleanup */ |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 2611 | free(node_match.node); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 2612 | |
| 2613 | return NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2614 | } |
| 2615 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2616 | /** |
| 2617 | * @brief Passes config flag down to children. Does not log. |
| 2618 | * |
| 2619 | * @param[in] node Parent node. |
| 2620 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2621 | static void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2622 | inherit_config_flag(struct lys_node *node) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2623 | { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2624 | LY_TREE_FOR(node, node) { |
| 2625 | node->flags |= node->parent->flags & LYS_CONFIG_MASK; |
| 2626 | inherit_config_flag(node->child); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2627 | } |
| 2628 | } |
| 2629 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2630 | /** |
Michal Vasko | d917334 | 2015-08-17 14:35:36 +0200 | [diff] [blame] | 2631 | * @brief Resolve augment target. Does not log. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2632 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 2633 | * @param[in] aug Augment to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2634 | * @param[in] siblings Nodes where to start the search in. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2635 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2636 | * @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] | 2637 | */ |
Michal Vasko | 4adc10f | 2015-08-11 15:26:17 +0200 | [diff] [blame] | 2638 | int |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 2639 | resolve_augment(struct lys_node_augment *aug, struct lys_node *siblings) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2640 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2641 | int rc; |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 2642 | struct lys_node *sub; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2643 | |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 2644 | assert(aug); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2645 | |
| 2646 | /* resolve target node */ |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 2647 | rc = resolve_schema_nodeid(aug->target_name, siblings, aug->module, LYS_AUGMENT, &aug->target); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2648 | if (rc) { |
| 2649 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2650 | } |
| 2651 | |
| 2652 | if (!aug->child) { |
| 2653 | /* nothing to do */ |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 2654 | LOGWRN("Augment \"%s\" without children.", aug->target_name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2655 | return EXIT_SUCCESS; |
| 2656 | } |
| 2657 | |
| 2658 | /* inherit config information from parent, augment does not have |
| 2659 | * config property, but we need to keep the information for subelements |
| 2660 | */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2661 | aug->flags |= aug->target->flags & LYS_CONFIG_MASK; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2662 | LY_TREE_FOR(aug->child, sub) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2663 | inherit_config_flag(sub); |
| 2664 | } |
| 2665 | |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 2666 | /* check identifier uniquness as in lys_node_addchild() */ |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 2667 | LY_TREE_FOR(aug->child, sub) { |
| 2668 | if (lys_check_id(sub, aug->parent, aug->module)) { |
Michal Vasko | 3e6665f | 2015-08-17 14:00:38 +0200 | [diff] [blame] | 2669 | return -1; |
Radek Krejci | 0791199 | 2015-08-14 15:13:31 +0200 | [diff] [blame] | 2670 | } |
| 2671 | } |
Radek Krejci | 0acbe1b | 2015-08-04 09:33:49 +0200 | [diff] [blame] | 2672 | /* reconnect augmenting data into the target - add them to the target child list */ |
| 2673 | if (aug->target->child) { |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 2674 | sub = aug->target->child->prev; /* remember current target's last node */ |
| 2675 | sub->next = aug->child; /* connect augmenting data after target's last node */ |
Radek Krejci | 0acbe1b | 2015-08-04 09:33:49 +0200 | [diff] [blame] | 2676 | 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] | 2677 | aug->child->prev = sub; /* finish connecting of both child lists */ |
Radek Krejci | 0acbe1b | 2015-08-04 09:33:49 +0200 | [diff] [blame] | 2678 | } else { |
| 2679 | aug->target->child = aug->child; |
| 2680 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2681 | |
| 2682 | return EXIT_SUCCESS; |
| 2683 | } |
| 2684 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2685 | /** |
| 2686 | * @brief Resolve uses, apply augments, refines. Logs directly. |
| 2687 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 2688 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2689 | * @param[in,out] unres List of unresolved items. |
| 2690 | * @param[in] line Line in the input file. |
| 2691 | * |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 2692 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2693 | */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2694 | static int |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 2695 | resolve_uses(struct lys_node_uses *uses, struct unres_schema *unres, uint32_t line) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2696 | { |
| 2697 | struct ly_ctx *ctx; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2698 | struct lys_node *node = NULL, *node_aux; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2699 | struct lys_refine *rfn; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 2700 | struct lys_restr *must, **old_must; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2701 | int i, j, rc; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 2702 | uint8_t size, *old_size; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2703 | |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 2704 | assert(uses->grp); |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 2705 | /* HACK just check that the grouing is resolved */ |
| 2706 | assert(!uses->grp->nacm); |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 2707 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2708 | /* copy the data nodes from grouping into the uses context */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2709 | LY_TREE_FOR(uses->grp->child, node) { |
Michal Vasko | 71e1aa8 | 2015-08-12 12:17:51 +0200 | [diff] [blame] | 2710 | node_aux = lys_node_dup(uses->module, node, uses->flags, uses->nacm, 1, unres); |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2711 | if (!node_aux) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 2712 | LOGVAL(LYE_SPEC, line, "Copying data from grouping failed."); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2713 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2714 | } |
Radek Krejci | 10c760e | 2015-08-14 14:45:43 +0200 | [diff] [blame] | 2715 | if (lys_node_addchild((struct lys_node *)uses, NULL, node_aux)) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 2716 | /* error logged */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2717 | lys_node_free(node_aux); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2718 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2719 | } |
| 2720 | } |
| 2721 | ctx = uses->module->ctx; |
| 2722 | |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 2723 | /* we managed to copy the grouping, the rest must be possible to resolve */ |
| 2724 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2725 | /* apply refines */ |
| 2726 | for (i = 0; i < uses->refine_size; i++) { |
| 2727 | rfn = &uses->refine[i]; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2728 | rc = resolve_schema_nodeid(rfn->target_name, uses->child, uses->module, LYS_LEAF, &node); |
| 2729 | if (rc) { |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 2730 | LOGVAL(LYE_INARG, line, rfn->target_name, "refine"); |
| 2731 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2732 | } |
| 2733 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2734 | if (rfn->target_type && !(node->nodetype & rfn->target_type)) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 2735 | LOGVAL(LYE_SPEC, line, "Refine substatements not applicable to the target-node."); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2736 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2737 | } |
| 2738 | |
| 2739 | /* description on any nodetype */ |
| 2740 | if (rfn->dsc) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2741 | lydict_remove(ctx, node->dsc); |
| 2742 | node->dsc = lydict_insert(ctx, rfn->dsc, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2743 | } |
| 2744 | |
| 2745 | /* reference on any nodetype */ |
| 2746 | if (rfn->ref) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2747 | lydict_remove(ctx, node->ref); |
| 2748 | node->ref = lydict_insert(ctx, rfn->ref, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2749 | } |
| 2750 | |
| 2751 | /* config on any nodetype */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2752 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2753 | node->flags &= ~LYS_CONFIG_MASK; |
| 2754 | node->flags |= (rfn->flags & LYS_CONFIG_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2755 | } |
| 2756 | |
| 2757 | /* default value ... */ |
| 2758 | if (rfn->mod.dflt) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2759 | if (node->nodetype == LYS_LEAF) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2760 | /* leaf */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2761 | lydict_remove(ctx, ((struct lys_node_leaf *)node)->dflt); |
| 2762 | ((struct lys_node_leaf *)node)->dflt = lydict_insert(ctx, rfn->mod.dflt, 0); |
| 2763 | } else if (node->nodetype == LYS_CHOICE) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2764 | /* choice */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2765 | rc = resolve_schema_nodeid(rfn->mod.dflt, node->child, node->module, LYS_CHOICE, &((struct lys_node_choice *)node)->dflt); |
| 2766 | if (rc) { |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 2767 | LOGVAL(LYE_INARG, line, rfn->mod.dflt, "default"); |
| 2768 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2769 | } |
| 2770 | } |
| 2771 | } |
| 2772 | |
| 2773 | /* mandatory on leaf, anyxml or choice */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 2774 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2775 | if (node->nodetype & (LYS_LEAF | LYS_ANYXML | LYS_CHOICE)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2776 | /* remove current value */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2777 | node->flags &= ~LYS_MAND_MASK; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2778 | |
| 2779 | /* set new value */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2780 | node->flags |= (rfn->flags & LYS_MAND_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2781 | } |
| 2782 | } |
| 2783 | |
| 2784 | /* presence on container */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2785 | if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) { |
| 2786 | lydict_remove(ctx, ((struct lys_node_container *)node)->presence); |
| 2787 | ((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] | 2788 | } |
| 2789 | |
| 2790 | /* min/max-elements on list or leaf-list */ |
| 2791 | /* magic - bit 3 in flags means min set, bit 4 says max set */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2792 | if (node->nodetype == LYS_LIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2793 | if (rfn->flags & 0x04) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2794 | ((struct lys_node_list *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2795 | } |
| 2796 | if (rfn->flags & 0x08) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2797 | ((struct lys_node_list *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2798 | } |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2799 | } else if (node->nodetype == LYS_LEAFLIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2800 | if (rfn->flags & 0x04) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2801 | ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2802 | } |
| 2803 | if (rfn->flags & 0x08) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2804 | ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2805 | } |
| 2806 | } |
| 2807 | |
| 2808 | /* must in leaf, leaf-list, list, container or anyxml */ |
| 2809 | if (rfn->must_size) { |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 2810 | switch (node->nodetype) { |
| 2811 | case LYS_LEAF: |
| 2812 | old_size = &((struct lys_node_leaf *)node)->must_size; |
| 2813 | old_must = &((struct lys_node_leaf *)node)->must; |
| 2814 | break; |
| 2815 | case LYS_LEAFLIST: |
| 2816 | old_size = &((struct lys_node_leaflist *)node)->must_size; |
| 2817 | old_must = &((struct lys_node_leaflist *)node)->must; |
| 2818 | break; |
| 2819 | case LYS_LIST: |
| 2820 | old_size = &((struct lys_node_list *)node)->must_size; |
| 2821 | old_must = &((struct lys_node_list *)node)->must; |
| 2822 | break; |
| 2823 | case LYS_CONTAINER: |
| 2824 | old_size = &((struct lys_node_container *)node)->must_size; |
| 2825 | old_must = &((struct lys_node_container *)node)->must; |
| 2826 | break; |
| 2827 | case LYS_ANYXML: |
| 2828 | old_size = &((struct lys_node_anyxml *)node)->must_size; |
| 2829 | old_must = &((struct lys_node_anyxml *)node)->must; |
| 2830 | break; |
| 2831 | default: |
| 2832 | LOGINT; |
Radek Krejci | e4e4d72 | 2015-10-05 16:53:50 +0200 | [diff] [blame] | 2833 | return -1; |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 2834 | } |
| 2835 | |
| 2836 | size = *old_size + rfn->must_size; |
| 2837 | must = realloc(*old_must, size * sizeof *rfn->must); |
| 2838 | if (!must) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2839 | LOGMEM; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2840 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2841 | } |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 2842 | for (i = 0, j = *old_size; i < rfn->must_size; i++, j++) { |
| 2843 | must[j].expr = lydict_insert(ctx, rfn->must[i].expr, 0); |
| 2844 | must[j].dsc = lydict_insert(ctx, rfn->must[i].dsc, 0); |
| 2845 | must[j].ref = lydict_insert(ctx, rfn->must[i].ref, 0); |
| 2846 | must[j].eapptag = lydict_insert(ctx, rfn->must[i].eapptag, 0); |
| 2847 | must[j].emsg = lydict_insert(ctx, rfn->must[i].emsg, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2848 | } |
| 2849 | |
Michal Vasko | ef2fdc8 | 2015-09-24 09:54:42 +0200 | [diff] [blame] | 2850 | *old_must = must; |
| 2851 | *old_size = size; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2852 | } |
| 2853 | } |
| 2854 | |
| 2855 | /* apply augments */ |
| 2856 | for (i = 0; i < uses->augment_size; i++) { |
Michal Vasko | 1d87a92 | 2015-08-21 12:57:16 +0200 | [diff] [blame] | 2857 | rc = resolve_augment(&uses->augment[i], uses->child); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2858 | if (rc) { |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 2859 | LOGVAL(LYE_INRESOLV, line, "augment", uses->augment[i].target_name); |
| 2860 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2861 | } |
| 2862 | } |
| 2863 | |
| 2864 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2865 | } |
| 2866 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2867 | /** |
| 2868 | * @brief Resolve base identity recursively. Does not log. |
| 2869 | * |
| 2870 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 2871 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2872 | * @param[in] basename Base name of the identity. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2873 | * @param[out] ret Pointer to the resolved identity. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2874 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2875 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2876 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2877 | static int |
| 2878 | resolve_base_ident_sub(struct lys_module *module, struct lys_ident *ident, const char *basename, |
| 2879 | struct lys_ident **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2880 | { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2881 | uint32_t i, j; |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 2882 | struct lys_ident *base_iter = NULL; |
| 2883 | struct lys_ident_der *der; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2884 | |
| 2885 | /* search module */ |
| 2886 | for (i = 0; i < module->ident_size; i++) { |
| 2887 | if (!strcmp(basename, module->ident[i].name)) { |
| 2888 | |
| 2889 | if (!ident) { |
| 2890 | /* just search for type, so do not modify anything, just return |
| 2891 | * the base identity pointer |
| 2892 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2893 | if (ret) { |
| 2894 | *ret = &module->ident[i]; |
| 2895 | } |
| 2896 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2897 | } |
| 2898 | |
| 2899 | /* we are resolving identity definition, so now update structures */ |
| 2900 | ident->base = base_iter = &module->ident[i]; |
| 2901 | |
| 2902 | break; |
| 2903 | } |
| 2904 | } |
| 2905 | |
| 2906 | /* search submodules */ |
| 2907 | if (!base_iter) { |
| 2908 | for (j = 0; j < module->inc_size; j++) { |
| 2909 | for (i = 0; i < module->inc[j].submodule->ident_size; i++) { |
| 2910 | if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) { |
| 2911 | |
| 2912 | if (!ident) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2913 | if (ret) { |
| 2914 | *ret = &module->inc[j].submodule->ident[i]; |
| 2915 | } |
| 2916 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2917 | } |
| 2918 | |
| 2919 | ident->base = base_iter = &module->inc[j].submodule->ident[i]; |
| 2920 | break; |
| 2921 | } |
| 2922 | } |
| 2923 | } |
| 2924 | } |
| 2925 | |
| 2926 | /* we found it somewhere */ |
| 2927 | if (base_iter) { |
| 2928 | while (base_iter) { |
| 2929 | for (der = base_iter->der; der && der->next; der = der->next); |
| 2930 | if (der) { |
| 2931 | der->next = malloc(sizeof *der); |
| 2932 | der = der->next; |
| 2933 | } else { |
| 2934 | ident->base->der = der = malloc(sizeof *der); |
| 2935 | } |
| 2936 | der->next = NULL; |
| 2937 | der->ident = ident; |
| 2938 | |
| 2939 | base_iter = base_iter->base; |
| 2940 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2941 | if (ret) { |
| 2942 | *ret = ident->base; |
| 2943 | } |
| 2944 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2945 | } |
| 2946 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2947 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2948 | } |
| 2949 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2950 | /** |
| 2951 | * @brief Resolve base identity. Logs directly. |
| 2952 | * |
| 2953 | * @param[in] module Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 2954 | * @param[in] ident Identity to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2955 | * @param[in] basename Base name of the identity. |
| 2956 | * @param[in] parent Either "type" or "ident". |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2957 | * @param[in] first Whether this is the first resolution try. Affects logging. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2958 | * @param[in] line Line in the input file. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2959 | * @param[out] ret Pointer to the resolved identity. Can be NULL. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2960 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2961 | * @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] | 2962 | */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2963 | static int |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2964 | resolve_base_ident(struct lys_module *module, struct lys_ident *ident, const char *basename, const char* parent, |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 2965 | int first, uint32_t line, struct lys_ident **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2966 | { |
| 2967 | const char *name; |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 2968 | int i, mod_name_len = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2969 | |
| 2970 | /* search for the base identity */ |
| 2971 | name = strchr(basename, ':'); |
| 2972 | if (name) { |
| 2973 | /* set name to correct position after colon */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 2974 | mod_name_len = name - basename; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2975 | name++; |
| 2976 | |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 2977 | 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] | 2978 | /* prefix refers to the current module, ignore it */ |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 2979 | mod_name_len = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2980 | } |
| 2981 | } else { |
| 2982 | name = basename; |
| 2983 | } |
| 2984 | |
Michal Vasko | 2d851a9 | 2015-10-20 16:16:36 +0200 | [diff] [blame] | 2985 | if (mod_name_len) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2986 | /* get module where to search */ |
Michal Vasko | b6729c6 | 2015-10-21 12:09:47 +0200 | [diff] [blame] | 2987 | module = lys_get_import_module(module, NULL, 0, basename, mod_name_len); |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 2988 | if (!module) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2989 | /* identity refers unknown data model */ |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 2990 | LOGVAL(LYE_INMOD, line, basename); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2991 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2992 | } |
| 2993 | } else { |
| 2994 | /* search in submodules */ |
| 2995 | for (i = 0; i < module->inc_size; i++) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 2996 | if (!resolve_base_ident_sub((struct lys_module *)module->inc[i].submodule, ident, name, ret)) { |
| 2997 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2998 | } |
| 2999 | } |
| 3000 | } |
| 3001 | |
| 3002 | /* search in the identified module */ |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3003 | if (!resolve_base_ident_sub(module, ident, name, ret)) { |
| 3004 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3005 | } |
| 3006 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3007 | if (!first) { |
| 3008 | LOGVAL(LYE_INARG, line, basename, parent); |
| 3009 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3010 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3011 | } |
| 3012 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3013 | /** |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3014 | * @brief Resolve JSON data format identityref. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3015 | * |
| 3016 | * @param[in] base Base identity. |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 3017 | * @param[in] ident_name Identityref name. |
| 3018 | * @param[in] line Line from the input file. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3019 | * |
| 3020 | * @return Pointer to the identity resolvent, NULL on error. |
| 3021 | */ |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 3022 | struct lys_ident * |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3023 | resolve_identref(struct lys_ident *base, const char *ident_name, uint32_t line) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3024 | { |
Michal Vasko | c633ca0 | 2015-08-21 14:03:51 +0200 | [diff] [blame] | 3025 | const char *mod_name, *name; |
| 3026 | int mod_name_len, rc; |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 3027 | struct lys_ident_der *der; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3028 | |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 3029 | if (!base || !ident_name) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3030 | return NULL; |
| 3031 | } |
| 3032 | |
Michal Vasko | c633ca0 | 2015-08-21 14:03:51 +0200 | [diff] [blame] | 3033 | rc = parse_node_identifier(ident_name, &mod_name, &mod_name_len, &name, NULL); |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 3034 | if (rc < (signed)strlen(ident_name)) { |
| 3035 | LOGVAL(LYE_INCHAR, line, ident_name[-rc], &ident_name[-rc]); |
| 3036 | return NULL; |
| 3037 | } |
| 3038 | |
Michal Vasko | c633ca0 | 2015-08-21 14:03:51 +0200 | [diff] [blame] | 3039 | if (!strcmp(base->name, name) && (!mod_name |
| 3040 | || (!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] | 3041 | return base; |
| 3042 | } |
| 3043 | |
| 3044 | for (der = base->der; der; der = der->next) { |
Michal Vasko | c633ca0 | 2015-08-21 14:03:51 +0200 | [diff] [blame] | 3045 | if (!strcmp(der->ident->name, name) && (!mod_name |
| 3046 | || (!strncmp(der->ident->module->name, mod_name, mod_name_len) |
| 3047 | && !der->ident->module->name[mod_name_len]))) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3048 | /* we have match */ |
| 3049 | return der->ident; |
| 3050 | } |
| 3051 | } |
| 3052 | |
Michal Vasko | fb0873c | 2015-08-21 09:00:07 +0200 | [diff] [blame] | 3053 | LOGVAL(LYE_INRESOLV, line, "identityref", ident_name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3054 | return NULL; |
| 3055 | } |
| 3056 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3057 | /** |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 3058 | * @brief Resolve (find) choice default case. Does not log. |
| 3059 | * |
| 3060 | * @param[in] choic Choice to use. |
| 3061 | * @param[in] dflt Name of the default case. |
| 3062 | * |
| 3063 | * @return Pointer to the default node or NULL. |
| 3064 | */ |
| 3065 | static struct lys_node * |
| 3066 | resolve_choice_dflt(struct lys_node_choice *choic, const char *dflt) |
| 3067 | { |
| 3068 | struct lys_node *child, *ret; |
| 3069 | |
| 3070 | LY_TREE_FOR(choic->child, child) { |
| 3071 | if (child->nodetype == LYS_USES) { |
| 3072 | ret = resolve_choice_dflt((struct lys_node_choice *)child, dflt); |
| 3073 | if (ret) { |
| 3074 | return ret; |
| 3075 | } |
| 3076 | } |
| 3077 | |
| 3078 | if ((child->name == dflt) && (child->nodetype & (LYS_ANYXML | LYS_CASE |
| 3079 | | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST))) { |
| 3080 | return child; |
| 3081 | } |
| 3082 | } |
| 3083 | |
| 3084 | return NULL; |
| 3085 | } |
| 3086 | |
| 3087 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3088 | * @brief Resolve unresolved uses. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3089 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3090 | * @param[in] uses Uses to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3091 | * @param[in] unres Specific unres item. |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 3092 | * @param[in] first Whether this is the first resolution try. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3093 | * @param[in] line Line in the input file. |
| 3094 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3095 | * @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] | 3096 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3097 | static int |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 3098 | resolve_unres_schema_uses(struct lys_node_uses *uses, struct unres_schema *unres, int first, uint32_t line) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3099 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3100 | int rc; |
Michal Vasko | e91afce | 2015-08-12 12:21:00 +0200 | [diff] [blame] | 3101 | struct lys_node *parent; |
| 3102 | |
| 3103 | /* HACK change unres uses count if it's in a grouping (nacm field used for it) */ |
| 3104 | for (parent = uses->parent; parent && (parent->nodetype != LYS_GROUPING); parent = parent->parent); |
| 3105 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3106 | if (!uses->grp) { |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3107 | rc = resolve_grouping(uses, first, line); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3108 | if (rc) { |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 3109 | if (parent && first && (rc == EXIT_FAILURE)) { |
| 3110 | ++parent->nacm; |
| 3111 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3112 | return rc; |
Michal Vasko | 12e3084 | 2015-08-04 11:54:00 +0200 | [diff] [blame] | 3113 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3114 | } |
| 3115 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3116 | if (uses->grp->nacm) { |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 3117 | if (parent && first) { |
| 3118 | ++parent->nacm; |
| 3119 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3120 | return EXIT_FAILURE; |
| 3121 | } |
| 3122 | |
Michal Vasko | def0db1 | 2015-10-07 13:22:48 +0200 | [diff] [blame] | 3123 | rc = resolve_uses(uses, unres, line); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3124 | if (!rc) { |
| 3125 | /* decrease unres count only if not first try */ |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 3126 | if (parent && !first) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3127 | if (!parent->nacm) { |
| 3128 | LOGINT; |
| 3129 | return -1; |
| 3130 | } |
| 3131 | --parent->nacm; |
| 3132 | } |
| 3133 | return EXIT_SUCCESS; |
| 3134 | } |
| 3135 | |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 3136 | if (parent && first && (rc == EXIT_FAILURE)) { |
Michal Vasko | e91afce | 2015-08-12 12:21:00 +0200 | [diff] [blame] | 3137 | ++parent->nacm; |
| 3138 | } |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3139 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3140 | } |
| 3141 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3142 | /** |
Michal Vasko | 9957e59 | 2015-08-17 15:04:09 +0200 | [diff] [blame] | 3143 | * @brief Resolve list keys. Logs directly. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3144 | * |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3145 | * @param[in] list List to use. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3146 | * @param[in] keys_str Keys node value. |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3147 | * @param[in] first Whether this is the first resolution try. Affects logging. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 3148 | * @param[in] line Line in the input file. |
| 3149 | * |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3150 | * @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] | 3151 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3152 | static int |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3153 | resolve_list_keys(struct lys_module *mod, struct lys_node_list *list, const char *keys_str, int first, uint32_t line) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3154 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3155 | int i, len, rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3156 | const char *value; |
| 3157 | |
| 3158 | for (i = 0; i < list->keys_size; ++i) { |
| 3159 | /* get the key name */ |
| 3160 | if ((value = strpbrk(keys_str, " \t\n"))) { |
| 3161 | len = value - keys_str; |
| 3162 | while (isspace(value[0])) { |
| 3163 | value++; |
| 3164 | } |
| 3165 | } else { |
| 3166 | len = strlen(keys_str); |
| 3167 | } |
| 3168 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3169 | rc = lys_getsibling(mod, list->child, NULL, 0, keys_str, len, LYS_LEAF, (struct lys_node **)&list->keys[i]); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3170 | if (rc) { |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3171 | if ((rc == -1) || !first) { |
| 3172 | LOGVAL(LYE_INRESOLV, line, "list keys", keys_str); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3173 | } |
| 3174 | return rc; |
| 3175 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3176 | |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 3177 | if (check_key(list->keys[i], list->flags, list->keys, i, keys_str, len, line)) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3178 | /* check_key logs */ |
| 3179 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3180 | } |
| 3181 | |
| 3182 | /* prepare for next iteration */ |
| 3183 | while (value && isspace(value[0])) { |
| 3184 | value++; |
| 3185 | } |
| 3186 | keys_str = value; |
| 3187 | } |
| 3188 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3189 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3190 | } |
| 3191 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3192 | /** |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 3193 | * @brief Resolve (check) all must conditions of \p node. |
| 3194 | * Logs directly. |
| 3195 | * |
| 3196 | * @param[in] node Data node with optional must statements. |
| 3197 | * @param[in] first Whether this is the first resolution to try. |
| 3198 | * @param[in] line Line in the input file. |
| 3199 | * |
| 3200 | * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error. |
| 3201 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3202 | static int |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 3203 | resolve_must(struct lyd_node *node, int first, uint32_t line) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3204 | { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 3205 | uint8_t i, must_size; |
| 3206 | struct lys_restr *must; |
| 3207 | struct lyxp_set set; |
| 3208 | |
| 3209 | assert(node); |
| 3210 | memset(&set, 0, sizeof set); |
| 3211 | |
| 3212 | switch (node->schema->nodetype) { |
| 3213 | case LYS_CONTAINER: |
| 3214 | must_size = ((struct lys_node_container *)node->schema)->must_size; |
| 3215 | must = ((struct lys_node_container *)node->schema)->must; |
| 3216 | break; |
| 3217 | case LYS_LEAF: |
| 3218 | must_size = ((struct lys_node_leaf *)node->schema)->must_size; |
| 3219 | must = ((struct lys_node_leaf *)node->schema)->must; |
| 3220 | break; |
| 3221 | case LYS_LEAFLIST: |
| 3222 | must_size = ((struct lys_node_leaflist *)node->schema)->must_size; |
| 3223 | must = ((struct lys_node_leaflist *)node->schema)->must; |
| 3224 | break; |
| 3225 | case LYS_LIST: |
| 3226 | must_size = ((struct lys_node_list *)node->schema)->must_size; |
| 3227 | must = ((struct lys_node_list *)node->schema)->must; |
| 3228 | break; |
| 3229 | case LYS_ANYXML: |
| 3230 | must_size = ((struct lys_node_anyxml *)node->schema)->must_size; |
| 3231 | must = ((struct lys_node_anyxml *)node->schema)->must; |
| 3232 | break; |
| 3233 | default: |
| 3234 | must_size = 0; |
| 3235 | break; |
| 3236 | } |
| 3237 | |
| 3238 | for (i = 0; i < must_size; ++i) { |
| 3239 | if (lyxp_eval(must[i].expr, node, &set, line)) { |
| 3240 | return -1; |
| 3241 | } |
| 3242 | |
| 3243 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node); |
| 3244 | |
| 3245 | if (!set.value.bool) { |
| 3246 | if (!first) { |
| 3247 | LOGVAL(LYE_NOCOND, line, "Must", must[i].expr); |
| 3248 | } |
| 3249 | return 1; |
| 3250 | } |
| 3251 | } |
| 3252 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3253 | return EXIT_SUCCESS; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3254 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3255 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 3256 | /** |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3257 | * @brief Resolve (find) when condition context node. Does not log. |
| 3258 | * |
| 3259 | * @param[in] node Data node, whose conditional definition is being decided. |
| 3260 | * @param[in] schema Schema node with a when condition. |
| 3261 | * |
| 3262 | * @return Context node. |
| 3263 | */ |
| 3264 | static struct lyd_node * |
| 3265 | resolve_when_ctx_node(struct lyd_node *node, struct lys_node *schema) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3266 | { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3267 | struct lyd_node *parent; |
| 3268 | struct lys_node *sparent; |
| 3269 | uint16_t i, data_depth, schema_depth; |
| 3270 | |
| 3271 | /* find a not schema-only node */ |
| 3272 | while (schema->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE | LYS_AUGMENT | LYS_INPUT | LYS_OUTPUT)) { |
| 3273 | schema = lys_parent(schema); |
| 3274 | if (!schema) { |
| 3275 | return NULL; |
| 3276 | } |
| 3277 | } |
| 3278 | |
| 3279 | /* get node depths */ |
| 3280 | for (parent = node, data_depth = 0; parent; parent = parent->parent, ++data_depth); |
| 3281 | for (sparent = lys_parent(schema), schema_depth = 1; sparent; sparent = lys_parent(sparent)) { |
| 3282 | if (sparent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYXML | LYS_NOTIF | LYS_RPC)) { |
| 3283 | ++schema_depth; |
| 3284 | } |
| 3285 | } |
| 3286 | if (data_depth < schema_depth) { |
| 3287 | return NULL; |
| 3288 | } |
| 3289 | |
| 3290 | /* find the corresponding data node */ |
| 3291 | for (i = 0; i < data_depth - schema_depth; ++i) { |
| 3292 | node = node->parent; |
| 3293 | } |
| 3294 | if (node->schema != schema) { |
| 3295 | return NULL; |
| 3296 | } |
| 3297 | |
| 3298 | return node; |
| 3299 | } |
| 3300 | |
| 3301 | /** |
| 3302 | * @brief Resolve (check) all when conditions relevant for \p node. |
| 3303 | * Logs directly. |
| 3304 | * |
| 3305 | * @param[in] node Data node, whose conditional reference, if such, is being decided. |
| 3306 | * @param[in] first Whether this is the first resolution to try. |
| 3307 | * @param[in] line Line in the input file. |
| 3308 | * |
| 3309 | * @return EXIT_SUCCESS on pass, EXIT_FAILURE on fail, -1 on error. |
| 3310 | */ |
| 3311 | static int |
| 3312 | resolve_when(struct lyd_node *node, int first, uint32_t line) |
| 3313 | { |
| 3314 | struct lyd_node *ctx_node = NULL; |
| 3315 | struct lys_node *parent; |
| 3316 | struct lyxp_set set; |
| 3317 | |
| 3318 | assert(node); |
| 3319 | memset(&set, 0, sizeof set); |
| 3320 | |
| 3321 | if (!(node->schema->nodetype & (LYS_NOTIF | LYS_RPC)) && (((struct lys_node_container *)node->schema)->when)) { |
| 3322 | if (lyxp_eval(((struct lys_node_container *)node->schema)->when->cond, node, &set, line)) { |
| 3323 | return -1; |
| 3324 | } |
| 3325 | |
| 3326 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, node); |
| 3327 | |
| 3328 | if (!set.value.bool) { |
| 3329 | if (!first) { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 3330 | LOGVAL(LYE_NOCOND, line, "When", ((struct lys_node_container *)node->schema)->when->cond); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3331 | } |
| 3332 | return 1; |
| 3333 | } |
| 3334 | } |
| 3335 | |
| 3336 | parent = node->schema; |
| 3337 | goto check_augment; |
| 3338 | |
| 3339 | /* check when in every schema node that affects node */ |
| 3340 | while (parent && (parent->nodetype & (LYS_USES | LYS_CHOICE | LYS_CASE))) { |
| 3341 | if (((struct lys_node_uses *)parent)->when) { |
| 3342 | if (!ctx_node) { |
| 3343 | ctx_node = resolve_when_ctx_node(node, parent); |
| 3344 | if (!ctx_node) { |
| 3345 | LOGINT; |
| 3346 | return -1; |
| 3347 | } |
| 3348 | } |
| 3349 | if (lyxp_eval(((struct lys_node_uses *)parent)->when->cond, ctx_node, &set, line)) { |
| 3350 | return -1; |
| 3351 | } |
| 3352 | |
| 3353 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node); |
| 3354 | |
| 3355 | if (!set.value.bool) { |
| 3356 | if (!first) { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 3357 | LOGVAL(LYE_NOCOND, line, "When", ((struct lys_node_uses *)parent)->when->cond); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3358 | } |
| 3359 | return 1; |
| 3360 | } |
| 3361 | } |
| 3362 | |
| 3363 | check_augment: |
| 3364 | if ((parent->parent && (parent->parent->nodetype == LYS_AUGMENT) && (((struct lys_node_augment *)parent->parent)->when))) { |
| 3365 | if (!ctx_node) { |
| 3366 | ctx_node = resolve_when_ctx_node(node, parent->parent); |
| 3367 | if (!ctx_node) { |
| 3368 | LOGINT; |
| 3369 | return -1; |
| 3370 | } |
| 3371 | } |
| 3372 | if (lyxp_eval(((struct lys_node_augment *)parent->parent)->when->cond, ctx_node, &set, line)) { |
| 3373 | return -1; |
| 3374 | } |
| 3375 | |
| 3376 | lyxp_set_cast(&set, LYXP_SET_BOOLEAN, ctx_node); |
| 3377 | |
| 3378 | if (!set.value.bool) { |
| 3379 | if (!first) { |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 3380 | LOGVAL(LYE_NOCOND, line, "When", ((struct lys_node_augment *)parent->parent)->when->cond); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3381 | } |
| 3382 | return 1; |
| 3383 | } |
| 3384 | } |
| 3385 | |
| 3386 | parent = lys_parent(parent); |
| 3387 | } |
| 3388 | |
| 3389 | return 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3390 | } |
| 3391 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3392 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3393 | * @brief Resolve a single unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3394 | * |
| 3395 | * @param[in] mod Main module. |
| 3396 | * @param[in] item Item to resolve. Type determined by \p type. |
| 3397 | * @param[in] type Type of the unresolved item. |
| 3398 | * @param[in] str_snode String, a schema node, or NULL. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3399 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 3400 | * @param[in] first Whether this is the first resolution try. |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3401 | * @param[in] line Line in the input file. 0 skips line print. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3402 | * |
| 3403 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 3404 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3405 | static int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 3406 | resolve_unres_schema_item(struct lys_module *mod, void *item, enum UNRES_ITEM type, void *str_snode, |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 3407 | struct unres_schema *unres, int first, uint32_t line) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3408 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3409 | int rc = -1, has_str = 0; |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 3410 | struct lys_node *node; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 3411 | const char *base_name; |
| 3412 | |
| 3413 | struct lys_ident *ident; |
| 3414 | struct lys_type *stype; |
| 3415 | struct lys_feature **feat_ptr; |
| 3416 | struct lys_node_choice *choic; |
| 3417 | struct lys_unique *uniq; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3418 | |
| 3419 | switch (type) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3420 | case UNRES_IDENT: |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 3421 | base_name = str_snode; |
| 3422 | ident = item; |
| 3423 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3424 | rc = resolve_base_ident(mod, ident, base_name, "ident", first, line, NULL); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3425 | has_str = 1; |
| 3426 | break; |
| 3427 | case UNRES_TYPE_IDENTREF: |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 3428 | base_name = str_snode; |
| 3429 | stype = item; |
| 3430 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3431 | rc = resolve_base_ident(mod, NULL, base_name, "type", first, line, &stype->info.ident.ref); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3432 | has_str = 1; |
| 3433 | break; |
| 3434 | case UNRES_TYPE_LEAFREF: |
Michal Vasko | 563ef09 | 2015-09-04 13:17:23 +0200 | [diff] [blame] | 3435 | node = str_snode; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 3436 | stype = item; |
| 3437 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3438 | rc = resolve_path_arg_schema(mod, stype->info.lref.path, node, first, line, |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 3439 | (struct lys_node **)&stype->info.lref.target); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3440 | has_str = 0; |
| 3441 | break; |
| 3442 | case UNRES_TYPE_DER: |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 3443 | base_name = str_snode; |
| 3444 | stype = item; |
| 3445 | |
| 3446 | /* HACK type->der is temporarily its parent */ |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3447 | rc = resolve_superior_type(base_name, stype->module_name, mod, (struct lys_node *)stype->der, &stype->der); |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 3448 | if (rc == -1) { |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3449 | LOGVAL(LYE_INMOD, line, stype->module_name); |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 3450 | } else if (!rc) { |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 3451 | stype->base = stype->der->type.base; |
| 3452 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3453 | has_str = 1; |
| 3454 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3455 | case UNRES_IFFEAT: |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 3456 | base_name = str_snode; |
| 3457 | feat_ptr = item; |
| 3458 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3459 | rc = resolve_feature(base_name, mod, first, line, feat_ptr); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3460 | has_str = 1; |
| 3461 | break; |
| 3462 | case UNRES_USES: |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 3463 | rc = resolve_unres_schema_uses(item, unres, first, line); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3464 | has_str = 0; |
| 3465 | break; |
| 3466 | case UNRES_TYPE_DFLT: |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 3467 | base_name = str_snode; |
| 3468 | stype = item; |
| 3469 | |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 3470 | rc = check_default(stype, base_name, first, line); |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 3471 | /* do not remove base_name (dflt), it's in a typedef */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3472 | has_str = 0; |
| 3473 | break; |
| 3474 | case UNRES_CHOICE_DFLT: |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 3475 | base_name = str_snode; |
| 3476 | choic = item; |
| 3477 | |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 3478 | choic->dflt = resolve_choice_dflt(choic, base_name); |
| 3479 | if (choic->dflt) { |
| 3480 | rc = EXIT_SUCCESS; |
| 3481 | } else { |
| 3482 | rc = EXIT_FAILURE; |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 3483 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3484 | has_str = 1; |
| 3485 | break; |
| 3486 | case UNRES_LIST_KEYS: |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3487 | rc = resolve_list_keys(mod, item, str_snode, first, line); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3488 | has_str = 1; |
| 3489 | break; |
| 3490 | case UNRES_LIST_UNIQ: |
Michal Vasko | 5fcfe7e | 2015-08-17 14:59:57 +0200 | [diff] [blame] | 3491 | /* actually the unique string */ |
| 3492 | base_name = str_snode; |
| 3493 | uniq = item; |
| 3494 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3495 | rc = resolve_unique((struct lys_node *)uniq->leafs, base_name, uniq, first, line); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3496 | has_str = 1; |
| 3497 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3498 | default: |
| 3499 | LOGINT; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3500 | break; |
| 3501 | } |
| 3502 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3503 | if (has_str && !rc) { |
| 3504 | lydict_remove(mod->ctx, str_snode); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3505 | } |
| 3506 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3507 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3508 | } |
| 3509 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3510 | /* logs directly */ |
| 3511 | static void |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 3512 | print_unres_schema_item_fail(void *item, enum UNRES_ITEM type, void *str_node, uint32_t line) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3513 | { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3514 | char line_str[18]; |
| 3515 | |
| 3516 | if (line) { |
| 3517 | sprintf(line_str, " (line %u)", line); |
| 3518 | } else { |
| 3519 | line_str[0] = '\0'; |
| 3520 | } |
| 3521 | |
| 3522 | switch (type) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3523 | case UNRES_IDENT: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3524 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later%s.", "identity", (char *)str_node, line_str); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3525 | break; |
| 3526 | case UNRES_TYPE_IDENTREF: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3527 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later%s.", "identityref", (char *)str_node, line_str); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3528 | break; |
| 3529 | case UNRES_TYPE_LEAFREF: |
| 3530 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later%s.", "leafref", ((struct lys_type *)item)->info.lref.path, line_str); |
| 3531 | break; |
| 3532 | case UNRES_TYPE_DER: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3533 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later%s.", "derived type", (char *)str_node, line_str); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3534 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3535 | case UNRES_IFFEAT: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3536 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later%s.", "if-feature", (char *)str_node, line_str); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3537 | break; |
| 3538 | case UNRES_USES: |
| 3539 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later%s.", "uses", ((struct lys_node_uses *)item)->name, line_str); |
| 3540 | break; |
| 3541 | case UNRES_TYPE_DFLT: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3542 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later%s.", "type default", (char *)str_node, line_str); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3543 | break; |
| 3544 | case UNRES_CHOICE_DFLT: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3545 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later%s.", "choice default", (char *)str_node, line_str); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3546 | break; |
| 3547 | case UNRES_LIST_KEYS: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3548 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later%s.", "list keys", (char *)str_node, line_str); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3549 | break; |
| 3550 | case UNRES_LIST_UNIQ: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3551 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later%s.", "list unique", (char *)str_node, line_str); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3552 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3553 | default: |
| 3554 | LOGINT; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3555 | break; |
| 3556 | } |
| 3557 | } |
| 3558 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3559 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3560 | * @brief Resolve every unres schema item in the structure. Logs directly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3561 | * |
| 3562 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3563 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3564 | * |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 3565 | * @return EXIT_SUCCESS on success, -1 on error. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3566 | */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3567 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 3568 | resolve_unres_schema(struct lys_module *mod, struct unres_schema *unres) |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3569 | { |
Michal Vasko | 1c4e0ef | 2015-08-19 11:19:28 +0200 | [diff] [blame] | 3570 | uint32_t i, resolved, unres_uses, res_uses; |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3571 | int rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3572 | |
| 3573 | assert(unres); |
| 3574 | |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 3575 | resolved = 0; |
| 3576 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3577 | /* uses */ |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 3578 | do { |
| 3579 | unres_uses = 0; |
| 3580 | res_uses = 0; |
| 3581 | |
| 3582 | for (i = 0; i < unres->count; ++i) { |
| 3583 | if (unres->type[i] != UNRES_USES) { |
| 3584 | continue; |
| 3585 | } |
| 3586 | |
| 3587 | ++unres_uses; |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 3588 | rc = resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres, 0, |
| 3589 | LOGLINE_IDX(unres, i)); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3590 | if (!rc) { |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 3591 | unres->type[i] = UNRES_RESOLVED; |
| 3592 | ++resolved; |
| 3593 | ++res_uses; |
Michal Vasko | 89e1532 | 2015-08-17 15:46:55 +0200 | [diff] [blame] | 3594 | } else if (rc == -1) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3595 | return -1; |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 3596 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3597 | } |
Michal Vasko | 51054ca | 2015-08-12 12:20:00 +0200 | [diff] [blame] | 3598 | } while (res_uses && (res_uses < unres_uses)); |
| 3599 | |
| 3600 | if (res_uses < unres_uses) { |
| 3601 | LOGVAL(LYE_SPEC, 0, "There are unresolved uses left."); |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 3602 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3603 | } |
| 3604 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3605 | /* the rest */ |
| 3606 | for (i = 0; i < unres->count; ++i) { |
| 3607 | if (unres->type[i] == UNRES_RESOLVED) { |
| 3608 | continue; |
| 3609 | } |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 3610 | |
Michal Vasko | 407f1bb | 2015-09-23 15:51:07 +0200 | [diff] [blame] | 3611 | rc = resolve_unres_schema_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres, 0, |
| 3612 | LOGLINE_IDX(unres, i)); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3613 | if (rc) { |
| 3614 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3615 | } |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3616 | |
| 3617 | unres->type[i] = UNRES_RESOLVED; |
| 3618 | ++resolved; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3619 | } |
| 3620 | |
| 3621 | if (resolved < unres->count) { |
Michal Vasko | 92b8a38 | 2015-08-19 14:03:49 +0200 | [diff] [blame] | 3622 | LOGVAL(LYE_SPEC, 0, "There are unresolved schema items left."); |
| 3623 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3624 | } |
| 3625 | |
| 3626 | return EXIT_SUCCESS; |
| 3627 | } |
| 3628 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3629 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3630 | * @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] | 3631 | * |
| 3632 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3633 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3634 | * @param[in] item Item to resolve. Type determined by \p type. |
| 3635 | * @param[in] type Type of the unresolved item. |
| 3636 | * @param[in] str String argument. |
| 3637 | * @param[in] line Line in the input file. |
| 3638 | * |
| 3639 | * @return EXIT_SUCCESS on success or storing the item in unres, -1 on error. |
| 3640 | */ |
| 3641 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 3642 | unres_schema_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type, const char *str, |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 3643 | uint32_t line) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3644 | { |
| 3645 | str = lydict_insert(mod->ctx, str, 0); |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 3646 | return unres_schema_add_node(mod, unres, item, type, (struct lys_node *)str, line); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3647 | } |
| 3648 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3649 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3650 | * @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] | 3651 | * |
| 3652 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3653 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3654 | * @param[in] item Item to resolve. Type determined by \p type. |
| 3655 | * @param[in] type Type of the unresolved item. |
| 3656 | * @param[in] snode Schema node argument. |
| 3657 | * @param[in] line Line in the input file. |
| 3658 | * |
| 3659 | * @return EXIT_SUCCESS on success or storing the item in unres, -1 on error. |
| 3660 | */ |
| 3661 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 3662 | unres_schema_add_node(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type, |
Michal Vasko | 7955b36 | 2015-09-04 14:18:15 +0200 | [diff] [blame] | 3663 | struct lys_node *snode, uint32_t line) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3664 | { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3665 | int rc; |
| 3666 | |
Michal Vasko | 9bf425b | 2015-10-22 11:42:03 +0200 | [diff] [blame] | 3667 | assert(unres && item && ((type != UNRES_LEAFREF) && (type != UNRES_INSTID) && (type != UNRES_WHEN) |
| 3668 | && (type != UNRES_MUST))); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3669 | |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3670 | rc = resolve_unres_schema_item(mod, item, type, snode, unres, 1, line); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3671 | if (rc != EXIT_FAILURE) { |
| 3672 | return rc; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3673 | } |
| 3674 | |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 3675 | print_unres_schema_item_fail(item, type, snode, line); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3676 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3677 | unres->count++; |
| 3678 | unres->item = realloc(unres->item, unres->count*sizeof *unres->item); |
| 3679 | unres->item[unres->count-1] = item; |
| 3680 | unres->type = realloc(unres->type, unres->count*sizeof *unres->type); |
| 3681 | unres->type[unres->count-1] = type; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 3682 | unres->str_snode = realloc(unres->str_snode, unres->count*sizeof *unres->str_snode); |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3683 | unres->str_snode[unres->count-1] = snode; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 3684 | #ifndef NDEBUG |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3685 | unres->line = realloc(unres->line, unres->count*sizeof *unres->line); |
| 3686 | unres->line[unres->count-1] = line; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 3687 | #endif |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3688 | |
| 3689 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3690 | } |
| 3691 | |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3692 | /** |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3693 | * @brief Duplicate an unres schema item. Logs indirectly. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3694 | * |
| 3695 | * @param[in] mod Main module. |
Michal Vasko | bb21112 | 2015-08-19 14:03:11 +0200 | [diff] [blame] | 3696 | * @param[in] unres Unres schema structure to use. |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3697 | * @param[in] item Old item to be resolved. |
| 3698 | * @param[in] type Type of the old unresolved item. |
| 3699 | * @param[in] new_item New item to use in the duplicate. |
| 3700 | * |
| 3701 | * @return EXIT_SUCCESS on success, -1 on error. |
| 3702 | */ |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 3703 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 3704 | 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] | 3705 | { |
| 3706 | int i; |
| 3707 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3708 | 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] | 3709 | |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 3710 | i = unres_schema_find(unres, item, type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3711 | |
| 3712 | if (i == -1) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3713 | return -1; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3714 | } |
| 3715 | |
Michal Vasko | 0d20459 | 2015-10-07 09:50:04 +0200 | [diff] [blame] | 3716 | if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT)) { |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 3717 | if (unres_schema_add_node(mod, unres, new_item, type, unres->str_snode[i], 0) == -1) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3718 | LOGINT; |
| 3719 | return -1; |
| 3720 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3721 | } else { |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 3722 | if (unres_schema_add_str(mod, unres, new_item, type, unres->str_snode[i], 0) == -1) { |
Michal Vasko | 3ab70fc | 2015-08-17 14:06:23 +0200 | [diff] [blame] | 3723 | LOGINT; |
| 3724 | return -1; |
| 3725 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3726 | } |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 3727 | |
| 3728 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3729 | } |
| 3730 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 3731 | /* does not log */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 3732 | int |
Michal Vasko | 0bd29d1 | 2015-08-19 11:45:49 +0200 | [diff] [blame] | 3733 | 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] | 3734 | { |
| 3735 | uint32_t ret = -1, i; |
| 3736 | |
| 3737 | for (i = 0; i < unres->count; ++i) { |
| 3738 | if ((unres->item[i] == item) && (unres->type[i] == type)) { |
| 3739 | ret = i; |
| 3740 | break; |
| 3741 | } |
| 3742 | } |
| 3743 | |
| 3744 | return ret; |
| 3745 | } |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3746 | |
| 3747 | /* logs directly */ |
| 3748 | static void |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3749 | print_unres_data_item_fail(struct lyd_node *node, enum UNRES_ITEM type, uint32_t line) |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3750 | { |
| 3751 | struct lys_node_leaf *sleaf; |
| 3752 | char line_str[18]; |
| 3753 | |
| 3754 | if (line) { |
| 3755 | sprintf(line_str, " (line %u)", line); |
| 3756 | } else { |
| 3757 | line_str[0] = '\0'; |
| 3758 | } |
| 3759 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3760 | sleaf = (struct lys_node_leaf *)node->schema; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3761 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3762 | switch (type) { |
| 3763 | case UNRES_LEAFREF: |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3764 | LOGVRB("Leafref \"%s\" could not be resolved, it will be attempted later%s.", |
| 3765 | sleaf->type.info.lref.path, line_str); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3766 | break; |
| 3767 | case UNRES_INSTID: |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3768 | LOGVRB("Instance-identifier \"%s\" could not be resolved, it will be attempted later%s.", |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 3769 | ((struct lyd_node_leaf_list *)node)->value_str, line_str); |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3770 | break; |
| 3771 | case UNRES_WHEN: |
| 3772 | LOGVRB("There was an unsatisfied when condition, evaluation will be attempted later%s.", line_str); |
| 3773 | break; |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 3774 | case UNRES_MUST: |
| 3775 | LOGVRB("There was an unsatisfied must condition, evaluation will be attempted later%s.", line_str); |
| 3776 | break; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3777 | default: |
| 3778 | LOGINT; |
| 3779 | break; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3780 | } |
| 3781 | } |
| 3782 | |
| 3783 | /** |
| 3784 | * @brief Resolve a single unres data item. Logs directly. |
| 3785 | * |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3786 | * @param[in] node Data node to resolve. |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3787 | * @param[in] first Whether this is the first resolution try. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3788 | * @param[in] type Type of the unresolved item. |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3789 | * @param[in] line Line in the input file. 0 skips line print. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3790 | * |
| 3791 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error. |
| 3792 | */ |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 3793 | int |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3794 | resolve_unres_data_item(struct lyd_node *node, enum UNRES_ITEM type, int first, uint32_t line) |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3795 | { |
| 3796 | uint32_t i; |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3797 | int rc; |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 3798 | struct lyd_node_leaf_list *leaf; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3799 | struct lys_node_leaf *sleaf; |
| 3800 | struct unres_data matches; |
| 3801 | |
| 3802 | memset(&matches, 0, sizeof matches); |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 3803 | leaf = (struct lyd_node_leaf_list *)node; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3804 | sleaf = (struct lys_node_leaf *)leaf->schema; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3805 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3806 | switch (type) { |
| 3807 | case UNRES_LEAFREF: |
| 3808 | assert(sleaf->type.base == LY_TYPE_LEAFREF); |
Michal Vasko | f767761 | 2015-10-16 14:27:23 +0200 | [diff] [blame] | 3809 | if ((rc = resolve_path_arg_data(node, sleaf->type.info.lref.path, first, line, &matches))) { |
Michal Vasko | 0491ab3 | 2015-08-19 14:28:29 +0200 | [diff] [blame] | 3810 | return rc; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3811 | } |
| 3812 | |
| 3813 | /* check that value matches */ |
| 3814 | for (i = 0; i < matches.count; ++i) { |
Michal Vasko | 83a6c46 | 2015-10-08 16:43:53 +0200 | [diff] [blame] | 3815 | if (leaf->value_str == ((struct lyd_node_leaf_list *)matches.node[i])->value_str) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3816 | leaf->value.leafref = matches.node[i]; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3817 | break; |
| 3818 | } |
| 3819 | } |
| 3820 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3821 | free(matches.node); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3822 | memset(&matches, 0, sizeof matches); |
| 3823 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3824 | if (!leaf->value.leafref) { |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3825 | /* reference not found */ |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3826 | if (!first) { |
| 3827 | LOGVAL(LYE_SPEC, line, "Leafref \"%s\" value \"%s\" did not match any node value.", |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3828 | sleaf->type.info.lref.path, leaf->value_str); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3829 | } |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3830 | return EXIT_FAILURE; |
| 3831 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3832 | break; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3833 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3834 | case UNRES_INSTID: |
| 3835 | assert(sleaf->type.base == LY_TYPE_INST); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3836 | ly_errno = 0; |
Michal Vasko | f39142b | 2015-10-21 11:40:05 +0200 | [diff] [blame] | 3837 | if (!resolve_instid(node, leaf->value_str, line)) { |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3838 | if (ly_errno) { |
| 3839 | return -1; |
| 3840 | } else if (sleaf->type.info.inst.req > -1) { |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3841 | if (!first) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3842 | LOGVAL(LYE_SPEC, line, "There is no instance of \"%s\".", leaf->value_str); |
Michal Vasko | 184521f | 2015-09-24 13:14:26 +0200 | [diff] [blame] | 3843 | } |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3844 | return EXIT_FAILURE; |
| 3845 | } else { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3846 | LOGVRB("There is no instance of \"%s\", but is not required.", leaf->value_str); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3847 | } |
| 3848 | } |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3849 | break; |
| 3850 | |
| 3851 | case UNRES_WHEN: |
| 3852 | if ((rc = resolve_when(node, first, line))) { |
| 3853 | return rc; |
| 3854 | } |
| 3855 | break; |
| 3856 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 3857 | case UNRES_MUST: |
| 3858 | if ((rc = resolve_must(node, first, line))) { |
| 3859 | return rc; |
| 3860 | } |
| 3861 | break; |
| 3862 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3863 | default: |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3864 | LOGINT; |
| 3865 | return -1; |
| 3866 | } |
| 3867 | |
| 3868 | return EXIT_SUCCESS; |
| 3869 | } |
| 3870 | |
| 3871 | /** |
| 3872 | * @brief Try to resolve an unres data item. Logs indirectly. |
| 3873 | * |
| 3874 | * @param[in] unres Unres data structure to use. |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3875 | * @param[in] node Data node to use. |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3876 | * @param[in] line Line in the input file. |
| 3877 | * |
| 3878 | * @return EXIT_SUCCESS on success or storing the item in unres, -1 on error. |
| 3879 | */ |
| 3880 | int |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3881 | unres_data_add(struct unres_data *unres, struct lyd_node *node, enum UNRES_ITEM type, uint32_t line) |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3882 | { |
| 3883 | int rc; |
| 3884 | |
Michal Vasko | bf19d25 | 2015-10-08 15:39:17 +0200 | [diff] [blame] | 3885 | assert(unres && node && ((type == UNRES_LEAFREF) || (type == UNRES_INSTID) || (type == UNRES_WHEN) || (type == UNRES_MUST))); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3886 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3887 | rc = resolve_unres_data_item(node, type, 1, line); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3888 | if (rc != EXIT_FAILURE) { |
| 3889 | return rc; |
| 3890 | } |
| 3891 | |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3892 | print_unres_data_item_fail(node, type, line); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3893 | |
| 3894 | ++unres->count; |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3895 | unres->node = realloc(unres->node, unres->count * sizeof *unres->node); |
| 3896 | unres->node[unres->count - 1] = node; |
| 3897 | unres->type = realloc(unres->type, unres->count * sizeof *unres->type); |
| 3898 | unres->type[unres->count - 1] = type; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3899 | #ifndef NDEBUG |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3900 | unres->line = realloc(unres->line, unres->count * sizeof *unres->line); |
| 3901 | unres->line[unres->count - 1] = line; |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3902 | #endif |
| 3903 | |
| 3904 | return EXIT_SUCCESS; |
| 3905 | } |
| 3906 | |
| 3907 | /** |
| 3908 | * @brief Resolve every unres data item in the structure. Logs directly. |
| 3909 | * |
| 3910 | * @param[in] unres Unres data structure to use. |
| 3911 | * |
| 3912 | * @return EXIT_SUCCESS on success, -1 on error. |
| 3913 | */ |
| 3914 | int |
| 3915 | resolve_unres_data(struct unres_data *unres) |
| 3916 | { |
| 3917 | uint32_t i; |
| 3918 | int rc; |
| 3919 | |
| 3920 | for (i = 0; i < unres->count; ++i) { |
Michal Vasko | cf02470 | 2015-10-08 15:01:42 +0200 | [diff] [blame] | 3921 | rc = resolve_unres_data_item(unres->node[i], unres->type[i], 0, LOGLINE_IDX(unres, i)); |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame] | 3922 | if (rc) { |
| 3923 | LOGVAL(LYE_SPEC, 0, "There are unresolved data items left."); |
| 3924 | return -1; |
| 3925 | } |
| 3926 | } |
| 3927 | |
| 3928 | return EXIT_SUCCESS; |
| 3929 | } |