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" |
| 33 | #include "parse.h" |
| 34 | #include "dict.h" |
| 35 | #include "tree_internal.h" |
| 36 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 37 | /** |
| 38 | * @brief Resolves length or range intervals. Does not log. |
| 39 | * Syntax is assumed to be correct, *local_intv MUST be NULL. |
| 40 | * |
| 41 | * @param[in] str_restr The restriction as a string. |
| 42 | * @param[in] type The type of the restriction. |
| 43 | * @param[in] superior_restr Flag whether to check superior |
| 44 | * types. |
| 45 | * @param[out] local_intv The final interval structure. |
| 46 | * |
| 47 | * @return EXIT_SUCCESS on succes, EXIT_FAILURE otherwise. |
| 48 | */ |
Michal Vasko | b2daf5b | 2015-08-05 16:15:37 +0200 | [diff] [blame] | 49 | int |
| 50 | resolve_len_ran_interval(const char *str_restr, struct lys_type *type, int superior_restr, struct len_ran_intv** local_intv) |
| 51 | { |
| 52 | /* 0 - unsigned, 1 - signed, 2 - floating point */ |
| 53 | int kind, ret = 0; |
| 54 | int64_t local_smin, local_smax; |
| 55 | uint64_t local_umin, local_umax; |
| 56 | long double local_fmin, local_fmax; |
| 57 | const char *seg_ptr, *ptr; |
| 58 | struct len_ran_intv *tmp_local_intv, *tmp_intv, *intv = NULL; |
| 59 | |
| 60 | switch (type->base) { |
| 61 | case LY_TYPE_BINARY: |
| 62 | kind = 0; |
| 63 | local_umin = 0; |
| 64 | local_umax = 18446744073709551615UL; |
| 65 | |
| 66 | if (!str_restr && type->info.binary.length) { |
| 67 | str_restr = type->info.binary.length->expr; |
| 68 | } |
| 69 | break; |
| 70 | case LY_TYPE_DEC64: |
| 71 | kind = 2; |
| 72 | local_fmin = -9223372036854775808.0; |
| 73 | local_fmin /= 1 << type->info.dec64.dig; |
| 74 | local_fmax = 9223372036854775807.0; |
| 75 | local_fmax /= 1 << type->info.dec64.dig; |
| 76 | |
| 77 | if (!str_restr && type->info.dec64.range) { |
| 78 | str_restr = type->info.dec64.range->expr; |
| 79 | } |
| 80 | break; |
| 81 | case LY_TYPE_INT8: |
| 82 | kind = 1; |
| 83 | local_smin = -128; |
| 84 | local_smax = 127; |
| 85 | |
| 86 | if (!str_restr && type->info.num.range) { |
| 87 | str_restr = type->info.num.range->expr; |
| 88 | } |
| 89 | break; |
| 90 | case LY_TYPE_INT16: |
| 91 | kind = 1; |
| 92 | local_smin = -32768; |
| 93 | local_smax = 32767; |
| 94 | |
| 95 | if (!str_restr && type->info.num.range) { |
| 96 | str_restr = type->info.num.range->expr; |
| 97 | } |
| 98 | break; |
| 99 | case LY_TYPE_INT32: |
| 100 | kind = 1; |
| 101 | local_smin = -2147483648; |
| 102 | local_smax = 2147483647; |
| 103 | |
| 104 | if (!str_restr && type->info.num.range) { |
| 105 | str_restr = type->info.num.range->expr; |
| 106 | } |
| 107 | break; |
| 108 | case LY_TYPE_INT64: |
| 109 | kind = 1; |
| 110 | local_smin = -9223372036854775807L - 1L; |
| 111 | local_smax = 9223372036854775807L; |
| 112 | |
| 113 | if (!str_restr && type->info.num.range) { |
| 114 | str_restr = type->info.num.range->expr; |
| 115 | } |
| 116 | break; |
| 117 | case LY_TYPE_UINT8: |
| 118 | kind = 0; |
| 119 | local_umin = 0; |
| 120 | local_umax = 255; |
| 121 | |
| 122 | if (!str_restr && type->info.num.range) { |
| 123 | str_restr = type->info.num.range->expr; |
| 124 | } |
| 125 | break; |
| 126 | case LY_TYPE_UINT16: |
| 127 | kind = 0; |
| 128 | local_umin = 0; |
| 129 | local_umax = 65535; |
| 130 | |
| 131 | if (!str_restr && type->info.num.range) { |
| 132 | str_restr = type->info.num.range->expr; |
| 133 | } |
| 134 | break; |
| 135 | case LY_TYPE_UINT32: |
| 136 | kind = 0; |
| 137 | local_umin = 0; |
| 138 | local_umax = 4294967295; |
| 139 | |
| 140 | if (!str_restr && type->info.num.range) { |
| 141 | str_restr = type->info.num.range->expr; |
| 142 | } |
| 143 | break; |
| 144 | case LY_TYPE_UINT64: |
| 145 | kind = 0; |
| 146 | local_umin = 0; |
| 147 | local_umax = 18446744073709551615UL; |
| 148 | |
| 149 | if (!str_restr && type->info.num.range) { |
| 150 | str_restr = type->info.num.range->expr; |
| 151 | } |
| 152 | break; |
| 153 | case LY_TYPE_STRING: |
| 154 | kind = 0; |
| 155 | local_umin = 0; |
| 156 | local_umax = 18446744073709551615UL; |
| 157 | |
| 158 | if (!str_restr && type->info.str.length) { |
| 159 | str_restr = type->info.str.length->expr; |
| 160 | } |
| 161 | break; |
| 162 | default: |
| 163 | LOGINT; |
| 164 | return EXIT_FAILURE; |
| 165 | } |
| 166 | |
| 167 | /* process superior types */ |
| 168 | if (type->der && superior_restr) { |
| 169 | assert(!resolve_len_ran_interval(NULL, &type->der->type, superior_restr, &intv)); |
| 170 | assert(!intv || (intv->kind == kind)); |
| 171 | } |
| 172 | |
| 173 | if (!str_restr) { |
| 174 | /* we are validating data and not have any restriction, but a superior type might have */ |
| 175 | if (type->der && !superior_restr && !intv) { |
| 176 | assert(!resolve_len_ran_interval(NULL, &type->der->type, superior_restr, &intv)); |
| 177 | assert(!intv || (intv->kind == kind)); |
| 178 | } |
| 179 | *local_intv = intv; |
| 180 | return EXIT_SUCCESS; |
| 181 | } |
| 182 | |
| 183 | /* adjust local min and max */ |
| 184 | if (intv) { |
| 185 | tmp_intv = intv; |
| 186 | |
| 187 | if (kind == 0) { |
| 188 | local_umin = tmp_intv->value.uval.min; |
| 189 | } else if (kind == 1) { |
| 190 | local_smin = tmp_intv->value.sval.min; |
| 191 | } else if (kind == 2) { |
| 192 | local_fmin = tmp_intv->value.fval.min; |
| 193 | } |
| 194 | |
| 195 | while (tmp_intv->next) { |
| 196 | tmp_intv = tmp_intv->next; |
| 197 | } |
| 198 | |
| 199 | if (kind == 0) { |
| 200 | local_umax = tmp_intv->value.uval.max; |
| 201 | } else if (kind == 1) { |
| 202 | local_smax = tmp_intv->value.sval.max; |
| 203 | } else if (kind == 2) { |
| 204 | local_fmax = tmp_intv->value.fval.max; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /* finally parse our restriction */ |
| 209 | seg_ptr = str_restr; |
| 210 | while (1) { |
| 211 | if (!*local_intv) { |
| 212 | *local_intv = malloc(sizeof **local_intv); |
| 213 | tmp_local_intv = *local_intv; |
| 214 | } else { |
| 215 | tmp_local_intv->next = malloc(sizeof **local_intv); |
| 216 | tmp_local_intv = tmp_local_intv->next; |
| 217 | } |
| 218 | |
| 219 | tmp_local_intv->kind = kind; |
| 220 | tmp_local_intv->next = NULL; |
| 221 | |
| 222 | /* min */ |
| 223 | ptr = seg_ptr; |
| 224 | while (isspace(ptr[0])) { |
| 225 | ++ptr; |
| 226 | } |
| 227 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 228 | if (kind == 0) { |
| 229 | tmp_local_intv->value.uval.min = atoll(ptr); |
| 230 | } else if (kind == 1) { |
| 231 | tmp_local_intv->value.sval.min = atoll(ptr); |
| 232 | } else if (kind == 2) { |
| 233 | tmp_local_intv->value.fval.min = atoll(ptr); |
| 234 | } |
| 235 | |
| 236 | if ((ptr[0] == '+') || (ptr[0] == '-')) { |
| 237 | ++ptr; |
| 238 | } |
| 239 | while (isdigit(ptr[0])) { |
| 240 | ++ptr; |
| 241 | } |
| 242 | } else if (!strncmp(ptr, "min", 3)) { |
| 243 | if (kind == 0) { |
| 244 | tmp_local_intv->value.uval.min = local_umin; |
| 245 | } else if (kind == 1) { |
| 246 | tmp_local_intv->value.sval.min = local_smin; |
| 247 | } else if (kind == 2) { |
| 248 | tmp_local_intv->value.fval.min = local_fmin; |
| 249 | } |
| 250 | |
| 251 | ptr += 3; |
| 252 | } else if (!strncmp(ptr, "max", 3)) { |
| 253 | if (kind == 0) { |
| 254 | tmp_local_intv->value.uval.min = local_umax; |
| 255 | } else if (kind == 1) { |
| 256 | tmp_local_intv->value.sval.min = local_smax; |
| 257 | } else if (kind == 2) { |
| 258 | tmp_local_intv->value.fval.min = local_fmax; |
| 259 | } |
| 260 | |
| 261 | ptr += 3; |
| 262 | } else { |
| 263 | assert(0); |
| 264 | } |
| 265 | |
| 266 | while (isspace(ptr[0])) { |
| 267 | ptr++; |
| 268 | } |
| 269 | |
| 270 | /* no interval or interval */ |
| 271 | if ((ptr[0] == '|') || !ptr[0]) { |
| 272 | if (kind == 0) { |
| 273 | tmp_local_intv->value.uval.max = tmp_local_intv->value.uval.min; |
| 274 | } else if (kind == 1) { |
| 275 | tmp_local_intv->value.sval.max = tmp_local_intv->value.sval.min; |
| 276 | } else if (kind == 2) { |
| 277 | tmp_local_intv->value.fval.max = tmp_local_intv->value.fval.min; |
| 278 | } |
| 279 | } else if (!strncmp(ptr, "..", 2)) { |
| 280 | /* skip ".." */ |
| 281 | ptr += 2; |
| 282 | while (isspace(ptr[0])) { |
| 283 | ++ptr; |
| 284 | } |
| 285 | |
| 286 | /* max */ |
| 287 | if (isdigit(ptr[0]) || (ptr[0] == '+') || (ptr[0] == '-')) { |
| 288 | if (kind == 0) { |
| 289 | tmp_local_intv->value.uval.max = atoll(ptr); |
| 290 | } else if (kind == 1) { |
| 291 | tmp_local_intv->value.sval.max = atoll(ptr); |
| 292 | } else if (kind == 2) { |
| 293 | tmp_local_intv->value.fval.max = atoll(ptr); |
| 294 | } |
| 295 | } else if (!strncmp(ptr, "max", 3)) { |
| 296 | if (kind == 0) { |
| 297 | tmp_local_intv->value.uval.max = local_umax; |
| 298 | } else if (kind == 1) { |
| 299 | tmp_local_intv->value.sval.max = local_smax; |
| 300 | } else if (kind == 2) { |
| 301 | tmp_local_intv->value.fval.max = local_fmax; |
| 302 | } |
| 303 | } else { |
| 304 | assert(0); |
| 305 | } |
| 306 | } else { |
| 307 | assert(0); |
| 308 | } |
| 309 | |
| 310 | /* next segment (next OR) */ |
| 311 | seg_ptr = strchr(seg_ptr, '|'); |
| 312 | if (!seg_ptr) { |
| 313 | break; |
| 314 | } |
| 315 | seg_ptr++; |
| 316 | } |
| 317 | |
| 318 | /* check local restrictions against superior ones */ |
| 319 | if (intv) { |
| 320 | tmp_intv = intv; |
| 321 | tmp_local_intv = *local_intv; |
| 322 | |
| 323 | while (tmp_local_intv && tmp_intv) { |
| 324 | /* reuse local variables */ |
| 325 | if (kind == 0) { |
| 326 | local_umin = tmp_local_intv->value.uval.min; |
| 327 | local_umax = tmp_local_intv->value.uval.max; |
| 328 | |
| 329 | /* it must be in this interval */ |
| 330 | if ((local_umin >= tmp_intv->value.uval.min) && (local_umin <= tmp_intv->value.uval.max)) { |
| 331 | /* this interval is covered, next one */ |
| 332 | if (local_umax <= tmp_intv->value.uval.max) { |
| 333 | tmp_local_intv = tmp_local_intv->next; |
| 334 | continue; |
| 335 | /* ascending order of restrictions -> fail */ |
| 336 | } else { |
| 337 | ret = EXIT_FAILURE; |
| 338 | goto cleanup; |
| 339 | } |
| 340 | } |
| 341 | } else if (kind == 1) { |
| 342 | local_smin = tmp_local_intv->value.sval.min; |
| 343 | local_smax = tmp_local_intv->value.sval.max; |
| 344 | |
| 345 | if ((local_smin >= tmp_intv->value.sval.min) && (local_smin <= tmp_intv->value.sval.max)) { |
| 346 | if (local_smax <= tmp_intv->value.sval.max) { |
| 347 | tmp_local_intv = tmp_local_intv->next; |
| 348 | continue; |
| 349 | } else { |
| 350 | ret = EXIT_FAILURE; |
| 351 | goto cleanup; |
| 352 | } |
| 353 | } |
| 354 | } else if (kind == 2) { |
| 355 | local_fmin = tmp_local_intv->value.fval.min; |
| 356 | local_fmax = tmp_local_intv->value.fval.max; |
| 357 | |
| 358 | if ((local_fmin >= tmp_intv->value.fval.min) && (local_fmin <= tmp_intv->value.fval.max)) { |
| 359 | if (local_fmax <= tmp_intv->value.fval.max) { |
| 360 | tmp_local_intv = tmp_local_intv->next; |
| 361 | continue; |
| 362 | } else { |
| 363 | ret = EXIT_FAILURE; |
| 364 | goto cleanup; |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | tmp_intv = tmp_intv->next; |
| 370 | } |
| 371 | |
| 372 | /* some interval left uncovered -> fail */ |
| 373 | if (tmp_local_intv) { |
| 374 | ret = EXIT_FAILURE; |
| 375 | } |
| 376 | |
| 377 | } |
| 378 | |
| 379 | cleanup: |
| 380 | while (intv) { |
| 381 | tmp_intv = intv->next; |
| 382 | free(intv); |
| 383 | intv = tmp_intv; |
| 384 | } |
| 385 | |
| 386 | /* fail */ |
| 387 | if (ret) { |
| 388 | while (*local_intv) { |
| 389 | tmp_local_intv = (*local_intv)->next; |
| 390 | free(*local_intv); |
| 391 | *local_intv = tmp_local_intv; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | return ret; |
| 396 | } |
| 397 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 398 | /** |
| 399 | * @brief Resolve a typedef. Does not log. |
| 400 | * |
| 401 | * @param[in] name Typedef name. |
| 402 | * @param[in] prefix Typedef name prefix. |
| 403 | * @param[in] module The main module. |
| 404 | * @param[in] parent The parent of the resolved type definition. |
| 405 | * |
| 406 | * @return Typedef pointer on succes, NULL otherwise. |
| 407 | */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 408 | struct lys_tpdf * |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 409 | resolve_superior_type(const char *name, const char *prefix, struct lys_module *module, struct lys_node *parent) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 410 | { |
| 411 | int i, j, found = 0; |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 412 | struct lys_tpdf *tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 413 | int tpdf_size; |
| 414 | |
| 415 | if (!prefix) { |
| 416 | /* no prefix, try built-in types */ |
| 417 | for (i = 1; i < LY_DATA_TYPE_COUNT; i++) { |
| 418 | if (!strcmp(ly_types[i].def->name, name)) { |
| 419 | return ly_types[i].def; |
| 420 | } |
| 421 | } |
| 422 | } else { |
| 423 | if (!strcmp(prefix, module->prefix)) { |
| 424 | /* prefix refers to the current module, ignore it */ |
| 425 | prefix = NULL; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | if (!prefix && parent) { |
| 430 | /* search in local typedefs */ |
| 431 | while (parent) { |
| 432 | switch (parent->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 433 | case LYS_CONTAINER: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 434 | tpdf_size = ((struct lys_node_container *)parent)->tpdf_size; |
| 435 | tpdf = ((struct lys_node_container *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 436 | break; |
| 437 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 438 | case LYS_LIST: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 439 | tpdf_size = ((struct lys_node_list *)parent)->tpdf_size; |
| 440 | tpdf = ((struct lys_node_list *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 441 | break; |
| 442 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 443 | case LYS_GROUPING: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 444 | tpdf_size = ((struct lys_node_grp *)parent)->tpdf_size; |
| 445 | tpdf = ((struct lys_node_grp *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 446 | break; |
| 447 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 448 | case LYS_RPC: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 449 | tpdf_size = ((struct lys_node_rpc *)parent)->tpdf_size; |
| 450 | tpdf = ((struct lys_node_rpc *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 451 | break; |
| 452 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 453 | case LYS_NOTIF: |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 454 | tpdf_size = ((struct lys_node_notif *)parent)->tpdf_size; |
| 455 | tpdf = ((struct lys_node_notif *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 456 | break; |
| 457 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 458 | case LYS_INPUT: |
| 459 | case LYS_OUTPUT: |
Radek Krejci | 4608ada | 2015-08-05 16:04:37 +0200 | [diff] [blame] | 460 | tpdf_size = ((struct lys_node_rpc_inout *)parent)->tpdf_size; |
| 461 | tpdf = ((struct lys_node_rpc_inout *)parent)->tpdf; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 462 | break; |
| 463 | |
| 464 | default: |
| 465 | parent = parent->parent; |
| 466 | continue; |
| 467 | } |
| 468 | |
| 469 | for (i = 0; i < tpdf_size; i++) { |
| 470 | if (!strcmp(tpdf[i].name, name)) { |
| 471 | return &tpdf[i]; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | parent = parent->parent; |
| 476 | } |
| 477 | } else if (prefix) { |
| 478 | /* get module where to search */ |
| 479 | for (i = 0; i < module->imp_size; i++) { |
| 480 | if (!strcmp(module->imp[i].prefix, prefix)) { |
| 481 | module = module->imp[i].module; |
| 482 | found = 1; |
| 483 | break; |
| 484 | } |
| 485 | } |
| 486 | if (!found) { |
| 487 | return NULL; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | /* search in top level typedefs */ |
| 492 | for (i = 0; i < module->tpdf_size; i++) { |
| 493 | if (!strcmp(module->tpdf[i].name, name)) { |
| 494 | return &module->tpdf[i]; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | /* search in submodules */ |
| 499 | for (i = 0; i < module->inc_size; i++) { |
| 500 | for (j = 0; j < module->inc[i].submodule->tpdf_size; j++) { |
| 501 | if (!strcmp(module->inc[i].submodule->tpdf[j].name, name)) { |
| 502 | return &module->inc[i].submodule->tpdf[j]; |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | return NULL; |
| 508 | } |
| 509 | |
| 510 | static int |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 511 | check_default(struct lys_type *type, const char *value) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 512 | { |
| 513 | /* TODO - RFC 6020, sec. 7.3.4 */ |
| 514 | (void)type; |
| 515 | (void)value; |
| 516 | return EXIT_SUCCESS; |
| 517 | } |
| 518 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 519 | /** |
| 520 | * @brief Check a key for mandatory attributes. Logs directly. |
| 521 | * |
| 522 | * @param[in] key The key to check. |
| 523 | * @param[in] flags What flags to check. |
| 524 | * @param[in] list The list of all the keys. |
| 525 | * @param[in] index Index of the key in the key list. |
| 526 | * @param[in] name The name of the keys. |
| 527 | * @param[in] len The name length. |
| 528 | * @param[in] line The line in the input file. |
| 529 | * |
| 530 | * @return EXIT_SUCCESS on succes, EXIT_FAILURE otherwise. |
| 531 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 532 | static int |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 533 | check_key(struct lys_node_leaf *key, uint8_t flags, struct lys_node_leaf **list, int index, const char *name, int len, |
| 534 | uint32_t line) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 535 | { |
| 536 | char *dup = NULL; |
| 537 | int j; |
| 538 | |
| 539 | /* existence */ |
| 540 | if (!key) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 541 | if (name[len] != '\0') { |
| 542 | dup = strdup(name); |
| 543 | dup[len] = '\0'; |
| 544 | name = dup; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 545 | } |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 546 | LOGVAL(LYE_KEY_MISS, line, name); |
| 547 | free(dup); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 548 | return EXIT_FAILURE; |
| 549 | } |
| 550 | |
| 551 | /* uniqueness */ |
| 552 | for (j = index - 1; j >= 0; j--) { |
| 553 | if (list[index] == list[j]) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 554 | LOGVAL(LYE_KEY_DUP, line, key->name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 555 | return EXIT_FAILURE; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | /* key is a leaf */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 560 | if (key->nodetype != LYS_LEAF) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 561 | LOGVAL(LYE_KEY_NLEAF, line, key->name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 562 | return EXIT_FAILURE; |
| 563 | } |
| 564 | |
| 565 | /* type of the leaf is not built-in empty */ |
| 566 | if (key->type.base == LY_TYPE_EMPTY) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 567 | LOGVAL(LYE_KEY_TYPE, line, key->name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 568 | return EXIT_FAILURE; |
| 569 | } |
| 570 | |
| 571 | /* config attribute is the same as of the list */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 572 | if ((flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 573 | LOGVAL(LYE_KEY_CONFIG, line, key->name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 574 | return EXIT_FAILURE; |
| 575 | } |
| 576 | |
| 577 | return EXIT_SUCCESS; |
| 578 | } |
| 579 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 580 | /** |
| 581 | * @brief Resolve (find) a prefix in a module include import. Does not log. |
| 582 | * |
| 583 | * @param[in] mod The module with the import. |
| 584 | * @param[in] prefix The prefix to find. |
| 585 | * @param[in] pref_len The prefix length. |
| 586 | * |
| 587 | * @return The matching module on success, NULL on error. |
| 588 | */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 589 | static struct lys_module * |
| 590 | resolve_import_in_includes_recursive(struct lys_module *mod, const char *prefix, uint32_t pref_len) |
| 591 | { |
| 592 | int i, j; |
| 593 | struct lys_submodule *sub_mod; |
| 594 | struct lys_module *ret; |
| 595 | |
| 596 | for (i = 0; i < mod->inc_size; i++) { |
| 597 | sub_mod = mod->inc[i].submodule; |
| 598 | for (j = 0; j < sub_mod->imp_size; j++) { |
| 599 | if ((pref_len == strlen(sub_mod->imp[j].prefix)) |
| 600 | && !strncmp(sub_mod->imp[j].prefix, prefix, pref_len)) { |
| 601 | return sub_mod->imp[j].module; |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | for (i = 0; i < mod->inc_size; i++) { |
| 607 | ret = resolve_import_in_includes_recursive((struct lys_module *)mod->inc[i].submodule, prefix, pref_len); |
| 608 | if (ret) { |
| 609 | return ret; |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | return NULL; |
| 614 | } |
| 615 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 616 | /** |
| 617 | * @brief Resolve (find) a prefix in a module import. Does not log. |
| 618 | * |
| 619 | * @param[in] mod The module with the import. |
| 620 | * @param[in] prefix The prefix to find. |
| 621 | * @param[in] pref_len The prefix length. |
| 622 | * |
| 623 | * @return The matching module on success, NULL on error. |
| 624 | */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 625 | static struct lys_module * |
| 626 | resolve_prefixed_module(struct lys_module *mod, const char *prefix, uint32_t pref_len) |
| 627 | { |
| 628 | int i; |
| 629 | |
| 630 | /* module itself */ |
| 631 | if (!strncmp(mod->prefix, prefix, pref_len) && mod->prefix[pref_len] == '\0') { |
| 632 | return mod; |
| 633 | } |
| 634 | |
| 635 | /* imported modules */ |
| 636 | for (i = 0; i < mod->imp_size; i++) { |
| 637 | if (!strncmp(mod->imp[i].prefix, prefix, pref_len) && mod->imp[i].prefix[pref_len] == '\0') { |
| 638 | return mod->imp[i].module; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | /* imports in includes */ |
| 643 | return resolve_import_in_includes_recursive(mod, prefix, pref_len); |
| 644 | } |
| 645 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 646 | /** |
| 647 | * @brief Resolve (fill) a unique. Logs directly. |
| 648 | * |
| 649 | * @param[in] parent The parent node of the unique structure. |
| 650 | * @param[in] uniq_str The value of the unique node. |
| 651 | * @param[in] uniq_s The unique structure in question. |
| 652 | * @param[in] line The line in the input file. |
| 653 | * |
| 654 | * @return EXIT_SUCCESS on succes, EXIT_FAILURE otherwise. |
| 655 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 656 | int |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 657 | resolve_unique(struct lys_node *parent, const char *uniq_str, struct lys_unique *uniq_s, uint32_t line) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 658 | { |
| 659 | char *uniq_val, *uniq_begin, *start; |
| 660 | int i, j; |
| 661 | |
| 662 | /* count the number of unique values */ |
| 663 | uniq_val = uniq_begin = strdup(uniq_str); |
| 664 | uniq_s->leafs_size = 0; |
| 665 | while ((uniq_val = strpbrk(uniq_val, " \t\n"))) { |
| 666 | uniq_s->leafs_size++; |
| 667 | while (isspace(*uniq_val)) { |
| 668 | uniq_val++; |
| 669 | } |
| 670 | } |
| 671 | uniq_s->leafs_size++; |
| 672 | uniq_s->leafs = calloc(uniq_s->leafs_size, sizeof *uniq_s->leafs); |
| 673 | |
| 674 | /* interconnect unique values with the leafs */ |
| 675 | uniq_val = uniq_begin; |
| 676 | for (i = 0; uniq_val && i < uniq_s->leafs_size; i++) { |
| 677 | start = uniq_val; |
| 678 | if ((uniq_val = strpbrk(start, " \t\n"))) { |
| 679 | *uniq_val = '\0'; /* add terminating NULL byte */ |
| 680 | uniq_val++; |
| 681 | while (isspace(*uniq_val)) { |
| 682 | uniq_val++; |
| 683 | } |
| 684 | } /* else only one nodeid present/left already NULL byte terminated */ |
| 685 | |
Michal Vasko | 2e1a7e4 | 2015-08-06 15:08:32 +0200 | [diff] [blame] | 686 | uniq_s->leafs[i] = (struct lys_node_leaf *)resolve_schema_nodeid(start, parent->child, parent->module, LYS_LEAF); |
| 687 | if (!uniq_s->leafs[i] || (uniq_s->leafs[i]->nodetype != LYS_LEAF)) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 688 | LOGVAL(LYE_INARG, line, start, "unique"); |
| 689 | if (!uniq_s->leafs[i]) { |
| 690 | LOGVAL(LYE_SPEC, 0, "Target leaf not found."); |
| 691 | } else { |
| 692 | LOGVAL(LYE_SPEC, 0, "Target is not a leaf."); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 693 | } |
| 694 | goto error; |
| 695 | } |
| 696 | |
| 697 | for (j = 0; j < i; j++) { |
| 698 | if (uniq_s->leafs[j] == uniq_s->leafs[i]) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 699 | LOGVAL(LYE_INARG, line, start, "unique"); |
| 700 | LOGVAL(LYE_SPEC, 0, "The identifier is not unique"); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 701 | goto error; |
| 702 | } |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | free(uniq_begin); |
| 707 | return EXIT_SUCCESS; |
| 708 | |
| 709 | error: |
| 710 | |
| 711 | free(uniq_s->leafs); |
| 712 | free(uniq_begin); |
| 713 | |
| 714 | return EXIT_FAILURE; |
| 715 | } |
| 716 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 717 | /** |
| 718 | * @brief Resolve (fill) a grouping in an uses. Logs directly. |
| 719 | * |
| 720 | * @param[in] parent The parent node of the uses. |
| 721 | * @param[in] uses The uses in question. |
| 722 | * @param[in] line The line in the input file. |
| 723 | * |
| 724 | * @return EXIT_SUCCESS on success, EXIT_FAILURE otherwise. |
| 725 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 726 | static int |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 727 | resolve_grouping(struct lys_node *parent, struct lys_node_uses *uses, uint32_t line) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 728 | { |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 729 | struct lys_module *searchmod = NULL, *module = uses->module; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 730 | struct lys_node *node, *node_aux; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 731 | const char *name; |
| 732 | int prefix_len = 0; |
| 733 | int i; |
| 734 | |
| 735 | /* get referenced grouping */ |
| 736 | name = strchr(uses->name, ':'); |
| 737 | if (!name) { |
| 738 | /* no prefix, search in local tree */ |
| 739 | name = uses->name; |
| 740 | } else { |
| 741 | /* there is some prefix, check if it refer the same data model */ |
| 742 | |
| 743 | /* set name to correct position after colon */ |
| 744 | prefix_len = name - uses->name; |
| 745 | name++; |
| 746 | |
| 747 | if (!strncmp(uses->name, module->prefix, prefix_len) && !module->prefix[prefix_len]) { |
| 748 | /* prefix refers to the current module, ignore it */ |
| 749 | prefix_len = 0; |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | /* search */ |
| 754 | if (prefix_len) { |
| 755 | /* in top-level groupings of some other module */ |
| 756 | for (i = 0; i < module->imp_size; i++) { |
| 757 | if (!strncmp(module->imp[i].prefix, uses->name, prefix_len) |
| 758 | && !module->imp[i].prefix[prefix_len]) { |
| 759 | searchmod = module->imp[i].module; |
| 760 | break; |
| 761 | } |
| 762 | } |
| 763 | if (!searchmod) { |
| 764 | /* uses refers unknown data model */ |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 765 | LOGVAL(LYE_INPREF, line, name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 766 | return EXIT_FAILURE; |
| 767 | } |
| 768 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 769 | LY_TREE_FOR(searchmod->data, node) { |
| 770 | if (node->nodetype == LYS_GROUPING && !strcmp(node->name, name)) { |
| 771 | uses->grp = (struct lys_node_grp *)node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 772 | return EXIT_SUCCESS; |
| 773 | } |
| 774 | } |
| 775 | } else { |
| 776 | /* in local tree hierarchy */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 777 | for (node_aux = parent; node_aux; node_aux = node_aux->parent) { |
| 778 | LY_TREE_FOR(node_aux->child, node) { |
| 779 | if (node->nodetype == LYS_GROUPING && !strcmp(node->name, name)) { |
| 780 | uses->grp = (struct lys_node_grp *)node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 781 | return EXIT_SUCCESS; |
| 782 | } |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | /* search in top level of the current module */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 787 | LY_TREE_FOR(module->data, node) { |
| 788 | if (node->nodetype == LYS_GROUPING && !strcmp(node->name, name)) { |
| 789 | uses->grp = (struct lys_node_grp *)node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 790 | return EXIT_SUCCESS; |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | /* search in top-level of included modules */ |
| 795 | for (i = 0; i < module->inc_size; i++) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 796 | LY_TREE_FOR(module->inc[i].submodule->data, node) { |
| 797 | if (node->nodetype == LYS_GROUPING && !strcmp(node->name, name)) { |
| 798 | uses->grp = (struct lys_node_grp *)node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 799 | return EXIT_SUCCESS; |
| 800 | } |
| 801 | } |
| 802 | } |
| 803 | } |
| 804 | |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 805 | LOGVAL(LYE_INRESOLV, line, "grouping", uses->name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 806 | return EXIT_FAILURE; |
| 807 | } |
| 808 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 809 | /** |
| 810 | * @brief Resolve (find) a feature definition. Logs directly. |
| 811 | * |
| 812 | * @param[in] name Feature name. |
| 813 | * @param[in] module Module to search in. |
| 814 | * @param[in] line The line in the input file. |
| 815 | * |
| 816 | * @return EXIT_SUCCESS on success, EXIT_FAILURE otherwise. |
| 817 | */ |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 818 | static struct lys_feature * |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 819 | resolve_feature(const char *name, struct lys_module *module, uint32_t line) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 820 | { |
| 821 | const char *prefix; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 822 | uint32_t prefix_len = 0; |
| 823 | int i, j; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 824 | |
| 825 | assert(name); |
| 826 | assert(module); |
| 827 | |
| 828 | /* check prefix */ |
| 829 | prefix = name; |
| 830 | name = strchr(prefix, ':'); |
| 831 | if (name) { |
| 832 | /* there is prefix */ |
| 833 | prefix_len = name - prefix; |
| 834 | name++; |
| 835 | |
| 836 | /* check whether the prefix points to the current module */ |
| 837 | if (!strncmp(prefix, module->prefix, prefix_len) && !module->prefix[prefix_len]) { |
| 838 | /* then ignore prefix and works as there is no prefix */ |
| 839 | prefix_len = 0; |
| 840 | } |
| 841 | } else { |
| 842 | /* no prefix, set pointers correctly */ |
| 843 | name = prefix; |
| 844 | } |
| 845 | |
| 846 | if (prefix_len) { |
| 847 | /* search in imported modules */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 848 | module = resolve_prefixed_module(module, prefix, prefix_len); |
| 849 | if (!module) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 850 | /* identity refers unknown data model */ |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 851 | LOGVAL(LYE_INPREF, line, prefix); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 852 | return NULL; |
| 853 | } |
| 854 | } else { |
| 855 | /* search in submodules */ |
| 856 | for (i = 0; i < module->inc_size; i++) { |
| 857 | for (j = 0; j < module->inc[i].submodule->features_size; j++) { |
| 858 | if (!strcmp(name, module->inc[i].submodule->features[j].name)) { |
| 859 | return &(module->inc[i].submodule->features[j]); |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | /* search in the identified module */ |
| 866 | for (j = 0; j < module->features_size; j++) { |
| 867 | if (!strcmp(name, module->features[j].name)) { |
| 868 | return &module->features[j]; |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | /* not found */ |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 873 | LOGVAL(LYE_INRESOLV, line, "feature", prefix); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 874 | return NULL; |
| 875 | } |
| 876 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 877 | /** |
| 878 | * @brief Resolve (find) a valid child of a parent. Does not log. |
| 879 | * |
| 880 | * Valid child means a chema pointer to a node that is part of |
| 881 | * the data meaning uses are skipped. |
| 882 | * |
| 883 | * @param[in] parent Parent to search in. |
| 884 | * @param[in] name Child name. |
| 885 | * @param[in] len Child name length. |
| 886 | * @param[in] type ORed desired type of the node. |
| 887 | * |
| 888 | * @return Node of the desired type, NULL if no matching node was found. |
| 889 | */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 890 | struct lys_node * |
| 891 | resolve_child(struct lys_node *parent, const char *name, int len, LYS_NODE type) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 892 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 893 | struct lys_node *child, *result; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 894 | |
| 895 | if (!len) { |
| 896 | len = strlen(name); |
| 897 | } |
| 898 | |
| 899 | LY_TREE_FOR(parent->child, child) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 900 | if (child->nodetype == LYS_USES) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 901 | /* search recursively */ |
| 902 | result = resolve_child(child, name, len, type); |
| 903 | if (result) { |
| 904 | return result; |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | if (child->nodetype & type) { |
| 909 | /* direct check */ |
| 910 | if (child->name == name || (!strncmp(child->name, name, len) && !child->name[len])) { |
| 911 | return child; |
| 912 | } |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | return NULL; |
| 917 | } |
| 918 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 919 | /** |
| 920 | * @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] | 921 | * |
Michal Vasko | cc9e12e | 2015-08-04 16:14:37 +0200 | [diff] [blame] | 922 | * node_type - LYS_AUGMENT (searches also RPCs and notifications) |
Michal Vasko | 2e1a7e4 | 2015-08-06 15:08:32 +0200 | [diff] [blame] | 923 | * - 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] | 924 | * - LYS_CHOICE (search only start->child, only descendant-schema-nodeid allowed) |
Michal Vasko | 2e1a7e4 | 2015-08-06 15:08:32 +0200 | [diff] [blame] | 925 | * - LYS_LEAF (like LYS_USES, but always returns a data node) |
| 926 | * |
| 927 | * If id is absolute, start is ignored. If id is relative, start must be the first child to be searched |
| 928 | * continuing with its siblings. |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 929 | * |
| 930 | * @param[in] id Schema-nodeid string. |
| 931 | * @param[in] start Start of the relative search. |
| 932 | * @param[in] mod Module in question. |
| 933 | * @param[in] node_type Decides how to modify the search. |
| 934 | * |
| 935 | * @return Matching node, NULL on fail. |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 936 | */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 937 | struct lys_node * |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 938 | resolve_schema_nodeid(const char *id, struct lys_node *start, struct lys_module *mod, LYS_NODE node_type) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 939 | { |
Michal Vasko | cc9e12e | 2015-08-04 16:14:37 +0200 | [diff] [blame] | 940 | const char *name, *prefix; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 941 | struct lys_node *sibling; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 942 | int ret, nam_len, pref_len, is_relative = -1; |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 943 | struct lys_module *prefix_mod, *start_mod; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 944 | /* 0 - in module, 1 - in 1st submodule, 2 - in 2nd submodule, ... */ |
| 945 | uint8_t in_submod = 0; |
Michal Vasko | cc9e12e | 2015-08-04 16:14:37 +0200 | [diff] [blame] | 946 | /* 0 - in data, 1 - in RPCs, 2 - in notifications (relevant only with LYS_AUGMENT) */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 947 | uint8_t in_mod_part = 0; |
| 948 | |
| 949 | assert(mod); |
| 950 | assert(id); |
| 951 | |
| 952 | if ((ret = parse_schema_nodeid(id, &prefix, &pref_len, &name, &nam_len, &is_relative)) < 1) { |
| 953 | return NULL; |
| 954 | } |
| 955 | id += ret; |
| 956 | |
Michal Vasko | 2e1a7e4 | 2015-08-06 15:08:32 +0200 | [diff] [blame] | 957 | if (!is_relative && (node_type & (LYS_USES | LYS_CHOICE | LYS_LEAF))) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 958 | return NULL; |
| 959 | } |
| 960 | |
| 961 | /* absolute-schema-nodeid */ |
| 962 | if (!is_relative) { |
| 963 | if (prefix) { |
| 964 | start_mod = resolve_prefixed_module(mod, prefix, pref_len); |
| 965 | if (!start_mod) { |
| 966 | return NULL; |
| 967 | } |
| 968 | start = start_mod->data; |
| 969 | } else { |
| 970 | start = mod->data; |
| 971 | start_mod = mod; |
| 972 | } |
| 973 | /* descendant-schema-nodeid */ |
| 974 | } else { |
Michal Vasko | 2e1a7e4 | 2015-08-06 15:08:32 +0200 | [diff] [blame] | 975 | if (start) { |
| 976 | start_mod = start->module; |
| 977 | } else { |
| 978 | start_mod = mod; |
| 979 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | while (1) { |
Michal Vasko | 1e989c0 | 2015-08-04 12:33:00 +0200 | [diff] [blame] | 983 | sibling = NULL; |
| 984 | LY_TREE_FOR(start, sibling) { |
| 985 | /* name match */ |
Michal Vasko | 2e1a7e4 | 2015-08-06 15:08:32 +0200 | [diff] [blame] | 986 | if (((sibling->nodetype != LYS_GROUPING) || (node_type == LYS_USES)) |
| 987 | && ((sibling->name && !strncmp(name, sibling->name, nam_len) && !sibling->name[nam_len]) |
Michal Vasko | 1e989c0 | 2015-08-04 12:33:00 +0200 | [diff] [blame] | 988 | || (!strncmp(name, "input", 5) && (nam_len == 5) && (sibling->nodetype == LYS_INPUT)) |
Michal Vasko | dcc7a80 | 2015-08-06 11:59:47 +0200 | [diff] [blame] | 989 | || (!strncmp(name, "output", 6) && (nam_len == 6) && (sibling->nodetype == LYS_OUTPUT)))) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 990 | |
Michal Vasko | 1e989c0 | 2015-08-04 12:33:00 +0200 | [diff] [blame] | 991 | /* prefix match check */ |
| 992 | if (prefix) { |
Michal Vasko | 1e989c0 | 2015-08-04 12:33:00 +0200 | [diff] [blame] | 993 | prefix_mod = resolve_prefixed_module(mod, prefix, pref_len); |
| 994 | if (!prefix_mod) { |
| 995 | return NULL; |
| 996 | } |
Michal Vasko | 2e1a7e4 | 2015-08-06 15:08:32 +0200 | [diff] [blame] | 997 | } else { |
| 998 | prefix_mod = mod; |
| 999 | if (prefix_mod->type) { |
| 1000 | prefix_mod = ((struct lys_submodule *)prefix_mod)->belongsto; |
| 1001 | } |
| 1002 | } |
Michal Vasko | 1e989c0 | 2015-08-04 12:33:00 +0200 | [diff] [blame] | 1003 | |
Michal Vasko | 2e1a7e4 | 2015-08-06 15:08:32 +0200 | [diff] [blame] | 1004 | /* modules need to always be checked, we want to skip augments */ |
| 1005 | if (!sibling->module->type) { |
| 1006 | if (prefix_mod != sibling->module) { |
| 1007 | continue; |
| 1008 | } |
| 1009 | } else { |
| 1010 | if (prefix_mod != ((struct lys_submodule *)sibling->module)->belongsto) { |
| 1011 | continue; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1012 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1013 | } |
Michal Vasko | 1e989c0 | 2015-08-04 12:33:00 +0200 | [diff] [blame] | 1014 | |
Michal Vasko | 1e989c0 | 2015-08-04 12:33:00 +0200 | [diff] [blame] | 1015 | /* the result node? */ |
| 1016 | if (!id[0]) { |
Michal Vasko | 2e1a7e4 | 2015-08-06 15:08:32 +0200 | [diff] [blame] | 1017 | /* we're looking only for groupings, this is a data node */ |
| 1018 | if ((node_type == LYS_USES) && (sibling->nodetype != LYS_GROUPING)) { |
| 1019 | continue; |
| 1020 | } |
Michal Vasko | 1e989c0 | 2015-08-04 12:33:00 +0200 | [diff] [blame] | 1021 | return sibling; |
| 1022 | } |
| 1023 | |
Michal Vasko | dcc7a80 | 2015-08-06 11:59:47 +0200 | [diff] [blame] | 1024 | /* we're looking for a grouping (node_type == LYS_USES), |
| 1025 | * but this isn't it, we cannot search inside |
| 1026 | */ |
| 1027 | if (sibling->nodetype == LYS_GROUPING) { |
| 1028 | continue; |
| 1029 | } |
| 1030 | |
Michal Vasko | 1e989c0 | 2015-08-04 12:33:00 +0200 | [diff] [blame] | 1031 | /* check for shorthand cases - then 'start' does not change */ |
| 1032 | if (!sibling->parent || (sibling->parent->nodetype != LYS_CHOICE) |
| 1033 | || (sibling->nodetype == LYS_CASE)) { |
| 1034 | start = sibling->child; |
| 1035 | } |
| 1036 | break; |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | /* we did not find the case in direct siblings */ |
| 1041 | if (node_type == LYS_CHOICE) { |
| 1042 | return NULL; |
| 1043 | } |
| 1044 | |
| 1045 | /* no match */ |
| 1046 | if (!sibling) { |
| 1047 | /* on augment search also RPCs and notifications, if we are in top-level */ |
| 1048 | if ((node_type == LYS_AUGMENT) && (!start || !start->parent)) { |
| 1049 | /* we have searched all the data nodes */ |
| 1050 | if (in_mod_part == 0) { |
| 1051 | if (!in_submod) { |
| 1052 | start = start_mod->rpc; |
| 1053 | } else { |
| 1054 | start = start_mod->inc[in_submod-1].submodule->rpc; |
| 1055 | } |
| 1056 | in_mod_part = 1; |
| 1057 | continue; |
| 1058 | } |
| 1059 | /* we have searched all the RPCs */ |
| 1060 | if (in_mod_part == 1) { |
| 1061 | if (!in_submod) { |
| 1062 | start = start_mod->notif; |
| 1063 | } else { |
| 1064 | start = start_mod->inc[in_submod-1].submodule->notif; |
| 1065 | } |
| 1066 | in_mod_part = 2; |
| 1067 | continue; |
| 1068 | } |
| 1069 | /* we have searched all the notifications, nothing else to search in this module */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1070 | } |
| 1071 | |
Michal Vasko | 1e989c0 | 2015-08-04 12:33:00 +0200 | [diff] [blame] | 1072 | /* are we done with the included submodules as well? */ |
| 1073 | if (in_submod == start_mod->inc_size) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1074 | return NULL; |
| 1075 | } |
| 1076 | |
Michal Vasko | 1e989c0 | 2015-08-04 12:33:00 +0200 | [diff] [blame] | 1077 | /* we aren't, check the next one */ |
| 1078 | ++in_submod; |
| 1079 | in_mod_part = 0; |
| 1080 | start = start_mod->inc[in_submod-1].submodule->data; |
| 1081 | continue; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | /* we found our submodule */ |
| 1085 | if (in_submod) { |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 1086 | start_mod = (struct lys_module *)start_mod->inc[in_submod-1].submodule; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1087 | in_submod = 0; |
| 1088 | } |
| 1089 | |
| 1090 | if ((ret = parse_schema_nodeid(id, &prefix, &pref_len, &name, &nam_len, &is_relative)) < 1) { |
| 1091 | return NULL; |
| 1092 | } |
| 1093 | id += ret; |
| 1094 | } |
| 1095 | |
| 1096 | /* cannot get here */ |
| 1097 | return NULL; |
| 1098 | } |
| 1099 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1100 | /** |
| 1101 | * @brief Resolve (find) a data node. Does not log. |
| 1102 | * |
| 1103 | * @param[in] prefix Prefix of the data node. |
| 1104 | * @param[in] pref_len Length of the prefix. |
| 1105 | * @param[in] name Name of the data node. |
| 1106 | * @param[in] nam_len Length of the name. |
| 1107 | * @param[in] data_source Data node that defines the prefix and the name, |
| 1108 | * to find the correct module. |
| 1109 | * @param[in,out] parents Resolved nodes. If there are some parents, |
| 1110 | * they are replaced (!!) with the resolvents. |
| 1111 | * |
| 1112 | * @return EXIT_SUCCESS on success, EXIT_FAILURE otherwise. |
| 1113 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1114 | static int |
| 1115 | resolve_data_nodeid(const char *prefix, int pref_len, const char *name, int nam_len, struct lyd_node *data_source, |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 1116 | struct unres_data **parents) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1117 | { |
| 1118 | int flag; |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 1119 | struct lys_module *mod; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 1120 | struct unres_data *item, *par_iter; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1121 | struct lyd_node *node; |
| 1122 | |
| 1123 | if (prefix) { |
| 1124 | /* we have prefix, find appropriate module */ |
| 1125 | mod = resolve_prefixed_module(data_source->schema->module, prefix, pref_len); |
| 1126 | if (!mod) { |
| 1127 | /* invalid prefix */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 1128 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1129 | } |
| 1130 | } else { |
| 1131 | /* no prefix, module is the same as of current node */ |
| 1132 | mod = data_source->schema->module; |
| 1133 | } |
| 1134 | |
| 1135 | if (!*parents) { |
| 1136 | *parents = malloc(sizeof **parents); |
| 1137 | (*parents)->dnode = NULL; |
| 1138 | (*parents)->next = NULL; |
| 1139 | } |
| 1140 | for (par_iter = *parents; par_iter; par_iter = par_iter->next) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1141 | if (par_iter->dnode && (par_iter->dnode->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1142 | /* skip */ |
| 1143 | continue; |
| 1144 | } |
| 1145 | flag = 0; |
| 1146 | LY_TREE_FOR(par_iter->dnode ? par_iter->dnode->child : data_source, node) { |
| 1147 | if (node->schema->module == mod && !strncmp(node->schema->name, name, nam_len) |
| 1148 | && node->schema->name[nam_len] == '\0') { |
| 1149 | /* matching target */ |
| 1150 | if (!flag) { |
| 1151 | /* replace leafref instead of the current parent */ |
| 1152 | par_iter->dnode = node; |
| 1153 | flag = 1; |
| 1154 | } else { |
| 1155 | /* multiple matching, so create new leafref structure */ |
| 1156 | item = malloc(sizeof *item); |
| 1157 | item->dnode = node; |
| 1158 | item->next = par_iter->next; |
| 1159 | par_iter->next = item; |
| 1160 | par_iter = par_iter->next; |
| 1161 | } |
| 1162 | } |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | return !flag; |
| 1167 | } |
| 1168 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1169 | /** |
| 1170 | * @brief Resolve a path predicate (leafref) in data context. Logs directly. |
| 1171 | * |
| 1172 | * @param[in] pred Predicate in question. |
| 1173 | * @param[in,out] node_match Nodes satisfying the restriction |
| 1174 | * without the predicate. Nodes not |
| 1175 | * satisfying the predicate are removed. |
| 1176 | * @param[in] line Line in the input file. |
| 1177 | * |
| 1178 | * @return EXIT_SUCCESS on success, EXIT_FAILURE otherwise. |
| 1179 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1180 | static int |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 1181 | resolve_path_predicate_data(const char *pred, struct unres_data **node_match, uint32_t line) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1182 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1183 | /* ... /node[source = destination] ... */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 1184 | struct unres_data *source_match, *dest_match, *node, *node_prev = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1185 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
| 1186 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, parsed = 0, pke_parsed = 0; |
| 1187 | int has_predicate, dest_parent_times, i; |
| 1188 | |
| 1189 | do { |
| 1190 | if ((i = parse_path_predicate(pred, &sour_pref, &sour_pref_len, &source, &sour_len, &path_key_expr, |
| 1191 | &pke_len, &has_predicate)) < 1) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1192 | LOGVAL(LYE_INCHAR, line, pred[-i], pred-i); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1193 | return -parsed+i; |
| 1194 | } |
| 1195 | parsed += i; |
| 1196 | pred += i; |
| 1197 | |
| 1198 | for (node = *node_match; node;) { |
| 1199 | /* source */ |
| 1200 | source_match = NULL; |
| 1201 | /* must be leaf (key of a list) */ |
| 1202 | if (resolve_data_nodeid(sour_pref, sour_pref_len, source, sour_len, node->dnode, &source_match) |
| 1203 | || !source_match || source_match->next |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1204 | || (source_match->dnode->schema->nodetype != LYS_LEAF)) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1205 | LOGVAL(LYE_LINE, line); |
| 1206 | /* general error, the one written later will suffice */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1207 | return -parsed; |
| 1208 | } |
| 1209 | |
| 1210 | /* destination */ |
| 1211 | dest_match = calloc(1, sizeof *dest_match); |
| 1212 | dest_match->dnode = node->dnode; |
| 1213 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 1214 | &dest_parent_times)) < 1) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1215 | LOGVAL(LYE_INCHAR, line, path_key_expr[-i], path_key_expr-i); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1216 | return -parsed+i; |
| 1217 | } |
| 1218 | pke_parsed += i; |
| 1219 | for (i = 0; i < dest_parent_times; ++i) { |
| 1220 | dest_match->dnode = dest_match->dnode->parent; |
| 1221 | if (!dest_match->dnode) { |
| 1222 | free(dest_match); |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1223 | LOGVAL(LYE_LINE, line); |
| 1224 | /* general error, the one written later will suffice */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1225 | return -parsed; |
| 1226 | } |
| 1227 | } |
| 1228 | while (1) { |
| 1229 | if (resolve_data_nodeid(dest_pref, dest_pref_len, dest, dest_len, dest_match->dnode, &dest_match) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 1230 | || !dest_match->dnode || dest_match->next) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1231 | free(dest_match); |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1232 | LOGVAL(LYE_LINE, line); |
| 1233 | /* general error, the one written later will suffice */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1234 | return -parsed; |
| 1235 | } |
| 1236 | |
| 1237 | if (pke_len == pke_parsed) { |
| 1238 | break; |
| 1239 | } |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 1240 | 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] | 1241 | &dest_parent_times)) < 1) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1242 | LOGVAL(LYE_INCHAR, line, path_key_expr[-i], path_key_expr-i); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1243 | return -parsed+i; |
| 1244 | } |
| 1245 | pke_parsed += i; |
| 1246 | } |
| 1247 | |
| 1248 | /* check match between source and destination nodes */ |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 1249 | if (((struct lys_node_leaf *)source_match->dnode->schema)->type.base |
| 1250 | != ((struct lys_node_leaf *)dest_match->dnode->schema)->type.base) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1251 | goto remove_leafref; |
| 1252 | } |
| 1253 | |
| 1254 | if (((struct lyd_node_leaf *)source_match->dnode)->value_str |
| 1255 | != ((struct lyd_node_leaf *)dest_match->dnode)->value_str) { |
| 1256 | goto remove_leafref; |
| 1257 | } |
| 1258 | |
| 1259 | /* leafref is ok, continue check with next leafref */ |
| 1260 | node_prev = node; |
| 1261 | node = node->next; |
| 1262 | continue; |
| 1263 | |
| 1264 | remove_leafref: |
| 1265 | /* does not fulfill conditions, remove leafref record */ |
| 1266 | if (node_prev) { |
| 1267 | node_prev->next = node->next; |
| 1268 | free(node); |
| 1269 | node = node_prev->next; |
| 1270 | } else { |
| 1271 | node = (*node_match)->next; |
| 1272 | free(*node_match); |
| 1273 | *node_match = node; |
| 1274 | } |
| 1275 | } |
| 1276 | } while (has_predicate); |
| 1277 | |
| 1278 | return parsed; |
| 1279 | } |
| 1280 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1281 | /** |
| 1282 | * @brief Resolve a path (leafref) in data context. Logs directly. |
| 1283 | * |
| 1284 | * @param[in] unres Nodes matching the schema path. |
| 1285 | * @param[in] path Path in question. |
| 1286 | * @param[in,out] ret Matching nodes. |
| 1287 | * |
| 1288 | * @return EXIT_SUCCESS on success, EXIT_FAILURE otherwise. |
| 1289 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1290 | int |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 1291 | resolve_path_arg_data(struct unres_data *unres, const char *path, struct unres_data **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1292 | { |
Radek Krejci | 71b795b | 2015-08-10 16:20:39 +0200 | [diff] [blame] | 1293 | struct lyd_node *data = NULL; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 1294 | struct unres_data *riter = NULL, *raux; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1295 | const char *prefix, *name; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 1296 | int pref_len, nam_len, has_predicate, parent_times, i; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1297 | |
| 1298 | *ret = NULL; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1299 | parent_times = 0; |
| 1300 | |
| 1301 | /* searching for nodeset */ |
| 1302 | do { |
| 1303 | if ((i = parse_path_arg(path, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1304 | LOGVAL(LYE_INCHAR, unres->line, path[-i], path-i); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1305 | goto error; |
| 1306 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1307 | path += i; |
| 1308 | |
| 1309 | if (!*ret) { |
| 1310 | *ret = calloc(1, sizeof **ret); |
| 1311 | for (i = 0; i < parent_times; ++i) { |
| 1312 | /* relative path */ |
| 1313 | if (!*ret) { |
| 1314 | /* error, too many .. */ |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1315 | LOGVAL(LYE_INVAL, unres->line, path, unres->dnode->schema->name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1316 | goto error; |
| 1317 | } else if (!(*ret)->dnode) { |
| 1318 | /* first .. */ |
Radek Krejci | 71b795b | 2015-08-10 16:20:39 +0200 | [diff] [blame] | 1319 | data = (*ret)->dnode = unres->dnode->parent; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1320 | } else if (!(*ret)->dnode->parent) { |
| 1321 | /* we are in root */ |
| 1322 | free(*ret); |
| 1323 | *ret = NULL; |
| 1324 | } else { |
| 1325 | /* multiple .. */ |
Radek Krejci | 71b795b | 2015-08-10 16:20:39 +0200 | [diff] [blame] | 1326 | data = (*ret)->dnode = (*ret)->dnode->parent; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | /* absolute path */ |
| 1331 | if (parent_times == -1) { |
| 1332 | for (data = unres->dnode; data->parent; data = data->parent); |
| 1333 | for (; data->prev->next; data = data->prev); |
| 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | /* node identifier */ |
| 1338 | if (resolve_data_nodeid(prefix, pref_len, name, nam_len, data, ret)) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1339 | LOGVAL(LYE_INELEM_LEN, unres->line, nam_len, name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1340 | goto error; |
| 1341 | } |
| 1342 | |
| 1343 | if (has_predicate) { |
| 1344 | /* we have predicate, so the current results must be lists */ |
| 1345 | for (raux = NULL, riter = *ret; riter; ) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1346 | if (riter->dnode->schema->nodetype == LYS_LIST && |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 1347 | ((struct lys_node_list *)riter->dnode->schema)->keys) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1348 | /* leafref is ok, continue check with next leafref */ |
| 1349 | raux = riter; |
| 1350 | riter = riter->next; |
| 1351 | continue; |
| 1352 | } |
| 1353 | |
| 1354 | /* does not fulfill conditions, remove leafref record */ |
| 1355 | if (raux) { |
| 1356 | raux->next = riter->next; |
| 1357 | free(riter); |
| 1358 | riter = raux->next; |
| 1359 | } else { |
| 1360 | *ret = riter->next; |
| 1361 | free(riter); |
| 1362 | riter = *ret; |
| 1363 | } |
| 1364 | } |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1365 | if ((i = resolve_path_predicate_data(path, ret, unres->line)) < 1) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1366 | goto error; |
| 1367 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1368 | path += i; |
| 1369 | |
| 1370 | if (!*ret) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1371 | LOGVAL(LYE_LINE, unres->line); |
| 1372 | /* general error, the one written later will suffice */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1373 | goto error; |
| 1374 | } |
| 1375 | } |
| 1376 | } while (path[0] != '\0'); |
| 1377 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 1378 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1379 | |
| 1380 | error: |
| 1381 | |
| 1382 | while (*ret) { |
| 1383 | raux = (*ret)->next; |
| 1384 | free(*ret); |
| 1385 | *ret = raux; |
| 1386 | } |
| 1387 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 1388 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1389 | } |
| 1390 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1391 | /** |
| 1392 | * @brief Resolve a path (leafref) predicate in schema context. Logs directly. |
| 1393 | * |
| 1394 | * @param[in] path Path in question. |
| 1395 | * @param[in] mod Schema module. |
| 1396 | * @param[in] source_node Left operand node. |
| 1397 | * @param[in] dest_node Right ooperand node. |
| 1398 | * @param[in] line Line in the input file. |
| 1399 | * |
| 1400 | * @return EXIT_SUCCESS on success, EXIT_FAILURE otherwise. |
| 1401 | */ |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 1402 | static int |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1403 | resolve_path_predicate_schema(const char *path, struct lys_module *mod, struct lys_node *source_node, |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 1404 | struct lys_node *dest_node, uint32_t line) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 1405 | { |
| 1406 | struct lys_node *src_node, *dst_node; |
| 1407 | const char *path_key_expr, *source, *sour_pref, *dest, *dest_pref; |
| 1408 | int pke_len, sour_len, sour_pref_len, dest_len, dest_pref_len, parsed = 0, pke_parsed = 0; |
| 1409 | int has_predicate, dest_parent_times = 0, i; |
| 1410 | |
| 1411 | do { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1412 | 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] | 1413 | &pke_len, &has_predicate)) < 1) { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1414 | LOGVAL(LYE_INCHAR, line, path[-i], path-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 1415 | return -parsed+i; |
| 1416 | } |
| 1417 | parsed += i; |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1418 | path += i; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 1419 | |
| 1420 | /* source (must be leaf, from the same module) */ |
| 1421 | if (sour_pref && (resolve_prefixed_module(mod, sour_pref, sour_pref_len) != mod)) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1422 | LOGVAL(LYE_INPREF_LEN, line, sour_pref_len, sour_pref); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 1423 | return -parsed; |
| 1424 | } |
| 1425 | |
| 1426 | src_node = resolve_child(source_node, source, sour_len, LYS_LEAF); |
| 1427 | if (!src_node) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1428 | LOGVAL(LYE_LINE, line); |
| 1429 | /* general error, the one written later will suffice */ |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 1430 | return -parsed; |
| 1431 | } |
| 1432 | |
| 1433 | /* destination */ |
| 1434 | if ((i = parse_path_key_expr(path_key_expr, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 1435 | &dest_parent_times)) < 1) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1436 | LOGVAL(LYE_INCHAR, line, path_key_expr[-i], path_key_expr-i); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 1437 | return -parsed; |
| 1438 | } |
| 1439 | pke_parsed += i; |
| 1440 | |
| 1441 | /* dest_node is actually the parent of this leaf, so skip the first ".." */ |
| 1442 | dst_node = dest_node; |
| 1443 | for (i = 1; i < dest_parent_times; ++i) { |
| 1444 | dst_node = dst_node->parent; |
| 1445 | if (!dst_node) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1446 | LOGVAL(LYE_LINE, line); |
| 1447 | /* general error, the one written later will suffice */ |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 1448 | return -parsed; |
| 1449 | } |
| 1450 | } |
| 1451 | while (1) { |
| 1452 | if (dest_pref && (resolve_prefixed_module(mod, dest_pref, dest_pref_len) != mod)) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1453 | LOGVAL(LYE_INPREF_LEN, line, dest_pref_len, dest_pref); |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 1454 | return -parsed; |
| 1455 | } |
| 1456 | dst_node = resolve_child(dst_node, dest, dest_len, LYS_CONTAINER | LYS_LIST | LYS_LEAF); |
| 1457 | if (!dst_node) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1458 | LOGVAL(LYE_LINE, line); |
| 1459 | /* general error, the one written later will suffice */ |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 1460 | return -parsed; |
| 1461 | } |
| 1462 | |
| 1463 | if (pke_len == pke_parsed) { |
| 1464 | break; |
| 1465 | } |
| 1466 | |
| 1467 | if ((i = parse_path_key_expr(path_key_expr+pke_parsed, &dest_pref, &dest_pref_len, &dest, &dest_len, |
| 1468 | &dest_parent_times)) < 1) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1469 | 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] | 1470 | return -parsed; |
| 1471 | } |
| 1472 | pke_parsed += i; |
| 1473 | } |
| 1474 | |
| 1475 | /* check source - dest match */ |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 1476 | if ((dst_node->nodetype != LYS_LEAF) || ((struct lys_node_leaf *)dst_node)->type.base |
| 1477 | != ((struct lys_node_leaf *)src_node)->type.base) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1478 | LOGVAL(LYE_LINE, line); |
| 1479 | /* general error, the one written later will suffice */ |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 1480 | return -parsed; |
| 1481 | } |
| 1482 | } while (has_predicate); |
| 1483 | |
| 1484 | return parsed; |
| 1485 | } |
| 1486 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1487 | /** |
| 1488 | * @brief Resolve a path (leafref) in schema context. Logs indirectly. |
| 1489 | * |
| 1490 | * @param[in] mod Module in question. |
| 1491 | * @param[in] path Path in question. |
| 1492 | * @param[in] parent_node Parent of the leafref. |
| 1493 | * @param[in] line Line in the input file. |
| 1494 | * |
| 1495 | * @return EXIT_SUCCESS on success, EXIT_FAILURE otherwise. |
| 1496 | */ |
Michal Vasko | a5c4ad6 | 2015-08-11 14:59:43 +0200 | [diff] [blame] | 1497 | static struct lys_node* |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 1498 | resolve_path_arg_schema(struct lys_module *mod, const char *path, struct lys_node *parent_node, uint32_t line) |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 1499 | { |
| 1500 | struct lys_node *child, *node; |
| 1501 | const char *id, *prefix, *name; |
| 1502 | int pref_len, nam_len, parent_times, has_predicate; |
| 1503 | int i, first; |
| 1504 | |
| 1505 | first = 1; |
| 1506 | parent_times = 0; |
| 1507 | id = path; |
| 1508 | |
| 1509 | do { |
| 1510 | if ((i = parse_path_arg(id, &prefix, &pref_len, &name, &nam_len, &parent_times, &has_predicate)) < 1) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1511 | LOGVAL(LYE_INCHAR, line, id[-i], id-i); |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 1512 | return NULL; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 1513 | } |
| 1514 | id += i; |
| 1515 | |
| 1516 | if (first) { |
| 1517 | if (parent_times == -1) { |
| 1518 | node = mod->data; |
| 1519 | } else if (parent_times > 0) { |
| 1520 | node = parent_node; |
Michal Vasko | 73ae256 | 2015-08-06 11:58:13 +0200 | [diff] [blame] | 1521 | /* node is the parent already, skip one ".." */ |
| 1522 | for (i = 1; i < parent_times; ++i) { |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 1523 | node = node->parent; |
| 1524 | if (!node) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1525 | LOGVAL(LYE_LINE, line); |
| 1526 | /* general error, the one written later will suffice */ |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 1527 | return NULL; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 1528 | } |
| 1529 | } |
| 1530 | node = node->child; |
| 1531 | } |
| 1532 | first = 0; |
| 1533 | } else { |
| 1534 | node = node->child; |
| 1535 | } |
| 1536 | |
| 1537 | /* node identifier */ |
| 1538 | LY_TREE_FOR(node, child) { |
| 1539 | if (child->nodetype == LYS_GROUPING) { |
| 1540 | continue; |
| 1541 | } |
| 1542 | |
| 1543 | if (!strncmp(child->name, name, nam_len) && !child->name[nam_len]) { |
| 1544 | break; |
| 1545 | } |
| 1546 | } |
| 1547 | if (!child) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1548 | LOGVAL(LYE_LINE, line); |
| 1549 | /* general error, the one written later will suffice */ |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 1550 | return NULL; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 1551 | } |
| 1552 | node = child; |
| 1553 | |
| 1554 | if (has_predicate) { |
| 1555 | /* we have predicate, so the current result must be list */ |
| 1556 | if (node->nodetype != LYS_LIST) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1557 | LOGVAL(LYE_LINE, line); |
| 1558 | /* general error, the one written later will suffice */ |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 1559 | return NULL; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | if ((i = resolve_path_predicate_schema(id, mod, node, parent_node, line)) < 1) { |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 1563 | return NULL; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 1564 | } |
| 1565 | id += i; |
| 1566 | } |
| 1567 | } while (id[0]); |
| 1568 | |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 1569 | /* the target must be leaf or leaf-list */ |
| 1570 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 1571 | LOGVAL(LYE_LINE, line); |
| 1572 | /* general error, the one written later will suffice */ |
| 1573 | return NULL; |
| 1574 | } |
| 1575 | |
| 1576 | return node; |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 1577 | } |
| 1578 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1579 | /** |
| 1580 | * @brief Resolve instance-identifier predicate. Does not log. |
| 1581 | * |
| 1582 | * @param[in] pred Predicate in question. |
| 1583 | * @param[in,out] node_match Nodes matching the restriction without |
| 1584 | * the predicate. Nodes not satisfying |
| 1585 | * the predicate are removed. |
| 1586 | * |
| 1587 | * @return EXIT_SUCCESS on success, EXIT_FAILURE otherwise. |
| 1588 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1589 | static int |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 1590 | resolve_predicate(const char *pred, struct unres_data **node_match) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1591 | { |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1592 | /* ... /node[target = value] ... */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 1593 | struct unres_data *target_match, *node, *node_prev = NULL, *tmp; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1594 | const char *prefix, *name, *value; |
| 1595 | int pref_len, nam_len, val_len, i, has_predicate, cur_idx, idx, parsed; |
| 1596 | |
| 1597 | idx = -1; |
| 1598 | parsed = 0; |
| 1599 | |
| 1600 | do { |
| 1601 | if ((i = parse_predicate(pred, &prefix, &pref_len, &name, &nam_len, &value, &val_len, &has_predicate)) < 1) { |
| 1602 | return -parsed+i; |
| 1603 | } |
| 1604 | parsed += i; |
| 1605 | pred += i; |
| 1606 | |
| 1607 | if (isdigit(name[0])) { |
| 1608 | idx = atoi(name); |
| 1609 | } |
| 1610 | |
| 1611 | for (cur_idx = 0, node = *node_match; node; ++cur_idx) { |
| 1612 | /* target */ |
| 1613 | target_match = NULL; |
| 1614 | if ((name[0] == '.') || !value) { |
| 1615 | target_match = calloc(1, sizeof *target_match); |
| 1616 | target_match->dnode = node->dnode; |
| 1617 | } else if (resolve_data_nodeid(prefix, pref_len, name, nam_len, node->dnode, &target_match)) { |
| 1618 | return -parsed; |
| 1619 | } |
| 1620 | |
| 1621 | /* check that we have the correct type */ |
| 1622 | if (name[0] == '.') { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1623 | if (node->dnode->schema->nodetype != LYS_LEAFLIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1624 | goto remove_instid; |
| 1625 | } |
| 1626 | } else if (value) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1627 | if (node->dnode->schema->nodetype != LYS_LIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1628 | goto remove_instid; |
| 1629 | } |
| 1630 | } |
| 1631 | |
| 1632 | if ((value && (strncmp(((struct lyd_node_leaf *)target_match->dnode)->value_str, value, val_len) |
| 1633 | || ((struct lyd_node_leaf *)target_match->dnode)->value_str[val_len])) |
| 1634 | || (!value && (idx != cur_idx))) { |
| 1635 | goto remove_instid; |
| 1636 | } |
| 1637 | |
| 1638 | while (target_match) { |
| 1639 | tmp = target_match->next; |
| 1640 | free(target_match); |
| 1641 | target_match = tmp; |
| 1642 | } |
| 1643 | |
| 1644 | /* leafref is ok, continue check with next leafref */ |
| 1645 | node_prev = node; |
| 1646 | node = node->next; |
| 1647 | continue; |
| 1648 | |
| 1649 | remove_instid: |
| 1650 | while (target_match) { |
| 1651 | tmp = target_match->next; |
| 1652 | free(target_match); |
| 1653 | target_match = tmp; |
| 1654 | } |
| 1655 | |
| 1656 | /* does not fulfill conditions, remove leafref record */ |
| 1657 | if (node_prev) { |
| 1658 | node_prev->next = node->next; |
| 1659 | free(node); |
| 1660 | node = node_prev->next; |
| 1661 | } else { |
| 1662 | node = (*node_match)->next; |
| 1663 | free(*node_match); |
| 1664 | *node_match = node; |
| 1665 | } |
| 1666 | } |
| 1667 | } while (has_predicate); |
| 1668 | |
| 1669 | return parsed; |
| 1670 | } |
| 1671 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1672 | /** |
| 1673 | * @brief Resolve instance-identifier. Logs directly. |
| 1674 | * |
| 1675 | * @param[in] unres Node of the instance-id type. |
| 1676 | * @param[in] path Instance-identifier node value. |
| 1677 | * @param[in] path_len Path length. |
| 1678 | * @param[in,out] ret Matching nodes. |
| 1679 | * |
| 1680 | * @return EXIT_SUCCESS on success, EXIT_FAILURE otherwise. |
| 1681 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1682 | int |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 1683 | resolve_instid(struct unres_data *unres, const char *path, int path_len, struct unres_data **ret) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1684 | { |
| 1685 | struct lyd_node *data; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 1686 | struct unres_data *riter = NULL, *raux; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1687 | const char *apath = strndupa(path, path_len); |
| 1688 | const char *prefix, *name; |
| 1689 | int i, parsed, pref_len, nam_len, has_predicate; |
| 1690 | |
| 1691 | parsed = 0; |
| 1692 | |
| 1693 | /* we need root, absolute path */ |
| 1694 | for (data = unres->dnode; data->parent; data = data->parent); |
| 1695 | for (; data->prev->next; data = data->prev); |
| 1696 | |
| 1697 | /* searching for nodeset */ |
| 1698 | do { |
| 1699 | if ((i = parse_instance_identifier(apath, &prefix, &pref_len, &name, &nam_len, &has_predicate)) < 1) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1700 | LOGVAL(LYE_INCHAR, unres->line, apath[-i], apath-i); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1701 | goto error; |
| 1702 | } |
| 1703 | parsed += i; |
| 1704 | apath += i; |
| 1705 | |
| 1706 | if (resolve_data_nodeid(prefix, pref_len, name, nam_len, data, ret)) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1707 | LOGVAL(LYE_LINE, unres->line); |
| 1708 | /* general error, the one written later will suffice */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1709 | goto error; |
| 1710 | } |
| 1711 | |
| 1712 | if (has_predicate) { |
| 1713 | /* we have predicate, so the current results must be list or leaf-list */ |
| 1714 | for (raux = NULL, riter = *ret; riter; ) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1715 | if ((riter->dnode->schema->nodetype == LYS_LIST && |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 1716 | ((struct lys_node_list *)riter->dnode->schema)->keys) |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1717 | || (riter->dnode->schema->nodetype == LYS_LEAFLIST)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1718 | /* instid is ok, continue check with next instid */ |
| 1719 | raux = riter; |
| 1720 | riter = riter->next; |
| 1721 | continue; |
| 1722 | } |
| 1723 | |
| 1724 | /* does not fulfill conditions, remove inst record */ |
| 1725 | if (raux) { |
| 1726 | raux->next = riter->next; |
| 1727 | free(riter); |
| 1728 | riter = raux->next; |
| 1729 | } else { |
| 1730 | *ret = riter->next; |
| 1731 | free(riter); |
| 1732 | riter = *ret; |
| 1733 | } |
| 1734 | } |
| 1735 | if ((i = resolve_predicate(apath, ret)) < 1) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1736 | LOGVAL(LYE_INPRED, unres->line, apath-i); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1737 | goto error; |
| 1738 | } |
| 1739 | parsed += i; |
| 1740 | apath += i; |
| 1741 | |
| 1742 | if (!*ret) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1743 | LOGVAL(LYE_LINE, unres->line); |
| 1744 | /* general error, the one written later will suffice */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1745 | goto error; |
| 1746 | } |
| 1747 | } |
| 1748 | } while (apath[0] != '\0'); |
| 1749 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 1750 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1751 | |
| 1752 | error: |
| 1753 | while (*ret) { |
| 1754 | raux = (*ret)->next; |
| 1755 | free(*ret); |
| 1756 | *ret = raux; |
| 1757 | } |
| 1758 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 1759 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1760 | } |
| 1761 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1762 | /** |
| 1763 | * @brief Passes config flag down to children. Does not log. |
| 1764 | * |
| 1765 | * @param[in] node Parent node. |
| 1766 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1767 | static void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1768 | inherit_config_flag(struct lys_node *node) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1769 | { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1770 | LY_TREE_FOR(node, node) { |
| 1771 | node->flags |= node->parent->flags & LYS_CONFIG_MASK; |
| 1772 | inherit_config_flag(node->child); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1773 | } |
| 1774 | } |
| 1775 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1776 | /** |
| 1777 | * @brief Resolve augment target, Does not log. |
| 1778 | * |
| 1779 | * @param[in] aug Augment in question. |
| 1780 | * @param[in] siblings Nodes where to start the search in. |
| 1781 | * @param[in] module Main module. |
| 1782 | * |
| 1783 | * @return EXIT_SUCCESS on success, EXIT_FAILURE otherwise. |
| 1784 | */ |
Michal Vasko | 4adc10f | 2015-08-11 15:26:17 +0200 | [diff] [blame^] | 1785 | int |
Michal Vasko | 2e1a7e4 | 2015-08-06 15:08:32 +0200 | [diff] [blame] | 1786 | resolve_augment(struct lys_node_augment *aug, struct lys_node *siblings, struct lys_module *module) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1787 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1788 | struct lys_node *sub, *aux; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1789 | |
| 1790 | assert(module); |
| 1791 | |
| 1792 | /* resolve target node */ |
Michal Vasko | 2e1a7e4 | 2015-08-06 15:08:32 +0200 | [diff] [blame] | 1793 | aug->target = resolve_schema_nodeid(aug->target_name, siblings, module, LYS_AUGMENT); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1794 | if (!aug->target) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1795 | return EXIT_FAILURE; |
| 1796 | } |
| 1797 | |
| 1798 | if (!aug->child) { |
| 1799 | /* nothing to do */ |
| 1800 | return EXIT_SUCCESS; |
| 1801 | } |
| 1802 | |
| 1803 | /* inherit config information from parent, augment does not have |
| 1804 | * config property, but we need to keep the information for subelements |
| 1805 | */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1806 | aug->flags |= aug->target->flags & LYS_CONFIG_MASK; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1807 | LY_TREE_FOR(aug->child, sub) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1808 | inherit_config_flag(sub); |
| 1809 | } |
| 1810 | |
Radek Krejci | 0acbe1b | 2015-08-04 09:33:49 +0200 | [diff] [blame] | 1811 | /* reconnect augmenting data into the target - add them to the target child list */ |
| 1812 | if (aug->target->child) { |
| 1813 | aux = aug->target->child->prev; /* remember current target's last node */ |
| 1814 | aux->next = aug->child; /* connect augmenting data after target's last node */ |
| 1815 | aug->target->child->prev = aug->child->prev; /* new target's last node is last augmenting node */ |
| 1816 | aug->child->prev = aux; /* finish connecting of both child lists */ |
| 1817 | } else { |
| 1818 | aug->target->child = aug->child; |
| 1819 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1820 | |
| 1821 | return EXIT_SUCCESS; |
| 1822 | } |
| 1823 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1824 | /** |
| 1825 | * @brief Resolve uses, apply augments, refines. Logs directly. |
| 1826 | * |
| 1827 | * @param[in] uses Uses in question. |
| 1828 | * @param[in,out] unres List of unresolved items. |
| 1829 | * @param[in] line Line in the input file. |
| 1830 | * |
| 1831 | * @return EXIT_SUCCESS on success, EXIT_FAILURER otherwise. |
| 1832 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1833 | int |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 1834 | 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] | 1835 | { |
| 1836 | struct ly_ctx *ctx; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1837 | struct lys_node *node = NULL, *node_aux; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 1838 | struct lys_refine *rfn; |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1839 | struct lys_restr *newmust; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1840 | int i, j; |
| 1841 | uint8_t size; |
| 1842 | |
| 1843 | /* copy the data nodes from grouping into the uses context */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1844 | LY_TREE_FOR(uses->grp->child, node) { |
| 1845 | node_aux = lys_node_dup(uses->module, node, uses->flags, 1, unres); |
| 1846 | if (!node_aux) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1847 | LOGVAL(LYE_SPEC, line, "Copying data from grouping failed."); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1848 | return EXIT_FAILURE; |
| 1849 | } |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1850 | if (lys_node_addchild((struct lys_node *)uses, node_aux)) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1851 | /* error logged */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1852 | lys_node_free(node_aux); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1853 | return EXIT_FAILURE; |
| 1854 | } |
| 1855 | } |
| 1856 | ctx = uses->module->ctx; |
| 1857 | |
| 1858 | /* apply refines */ |
| 1859 | for (i = 0; i < uses->refine_size; i++) { |
| 1860 | rfn = &uses->refine[i]; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1861 | node = resolve_schema_nodeid(rfn->target_name, uses->child, uses->module, LYS_LEAF); |
| 1862 | if (!node) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1863 | LOGVAL(LYE_INARG, line, rfn->target_name, "refine"); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1864 | return EXIT_FAILURE; |
| 1865 | } |
| 1866 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1867 | if (rfn->target_type && !(node->nodetype & rfn->target_type)) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1868 | LOGVAL(LYE_SPEC, line, "Refine substatements not applicable to the target-node."); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1869 | return EXIT_FAILURE; |
| 1870 | } |
| 1871 | |
| 1872 | /* description on any nodetype */ |
| 1873 | if (rfn->dsc) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1874 | lydict_remove(ctx, node->dsc); |
| 1875 | node->dsc = lydict_insert(ctx, rfn->dsc, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1876 | } |
| 1877 | |
| 1878 | /* reference on any nodetype */ |
| 1879 | if (rfn->ref) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1880 | lydict_remove(ctx, node->ref); |
| 1881 | node->ref = lydict_insert(ctx, rfn->ref, 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1882 | } |
| 1883 | |
| 1884 | /* config on any nodetype */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1885 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1886 | node->flags &= ~LYS_CONFIG_MASK; |
| 1887 | node->flags |= (rfn->flags & LYS_CONFIG_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1888 | } |
| 1889 | |
| 1890 | /* default value ... */ |
| 1891 | if (rfn->mod.dflt) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1892 | if (node->nodetype == LYS_LEAF) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1893 | /* leaf */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1894 | lydict_remove(ctx, ((struct lys_node_leaf *)node)->dflt); |
| 1895 | ((struct lys_node_leaf *)node)->dflt = lydict_insert(ctx, rfn->mod.dflt, 0); |
| 1896 | } else if (node->nodetype == LYS_CHOICE) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1897 | /* choice */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1898 | ((struct lys_node_choice *)node)->dflt = resolve_schema_nodeid(rfn->mod.dflt, node->child, node->module, LYS_CHOICE); |
| 1899 | if (!((struct lys_node_choice *)node)->dflt) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1900 | LOGVAL(LYE_INARG, line, rfn->mod.dflt, "default"); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1901 | return EXIT_FAILURE; |
| 1902 | } |
| 1903 | } |
| 1904 | } |
| 1905 | |
| 1906 | /* mandatory on leaf, anyxml or choice */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 1907 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1908 | if (node->nodetype & (LYS_LEAF | LYS_ANYXML | LYS_CHOICE)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1909 | /* remove current value */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1910 | node->flags &= ~LYS_MAND_MASK; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1911 | |
| 1912 | /* set new value */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1913 | node->flags |= (rfn->flags & LYS_MAND_MASK); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1914 | } |
| 1915 | } |
| 1916 | |
| 1917 | /* presence on container */ |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1918 | if ((node->nodetype & LYS_CONTAINER) && rfn->mod.presence) { |
| 1919 | lydict_remove(ctx, ((struct lys_node_container *)node)->presence); |
| 1920 | ((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] | 1921 | } |
| 1922 | |
| 1923 | /* min/max-elements on list or leaf-list */ |
| 1924 | /* 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] | 1925 | if (node->nodetype == LYS_LIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1926 | if (rfn->flags & 0x04) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1927 | ((struct lys_node_list *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1928 | } |
| 1929 | if (rfn->flags & 0x08) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1930 | ((struct lys_node_list *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1931 | } |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1932 | } else if (node->nodetype == LYS_LEAFLIST) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1933 | if (rfn->flags & 0x04) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1934 | ((struct lys_node_leaflist *)node)->min = rfn->mod.list.min; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1935 | } |
| 1936 | if (rfn->flags & 0x08) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1937 | ((struct lys_node_leaflist *)node)->max = rfn->mod.list.max; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1938 | } |
| 1939 | } |
| 1940 | |
| 1941 | /* must in leaf, leaf-list, list, container or anyxml */ |
| 1942 | if (rfn->must_size) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1943 | size = ((struct lys_node_leaf *)node)->must_size + rfn->must_size; |
| 1944 | newmust = realloc(((struct lys_node_leaf *)node)->must, size * sizeof *rfn->must); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1945 | if (!newmust) { |
| 1946 | LOGMEM; |
| 1947 | return EXIT_FAILURE; |
| 1948 | } |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1949 | for (i = 0, j = ((struct lys_node_leaf *)node)->must_size; i < rfn->must_size; i++, j++) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1950 | newmust[j].expr = lydict_insert(ctx, rfn->must[i].expr, 0); |
| 1951 | newmust[j].dsc = lydict_insert(ctx, rfn->must[i].dsc, 0); |
| 1952 | newmust[j].ref = lydict_insert(ctx, rfn->must[i].ref, 0); |
| 1953 | newmust[j].eapptag = lydict_insert(ctx, rfn->must[i].eapptag, 0); |
| 1954 | newmust[j].emsg = lydict_insert(ctx, rfn->must[i].emsg, 0); |
| 1955 | } |
| 1956 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 1957 | ((struct lys_node_leaf *)node)->must = newmust; |
| 1958 | ((struct lys_node_leaf *)node)->must_size = size; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1959 | } |
| 1960 | } |
| 1961 | |
| 1962 | /* apply augments */ |
| 1963 | for (i = 0; i < uses->augment_size; i++) { |
Michal Vasko | 2e1a7e4 | 2015-08-06 15:08:32 +0200 | [diff] [blame] | 1964 | if (resolve_augment(&uses->augment[i], uses->child, uses->module)) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 1965 | LOGVAL(LYE_INRESOLV, line, "augment", uses->augment[i].target_name); |
| 1966 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1967 | } |
| 1968 | } |
| 1969 | |
| 1970 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1971 | } |
| 1972 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 1973 | /** |
| 1974 | * @brief Resolve base identity recursively. Does not log. |
| 1975 | * |
| 1976 | * @param[in] module Main module. |
| 1977 | * @param[in] ident Identity in question. |
| 1978 | * @param[in] basename Base name of the identity. |
| 1979 | * |
| 1980 | * @return Pointer to the identity, NULL on error. |
| 1981 | */ |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 1982 | static struct lys_ident * |
| 1983 | resolve_base_ident_sub(struct lys_module *module, struct lys_ident *ident, const char *basename) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1984 | { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 1985 | uint32_t i, j; |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 1986 | struct lys_ident *base_iter = NULL; |
| 1987 | struct lys_ident_der *der; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1988 | |
| 1989 | /* search module */ |
| 1990 | for (i = 0; i < module->ident_size; i++) { |
| 1991 | if (!strcmp(basename, module->ident[i].name)) { |
| 1992 | |
| 1993 | if (!ident) { |
| 1994 | /* just search for type, so do not modify anything, just return |
| 1995 | * the base identity pointer |
| 1996 | */ |
| 1997 | return &module->ident[i]; |
| 1998 | } |
| 1999 | |
| 2000 | /* we are resolving identity definition, so now update structures */ |
| 2001 | ident->base = base_iter = &module->ident[i]; |
| 2002 | |
| 2003 | break; |
| 2004 | } |
| 2005 | } |
| 2006 | |
| 2007 | /* search submodules */ |
| 2008 | if (!base_iter) { |
| 2009 | for (j = 0; j < module->inc_size; j++) { |
| 2010 | for (i = 0; i < module->inc[j].submodule->ident_size; i++) { |
| 2011 | if (!strcmp(basename, module->inc[j].submodule->ident[i].name)) { |
| 2012 | |
| 2013 | if (!ident) { |
| 2014 | return &module->inc[j].submodule->ident[i]; |
| 2015 | } |
| 2016 | |
| 2017 | ident->base = base_iter = &module->inc[j].submodule->ident[i]; |
| 2018 | break; |
| 2019 | } |
| 2020 | } |
| 2021 | } |
| 2022 | } |
| 2023 | |
| 2024 | /* we found it somewhere */ |
| 2025 | if (base_iter) { |
| 2026 | while (base_iter) { |
| 2027 | for (der = base_iter->der; der && der->next; der = der->next); |
| 2028 | if (der) { |
| 2029 | der->next = malloc(sizeof *der); |
| 2030 | der = der->next; |
| 2031 | } else { |
| 2032 | ident->base->der = der = malloc(sizeof *der); |
| 2033 | } |
| 2034 | der->next = NULL; |
| 2035 | der->ident = ident; |
| 2036 | |
| 2037 | base_iter = base_iter->base; |
| 2038 | } |
| 2039 | return ident->base; |
| 2040 | } |
| 2041 | |
| 2042 | return NULL; |
| 2043 | } |
| 2044 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2045 | /** |
| 2046 | * @brief Resolve base identity. Logs directly. |
| 2047 | * |
| 2048 | * @param[in] module Main module. |
| 2049 | * @param[in] ident Identity in question. |
| 2050 | * @param[in] basename Base name of the identity. |
| 2051 | * @param[in] parent Either "type" or "ident". |
| 2052 | * @param[in] line Line in the input file. |
| 2053 | * |
| 2054 | * @return Pointer to the base identity, NULL on error. |
| 2055 | */ |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 2056 | static struct lys_ident * |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2057 | resolve_base_ident(struct lys_module *module, struct lys_ident *ident, const char *basename, const char* parent, |
| 2058 | uint32_t line) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2059 | { |
| 2060 | const char *name; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2061 | int i, prefix_len = 0; |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 2062 | struct lys_ident *result; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2063 | |
| 2064 | /* search for the base identity */ |
| 2065 | name = strchr(basename, ':'); |
| 2066 | if (name) { |
| 2067 | /* set name to correct position after colon */ |
| 2068 | prefix_len = name - basename; |
| 2069 | name++; |
| 2070 | |
| 2071 | if (!strncmp(basename, module->prefix, prefix_len) && !module->prefix[prefix_len]) { |
| 2072 | /* prefix refers to the current module, ignore it */ |
| 2073 | prefix_len = 0; |
| 2074 | } |
| 2075 | } else { |
| 2076 | name = basename; |
| 2077 | } |
| 2078 | |
| 2079 | if (prefix_len) { |
| 2080 | /* get module where to search */ |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 2081 | module = resolve_prefixed_module(module, basename, prefix_len); |
| 2082 | if (!module) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2083 | /* identity refers unknown data model */ |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 2084 | LOGVAL(LYE_INPREF, line, basename); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2085 | return NULL; |
| 2086 | } |
| 2087 | } else { |
| 2088 | /* search in submodules */ |
| 2089 | for (i = 0; i < module->inc_size; i++) { |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2090 | result = resolve_base_ident_sub((struct lys_module *)module->inc[i].submodule, ident, name); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2091 | if (result) { |
| 2092 | return result; |
| 2093 | } |
| 2094 | } |
| 2095 | } |
| 2096 | |
| 2097 | /* search in the identified module */ |
| 2098 | result = resolve_base_ident_sub(module, ident, name); |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 2099 | if (!result) { |
| 2100 | LOGVAL(LYE_INARG, line, basename, parent); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2101 | } |
| 2102 | |
| 2103 | return result; |
| 2104 | } |
| 2105 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2106 | /** |
| 2107 | * @brief Resolve identityref. Does not log. |
| 2108 | * |
| 2109 | * @param[in] base Base identity. |
| 2110 | * @param[in] name Identityref name. |
| 2111 | * @param[in] ns Namespace of the identityref. |
| 2112 | * |
| 2113 | * @return Pointer to the identity resolvent, NULL on error. |
| 2114 | */ |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 2115 | struct lys_ident * |
| 2116 | resolve_identityref(struct lys_ident *base, const char *name, const char *ns) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2117 | { |
Radek Krejci | a52656e | 2015-08-05 13:41:50 +0200 | [diff] [blame] | 2118 | struct lys_ident_der *der; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2119 | |
| 2120 | if (!base || !name || !ns) { |
| 2121 | return NULL; |
| 2122 | } |
| 2123 | |
| 2124 | for(der = base->der; der; der = der->next) { |
| 2125 | if (!strcmp(der->ident->name, name) && ns == der->ident->module->ns) { |
| 2126 | /* we have match */ |
| 2127 | return der->ident; |
| 2128 | } |
| 2129 | } |
| 2130 | |
| 2131 | /* not found */ |
| 2132 | return NULL; |
| 2133 | } |
| 2134 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2135 | /** |
| 2136 | * @brief Resolve unres identity. Logs directly. |
| 2137 | * |
| 2138 | * @param[in] mod Main module. |
| 2139 | * @param[in] ident Identity in question. |
| 2140 | * @param[in] base_name Base name of the identity. |
| 2141 | * @param[in] line Line in the input file. |
| 2142 | * |
| 2143 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on error. |
| 2144 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2145 | static int |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2146 | resolve_unres_ident(struct lys_module *mod, struct lys_ident *ident, const char *base_name, uint32_t line) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2147 | { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2148 | if (resolve_base_ident(mod, ident, base_name, "ident", line)) { |
| 2149 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2150 | } |
| 2151 | |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 2152 | LOGVAL(LYE_INRESOLV, (line == UINT_MAX ? line : 0), "identity", base_name); |
| 2153 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2154 | } |
| 2155 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2156 | /** |
| 2157 | * @brief Resolve unres identityref. Logs directly. |
| 2158 | * |
| 2159 | * @param[in] mod Main module. |
| 2160 | * @param[in] type Type in question. |
| 2161 | * @param[in] base_name Base name of the identity. |
| 2162 | * @param[in] line Line in the input file. |
| 2163 | * |
| 2164 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on error. |
| 2165 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2166 | static int |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2167 | resolve_unres_type_identref(struct lys_module *mod, struct lys_type *type, const char *base_name, uint32_t line) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2168 | { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2169 | type->info.ident.ref = resolve_base_ident(mod, NULL, base_name, "type", line); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2170 | if (type->info.ident.ref) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2171 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2172 | } |
| 2173 | |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 2174 | LOGVAL(LYE_INRESOLV, (line == UINT_MAX ? line : 0), "identityref", base_name); |
| 2175 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2176 | } |
| 2177 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2178 | /** |
| 2179 | * @brief Resolve unres leafref. Logs directly. |
| 2180 | * |
| 2181 | * @param[in] mod Main module. |
| 2182 | * @param[in] type Type in question. |
| 2183 | * @param[in] node Leafref schema node. |
| 2184 | * @param[in] line Line in the input file. |
| 2185 | * |
| 2186 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on error. |
| 2187 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2188 | static int |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2189 | resolve_unres_type_leafref(struct lys_module *mod, struct lys_type *type, struct lys_node *node, uint32_t line) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2190 | { |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 2191 | type->info.lref.target = (struct lys_node_leaf *)resolve_path_arg_schema(mod, type->info.lref.path, node, line); |
| 2192 | if (type->info.lref.target) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2193 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2194 | } |
| 2195 | |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 2196 | LOGVAL(LYE_INRESOLV, (line == UINT_MAX ? line : 0), "leafref", type->info.lref.path); |
| 2197 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2198 | } |
| 2199 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2200 | /** |
| 2201 | * @brief Resolve unres derived type. Logs directly. |
| 2202 | * |
| 2203 | * @param[in] mod Main module. |
| 2204 | * @param[in] type Type in question. |
| 2205 | * @param[in] type_name Derived type name, |
| 2206 | * @param[in] line Line in the input file. |
| 2207 | * |
| 2208 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on error. |
| 2209 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2210 | static int |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2211 | resolve_unres_type_der(struct lys_module *mod, struct lys_type *type, const char *type_name, uint32_t line) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2212 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2213 | type->der = resolve_superior_type(type_name, type->prefix, mod, (struct lys_node *)type->der); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2214 | if (type->der) { |
Michal Vasko | a91333d | 2015-08-03 16:03:06 +0200 | [diff] [blame] | 2215 | type->base = type->der->type.base; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2216 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2217 | } |
| 2218 | |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 2219 | LOGVAL(LYE_INRESOLV, line, "type", type_name); |
| 2220 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2221 | } |
| 2222 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2223 | /** |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2224 | * @brief Resolve unres if-feature. Logs directly. |
| 2225 | * |
| 2226 | * @param[in] mod Main module. |
| 2227 | * @param[in,out] feat_ptr Pointer to the resolved feature. |
| 2228 | * @param[in] feat_name Name of the feature. |
| 2229 | * @param[in] line Line in the input file. |
| 2230 | * |
| 2231 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on error. |
| 2232 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2233 | static int |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2234 | resolve_unres_iffeature(struct lys_module *mod, struct lys_feature **feat_ptr, const char *feat_name, uint32_t line) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2235 | { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2236 | *feat_ptr = resolve_feature(feat_name, mod, line); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2237 | if (*feat_ptr) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2238 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2239 | } |
| 2240 | |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 2241 | LOGVAL(LYE_INRESOLV, (line == UINT_MAX ? line : 0), "if-feature", feat_name); |
| 2242 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2243 | } |
| 2244 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2245 | /** |
| 2246 | * @brief Resolve unres uses. Logs directly. |
| 2247 | * |
| 2248 | * @param[in] uses Uses in question. |
| 2249 | * @param[in] unres Specific unres item. |
| 2250 | * @param[in] line Line in the input file. |
| 2251 | * |
| 2252 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on error. |
| 2253 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2254 | static int |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2255 | resolve_unres_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] | 2256 | { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2257 | if (uses->grp || !resolve_grouping(uses->parent, uses, line)) { |
| 2258 | if (!resolve_uses(uses, unres, line)) { |
| 2259 | return EXIT_SUCCESS; |
Michal Vasko | 12e3084 | 2015-08-04 11:54:00 +0200 | [diff] [blame] | 2260 | } |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2261 | } |
| 2262 | |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 2263 | LOGVAL(LYE_INRESOLV, (line == UINT_MAX ? line : 0), "uses", uses->name); |
| 2264 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2265 | } |
| 2266 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2267 | /** |
| 2268 | * @brief Resolve unres identity. Logs directly. |
| 2269 | * |
| 2270 | * @param[in] type Type in question. |
| 2271 | * @param[in] dflt Default value. |
| 2272 | * @param[in] line Line in the input file. |
| 2273 | * |
| 2274 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on error. |
| 2275 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2276 | static int |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2277 | resolve_unres_type_dflt(struct lys_type *type, const char *dflt, uint32_t line) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2278 | { |
Michal Vasko | 12e3084 | 2015-08-04 11:54:00 +0200 | [diff] [blame] | 2279 | if (!check_default(type, dflt)) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2280 | return EXIT_SUCCESS; |
Michal Vasko | 12e3084 | 2015-08-04 11:54:00 +0200 | [diff] [blame] | 2281 | } |
| 2282 | |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 2283 | LOGVAL(LYE_INRESOLV, line, "type default", dflt); |
| 2284 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2285 | } |
| 2286 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2287 | /** |
| 2288 | * @brief Resolve choice default. Logs directly. |
| 2289 | * |
| 2290 | * @param[in] choice Main module. |
| 2291 | * @param[in] dflt Default case name. |
| 2292 | * @param[in] line Line in the input file. |
| 2293 | * |
| 2294 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on error. |
| 2295 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2296 | static int |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2297 | resolve_unres_choice_dflt(struct lys_node_choice *choice, const char *dflt, uint32_t line) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2298 | { |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 2299 | choice->dflt = resolve_child((struct lys_node *)choice, dflt, strlen(dflt), LYS_ANYXML | LYS_CASE |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 2300 | | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2301 | if (choice->dflt) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2302 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2303 | } |
| 2304 | |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 2305 | LOGVAL(LYE_INRESOLV, line, "choice default", dflt); |
| 2306 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2307 | } |
| 2308 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2309 | /** |
| 2310 | * @brief Resolve unres identity. Logs directly. |
| 2311 | * |
| 2312 | * @param[in] list List in question. |
| 2313 | * @param[in] keys_str Keys node value. |
| 2314 | * @param[in] line Line in the input file. |
| 2315 | * |
| 2316 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on error. |
| 2317 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2318 | static int |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2319 | resolve_unres_list_keys(struct lys_node_list *list, const char *keys_str, uint32_t line) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2320 | { |
| 2321 | int i, len; |
| 2322 | const char *value; |
| 2323 | |
| 2324 | for (i = 0; i < list->keys_size; ++i) { |
| 2325 | /* get the key name */ |
| 2326 | if ((value = strpbrk(keys_str, " \t\n"))) { |
| 2327 | len = value - keys_str; |
| 2328 | while (isspace(value[0])) { |
| 2329 | value++; |
| 2330 | } |
| 2331 | } else { |
| 2332 | len = strlen(keys_str); |
| 2333 | } |
| 2334 | |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 2335 | list->keys[i] = (struct lys_node_leaf *)resolve_child((struct lys_node *)list, keys_str, len, LYS_LEAF); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2336 | |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 2337 | if (check_key(list->keys[i], list->flags, list->keys, i, keys_str, len, line)) { |
| 2338 | LOGVAL(LYE_INRESOLV, (line == UINT_MAX ? line : 0), "list keys", keys_str); |
| 2339 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2340 | } |
| 2341 | |
| 2342 | /* prepare for next iteration */ |
| 2343 | while (value && isspace(value[0])) { |
| 2344 | value++; |
| 2345 | } |
| 2346 | keys_str = value; |
| 2347 | } |
| 2348 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2349 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2350 | } |
| 2351 | |
Michal Vasko | 730dfdf | 2015-08-11 14:48:05 +0200 | [diff] [blame] | 2352 | /** |
| 2353 | * @brief Resolve unres unique. Logs directly. |
| 2354 | * |
| 2355 | * @param[in] uniq Unique in question. |
| 2356 | * @param[in] uniq_str Unique node value. |
| 2357 | * @param[in] line Line in the input file. |
| 2358 | * |
| 2359 | * @return EXIT_SUCCESS on success, EXIT_FAILURE on error. |
| 2360 | */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2361 | static int |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2362 | resolve_unres_list_uniq(struct lys_unique *uniq, const char *uniq_str, uint32_t line) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2363 | { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2364 | if (!resolve_unique((struct lys_node *)uniq->leafs, uniq_str, uniq, line)) { |
| 2365 | return EXIT_SUCCESS; |
Michal Vasko | 12e3084 | 2015-08-04 11:54:00 +0200 | [diff] [blame] | 2366 | } |
| 2367 | |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 2368 | LOGVAL(LYE_INRESOLV, (line == UINT_MAX ? line : 0), "list unique", uniq_str); |
| 2369 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2370 | } |
| 2371 | |
| 2372 | static int |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2373 | resolve_unres_when(struct lys_when *UNUSED(when), struct lys_node *UNUSED(start), uint32_t UNUSED(line)) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2374 | { |
| 2375 | /* TODO */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2376 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2377 | } |
| 2378 | |
| 2379 | static int |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2380 | resolve_unres_must(struct lys_restr *UNUSED(must), struct lys_node *UNUSED(start), uint32_t UNUSED(line)) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2381 | { |
| 2382 | /* TODO */ |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2383 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2384 | } |
| 2385 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2386 | /* logs indirectly |
| 2387 | * line == -1 means do not log, 0 means unknown */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2388 | static int |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2389 | resolve_unres_item(struct lys_module *mod, void *item, enum UNRES_ITEM type, void *str_node, struct unres_schema *unres, |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2390 | uint32_t line) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2391 | { |
| 2392 | int ret = EXIT_FAILURE, has_str = 0; |
| 2393 | |
| 2394 | switch (type) { |
| 2395 | case UNRES_RESOLVED: |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 2396 | LOGINT; |
Michal Vasko | 45b4231 | 2015-08-05 09:30:11 +0200 | [diff] [blame] | 2397 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2398 | case UNRES_IDENT: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2399 | ret = resolve_unres_ident(mod, item, str_node, line); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2400 | has_str = 1; |
| 2401 | break; |
| 2402 | case UNRES_TYPE_IDENTREF: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2403 | ret = resolve_unres_type_identref(mod, item, str_node, line); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2404 | has_str = 1; |
| 2405 | break; |
| 2406 | case UNRES_TYPE_LEAFREF: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2407 | ret = resolve_unres_type_leafref(mod, item, str_node, line); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2408 | has_str = 0; |
| 2409 | break; |
| 2410 | case UNRES_TYPE_DER: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2411 | ret = resolve_unres_type_der(mod, item, str_node, line); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2412 | has_str = 1; |
| 2413 | break; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2414 | case UNRES_IFFEAT: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2415 | ret = resolve_unres_iffeature(mod, item, str_node, line); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2416 | has_str = 1; |
| 2417 | break; |
| 2418 | case UNRES_USES: |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2419 | ret = resolve_unres_uses(item, unres, line); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2420 | has_str = 0; |
| 2421 | break; |
| 2422 | case UNRES_TYPE_DFLT: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2423 | ret = resolve_unres_type_dflt(item, str_node, line); |
| 2424 | /* do not remove str_node (dflt), it's in a typedef */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2425 | has_str = 0; |
| 2426 | break; |
| 2427 | case UNRES_CHOICE_DFLT: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2428 | ret = resolve_unres_choice_dflt(item, str_node, line); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2429 | has_str = 1; |
| 2430 | break; |
| 2431 | case UNRES_LIST_KEYS: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2432 | ret = resolve_unres_list_keys(item, str_node, line); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2433 | has_str = 1; |
| 2434 | break; |
| 2435 | case UNRES_LIST_UNIQ: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2436 | ret = resolve_unres_list_uniq(item, str_node, line); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2437 | has_str = 1; |
| 2438 | break; |
| 2439 | case UNRES_WHEN: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2440 | ret = resolve_unres_when(item, str_node, line); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2441 | has_str = 0; |
| 2442 | break; |
| 2443 | case UNRES_MUST: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2444 | ret = resolve_unres_must(item, str_node, line); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2445 | has_str = 0; |
| 2446 | break; |
| 2447 | } |
| 2448 | |
| 2449 | if (has_str && !ret) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2450 | lydict_remove(mod->ctx, str_node); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2451 | } |
| 2452 | |
| 2453 | return ret; |
| 2454 | } |
| 2455 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2456 | /* logs directly */ |
| 2457 | static void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2458 | print_unres_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] | 2459 | { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2460 | char line_str[18]; |
| 2461 | |
| 2462 | if (line) { |
| 2463 | sprintf(line_str, " (line %u)", line); |
| 2464 | } else { |
| 2465 | line_str[0] = '\0'; |
| 2466 | } |
| 2467 | |
| 2468 | switch (type) { |
| 2469 | case UNRES_RESOLVED: |
| 2470 | LOGINT; |
| 2471 | break; |
| 2472 | case UNRES_IDENT: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2473 | 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] | 2474 | break; |
| 2475 | case UNRES_TYPE_IDENTREF: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2476 | 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] | 2477 | break; |
| 2478 | case UNRES_TYPE_LEAFREF: |
| 2479 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later%s.", "leafref", ((struct lys_type *)item)->info.lref.path, line_str); |
| 2480 | break; |
| 2481 | case UNRES_TYPE_DER: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2482 | 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] | 2483 | break; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2484 | case UNRES_IFFEAT: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2485 | 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] | 2486 | break; |
| 2487 | case UNRES_USES: |
| 2488 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later%s.", "uses", ((struct lys_node_uses *)item)->name, line_str); |
| 2489 | break; |
| 2490 | case UNRES_TYPE_DFLT: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2491 | 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] | 2492 | break; |
| 2493 | case UNRES_CHOICE_DFLT: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2494 | 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] | 2495 | break; |
| 2496 | case UNRES_LIST_KEYS: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2497 | 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] | 2498 | break; |
| 2499 | case UNRES_LIST_UNIQ: |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2500 | 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] | 2501 | break; |
| 2502 | case UNRES_WHEN: |
| 2503 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later%s.", "when", ((struct lys_when *)item)->cond, line_str); |
| 2504 | break; |
| 2505 | case UNRES_MUST: |
| 2506 | LOGVRB("Resolving %s \"%s\" failed, it will be attempted later%s.", "must", ((struct lys_restr *)item)->expr, line_str); |
| 2507 | break; |
| 2508 | } |
| 2509 | } |
| 2510 | |
| 2511 | /* logs indirectly */ |
| 2512 | int |
| 2513 | resolve_unres(struct lys_module *mod, struct unres_schema *unres) |
| 2514 | { |
| 2515 | uint32_t i, resolved = 0; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2516 | |
| 2517 | assert(unres); |
| 2518 | |
| 2519 | /* uses */ |
| 2520 | for (i = 0; i < unres->count; ++i) { |
| 2521 | if (unres->type[i] != UNRES_USES) { |
| 2522 | continue; |
| 2523 | } |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2524 | if (!resolve_unres_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres, unres->line[i])) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2525 | unres->type[i] = UNRES_RESOLVED; |
| 2526 | ++resolved; |
| 2527 | } |
| 2528 | } |
| 2529 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2530 | /* the rest */ |
| 2531 | for (i = 0; i < unres->count; ++i) { |
| 2532 | if (unres->type[i] == UNRES_RESOLVED) { |
| 2533 | continue; |
| 2534 | } |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2535 | if (!resolve_unres_item(mod, unres->item[i], unres->type[i], unres->str_snode[i], unres, unres->line[i])) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2536 | unres->type[i] = UNRES_RESOLVED; |
| 2537 | ++resolved; |
| 2538 | } |
| 2539 | } |
| 2540 | |
| 2541 | if (resolved < unres->count) { |
| 2542 | return EXIT_FAILURE; |
| 2543 | } |
| 2544 | |
| 2545 | return EXIT_SUCCESS; |
| 2546 | } |
| 2547 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2548 | /* logs indirectly */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2549 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2550 | unres_add_str(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type, const char *str, |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2551 | uint32_t line) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2552 | { |
| 2553 | str = lydict_insert(mod->ctx, str, 0); |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2554 | unres_add_node(mod, unres, item, type, (struct lys_node *)str, line); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2555 | } |
| 2556 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2557 | /* logs indirectly */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2558 | void |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2559 | unres_add_node(struct lys_module *mod, struct unres_schema *unres, void *item, enum UNRES_ITEM type, |
| 2560 | struct lys_node *node, uint32_t line) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2561 | { |
| 2562 | assert(unres && item); |
| 2563 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2564 | if (!resolve_unres_item(mod, item, type, node, unres, UINT_MAX)) { |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2565 | return; |
| 2566 | } |
| 2567 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2568 | print_unres_item_fail(item, type, node, line); |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2569 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2570 | unres->count++; |
| 2571 | unres->item = realloc(unres->item, unres->count*sizeof *unres->item); |
| 2572 | unres->item[unres->count-1] = item; |
| 2573 | unres->type = realloc(unres->type, unres->count*sizeof *unres->type); |
| 2574 | unres->type[unres->count-1] = type; |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2575 | unres->str_snode = realloc(unres->str_snode, unres->count*sizeof *unres->str_snode); |
| 2576 | unres->str_snode[unres->count-1] = node; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2577 | unres->line = realloc(unres->line, unres->count*sizeof *unres->line); |
| 2578 | unres->line[unres->count-1] = line; |
| 2579 | } |
| 2580 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2581 | /* logs indirectly */ |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 2582 | int |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2583 | unres_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] | 2584 | { |
| 2585 | int i; |
| 2586 | |
| 2587 | if (!item || !new_item) { |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 2588 | LOGINT; |
| 2589 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2590 | } |
| 2591 | |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2592 | i = unres_find(unres, item, type); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2593 | |
| 2594 | if (i == -1) { |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 2595 | return EXIT_FAILURE; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2596 | } |
| 2597 | |
Michal Vasko | 4adc10f | 2015-08-11 15:26:17 +0200 | [diff] [blame^] | 2598 | if ((type == UNRES_TYPE_LEAFREF) || (type == UNRES_USES) || (type == UNRES_TYPE_DFLT) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2599 | || (type == UNRES_WHEN) || (type == UNRES_MUST)) { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2600 | unres_add_node(mod, unres, new_item, type, unres->str_snode[i], 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2601 | } else { |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2602 | unres_add_str(mod, unres, new_item, type, unres->str_snode[i], 0); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2603 | } |
Michal Vasko | dad1940 | 2015-08-06 09:51:53 +0200 | [diff] [blame] | 2604 | |
| 2605 | return EXIT_SUCCESS; |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2606 | } |
| 2607 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 2608 | /* does not log */ |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2609 | int |
Radek Krejci | 1d82ef6 | 2015-08-07 14:44:40 +0200 | [diff] [blame] | 2610 | unres_find(struct unres_schema *unres, void *item, enum UNRES_ITEM type) |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 2611 | { |
| 2612 | uint32_t ret = -1, i; |
| 2613 | |
| 2614 | for (i = 0; i < unres->count; ++i) { |
| 2615 | if ((unres->item[i] == item) && (unres->type[i] == type)) { |
| 2616 | ret = i; |
| 2617 | break; |
| 2618 | } |
| 2619 | } |
| 2620 | |
| 2621 | return ret; |
| 2622 | } |