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