Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file tree_schema.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Schema tree implementation |
| 5 | * |
| 6 | * Copyright (c) 2015 - 2018 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
| 15 | #include "common.h" |
| 16 | |
| 17 | #include <ctype.h> |
| 18 | #include <dirent.h> |
| 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <linux/limits.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | #include "libyang.h" |
| 29 | #include "context.h" |
| 30 | #include "tree_schema_internal.h" |
| 31 | #include "xpath.h" |
| 32 | |
| 33 | /** |
| 34 | * @brief Duplicate string into dictionary |
| 35 | * @param[in] CTX libyang context of the dictionary. |
| 36 | * @param[in] ORIG String to duplicate. |
| 37 | * @param[out] DUP Where to store the result. |
| 38 | */ |
| 39 | #define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);} |
| 40 | |
| 41 | #define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, OPTIONS, ITER, FUNC, RET, GOTO) \ |
| 42 | if (ARRAY_P) { \ |
| 43 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \ |
| 44 | for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \ |
| 45 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
| 46 | RET = FUNC(CTX, &(ARRAY_P)[ITER], OPTIONS, &(ARRAY_C)[ITER]); \ |
| 47 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 48 | } \ |
| 49 | } |
| 50 | |
| 51 | #define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, OPTIONS, FUNC, RET, GOTO) \ |
| 52 | if (MEMBER_P) { \ |
| 53 | MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \ |
| 54 | LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \ |
| 55 | RET = FUNC(CTX, MEMBER_P, OPTIONS, MEMBER_C); \ |
| 56 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 57 | } |
| 58 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 59 | static struct lysc_ext_instance * |
| 60 | lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig) |
| 61 | { |
| 62 | /* TODO */ |
| 63 | (void) ctx; |
| 64 | (void) orig; |
| 65 | return NULL; |
| 66 | } |
| 67 | |
| 68 | static struct lysc_pattern* |
| 69 | lysc_pattern_dup(struct lysc_pattern *orig) |
| 70 | { |
| 71 | ++orig->refcount; |
| 72 | return orig; |
| 73 | } |
| 74 | |
| 75 | static struct lysc_pattern** |
| 76 | lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig) |
| 77 | { |
| 78 | struct lysc_pattern **dup; |
| 79 | unsigned int u; |
| 80 | |
| 81 | LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL); |
| 82 | LY_ARRAY_FOR(orig, u) { |
| 83 | dup[u] = lysc_pattern_dup(orig[u]); |
| 84 | LY_ARRAY_INCREMENT(dup); |
| 85 | } |
| 86 | return dup; |
| 87 | } |
| 88 | |
| 89 | struct lysc_range* |
| 90 | lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig) |
| 91 | { |
| 92 | struct lysc_range *dup; |
| 93 | LY_ERR ret; |
| 94 | |
| 95 | dup = calloc(1, sizeof *dup); |
| 96 | LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL); |
| 97 | if (orig->parts) { |
| 98 | LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup); |
| 99 | LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts); |
| 100 | memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts); |
| 101 | } |
| 102 | DUP_STRING(ctx, orig->eapptag, dup->eapptag); |
| 103 | DUP_STRING(ctx, orig->emsg, dup->emsg); |
| 104 | dup->exts = lysc_ext_instance_dup(ctx, orig->exts); |
| 105 | |
| 106 | return dup; |
| 107 | cleanup: |
| 108 | free(dup); |
| 109 | (void) ret; /* set but not used due to the return type */ |
| 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | struct iff_stack { |
| 114 | int size; |
| 115 | int index; /* first empty item */ |
| 116 | uint8_t *stack; |
| 117 | }; |
| 118 | |
| 119 | static LY_ERR |
| 120 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 121 | { |
| 122 | if (stack->index == stack->size) { |
| 123 | stack->size += 4; |
| 124 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 125 | LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM); |
| 126 | } |
| 127 | stack->stack[stack->index++] = value; |
| 128 | return LY_SUCCESS; |
| 129 | } |
| 130 | |
| 131 | static uint8_t |
| 132 | iff_stack_pop(struct iff_stack *stack) |
| 133 | { |
| 134 | stack->index--; |
| 135 | return stack->stack[stack->index]; |
| 136 | } |
| 137 | |
| 138 | static void |
| 139 | iff_stack_clean(struct iff_stack *stack) |
| 140 | { |
| 141 | stack->size = 0; |
| 142 | free(stack->stack); |
| 143 | } |
| 144 | |
| 145 | static void |
| 146 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 147 | { |
| 148 | uint8_t *item; |
| 149 | uint8_t mask = 3; |
| 150 | |
| 151 | assert(pos >= 0); |
| 152 | assert(op <= 3); /* max 2 bits */ |
| 153 | |
| 154 | item = &list[pos / 4]; |
| 155 | mask = mask << 2 * (pos % 4); |
| 156 | *item = (*item) & ~mask; |
| 157 | *item = (*item) | (op << 2 * (pos % 4)); |
| 158 | } |
| 159 | |
| 160 | #define LYS_IFF_LP 0x04 /* ( */ |
| 161 | #define LYS_IFF_RP 0x08 /* ) */ |
| 162 | |
| 163 | static struct lysc_feature * |
| 164 | lysc_feature_find(struct lysc_module *mod, const char *name, size_t len) |
| 165 | { |
| 166 | size_t i; |
| 167 | struct lysc_feature *f; |
| 168 | |
| 169 | for (i = 0; i < len; ++i) { |
| 170 | if (name[i] == ':') { |
| 171 | /* we have a prefixed feature */ |
| 172 | mod = lysc_module_find_prefix(mod, name, i); |
| 173 | LY_CHECK_RET(!mod, NULL); |
| 174 | |
| 175 | name = &name[i + 1]; |
| 176 | len = len - i - 1; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /* we have the correct module, get the feature */ |
| 181 | LY_ARRAY_FOR(mod->features, i) { |
| 182 | f = &mod->features[i]; |
| 183 | if (!strncmp(f->name, name, len) && f->name[len] == '\0') { |
| 184 | return f; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | return NULL; |
| 189 | } |
| 190 | |
| 191 | static LY_ERR |
| 192 | lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, int UNUSED(options), struct lysc_ext_instance *ext) |
| 193 | { |
| 194 | const char *name; |
| 195 | unsigned int u; |
| 196 | const struct lys_module *mod; |
| 197 | struct lysp_ext *edef = NULL; |
| 198 | |
| 199 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument); |
| 200 | ext->insubstmt = ext_p->insubstmt; |
| 201 | ext->insubstmt_index = ext_p->insubstmt_index; |
| 202 | |
| 203 | /* get module where the extension definition should be placed */ |
| 204 | for (u = 0; ext_p->name[u] != ':'; ++u); |
| 205 | mod = lys_module_find_prefix(ctx->mod, ext_p->name, u); |
| 206 | LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 207 | "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name), |
| 208 | LY_EVALID); |
| 209 | LY_CHECK_ERR_RET(!mod->parsed->extensions, |
| 210 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 211 | "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.", |
| 212 | ext_p->name, mod->parsed->name), |
| 213 | LY_EVALID); |
| 214 | name = &ext_p->name[u + 1]; |
| 215 | /* find the extension definition there */ |
| 216 | for (ext = NULL, u = 0; u < LY_ARRAY_SIZE(mod->parsed->extensions); ++u) { |
| 217 | if (!strcmp(name, mod->parsed->extensions[u].name)) { |
| 218 | edef = &mod->parsed->extensions[u]; |
| 219 | break; |
| 220 | } |
| 221 | } |
| 222 | LY_CHECK_ERR_RET(!edef, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 223 | "Extension definition of extension instance \"%s\" not found.", ext_p->name), |
| 224 | LY_EVALID); |
| 225 | /* TODO plugins */ |
| 226 | |
| 227 | return LY_SUCCESS; |
| 228 | } |
| 229 | |
| 230 | static LY_ERR |
| 231 | lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, int UNUSED(options), struct lysc_iffeature *iff) |
| 232 | { |
| 233 | const char *c = *value; |
| 234 | int r, rc = EXIT_FAILURE; |
| 235 | int i, j, last_not, checkversion = 0; |
| 236 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
| 237 | uint8_t op; |
| 238 | struct iff_stack stack = {0, 0, NULL}; |
| 239 | struct lysc_feature *f; |
| 240 | |
| 241 | assert(c); |
| 242 | |
| 243 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 244 | for (i = j = last_not = 0; c[i]; i++) { |
| 245 | if (c[i] == '(') { |
| 246 | j++; |
| 247 | checkversion = 1; |
| 248 | continue; |
| 249 | } else if (c[i] == ')') { |
| 250 | j--; |
| 251 | continue; |
| 252 | } else if (isspace(c[i])) { |
| 253 | checkversion = 1; |
| 254 | continue; |
| 255 | } |
| 256 | |
| 257 | if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) { |
| 258 | if (c[i + r] == '\0') { |
| 259 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 260 | "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value); |
| 261 | return LY_EVALID; |
| 262 | } else if (!isspace(c[i + r])) { |
| 263 | /* feature name starting with the not/and/or */ |
| 264 | last_not = 0; |
| 265 | f_size++; |
| 266 | } else if (c[i] == 'n') { /* not operation */ |
| 267 | if (last_not) { |
| 268 | /* double not */ |
| 269 | expr_size = expr_size - 2; |
| 270 | last_not = 0; |
| 271 | } else { |
| 272 | last_not = 1; |
| 273 | } |
| 274 | } else { /* and, or */ |
| 275 | f_exp++; |
| 276 | /* not a not operation */ |
| 277 | last_not = 0; |
| 278 | } |
| 279 | i += r; |
| 280 | } else { |
| 281 | f_size++; |
| 282 | last_not = 0; |
| 283 | } |
| 284 | expr_size++; |
| 285 | |
| 286 | while (!isspace(c[i])) { |
| 287 | if (!c[i] || c[i] == ')') { |
| 288 | i--; |
| 289 | break; |
| 290 | } |
| 291 | i++; |
| 292 | } |
| 293 | } |
| 294 | if (j || f_exp != f_size) { |
| 295 | /* not matching count of ( and ) */ |
| 296 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 297 | "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value); |
| 298 | return LY_EVALID; |
| 299 | } |
| 300 | |
| 301 | if (checkversion || expr_size > 1) { |
| 302 | /* check that we have 1.1 module */ |
| 303 | if (ctx->mod->compiled->version != LYS_VERSION_1_1) { |
| 304 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 305 | "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value); |
| 306 | return LY_EVALID; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /* allocate the memory */ |
| 311 | LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM); |
| 312 | iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr); |
| 313 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 314 | LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error); |
| 315 | |
| 316 | stack.size = expr_size; |
| 317 | f_size--; expr_size--; /* used as indexes from now */ |
| 318 | |
| 319 | for (i--; i >= 0; i--) { |
| 320 | if (c[i] == ')') { |
| 321 | /* push it on stack */ |
| 322 | iff_stack_push(&stack, LYS_IFF_RP); |
| 323 | continue; |
| 324 | } else if (c[i] == '(') { |
| 325 | /* pop from the stack into result all operators until ) */ |
| 326 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 327 | iff_setop(iff->expr, op, expr_size--); |
| 328 | } |
| 329 | continue; |
| 330 | } else if (isspace(c[i])) { |
| 331 | continue; |
| 332 | } |
| 333 | |
| 334 | /* end of operator or operand -> find beginning and get what is it */ |
| 335 | j = i + 1; |
| 336 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 337 | i--; |
| 338 | } |
| 339 | i++; /* go back by one step */ |
| 340 | |
| 341 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
| 342 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 343 | /* double not */ |
| 344 | iff_stack_pop(&stack); |
| 345 | } else { |
| 346 | /* not has the highest priority, so do not pop from the stack |
| 347 | * as in case of AND and OR */ |
| 348 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 349 | } |
| 350 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
| 351 | /* as for OR - pop from the stack all operators with the same or higher |
| 352 | * priority and store them to the result, then push the AND to the stack */ |
| 353 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 354 | op = iff_stack_pop(&stack); |
| 355 | iff_setop(iff->expr, op, expr_size--); |
| 356 | } |
| 357 | iff_stack_push(&stack, LYS_IFF_AND); |
| 358 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
| 359 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 360 | op = iff_stack_pop(&stack); |
| 361 | iff_setop(iff->expr, op, expr_size--); |
| 362 | } |
| 363 | iff_stack_push(&stack, LYS_IFF_OR); |
| 364 | } else { |
| 365 | /* feature name, length is j - i */ |
| 366 | |
| 367 | /* add it to the expression */ |
| 368 | iff_setop(iff->expr, LYS_IFF_F, expr_size--); |
| 369 | |
| 370 | /* now get the link to the feature definition */ |
| 371 | f = lysc_feature_find(ctx->mod->compiled, &c[i], j - i); |
| 372 | LY_CHECK_ERR_GOTO(!f, |
| 373 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 374 | "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]); |
| 375 | rc = LY_EVALID, |
| 376 | error) |
| 377 | iff->features[f_size] = f; |
| 378 | LY_ARRAY_INCREMENT(iff->features); |
| 379 | f_size--; |
| 380 | } |
| 381 | } |
| 382 | while (stack.index) { |
| 383 | op = iff_stack_pop(&stack); |
| 384 | iff_setop(iff->expr, op, expr_size--); |
| 385 | } |
| 386 | |
| 387 | if (++expr_size || ++f_size) { |
| 388 | /* not all expected operators and operands found */ |
| 389 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 390 | "Invalid value \"%s\" of if-feature - processing error.", *value); |
| 391 | rc = LY_EINT; |
| 392 | } else { |
| 393 | rc = LY_SUCCESS; |
| 394 | } |
| 395 | |
| 396 | error: |
| 397 | /* cleanup */ |
| 398 | iff_stack_clean(&stack); |
| 399 | |
| 400 | return rc; |
| 401 | } |
| 402 | |
| 403 | static LY_ERR |
| 404 | lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, int options, struct lysc_when *when) |
| 405 | { |
| 406 | unsigned int u; |
| 407 | LY_ERR ret = LY_SUCCESS; |
| 408 | |
| 409 | when->cond = lyxp_expr_parse(ctx->ctx, when_p->cond); |
| 410 | LY_CHECK_ERR_GOTO(!when->cond, ret = ly_errcode(ctx->ctx), done); |
| 411 | COMPILE_ARRAY_GOTO(ctx, when_p->exts, when->exts, options, u, lys_compile_ext, ret, done); |
| 412 | |
| 413 | done: |
| 414 | return ret; |
| 415 | } |
| 416 | |
| 417 | static LY_ERR |
| 418 | lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, int options, struct lysc_must *must) |
| 419 | { |
| 420 | unsigned int u; |
| 421 | LY_ERR ret = LY_SUCCESS; |
| 422 | |
| 423 | must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg); |
| 424 | LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done); |
| 425 | |
| 426 | DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag); |
| 427 | DUP_STRING(ctx->ctx, must_p->emsg, must->emsg); |
| 428 | COMPILE_ARRAY_GOTO(ctx, must_p->exts, must->exts, options, u, lys_compile_ext, ret, done); |
| 429 | |
| 430 | done: |
| 431 | return ret; |
| 432 | } |
| 433 | |
| 434 | static LY_ERR |
| 435 | lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, int options, struct lysc_import *imp) |
| 436 | { |
| 437 | unsigned int u; |
| 438 | struct lys_module *mod = NULL; |
| 439 | struct lysc_module *comp; |
| 440 | LY_ERR ret = LY_SUCCESS; |
| 441 | |
| 442 | DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix); |
| 443 | COMPILE_ARRAY_GOTO(ctx, imp_p->exts, imp->exts, options, u, lys_compile_ext, ret, done); |
| 444 | imp->module = imp_p->module; |
| 445 | |
| 446 | /* make sure that we have both versions (lysp_ and lysc_) of the imported module. To import groupings or |
| 447 | * typedefs, the lysp_ is needed. To augment or deviate imported module, we need the lysc_ structure */ |
| 448 | if (!imp->module->parsed) { |
| 449 | comp = imp->module->compiled; |
| 450 | /* try to get filepath from the compiled version */ |
| 451 | if (comp->filepath) { |
| 452 | mod = (struct lys_module*)lys_parse_path(ctx->ctx, comp->filepath, |
| 453 | !strcmp(&comp->filepath[strlen(comp->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG); |
| 454 | if (mod != imp->module) { |
| 455 | LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.", |
| 456 | comp->filepath, comp->name); |
| 457 | mod = NULL; |
| 458 | } |
| 459 | } |
| 460 | if (!mod) { |
| 461 | if (lysp_load_module(ctx->ctx, comp->name, comp->revision, 0, 1, &mod)) { |
| 462 | LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.", |
| 463 | comp->name, ctx->mod->compiled->name); |
| 464 | return LY_ENOTFOUND; |
| 465 | } |
| 466 | } |
| 467 | } else if (!imp->module->compiled) { |
| 468 | return lys_compile(imp->module, options); |
| 469 | } |
| 470 | |
| 471 | done: |
| 472 | return ret; |
| 473 | } |
| 474 | |
| 475 | static LY_ERR |
| 476 | lys_compile_identity(struct lysc_ctx *ctx, struct lysp_ident *ident_p, int options, struct lysc_ident *ident) |
| 477 | { |
| 478 | unsigned int u; |
| 479 | LY_ERR ret = LY_SUCCESS; |
| 480 | |
| 481 | DUP_STRING(ctx->ctx, ident_p->name, ident->name); |
| 482 | COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, options, u, lys_compile_iffeature, ret, done); |
| 483 | /* backlings (derived) can be added no sooner than when all the identities in the current module are present */ |
| 484 | COMPILE_ARRAY_GOTO(ctx, ident_p->exts, ident->exts, options, u, lys_compile_ext, ret, done); |
| 485 | ident->flags = ident_p->flags; |
| 486 | |
| 487 | done: |
| 488 | return ret; |
| 489 | } |
| 490 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 491 | /** |
| 492 | * @brief Find and process the referenced base identities from another identity or identityref |
| 493 | * |
| 494 | * For bases in identity se backlinks to them from the base identities. For identityref, store |
| 495 | * the array of pointers to the base identities. So one of the ident or bases parameter must be set |
| 496 | * to distinguish these two use cases. |
| 497 | * |
| 498 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 499 | * @param[in] bases_p Array of names (including prefix if necessary) of base identities. |
| 500 | * @param[in] ident Referencing identity to work with. |
| 501 | * @param[in] bases Array of bases of identityref to fill in. |
| 502 | * @return LY_ERR value. |
| 503 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 504 | static LY_ERR |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 505 | lys_compile_identity_bases(struct lysc_ctx *ctx, const char **bases_p, struct lysc_ident *ident, struct lysc_ident ***bases) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 506 | { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 507 | unsigned int u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 508 | const char *s, *name; |
| 509 | struct lysc_module *mod; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 510 | struct lysc_ident **idref; |
| 511 | |
| 512 | assert(ident || bases); |
| 513 | |
| 514 | if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod->compiled->version < 2) { |
| 515 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 516 | "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type"); |
| 517 | return LY_EVALID; |
| 518 | } |
| 519 | |
| 520 | for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) { |
| 521 | s = strchr(bases_p[u], ':'); |
| 522 | if (s) { |
| 523 | /* prefixed identity */ |
| 524 | name = &s[1]; |
| 525 | mod = lysc_module_find_prefix(ctx->mod->compiled, bases_p[u], s - bases_p[u]); |
| 526 | } else { |
| 527 | name = bases_p[u]; |
| 528 | mod = ctx->mod->compiled; |
| 529 | } |
| 530 | if (!mod) { |
| 531 | if (ident) { |
| 532 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 533 | "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 534 | } else { |
| 535 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 536 | "Invalid prefix used for base (%s) of identityref.", bases_p[u]); |
| 537 | } |
| 538 | return LY_EVALID; |
| 539 | } |
| 540 | idref = NULL; |
| 541 | if (mod->identities) { |
| 542 | for (v = 0; v < LY_ARRAY_SIZE(mod->identities); ++v) { |
| 543 | if (!strcmp(name, mod->identities[v].name)) { |
| 544 | if (ident) { |
| 545 | /* we have match! store the backlink */ |
| 546 | LY_ARRAY_NEW_RET(ctx->ctx, mod->identities[v].derived, idref, LY_EMEM); |
| 547 | *idref = ident; |
| 548 | } else { |
| 549 | /* we have match! store the found identity */ |
| 550 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
| 551 | *idref = &mod->identities[v]; |
| 552 | } |
| 553 | break; |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | if (!idref || !(*idref)) { |
| 558 | if (ident) { |
| 559 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 560 | "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 561 | } else { |
| 562 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 563 | "Unable to find base (%s) of identityref.", bases_p[u]); |
| 564 | } |
| 565 | return LY_EVALID; |
| 566 | } |
| 567 | } |
| 568 | return LY_SUCCESS; |
| 569 | } |
| 570 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 571 | /** |
| 572 | * @brief For the given array of identities, set the backlinks from all their base identities. |
| 573 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 574 | * @param[in] idents_p Array of identities definitions from the parsed schema structure. |
| 575 | * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set. |
| 576 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 577 | */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 578 | static LY_ERR |
| 579 | lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents) |
| 580 | { |
| 581 | unsigned int i; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 582 | |
| 583 | for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) { |
| 584 | if (!idents_p[i].bases) { |
| 585 | continue; |
| 586 | } |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 587 | LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 588 | } |
| 589 | return LY_SUCCESS; |
| 590 | } |
| 591 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 592 | /** |
| 593 | * @brief Create compiled feature structure. |
| 594 | * @param[in] ctx Compile context. |
| 595 | * @param[in] feature_p Parsed feature definition to compile. |
| 596 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 597 | * @param[in,out] feature Compiled feature structure to fill. |
| 598 | * @return LY_ERR value. |
| 599 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 600 | static LY_ERR |
| 601 | lys_compile_feature(struct lysc_ctx *ctx, struct lysp_feature *feature_p, int options, struct lysc_feature *feature) |
| 602 | { |
| 603 | unsigned int u, v; |
| 604 | LY_ERR ret = LY_SUCCESS; |
| 605 | struct lysc_feature **df; |
| 606 | |
| 607 | DUP_STRING(ctx->ctx, feature_p->name, feature->name); |
| 608 | feature->flags = feature_p->flags; |
| 609 | |
| 610 | COMPILE_ARRAY_GOTO(ctx, feature_p->exts, feature->exts, options, u, lys_compile_ext, ret, done); |
| 611 | COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, options, u, lys_compile_iffeature, ret, done); |
| 612 | if (feature->iffeatures) { |
| 613 | for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) { |
| 614 | if (feature->iffeatures[u].features) { |
| 615 | for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) { |
| 616 | /* add itself into the dependants list */ |
| 617 | LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM); |
| 618 | *df = feature; |
| 619 | } |
| 620 | /* TODO check for circular dependency */ |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | done: |
| 625 | return ret; |
| 626 | } |
| 627 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 628 | /** |
| 629 | * @brief Validate and normalize numeric value from a range definition. |
| 630 | * @param[in] ctx Compile context. |
| 631 | * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to |
| 632 | * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into |
| 633 | * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from |
| 634 | * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100. |
| 635 | * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64. |
| 636 | * @param[in] value String value of the range boundary. |
| 637 | * @param[out] len Number of the processed bytes from the value. Processing stops on the first character which is not part of the number boundary. |
| 638 | * @param[out] valcopy NULL-terminated string with the numeric value to parse and store. |
| 639 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value). |
| 640 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 641 | static LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 642 | range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value, size_t *len, char **valcopy) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 643 | { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 644 | size_t fraction = 0, size; |
| 645 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 646 | *len = 0; |
| 647 | |
| 648 | assert(value); |
| 649 | /* parse value */ |
| 650 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 651 | return LY_EVALID; |
| 652 | } |
| 653 | |
| 654 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 655 | ++(*len); |
| 656 | } |
| 657 | |
| 658 | while (isdigit(value[*len])) { |
| 659 | ++(*len); |
| 660 | } |
| 661 | |
| 662 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 663 | if (basetype == LY_TYPE_DEC64) { |
| 664 | goto decimal; |
| 665 | } else { |
| 666 | *valcopy = strndup(value, *len); |
| 667 | return LY_SUCCESS; |
| 668 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 669 | } |
| 670 | fraction = *len; |
| 671 | |
| 672 | ++(*len); |
| 673 | while (isdigit(value[*len])) { |
| 674 | ++(*len); |
| 675 | } |
| 676 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 677 | if (basetype == LY_TYPE_DEC64) { |
| 678 | decimal: |
| 679 | assert(frdigits); |
| 680 | if (*len - 1 - fraction > frdigits) { |
| 681 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 682 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
| 683 | *len, value, frdigits); |
| 684 | return LY_EINVAL; |
| 685 | } |
| 686 | if (fraction) { |
| 687 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 688 | } else { |
| 689 | size = (*len) + frdigits + 1; |
| 690 | } |
| 691 | *valcopy = malloc(size * sizeof **valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 692 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 693 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 694 | (*valcopy)[size - 1] = '\0'; |
| 695 | if (fraction) { |
| 696 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 697 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 698 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 699 | } else { |
| 700 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 701 | memset(&(*valcopy)[*len], '0', frdigits); |
| 702 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 703 | } |
| 704 | return LY_SUCCESS; |
| 705 | } |
| 706 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 707 | /** |
| 708 | * @brief Check that values in range are in ascendant order. |
| 709 | * @param[in] unsigned_value Flag to note that we are working with unsigned values. |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 710 | * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous, |
| 711 | * max can be also equal. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 712 | * @param[in] value Current value to check. |
| 713 | * @param[in] prev_value The last seen value. |
| 714 | * @return LY_SUCCESS or LY_EEXIST for invalid order. |
| 715 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 716 | static LY_ERR |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 717 | range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 718 | { |
| 719 | if (unsigned_value) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 720 | if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 721 | return LY_EEXIST; |
| 722 | } |
| 723 | } else { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 724 | if ((max && prev_value > value) || (!max && prev_value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 725 | return LY_EEXIST; |
| 726 | } |
| 727 | } |
| 728 | return LY_SUCCESS; |
| 729 | } |
| 730 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 731 | /** |
| 732 | * @brief Set min/max value of the range part. |
| 733 | * @param[in] ctx Compile context. |
| 734 | * @param[in] part Range part structure to fill. |
| 735 | * @param[in] max Flag to distinguish if storing min or max value. |
| 736 | * @param[in] prev The last seen value to check that all values in range are specified in ascendant order. |
| 737 | * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules. |
| 738 | * @param[in] first Flag for the first value of the range to avoid ascendancy order. |
| 739 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 740 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 741 | * @param[in] base_range Range from the type from which the current type is derived (if not built-in) to get type's min and max values. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 742 | * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set. |
| 743 | * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number), |
| 744 | * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the |
| 745 | * frdigits value), LY_EMEM. |
| 746 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 747 | static LY_ERR |
| 748 | range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, int max, int64_t prev, LY_DATA_TYPE basetype, int first, int length_restr, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 749 | uint8_t frdigits, struct lysc_range *base_range, const char **value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 750 | { |
| 751 | LY_ERR ret = LY_SUCCESS; |
| 752 | char *valcopy = NULL; |
| 753 | size_t len; |
| 754 | |
| 755 | if (value) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 756 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 757 | LY_CHECK_GOTO(ret, finalize); |
| 758 | } |
| 759 | if (!valcopy && base_range) { |
| 760 | if (max) { |
| 761 | part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64; |
| 762 | } else { |
| 763 | part->min_64 = base_range->parts[0].min_64; |
| 764 | } |
| 765 | if (!first) { |
| 766 | ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev); |
| 767 | } |
| 768 | goto finalize; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | switch (basetype) { |
| 772 | case LY_TYPE_BINARY: /* length */ |
| 773 | if (valcopy) { |
| 774 | ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64); |
| 775 | } else if (max) { |
| 776 | part->max_u64 = UINT64_C(18446744073709551615); |
| 777 | } else { |
| 778 | part->min_u64 = UINT64_C(0); |
| 779 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 780 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 781 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 782 | } |
| 783 | break; |
| 784 | case LY_TYPE_DEC64: /* range */ |
| 785 | if (valcopy) { |
| 786 | ret = ly_parse_int(valcopy, INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10, |
| 787 | max ? &part->max_64 : &part->min_64); |
| 788 | } else if (max) { |
| 789 | part->max_64 = INT64_C(9223372036854775807); |
| 790 | } else { |
| 791 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 792 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 793 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 794 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 795 | } |
| 796 | break; |
| 797 | case LY_TYPE_INT8: /* range */ |
| 798 | if (valcopy) { |
| 799 | ret = ly_parse_int(valcopy, INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64); |
| 800 | } else if (max) { |
| 801 | part->max_64 = INT64_C(127); |
| 802 | } else { |
| 803 | part->min_64 = INT64_C(-128); |
| 804 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 805 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 806 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 807 | } |
| 808 | break; |
| 809 | case LY_TYPE_INT16: /* range */ |
| 810 | if (valcopy) { |
| 811 | ret = ly_parse_int(valcopy, INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64); |
| 812 | } else if (max) { |
| 813 | part->max_64 = INT64_C(32767); |
| 814 | } else { |
| 815 | part->min_64 = INT64_C(-32768); |
| 816 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 817 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 818 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 819 | } |
| 820 | break; |
| 821 | case LY_TYPE_INT32: /* range */ |
| 822 | if (valcopy) { |
| 823 | ret = ly_parse_int(valcopy, INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64); |
| 824 | } else if (max) { |
| 825 | part->max_64 = INT64_C(2147483647); |
| 826 | } else { |
| 827 | part->min_64 = INT64_C(-2147483648); |
| 828 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 829 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 830 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 831 | } |
| 832 | break; |
| 833 | case LY_TYPE_INT64: /* range */ |
| 834 | if (valcopy) { |
| 835 | ret = ly_parse_int(valcopy, INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10, |
| 836 | max ? &part->max_64 : &part->min_64); |
| 837 | } else if (max) { |
| 838 | part->max_64 = INT64_C(9223372036854775807); |
| 839 | } else { |
| 840 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 841 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 842 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 843 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 844 | } |
| 845 | break; |
| 846 | case LY_TYPE_UINT8: /* range */ |
| 847 | if (valcopy) { |
| 848 | ret = ly_parse_uint(valcopy, UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64); |
| 849 | } else if (max) { |
| 850 | part->max_u64 = UINT64_C(255); |
| 851 | } else { |
| 852 | part->min_u64 = UINT64_C(0); |
| 853 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 854 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 855 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 856 | } |
| 857 | break; |
| 858 | case LY_TYPE_UINT16: /* range */ |
| 859 | if (valcopy) { |
| 860 | ret = ly_parse_uint(valcopy, UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64); |
| 861 | } else if (max) { |
| 862 | part->max_u64 = UINT64_C(65535); |
| 863 | } else { |
| 864 | part->min_u64 = UINT64_C(0); |
| 865 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 866 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 867 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 868 | } |
| 869 | break; |
| 870 | case LY_TYPE_UINT32: /* range */ |
| 871 | if (valcopy) { |
| 872 | ret = ly_parse_uint(valcopy, UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64); |
| 873 | } else if (max) { |
| 874 | part->max_u64 = UINT64_C(4294967295); |
| 875 | } else { |
| 876 | part->min_u64 = UINT64_C(0); |
| 877 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 878 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 879 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 880 | } |
| 881 | break; |
| 882 | case LY_TYPE_UINT64: /* range */ |
| 883 | if (valcopy) { |
| 884 | ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64); |
| 885 | } else if (max) { |
| 886 | part->max_u64 = UINT64_C(18446744073709551615); |
| 887 | } else { |
| 888 | part->min_u64 = UINT64_C(0); |
| 889 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 890 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 891 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 892 | } |
| 893 | break; |
| 894 | case LY_TYPE_STRING: /* length */ |
| 895 | if (valcopy) { |
| 896 | ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64); |
| 897 | } else if (max) { |
| 898 | part->max_u64 = UINT64_C(18446744073709551615); |
| 899 | } else { |
| 900 | part->min_u64 = UINT64_C(0); |
| 901 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 902 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 903 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 904 | } |
| 905 | break; |
| 906 | default: |
| 907 | LOGINT(ctx->ctx); |
| 908 | ret = LY_EINT; |
| 909 | } |
| 910 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 911 | finalize: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 912 | if (ret == LY_EDENIED) { |
| 913 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 914 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 915 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 916 | } else if (ret == LY_EVALID) { |
| 917 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 918 | "Invalid %s restriction - invalid value \"%s\".", |
| 919 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 920 | } else if (ret == LY_EEXIST) { |
| 921 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 922 | "Invalid %s restriction - values are not in ascending order (%s).", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 923 | length_restr ? "length" : "range", |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 924 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 925 | } else if (!ret && value) { |
| 926 | *value = *value + len; |
| 927 | } |
| 928 | free(valcopy); |
| 929 | return ret; |
| 930 | } |
| 931 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 932 | /** |
| 933 | * @brief Compile the parsed range restriction. |
| 934 | * @param[in] ctx Compile context. |
| 935 | * @param[in] range_p Parsed range structure to compile. |
| 936 | * @param[in] basetype Base YANG built-in type of the node with the range restriction. |
| 937 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 938 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 939 | * @param[in] base_range Range restriction of the type from which the current type is derived. The current |
| 940 | * range restriction must be more restrictive than the base_range. |
| 941 | * @param[in,out] range Pointer to the created current range structure. |
| 942 | * @return LY_ERR value. |
| 943 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 944 | static LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 945 | lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, int length_restr, uint8_t frdigits, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 946 | struct lysc_range *base_range, struct lysc_range **range) |
| 947 | { |
| 948 | LY_ERR ret = LY_EVALID; |
| 949 | const char *expr; |
| 950 | struct lysc_range_part *parts = NULL, *part; |
| 951 | int range_expected = 0, uns; |
| 952 | unsigned int parts_done = 0, u, v; |
| 953 | |
| 954 | assert(range); |
| 955 | assert(range_p); |
| 956 | |
| 957 | expr = range_p->arg; |
| 958 | while(1) { |
| 959 | if (isspace(*expr)) { |
| 960 | ++expr; |
| 961 | } else if (*expr == '\0') { |
| 962 | if (range_expected) { |
| 963 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 964 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
| 965 | length_restr ? "length" : "range", range_p->arg); |
| 966 | goto cleanup; |
| 967 | } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) { |
| 968 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 969 | "Invalid %s restriction - unexpected end of the expression (%s).", |
| 970 | length_restr ? "length" : "range", range_p->arg); |
| 971 | goto cleanup; |
| 972 | } |
| 973 | parts_done++; |
| 974 | break; |
| 975 | } else if (!strncmp(expr, "min", 3)) { |
| 976 | if (parts) { |
| 977 | /* min cannot be used elsewhere than in the first part */ |
| 978 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 979 | "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
| 980 | expr - range_p->arg, range_p->arg); |
| 981 | goto cleanup; |
| 982 | } |
| 983 | expr += 3; |
| 984 | |
| 985 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 986 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 987 | part->max_64 = part->min_64; |
| 988 | } else if (*expr == '|') { |
| 989 | if (!parts || range_expected) { |
| 990 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 991 | "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr); |
| 992 | goto cleanup; |
| 993 | } |
| 994 | expr++; |
| 995 | parts_done++; |
| 996 | /* process next part of the expression */ |
| 997 | } else if (!strncmp(expr, "..", 2)) { |
| 998 | expr += 2; |
| 999 | while (isspace(*expr)) { |
| 1000 | expr++; |
| 1001 | } |
| 1002 | if (!parts || LY_ARRAY_SIZE(parts) == parts_done) { |
| 1003 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1004 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
| 1005 | goto cleanup; |
| 1006 | } |
| 1007 | /* continue expecting the upper boundary */ |
| 1008 | range_expected = 1; |
| 1009 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 1010 | /* number */ |
| 1011 | if (range_expected) { |
| 1012 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1013 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1014 | range_expected = 0; |
| 1015 | } else { |
| 1016 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1017 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1018 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1019 | part->max_64 = part->min_64; |
| 1020 | } |
| 1021 | |
| 1022 | /* continue with possible another expression part */ |
| 1023 | } else if (!strncmp(expr, "max", 3)) { |
| 1024 | expr += 3; |
| 1025 | while (isspace(*expr)) { |
| 1026 | expr++; |
| 1027 | } |
| 1028 | if (*expr != '\0') { |
| 1029 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).", |
| 1030 | length_restr ? "length" : "range", expr); |
| 1031 | goto cleanup; |
| 1032 | } |
| 1033 | if (range_expected) { |
| 1034 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1035 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1036 | range_expected = 0; |
| 1037 | } else { |
| 1038 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1039 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1040 | basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1041 | part->min_64 = part->max_64; |
| 1042 | } |
| 1043 | } else { |
| 1044 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).", |
| 1045 | length_restr ? "length" : "range", expr); |
| 1046 | goto cleanup; |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | /* check with the previous range/length restriction */ |
| 1051 | if (base_range) { |
| 1052 | switch (basetype) { |
| 1053 | case LY_TYPE_BINARY: |
| 1054 | case LY_TYPE_UINT8: |
| 1055 | case LY_TYPE_UINT16: |
| 1056 | case LY_TYPE_UINT32: |
| 1057 | case LY_TYPE_UINT64: |
| 1058 | case LY_TYPE_STRING: |
| 1059 | uns = 1; |
| 1060 | break; |
| 1061 | case LY_TYPE_DEC64: |
| 1062 | case LY_TYPE_INT8: |
| 1063 | case LY_TYPE_INT16: |
| 1064 | case LY_TYPE_INT32: |
| 1065 | case LY_TYPE_INT64: |
| 1066 | uns = 0; |
| 1067 | break; |
| 1068 | default: |
| 1069 | LOGINT(ctx->ctx); |
| 1070 | ret = LY_EINT; |
| 1071 | goto cleanup; |
| 1072 | } |
| 1073 | for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) { |
| 1074 | if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) { |
| 1075 | goto baseerror; |
| 1076 | } |
| 1077 | /* current lower bound is not lower than the base */ |
| 1078 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 1079 | /* base has single value */ |
| 1080 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 1081 | /* both lower bounds are the same */ |
| 1082 | if (parts[u].min_64 != parts[u].max_64) { |
| 1083 | /* current continues with a range */ |
| 1084 | goto baseerror; |
| 1085 | } else { |
| 1086 | /* equal single values, move both forward */ |
| 1087 | ++v; |
| 1088 | continue; |
| 1089 | } |
| 1090 | } else { |
| 1091 | /* base is single value lower than current range, so the |
| 1092 | * value from base range is removed in the current, |
| 1093 | * move only base and repeat checking */ |
| 1094 | ++v; |
| 1095 | --u; |
| 1096 | continue; |
| 1097 | } |
| 1098 | } else { |
| 1099 | /* base is the range */ |
| 1100 | if (parts[u].min_64 == parts[u].max_64) { |
| 1101 | /* current is a single value */ |
| 1102 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1103 | /* current is behind the base range, so base range is omitted, |
| 1104 | * move the base and keep the current for further check */ |
| 1105 | ++v; |
| 1106 | --u; |
| 1107 | } /* else it is within the base range, so move the current, but keep the base */ |
| 1108 | continue; |
| 1109 | } else { |
| 1110 | /* both are ranges - check the higher bound, the lower was already checked */ |
| 1111 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1112 | /* higher bound is higher than the current higher bound */ |
| 1113 | if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) { |
| 1114 | /* but the current lower bound is also higher, so the base range is omitted, |
| 1115 | * continue with the same current, but move the base */ |
| 1116 | --u; |
| 1117 | ++v; |
| 1118 | continue; |
| 1119 | } |
| 1120 | /* current range starts within the base range but end behind it */ |
| 1121 | goto baseerror; |
| 1122 | } else { |
| 1123 | /* current range is smaller than the base, |
| 1124 | * move current, but stay with the base */ |
| 1125 | continue; |
| 1126 | } |
| 1127 | } |
| 1128 | } |
| 1129 | } |
| 1130 | if (u != parts_done) { |
| 1131 | baseerror: |
| 1132 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1133 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
| 1134 | length_restr ? "length" : "range", range_p->arg); |
| 1135 | goto cleanup; |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | if (!(*range)) { |
| 1140 | *range = calloc(1, sizeof **range); |
| 1141 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 1142 | } |
| 1143 | |
| 1144 | if (range_p->eapptag) { |
| 1145 | lydict_remove(ctx->ctx, (*range)->eapptag); |
| 1146 | (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0); |
| 1147 | } |
| 1148 | if (range_p->emsg) { |
| 1149 | lydict_remove(ctx->ctx, (*range)->emsg); |
| 1150 | (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0); |
| 1151 | } |
| 1152 | /* extensions are taken only from the last range by the caller */ |
| 1153 | |
| 1154 | (*range)->parts = parts; |
| 1155 | parts = NULL; |
| 1156 | ret = LY_SUCCESS; |
| 1157 | cleanup: |
| 1158 | /* TODO clean up */ |
| 1159 | LY_ARRAY_FREE(parts); |
| 1160 | |
| 1161 | return ret; |
| 1162 | } |
| 1163 | |
| 1164 | /** |
| 1165 | * @brief Checks pattern syntax. |
| 1166 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1167 | * @param[in] ctx Compile context. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1168 | * @param[in] pattern Pattern to check. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1169 | * @param[in,out] pcre_precomp Precompiled PCRE pattern. If NULL, the compiled information used to validate pattern are freed. |
| 1170 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1171 | */ |
| 1172 | static LY_ERR |
| 1173 | lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre **pcre_precomp) |
| 1174 | { |
| 1175 | int idx, idx2, start, end, err_offset, count; |
| 1176 | char *perl_regex, *ptr; |
| 1177 | const char *err_msg, *orig_ptr; |
| 1178 | pcre *precomp; |
| 1179 | #define URANGE_LEN 19 |
| 1180 | char *ublock2urange[][2] = { |
| 1181 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 1182 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 1183 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 1184 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 1185 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 1186 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 1187 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 1188 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 1189 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 1190 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 1191 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 1192 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 1193 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 1194 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 1195 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 1196 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 1197 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 1198 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 1199 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 1200 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 1201 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 1202 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 1203 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 1204 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 1205 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 1206 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 1207 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 1208 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 1209 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 1210 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 1211 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 1212 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 1213 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 1214 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 1215 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 1216 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 1217 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 1218 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 1219 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 1220 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 1221 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 1222 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 1223 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 1224 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 1225 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 1226 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 1227 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 1228 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 1229 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 1230 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 1231 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 1232 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 1233 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 1234 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 1235 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 1236 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 1237 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 1238 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 1239 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 1240 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 1241 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 1242 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 1243 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 1244 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 1245 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 1246 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 1247 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 1248 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 1249 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 1250 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 1251 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 1252 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 1253 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 1254 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 1255 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 1256 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 1257 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 1258 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 1259 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 1260 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 1261 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 1262 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 1263 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
| 1264 | {NULL, NULL} |
| 1265 | }; |
| 1266 | |
| 1267 | /* adjust the expression to a Perl equivalent |
| 1268 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 1269 | |
| 1270 | /* we need to replace all "$" with "\$", count them now */ |
| 1271 | for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$")); |
| 1272 | |
| 1273 | perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char)); |
| 1274 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM); |
| 1275 | perl_regex[0] = '\0'; |
| 1276 | |
| 1277 | ptr = perl_regex; |
| 1278 | |
| 1279 | if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) { |
| 1280 | /* we will add line-end anchoring */ |
| 1281 | ptr[0] = '('; |
| 1282 | ++ptr; |
| 1283 | } |
| 1284 | |
| 1285 | for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) { |
| 1286 | if (orig_ptr[0] == '$') { |
| 1287 | ptr += sprintf(ptr, "\\$"); |
| 1288 | } else if (orig_ptr[0] == '^') { |
| 1289 | ptr += sprintf(ptr, "\\^"); |
| 1290 | } else { |
| 1291 | ptr[0] = orig_ptr[0]; |
| 1292 | ++ptr; |
| 1293 | } |
| 1294 | } |
| 1295 | |
| 1296 | if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) { |
| 1297 | ptr += sprintf(ptr, ")$"); |
| 1298 | } else { |
| 1299 | ptr[0] = '\0'; |
| 1300 | ++ptr; |
| 1301 | } |
| 1302 | |
| 1303 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 1304 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 1305 | start = ptr - perl_regex; |
| 1306 | |
| 1307 | ptr = strchr(ptr, '}'); |
| 1308 | if (!ptr) { |
| 1309 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, |
| 1310 | pattern, perl_regex + start + 2, "unterminated character property"); |
| 1311 | free(perl_regex); |
| 1312 | return LY_EVALID; |
| 1313 | } |
| 1314 | end = (ptr - perl_regex) + 1; |
| 1315 | |
| 1316 | /* need more space */ |
| 1317 | if (end - start < URANGE_LEN) { |
| 1318 | perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1); |
| 1319 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM); |
| 1320 | } |
| 1321 | |
| 1322 | /* find our range */ |
| 1323 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 1324 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 1325 | break; |
| 1326 | } |
| 1327 | } |
| 1328 | if (!ublock2urange[idx][0]) { |
| 1329 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, |
| 1330 | pattern, perl_regex + start + 5, "unknown block name"); |
| 1331 | free(perl_regex); |
| 1332 | return LY_EVALID; |
| 1333 | } |
| 1334 | |
| 1335 | /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */ |
| 1336 | for (idx2 = 0, count = 0; idx2 < start; ++idx2) { |
| 1337 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 1338 | ++count; |
| 1339 | } |
| 1340 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 1341 | --count; |
| 1342 | } |
| 1343 | } |
| 1344 | if (count) { |
| 1345 | /* skip brackets */ |
| 1346 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 1347 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 1348 | } else { |
| 1349 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 1350 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 1351 | } |
| 1352 | } |
| 1353 | |
| 1354 | /* must return 0, already checked during parsing */ |
| 1355 | precomp = pcre_compile(perl_regex, PCRE_UTF8 | PCRE_ANCHORED | PCRE_DOLLAR_ENDONLY | PCRE_NO_AUTO_CAPTURE, |
| 1356 | &err_msg, &err_offset, NULL); |
| 1357 | if (!precomp) { |
| 1358 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg); |
| 1359 | free(perl_regex); |
| 1360 | return LY_EVALID; |
| 1361 | } |
| 1362 | free(perl_regex); |
| 1363 | |
| 1364 | if (pcre_precomp) { |
| 1365 | *pcre_precomp = precomp; |
| 1366 | } else { |
| 1367 | free(precomp); |
| 1368 | } |
| 1369 | |
| 1370 | return LY_SUCCESS; |
| 1371 | |
| 1372 | #undef URANGE_LEN |
| 1373 | } |
| 1374 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1375 | /** |
| 1376 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 1377 | * @param[in] ctx Compile context. |
| 1378 | * @param[in] patterns_p Array of parsed patterns from the current type to compile. |
| 1379 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 1380 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 1381 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 1382 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 1383 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 1384 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1385 | static LY_ERR |
| 1386 | lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p, int options, |
| 1387 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
| 1388 | { |
| 1389 | struct lysc_pattern **pattern; |
| 1390 | unsigned int u, v; |
| 1391 | const char *err_msg; |
| 1392 | LY_ERR ret = LY_SUCCESS; |
| 1393 | |
| 1394 | /* first, copy the patterns from the base type */ |
| 1395 | if (base_patterns) { |
| 1396 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 1397 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 1398 | } |
| 1399 | |
| 1400 | LY_ARRAY_FOR(patterns_p, u) { |
| 1401 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 1402 | *pattern = calloc(1, sizeof **pattern); |
| 1403 | ++(*pattern)->refcount; |
| 1404 | |
| 1405 | ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->expr); |
| 1406 | LY_CHECK_RET(ret); |
| 1407 | (*pattern)->expr_extra = pcre_study((*pattern)->expr, 0, &err_msg); |
| 1408 | if (err_msg) { |
| 1409 | LOGWRN(ctx->ctx, "Studying pattern \"%s\" failed (%s).", pattern, err_msg); |
| 1410 | } |
| 1411 | |
| 1412 | if (patterns_p[u].arg[0] == 0x15) { |
| 1413 | (*pattern)->inverted = 1; |
| 1414 | } |
| 1415 | DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag); |
| 1416 | DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg); |
| 1417 | COMPILE_ARRAY_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, |
| 1418 | options, v, lys_compile_ext, ret, done); |
| 1419 | } |
| 1420 | done: |
| 1421 | return ret; |
| 1422 | } |
| 1423 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1424 | /** |
| 1425 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 1426 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1427 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 1428 | 0 /* LY_TYPE_UNKNOWN */, |
| 1429 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1430 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 1431 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 1432 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 1433 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 1434 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1435 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 1436 | 0 /* LY_TYPE_BOOL */, |
| 1437 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 1438 | 0 /* LY_TYPE_EMPTY */, |
| 1439 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 1440 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 1441 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 1442 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1443 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 1444 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1445 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1446 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1447 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 1448 | }; |
| 1449 | |
| 1450 | /** |
| 1451 | * @brief stringification of the YANG built-in data types |
| 1452 | */ |
| 1453 | const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer", |
| 1454 | "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration", |
| 1455 | "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer" |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1456 | }; |
| 1457 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1458 | /** |
| 1459 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 1460 | * @param[in] ctx Compile context. |
| 1461 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 1462 | * @param[in] basetype Base YANG built-in type from which the current type is derived. Only LY_TYPE_ENUM and LY_TYPE_BITS are expected. |
| 1463 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 1464 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
| 1465 | * @param[out] enums Newly created array of the compiled enums information for the current type. |
| 1466 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1467 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1468 | static LY_ERR |
| 1469 | lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype, int options, |
| 1470 | struct lysc_type_enum_item *base_enums, struct lysc_type_enum_item **enums) |
| 1471 | { |
| 1472 | LY_ERR ret = LY_SUCCESS; |
| 1473 | unsigned int u, v, match; |
| 1474 | int32_t value = 0; |
| 1475 | uint32_t position = 0; |
| 1476 | struct lysc_type_enum_item *e, storage; |
| 1477 | |
| 1478 | if (base_enums && ctx->mod->compiled->version < 2) { |
| 1479 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
| 1480 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
| 1481 | return LY_EVALID; |
| 1482 | } |
| 1483 | |
| 1484 | LY_ARRAY_FOR(enums_p, u) { |
| 1485 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
| 1486 | DUP_STRING(ctx->ctx, enums_p[u].name, e->name); |
| 1487 | if (base_enums) { |
| 1488 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 1489 | LY_ARRAY_FOR(base_enums, v) { |
| 1490 | if (!strcmp(e->name, base_enums[v].name)) { |
| 1491 | break; |
| 1492 | } |
| 1493 | } |
| 1494 | if (v == LY_ARRAY_SIZE(base_enums)) { |
| 1495 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1496 | "Invalid %s - derived type adds new item \"%s\".", |
| 1497 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
| 1498 | return LY_EVALID; |
| 1499 | } |
| 1500 | match = v; |
| 1501 | } |
| 1502 | |
| 1503 | if (basetype == LY_TYPE_ENUM) { |
| 1504 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 1505 | e->value = (int32_t)enums_p[u].value; |
| 1506 | if (!u || e->value >= value) { |
| 1507 | value = e->value + 1; |
| 1508 | } |
| 1509 | /* check collision with other values */ |
| 1510 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 1511 | if (e->value == (*enums)[v].value) { |
| 1512 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1513 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 1514 | e->value, e->name, (*enums)[v].name); |
| 1515 | return LY_EVALID; |
| 1516 | } |
| 1517 | } |
| 1518 | } else if (base_enums) { |
| 1519 | /* inherit the assigned value */ |
| 1520 | e->value = base_enums[match].value; |
| 1521 | if (!u || e->value >= value) { |
| 1522 | value = e->value + 1; |
| 1523 | } |
| 1524 | } else { |
| 1525 | /* assign value automatically */ |
| 1526 | if (u && value == INT32_MIN) { |
| 1527 | /* counter overflow */ |
| 1528 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1529 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 1530 | "\"%s\" since the highest value is already 2147483647.", e->name); |
| 1531 | return LY_EVALID; |
| 1532 | } |
| 1533 | e->value = value++; |
| 1534 | } |
| 1535 | } else { /* LY_TYPE_BITS */ |
| 1536 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 1537 | e->value = (int32_t)enums_p[u].value; |
| 1538 | if (!u || (uint32_t)e->value >= position) { |
| 1539 | position = (uint32_t)e->value + 1; |
| 1540 | } |
| 1541 | /* check collision with other values */ |
| 1542 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 1543 | if (e->value == (*enums)[v].value) { |
| 1544 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1545 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
| 1546 | (uint32_t)e->value, e->name, (*enums)[v].name); |
| 1547 | return LY_EVALID; |
| 1548 | } |
| 1549 | } |
| 1550 | } else if (base_enums) { |
| 1551 | /* inherit the assigned value */ |
| 1552 | e->value = base_enums[match].value; |
| 1553 | if (!u || (uint32_t)e->value >= position) { |
| 1554 | position = (uint32_t)e->value + 1; |
| 1555 | } |
| 1556 | } else { |
| 1557 | /* assign value automatically */ |
| 1558 | if (u && position == 0) { |
| 1559 | /* counter overflow */ |
| 1560 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1561 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 1562 | "\"%s\" since the highest value is already 4294967295.", e->name); |
| 1563 | return LY_EVALID; |
| 1564 | } |
| 1565 | e->value = position++; |
| 1566 | } |
| 1567 | } |
| 1568 | |
| 1569 | if (base_enums) { |
| 1570 | /* the assigned values must not change from the derived type */ |
| 1571 | if (e->value != base_enums[match].value) { |
| 1572 | if (basetype == LY_TYPE_ENUM) { |
| 1573 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1574 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
| 1575 | e->name, base_enums[match].value, e->value); |
| 1576 | } else { |
| 1577 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1578 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 1579 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
| 1580 | } |
| 1581 | return LY_EVALID; |
| 1582 | } |
| 1583 | } |
| 1584 | |
| 1585 | COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, options, v, lys_compile_iffeature, ret, done); |
| 1586 | COMPILE_ARRAY_GOTO(ctx, enums_p[u].exts, e->exts, options, v, lys_compile_ext, ret, done); |
| 1587 | |
| 1588 | if (basetype == LY_TYPE_BITS) { |
| 1589 | /* keep bits ordered by position */ |
| 1590 | for (v = u; v && (*enums)[v - 1].value > e->value; --v); |
| 1591 | if (v != u) { |
| 1592 | memcpy(&storage, e, sizeof *e); |
| 1593 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 1594 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 1595 | } |
| 1596 | } |
| 1597 | } |
| 1598 | |
| 1599 | done: |
| 1600 | return ret; |
| 1601 | } |
| 1602 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1603 | #define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \ |
| 1604 | for ((NODE) = (NODE)->parent; \ |
| 1605 | (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \ |
| 1606 | (NODE) = (NODE)->parent); \ |
| 1607 | if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \ |
| 1608 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \ |
| 1609 | TERM; \ |
| 1610 | } |
| 1611 | |
| 1612 | /** |
| 1613 | * @brief Validate the predicate(s) from the leafref path. |
| 1614 | * @param[in] ctx Compile context |
| 1615 | * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s). |
| 1616 | * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated. |
| 1617 | * @param[in] context_node Predicate context node (where the predicate is placed). |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1618 | * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1619 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1620 | */ |
| 1621 | static LY_ERR |
| 1622 | lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1623 | const struct lysc_node *context_node, const struct lys_module *path_context) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1624 | { |
| 1625 | LY_ERR ret = LY_EVALID; |
| 1626 | const struct lys_module *mod; |
| 1627 | const struct lysc_node *src_node, *dst_node; |
| 1628 | const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix; |
| 1629 | size_t src_len, src_prefix_len, dst_len, dst_prefix_len; |
| 1630 | unsigned int dest_parent_times; |
| 1631 | const char *start, *end, *pke_end; |
| 1632 | struct ly_set keys = {0}; |
| 1633 | int i; |
| 1634 | |
| 1635 | while (**predicate == '[') { |
| 1636 | start = (*predicate)++; |
| 1637 | |
| 1638 | while (isspace(**predicate)) { |
| 1639 | ++(*predicate); |
| 1640 | } |
| 1641 | LY_CHECK_GOTO(lys_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup); |
| 1642 | while (isspace(**predicate)) { |
| 1643 | ++(*predicate); |
| 1644 | } |
| 1645 | if (**predicate != '=') { |
| 1646 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1647 | "Invalid leafref path predicate \"%.*s\" - missing \"=\".", *predicate - start + 1, *predicate); |
| 1648 | goto cleanup; |
| 1649 | } |
| 1650 | ++(*predicate); |
| 1651 | while (isspace(**predicate)) { |
| 1652 | ++(*predicate); |
| 1653 | } |
| 1654 | |
| 1655 | if ((end = pke_end = strchr(*predicate, ']')) == NULL) { |
| 1656 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1657 | "Invalid leafref path predicate \"%s\" - missing predicate termination.", start); |
| 1658 | goto cleanup; |
| 1659 | } |
| 1660 | --pke_end; |
| 1661 | while (isspace(*pke_end)) { |
| 1662 | --pke_end; |
| 1663 | } |
| 1664 | /* localize path-key-expr */ |
| 1665 | pke_start = path_key_expr = *predicate; |
| 1666 | /* move after the current predicate */ |
| 1667 | *predicate = end + 1; |
| 1668 | |
| 1669 | /* source (must be leaf or leaf-list) */ |
| 1670 | if (src_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1671 | mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1672 | } else { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1673 | mod = path_context; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1674 | } |
| 1675 | src_node = lys_child(context_node, mod, src, src_len, |
| 1676 | mod->compiled->version < LYS_VERSION_1_1 ? LYS_LEAF : LYS_LEAF | LYS_LEAFLIST, LYS_GETNEXT_NOSTATECHECK); |
| 1677 | if (!src_node) { |
| 1678 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1679 | "Invalid leafref path predicate \"%.*s\" - key node \"%.*s\" from module \"%s\" not found.", |
| 1680 | *predicate - start, start, src_len, src, mod->compiled->name); |
| 1681 | goto cleanup; |
| 1682 | } |
| 1683 | /* TODO - check the src_node is really a key of the context_node */ |
| 1684 | |
| 1685 | /* check that there is only one predicate for the */ |
| 1686 | i = ly_set_add(&keys, (void*)src_node, 0); |
| 1687 | LY_CHECK_GOTO(i == -1, cleanup); |
| 1688 | if (keys.count != (unsigned int)i + 1) { |
| 1689 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1690 | "Invalid leafref path predicate \"%.*s\" - multiple equality test for the key %s.", |
| 1691 | *predicate - start, start, src_node->name); |
| 1692 | goto cleanup; |
| 1693 | } |
| 1694 | |
| 1695 | /* destination */ |
| 1696 | dest_parent_times = 0; |
| 1697 | dst_node = context_node; |
| 1698 | |
| 1699 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
| 1700 | if (strncmp(path_key_expr, "current()", 9)) { |
| 1701 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1702 | "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.", |
| 1703 | *predicate - start, start); |
| 1704 | goto cleanup; |
| 1705 | } |
| 1706 | path_key_expr += 9; |
| 1707 | while (isspace(*path_key_expr)) { |
| 1708 | ++path_key_expr; |
| 1709 | } |
| 1710 | |
| 1711 | if (*path_key_expr != '/') { |
| 1712 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1713 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.", |
| 1714 | *predicate - start, start); |
| 1715 | goto cleanup; |
| 1716 | } |
| 1717 | ++path_key_expr; |
| 1718 | while (isspace(*path_key_expr)) { |
| 1719 | ++path_key_expr; |
| 1720 | } |
| 1721 | |
| 1722 | /* rel-path-keyexpr: |
| 1723 | * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */ |
| 1724 | while (!strncmp(path_key_expr, "..", 2)) { |
| 1725 | ++dest_parent_times; |
| 1726 | path_key_expr += 2; |
| 1727 | while (isspace(*path_key_expr)) { |
| 1728 | ++path_key_expr; |
| 1729 | } |
| 1730 | if (*path_key_expr != '/') { |
| 1731 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1732 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.", |
| 1733 | *predicate - start, start); |
| 1734 | goto cleanup; |
| 1735 | } |
| 1736 | ++path_key_expr; |
| 1737 | while (isspace(*path_key_expr)) { |
| 1738 | ++path_key_expr; |
| 1739 | } |
| 1740 | |
| 1741 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 1742 | * all schema nodes that cannot be instantiated in data tree */ |
| 1743 | MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup, |
| 1744 | "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.", |
| 1745 | *predicate - start, start); |
| 1746 | } |
| 1747 | if (!dest_parent_times) { |
| 1748 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1749 | "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.", |
| 1750 | *predicate - start, start); |
| 1751 | goto cleanup; |
| 1752 | } |
| 1753 | if (path_key_expr == pke_end) { |
| 1754 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1755 | "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.", |
| 1756 | *predicate - start, start); |
| 1757 | goto cleanup; |
| 1758 | } |
| 1759 | |
| 1760 | while(path_key_expr != pke_end) { |
| 1761 | if (lys_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) { |
| 1762 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1763 | "Invalid node identifier in leafref path predicate - character %d (%.*s).", |
| 1764 | path_key_expr - start, *predicate - start, start); |
| 1765 | goto cleanup; |
| 1766 | } |
| 1767 | |
| 1768 | if (dst_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1769 | mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1770 | } else { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1771 | mod = path_context; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1772 | } |
| 1773 | if (!mod) { |
| 1774 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1775 | "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path_keyexpr.", |
| 1776 | *predicate - start, start, dst_len, dst); |
| 1777 | goto cleanup; |
| 1778 | } |
| 1779 | |
| 1780 | dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK); |
| 1781 | if (!dst_node) { |
| 1782 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1783 | "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path_keyexpr.", |
| 1784 | *predicate - start, start, path_key_expr - pke_start, pke_start); |
| 1785 | goto cleanup; |
| 1786 | } |
| 1787 | } |
| 1788 | if (!(dst_node->nodetype & (dst_node->module->compiled->version < LYS_VERSION_1_1 ? LYS_LEAF : LYS_LEAF | LYS_LEAFLIST))) { |
| 1789 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1790 | "Invalid leafref path predicate \"%.*s\" - rel-path_keyexpr \"%.*s\" refers %s.", |
| 1791 | *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype)); |
| 1792 | goto cleanup; |
| 1793 | } |
| 1794 | } |
| 1795 | |
| 1796 | ret = LY_SUCCESS; |
| 1797 | cleanup: |
| 1798 | ly_set_erase(&keys, NULL); |
| 1799 | return ret; |
| 1800 | } |
| 1801 | |
| 1802 | /** |
| 1803 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 1804 | * |
| 1805 | * path-arg = absolute-path / relative-path |
| 1806 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 1807 | * relative-path = 1*(".." "/") descendant-path |
| 1808 | * |
| 1809 | * @param[in,out] path Path to parse. |
| 1810 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 1811 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 1812 | * @param[out] name Name of the token. |
| 1813 | * @param[out] nam_len Length of the name. |
| 1814 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 1815 | * must not be changed between consecutive calls. -1 if the |
| 1816 | * path is absolute. |
| 1817 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 1818 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 1819 | */ |
| 1820 | static LY_ERR |
| 1821 | lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len, |
| 1822 | int *parent_times, int *has_predicate) |
| 1823 | { |
| 1824 | int par_times = 0; |
| 1825 | |
| 1826 | assert(path && *path); |
| 1827 | assert(parent_times); |
| 1828 | assert(prefix); |
| 1829 | assert(prefix_len); |
| 1830 | assert(name); |
| 1831 | assert(name_len); |
| 1832 | assert(has_predicate); |
| 1833 | |
| 1834 | *prefix = NULL; |
| 1835 | *prefix_len = 0; |
| 1836 | *name = NULL; |
| 1837 | *name_len = 0; |
| 1838 | *has_predicate = 0; |
| 1839 | |
| 1840 | if (!*parent_times) { |
| 1841 | if (!strncmp(*path, "..", 2)) { |
| 1842 | *path += 2; |
| 1843 | ++par_times; |
| 1844 | while (!strncmp(*path, "/..", 3)) { |
| 1845 | *path += 3; |
| 1846 | ++par_times; |
| 1847 | } |
| 1848 | } |
| 1849 | if (par_times) { |
| 1850 | *parent_times = par_times; |
| 1851 | } else { |
| 1852 | *parent_times = -1; |
| 1853 | } |
| 1854 | } |
| 1855 | |
| 1856 | if (**path != '/') { |
| 1857 | return LY_EINVAL; |
| 1858 | } |
| 1859 | /* skip '/' */ |
| 1860 | ++(*path); |
| 1861 | |
| 1862 | /* node-identifier ([prefix:]name) */ |
| 1863 | LY_CHECK_RET(lys_parse_nodeid(path, prefix, prefix_len, name, name_len)); |
| 1864 | |
| 1865 | if ((**path == '/' && (*path)[1]) || !**path) { |
| 1866 | /* path continues by another token or this is the last token */ |
| 1867 | return LY_SUCCESS; |
| 1868 | } else if ((*path)[0] != '[') { |
| 1869 | /* unexpected character */ |
| 1870 | return LY_EINVAL; |
| 1871 | } else { |
| 1872 | /* predicate starting with [ */ |
| 1873 | *has_predicate = 1; |
| 1874 | return LY_SUCCESS; |
| 1875 | } |
| 1876 | } |
| 1877 | |
| 1878 | /** |
| 1879 | * @brief Validate the leafref path. |
| 1880 | * @param[in] ctx Compile context |
| 1881 | * @param[in] startnode Path context node (where the leafref path begins/is placed). |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame^] | 1882 | * @param[in] leafref Leafref to validate. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1883 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1884 | */ |
| 1885 | static LY_ERR |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame^] | 1886 | lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1887 | { |
| 1888 | const struct lysc_node *node = NULL, *parent = NULL; |
| 1889 | const struct lys_module *mod; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame^] | 1890 | struct lysc_type *type; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1891 | const char *id, *prefix, *name; |
| 1892 | size_t prefix_len, name_len; |
| 1893 | int parent_times = 0, has_predicate; |
| 1894 | unsigned int iter, u; |
| 1895 | LY_ERR ret = LY_SUCCESS; |
| 1896 | |
| 1897 | assert(ctx); |
| 1898 | assert(startnode); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame^] | 1899 | assert(leafref); |
| 1900 | |
| 1901 | /* TODO leafref targets may be not implemented, in such a case we actually could make (we did it in libyang1) such a models implemented */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1902 | |
| 1903 | iter = 0; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame^] | 1904 | id = leafref->path; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1905 | while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) { |
| 1906 | if (!iter) { /* first iteration */ |
| 1907 | /* precess ".." in relative paths */ |
| 1908 | if (parent_times > 0) { |
| 1909 | /* move from the context node */ |
| 1910 | for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) { |
| 1911 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 1912 | * all schema nodes that cannot be instantiated in data tree */ |
| 1913 | MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame^] | 1914 | "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1915 | } |
| 1916 | } |
| 1917 | } |
| 1918 | |
| 1919 | if (prefix) { |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame^] | 1920 | mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1921 | } else { |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame^] | 1922 | mod = leafref->path_context; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1923 | } |
| 1924 | if (!mod) { |
| 1925 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame^] | 1926 | "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".", |
| 1927 | id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1928 | return LY_EVALID; |
| 1929 | } |
| 1930 | |
| 1931 | node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK); |
| 1932 | if (!node) { |
| 1933 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame^] | 1934 | "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1935 | return LY_EVALID; |
| 1936 | } |
| 1937 | parent = node; |
| 1938 | |
| 1939 | if (has_predicate) { |
| 1940 | /* we have predicate, so the current result must be list */ |
| 1941 | if (node->nodetype != LYS_LIST) { |
| 1942 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1943 | "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.", |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame^] | 1944 | id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1945 | return LY_EVALID; |
| 1946 | } |
| 1947 | |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame^] | 1948 | LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, node, leafref->path_context), LY_EVALID); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1949 | } |
| 1950 | |
| 1951 | ++iter; |
| 1952 | } |
| 1953 | if (ret) { |
| 1954 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame^] | 1955 | "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1956 | return LY_EVALID; |
| 1957 | } |
| 1958 | |
| 1959 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 1960 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1961 | "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.", |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame^] | 1962 | leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1963 | return LY_EVALID; |
| 1964 | } |
| 1965 | |
| 1966 | /* check status */ |
| 1967 | if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) { |
| 1968 | return LY_EVALID; |
| 1969 | } |
| 1970 | |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame^] | 1971 | /* store the target's type and check for circular chain of leafrefs */ |
| 1972 | leafref->realtype = ((struct lysc_node_leaf*)node)->type; |
| 1973 | for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) { |
| 1974 | if (type == (struct lysc_type*)leafref) { |
| 1975 | /* circular chain detected */ |
| 1976 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1977 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path); |
| 1978 | return LY_EVALID; |
| 1979 | } |
| 1980 | } |
| 1981 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1982 | return LY_SUCCESS; |
| 1983 | } |
| 1984 | |
| 1985 | /** |
| 1986 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 1987 | * @param[in] ctx Compile context. |
| 1988 | * @param[in] type_p Parsed type to compile. |
| 1989 | * @param[in] basetype Base YANG built-in type of the type to compile. |
| 1990 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 1991 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 1992 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 1993 | * @param[out] type Newly created type structure with the filled information about the type. |
| 1994 | * @return LY_ERR value. |
| 1995 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1996 | static LY_ERR |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1997 | lys_compile_type_(struct lysc_ctx *ctx, struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, int options, |
| 1998 | const char *tpdfname, struct lysc_type *base, struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 1999 | { |
| 2000 | LY_ERR ret = LY_SUCCESS; |
| 2001 | unsigned int u; |
| 2002 | struct lysc_type_bin *bin; |
| 2003 | struct lysc_type_num *num; |
| 2004 | struct lysc_type_str *str; |
| 2005 | struct lysc_type_bits *bits; |
| 2006 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2007 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2008 | struct lysc_type_identityref *idref; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2009 | |
| 2010 | switch (basetype) { |
| 2011 | case LY_TYPE_BINARY: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2012 | bin = (struct lysc_type_bin*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2013 | |
| 2014 | /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2015 | if (type_p->length) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2016 | ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2017 | base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length); |
| 2018 | LY_CHECK_RET(ret); |
| 2019 | if (!tpdfname) { |
| 2020 | COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts, |
| 2021 | options, u, lys_compile_ext, ret, done); |
| 2022 | } |
| 2023 | } |
| 2024 | |
| 2025 | if (tpdfname) { |
| 2026 | type_p->compiled = *type; |
| 2027 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2028 | } |
| 2029 | break; |
| 2030 | case LY_TYPE_BITS: |
| 2031 | /* RFC 7950 9.7 - bits */ |
| 2032 | bits = (struct lysc_type_bits*)(*type); |
| 2033 | if (type_p->bits) { |
| 2034 | ret = lys_compile_type_enums(ctx, type_p->bits, basetype, options, |
| 2035 | base ? (struct lysc_type_enum_item*)((struct lysc_type_bits*)base)->bits : NULL, |
| 2036 | (struct lysc_type_enum_item**)&bits->bits); |
| 2037 | LY_CHECK_RET(ret); |
| 2038 | } |
| 2039 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2040 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2041 | /* type derived from bits built-in type must contain at least one bit */ |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2042 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2043 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2044 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2045 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", ""); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2046 | free(*type); |
| 2047 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2048 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2049 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | if (tpdfname) { |
| 2053 | type_p->compiled = *type; |
| 2054 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2055 | } |
| 2056 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2057 | case LY_TYPE_DEC64: |
| 2058 | dec = (struct lysc_type_dec*)(*type); |
| 2059 | |
| 2060 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2061 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2062 | if (!type_p->fraction_digits) { |
| 2063 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2064 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname); |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2065 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2066 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", ""); |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2067 | free(*type); |
| 2068 | *type = NULL; |
| 2069 | } |
| 2070 | return LY_EVALID; |
| 2071 | } |
| 2072 | } else if (type_p->fraction_digits) { |
| 2073 | /* fraction digits is prohibited in types not directly derived from built-in decimal64 */ |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2074 | if (tpdfname) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2075 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2076 | "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2077 | tpdfname); |
| 2078 | } else { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2079 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2080 | "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type."); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2081 | free(*type); |
| 2082 | *type = NULL; |
| 2083 | } |
| 2084 | return LY_EVALID; |
| 2085 | } |
| 2086 | dec->fraction_digits = type_p->fraction_digits; |
| 2087 | |
| 2088 | /* RFC 7950 9.2.4 - range */ |
| 2089 | if (type_p->range) { |
| 2090 | ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits, |
| 2091 | base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range); |
| 2092 | LY_CHECK_RET(ret); |
| 2093 | if (!tpdfname) { |
| 2094 | COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts, |
| 2095 | options, u, lys_compile_ext, ret, done); |
| 2096 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2097 | } |
| 2098 | |
| 2099 | if (tpdfname) { |
| 2100 | type_p->compiled = *type; |
| 2101 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2102 | } |
| 2103 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2104 | case LY_TYPE_STRING: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2105 | str = (struct lysc_type_str*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2106 | |
| 2107 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2108 | if (type_p->length) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2109 | ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2110 | base ? ((struct lysc_type_str*)base)->length : NULL, &str->length); |
| 2111 | LY_CHECK_RET(ret); |
| 2112 | if (!tpdfname) { |
| 2113 | COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts, |
| 2114 | options, u, lys_compile_ext, ret, done); |
| 2115 | } |
| 2116 | } else if (base && ((struct lysc_type_str*)base)->length) { |
| 2117 | str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length); |
| 2118 | } |
| 2119 | |
| 2120 | /* RFC 7950 9.4.5 - pattern */ |
| 2121 | if (type_p->patterns) { |
| 2122 | ret = lys_compile_type_patterns(ctx, type_p->patterns, options, |
| 2123 | base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns); |
| 2124 | LY_CHECK_RET(ret); |
| 2125 | } else if (base && ((struct lysc_type_str*)base)->patterns) { |
| 2126 | str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns); |
| 2127 | } |
| 2128 | |
| 2129 | if (tpdfname) { |
| 2130 | type_p->compiled = *type; |
| 2131 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2132 | } |
| 2133 | break; |
| 2134 | case LY_TYPE_ENUM: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2135 | enumeration = (struct lysc_type_enum*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2136 | |
| 2137 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2138 | if (type_p->enums) { |
| 2139 | ret = lys_compile_type_enums(ctx, type_p->enums, basetype, options, |
| 2140 | base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums); |
| 2141 | LY_CHECK_RET(ret); |
| 2142 | } |
| 2143 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2144 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2145 | /* type derived from enumerations built-in type must contain at least one enum */ |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2146 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2147 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2148 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2149 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", ""); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2150 | free(*type); |
| 2151 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2152 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2153 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2154 | } |
| 2155 | |
| 2156 | if (tpdfname) { |
| 2157 | type_p->compiled = *type; |
| 2158 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2159 | } |
| 2160 | break; |
| 2161 | case LY_TYPE_INT8: |
| 2162 | case LY_TYPE_UINT8: |
| 2163 | case LY_TYPE_INT16: |
| 2164 | case LY_TYPE_UINT16: |
| 2165 | case LY_TYPE_INT32: |
| 2166 | case LY_TYPE_UINT32: |
| 2167 | case LY_TYPE_INT64: |
| 2168 | case LY_TYPE_UINT64: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2169 | num = (struct lysc_type_num*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2170 | |
| 2171 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2172 | if (type_p->range) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2173 | ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, 0, |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2174 | base ? ((struct lysc_type_num*)base)->range : NULL, &num->range); |
| 2175 | LY_CHECK_RET(ret); |
| 2176 | if (!tpdfname) { |
| 2177 | COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts, |
| 2178 | options, u, lys_compile_ext, ret, done); |
| 2179 | } |
| 2180 | } |
| 2181 | |
| 2182 | if (tpdfname) { |
| 2183 | type_p->compiled = *type; |
| 2184 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2185 | } |
| 2186 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2187 | case LY_TYPE_IDENT: |
| 2188 | idref = (struct lysc_type_identityref*)(*type); |
| 2189 | |
| 2190 | /* RFC 7950 9.10.2 - base */ |
| 2191 | if (type_p->bases) { |
| 2192 | if (base) { |
| 2193 | /* only the directly derived identityrefs can contain base specification */ |
| 2194 | if (tpdfname) { |
| 2195 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2196 | "Invalid base substatement for type \"%s\" not directly derived from identityref built-in type.", |
| 2197 | tpdfname); |
| 2198 | } else { |
| 2199 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2200 | "Invalid base substatement for type not directly derived from identityref built-in type."); |
| 2201 | free(*type); |
| 2202 | *type = NULL; |
| 2203 | } |
| 2204 | return LY_EVALID; |
| 2205 | } |
| 2206 | ret = lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases); |
| 2207 | LY_CHECK_RET(ret); |
| 2208 | } |
| 2209 | |
| 2210 | if (!base && !type_p->flags) { |
| 2211 | /* type derived from identityref built-in type must contain at least one base */ |
| 2212 | if (tpdfname) { |
| 2213 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 2214 | } else { |
| 2215 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
| 2216 | free(*type); |
| 2217 | *type = NULL; |
| 2218 | } |
| 2219 | return LY_EVALID; |
| 2220 | } |
| 2221 | |
| 2222 | if (tpdfname) { |
| 2223 | type_p->compiled = *type; |
| 2224 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2225 | } |
| 2226 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2227 | case LY_TYPE_LEAFREF: |
| 2228 | /* RFC 7950 9.9.3 - require-instance */ |
| 2229 | if (type_p->flags & LYS_SET_REQINST) { |
| 2230 | ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame^] | 2231 | } else if (base) { |
| 2232 | /* inherit */ |
| 2233 | ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2234 | } else { |
| 2235 | /* default is true */ |
| 2236 | ((struct lysc_type_leafref*)(*type))->require_instance = 1; |
| 2237 | } |
| 2238 | if (type_p->path) { |
| 2239 | DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path); |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2240 | ((struct lysc_type_leafref*)(*type))->path_context = module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2241 | } else if (base) { |
| 2242 | DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path); |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2243 | ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2244 | } else if (tpdfname) { |
| 2245 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 2246 | return LY_EVALID; |
| 2247 | } else { |
| 2248 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
| 2249 | free(*type); |
| 2250 | *type = NULL; |
| 2251 | return LY_EVALID; |
| 2252 | } |
| 2253 | if (tpdfname) { |
| 2254 | type_p->compiled = *type; |
| 2255 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2256 | } |
| 2257 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2258 | case LY_TYPE_INST: |
| 2259 | /* RFC 7950 9.9.3 - require-instance */ |
| 2260 | if (type_p->flags & LYS_SET_REQINST) { |
| 2261 | ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance; |
| 2262 | } else { |
| 2263 | /* default is true */ |
| 2264 | ((struct lysc_type_instanceid*)(*type))->require_instance = 1; |
| 2265 | } |
| 2266 | |
| 2267 | if (tpdfname) { |
| 2268 | type_p->compiled = *type; |
| 2269 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2270 | } |
| 2271 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2272 | case LY_TYPE_BOOL: |
| 2273 | case LY_TYPE_EMPTY: |
| 2274 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 2275 | break; |
| 2276 | } |
| 2277 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
| 2278 | done: |
| 2279 | return ret; |
| 2280 | } |
| 2281 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2282 | /** |
| 2283 | * @brief Compile information about the leaf/leaf-list's type. |
| 2284 | * @param[in] ctx Compile context. |
| 2285 | * @param[in] leaf_p Parsed leaf with the type to compile. |
| 2286 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 2287 | * @param[out] type Newly created (or reused with increased refcount) type structure with the filled information about the type. |
| 2288 | * @return LY_ERR value. |
| 2289 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2290 | static LY_ERR |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2291 | lys_compile_type(struct lysc_ctx *ctx, struct lysp_node_leaf *leaf_p, int options, struct lysc_type **type) |
| 2292 | { |
| 2293 | LY_ERR ret = LY_SUCCESS; |
| 2294 | unsigned int u; |
| 2295 | struct lysp_type *type_p = &leaf_p->type; |
| 2296 | struct type_context { |
| 2297 | const struct lysp_tpdf *tpdf; |
| 2298 | struct lysp_node *node; |
| 2299 | struct lysp_module *mod; |
| 2300 | } *tctx, *tctx_prev = NULL; |
| 2301 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2302 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2303 | struct ly_set tpdf_chain = {0}; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2304 | |
| 2305 | (*type) = NULL; |
| 2306 | |
| 2307 | tctx = calloc(1, sizeof *tctx); |
| 2308 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 2309 | for (ret = lysp_type_find(type_p->name, (struct lysp_node*)leaf_p, ctx->mod->parsed, |
| 2310 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
| 2311 | ret == LY_SUCCESS; |
| 2312 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
| 2313 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
| 2314 | if (basetype) { |
| 2315 | break; |
| 2316 | } |
| 2317 | |
| 2318 | /* check status */ |
| 2319 | ret = lysc_check_status(ctx, leaf_p->flags, ctx->mod->parsed, leaf_p->name, |
| 2320 | tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->mod->name); |
| 2321 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 2322 | |
| 2323 | if (tctx->tpdf->type.compiled) { |
| 2324 | /* it is not necessary to continue, the rest of the chain was already compiled */ |
| 2325 | basetype = tctx->tpdf->type.compiled->basetype; |
| 2326 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 2327 | tctx = NULL; |
| 2328 | break; |
| 2329 | } |
| 2330 | |
| 2331 | /* store information for the following processing */ |
| 2332 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 2333 | |
| 2334 | /* prepare next loop */ |
| 2335 | tctx_prev = tctx; |
| 2336 | tctx = calloc(1, sizeof *tctx); |
| 2337 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 2338 | } |
| 2339 | free(tctx); |
| 2340 | |
| 2341 | /* allocate type according to the basetype */ |
| 2342 | switch (basetype) { |
| 2343 | case LY_TYPE_BINARY: |
| 2344 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2345 | break; |
| 2346 | case LY_TYPE_BITS: |
| 2347 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2348 | break; |
| 2349 | case LY_TYPE_BOOL: |
| 2350 | case LY_TYPE_EMPTY: |
| 2351 | *type = calloc(1, sizeof(struct lysc_type)); |
| 2352 | break; |
| 2353 | case LY_TYPE_DEC64: |
| 2354 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2355 | break; |
| 2356 | case LY_TYPE_ENUM: |
| 2357 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2358 | break; |
| 2359 | case LY_TYPE_IDENT: |
| 2360 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2361 | break; |
| 2362 | case LY_TYPE_INST: |
| 2363 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2364 | break; |
| 2365 | case LY_TYPE_LEAFREF: |
| 2366 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2367 | break; |
| 2368 | case LY_TYPE_STRING: |
| 2369 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2370 | break; |
| 2371 | case LY_TYPE_UNION: |
| 2372 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 2373 | break; |
| 2374 | case LY_TYPE_INT8: |
| 2375 | case LY_TYPE_UINT8: |
| 2376 | case LY_TYPE_INT16: |
| 2377 | case LY_TYPE_UINT16: |
| 2378 | case LY_TYPE_INT32: |
| 2379 | case LY_TYPE_UINT32: |
| 2380 | case LY_TYPE_INT64: |
| 2381 | case LY_TYPE_UINT64: |
| 2382 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2383 | break; |
| 2384 | case LY_TYPE_UNKNOWN: |
| 2385 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2386 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 2387 | ret = LY_EVALID; |
| 2388 | goto cleanup; |
| 2389 | } |
| 2390 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
| 2391 | if (~type_substmt_map[basetype] & leaf_p->type.flags) { |
| 2392 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
| 2393 | ly_data_type2str[basetype]); |
| 2394 | free(*type); |
| 2395 | (*type) = NULL; |
| 2396 | ret = LY_EVALID; |
| 2397 | goto cleanup; |
| 2398 | } |
| 2399 | |
| 2400 | /* get restrictions from the referred typedefs */ |
| 2401 | for (u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
| 2402 | tctx = (struct type_context*)tpdf_chain.objs[u]; |
| 2403 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 2404 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
| 2405 | tctx->tpdf->name, ly_data_type2str[basetype]); |
| 2406 | ret = LY_EVALID; |
| 2407 | goto cleanup; |
| 2408 | } else if (tctx->tpdf->type.compiled) { |
| 2409 | base = tctx->tpdf->type.compiled; |
| 2410 | continue; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame^] | 2411 | } else if ((basetype != LY_TYPE_LEAFREF) && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2412 | /* no change, just use the type information from the base */ |
| 2413 | base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled; |
| 2414 | ++base->refcount; |
| 2415 | continue; |
| 2416 | } |
| 2417 | |
| 2418 | ++(*type)->refcount; |
| 2419 | (*type)->basetype = basetype; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2420 | prev_type = *type; |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2421 | ret = lys_compile_type_(ctx, &((struct lysp_tpdf*)tctx->tpdf)->type, |
| 2422 | basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL, |
| 2423 | basetype, options, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2424 | LY_CHECK_GOTO(ret, cleanup); |
| 2425 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2426 | } |
| 2427 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2428 | /* process the type definition in leaf */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame^] | 2429 | if (leaf_p->type.flags || !base || basetype == LY_TYPE_LEAFREF) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2430 | /* get restrictions from the node itself */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2431 | (*type)->basetype = basetype; |
| 2432 | ++(*type)->refcount; |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2433 | ret = lys_compile_type_(ctx, &leaf_p->type, ctx->mod, basetype, options, NULL, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2434 | LY_CHECK_GOTO(ret, cleanup); |
| 2435 | } else { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2436 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 2437 | free(*type); |
| 2438 | (*type) = base; |
| 2439 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2440 | } |
| 2441 | |
| 2442 | COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, options, u, lys_compile_ext, ret, cleanup); |
| 2443 | |
| 2444 | cleanup: |
| 2445 | ly_set_erase(&tpdf_chain, free); |
| 2446 | return ret; |
| 2447 | } |
| 2448 | |
| 2449 | static LY_ERR lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *parent); |
| 2450 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2451 | /** |
| 2452 | * @brief Compile parsed container node information. |
| 2453 | * @param[in] ctx Compile context |
| 2454 | * @param[in] node_p Parsed container node. |
| 2455 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 2456 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 2457 | * is enriched with the container-specific information. |
| 2458 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2459 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2460 | static LY_ERR |
| 2461 | lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node) |
| 2462 | { |
| 2463 | struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p; |
| 2464 | struct lysc_node_container *cont = (struct lysc_node_container*)node; |
| 2465 | struct lysp_node *child_p; |
| 2466 | unsigned int u; |
| 2467 | LY_ERR ret = LY_SUCCESS; |
| 2468 | |
| 2469 | COMPILE_MEMBER_GOTO(ctx, cont_p->when, cont->when, options, lys_compile_when, ret, done); |
| 2470 | COMPILE_ARRAY_GOTO(ctx, cont_p->iffeatures, cont->iffeatures, options, u, lys_compile_iffeature, ret, done); |
| 2471 | |
| 2472 | LY_LIST_FOR(cont_p->child, child_p) { |
| 2473 | LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node)); |
| 2474 | } |
| 2475 | |
| 2476 | COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, options, u, lys_compile_must, ret, done); |
| 2477 | //COMPILE_ARRAY_GOTO(ctx, cont_p->actions, cont->actions, options, u, lys_compile_action, ret, done); |
| 2478 | //COMPILE_ARRAY_GOTO(ctx, cont_p->notifs, cont->notifs, options, u, lys_compile_notif, ret, done); |
| 2479 | |
| 2480 | done: |
| 2481 | return ret; |
| 2482 | } |
| 2483 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2484 | /** |
| 2485 | * @brief Compile parsed leaf node information. |
| 2486 | * @param[in] ctx Compile context |
| 2487 | * @param[in] node_p Parsed leaf node. |
| 2488 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 2489 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 2490 | * is enriched with the leaf-specific information. |
| 2491 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2492 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2493 | static LY_ERR |
| 2494 | lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node) |
| 2495 | { |
| 2496 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p; |
| 2497 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 2498 | unsigned int u; |
| 2499 | LY_ERR ret = LY_SUCCESS; |
| 2500 | |
| 2501 | COMPILE_MEMBER_GOTO(ctx, leaf_p->when, leaf->when, options, lys_compile_when, ret, done); |
| 2502 | COMPILE_ARRAY_GOTO(ctx, leaf_p->iffeatures, leaf->iffeatures, options, u, lys_compile_iffeature, ret, done); |
| 2503 | |
| 2504 | COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, options, u, lys_compile_must, ret, done); |
| 2505 | ret = lys_compile_type(ctx, leaf_p, options, &leaf->type); |
| 2506 | LY_CHECK_GOTO(ret, done); |
| 2507 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2508 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 2509 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 2510 | ly_set_add(&ctx->unres, leaf, 0); |
| 2511 | } |
| 2512 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2513 | DUP_STRING(ctx->ctx, leaf_p->units, leaf->units); |
| 2514 | DUP_STRING(ctx->ctx, leaf_p->dflt, leaf->dflt); |
| 2515 | done: |
| 2516 | return ret; |
| 2517 | } |
| 2518 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2519 | /** |
| 2520 | * @brief Compile parsed schema node information. |
| 2521 | * @param[in] ctx Compile context |
| 2522 | * @param[in] node_p Parsed schema node. |
| 2523 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 2524 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 2525 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 2526 | * the compile context. |
| 2527 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2528 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2529 | static LY_ERR |
| 2530 | lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *parent) |
| 2531 | { |
| 2532 | LY_ERR ret = LY_EVALID; |
| 2533 | struct lysc_node *node, **children; |
| 2534 | unsigned int u; |
| 2535 | LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, int, struct lysc_node*); |
| 2536 | |
| 2537 | switch (node_p->nodetype) { |
| 2538 | case LYS_CONTAINER: |
| 2539 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container)); |
| 2540 | node_compile_spec = lys_compile_node_container; |
| 2541 | break; |
| 2542 | case LYS_LEAF: |
| 2543 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf)); |
| 2544 | node_compile_spec = lys_compile_node_leaf; |
| 2545 | break; |
| 2546 | case LYS_LIST: |
| 2547 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list)); |
| 2548 | break; |
| 2549 | case LYS_LEAFLIST: |
| 2550 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist)); |
| 2551 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2552 | case LYS_CHOICE: |
| 2553 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice)); |
| 2554 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2555 | case LYS_ANYXML: |
| 2556 | case LYS_ANYDATA: |
| 2557 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata)); |
| 2558 | break; |
| 2559 | default: |
| 2560 | LOGINT(ctx->ctx); |
| 2561 | return LY_EINT; |
| 2562 | } |
| 2563 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 2564 | node->nodetype = node_p->nodetype; |
| 2565 | node->module = ctx->mod; |
| 2566 | node->prev = node; |
| 2567 | node->flags = node_p->flags; |
| 2568 | |
| 2569 | /* config */ |
| 2570 | if (!(node->flags & LYS_CONFIG_MASK)) { |
| 2571 | /* config not explicitely set, inherit it from parent */ |
| 2572 | if (parent) { |
| 2573 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 2574 | } else { |
| 2575 | /* default is config true */ |
| 2576 | node->flags |= LYS_CONFIG_W; |
| 2577 | } |
| 2578 | } |
| 2579 | |
| 2580 | /* status - it is not inherited by specification, but it does not make sense to have |
| 2581 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 2582 | if (!(node->flags & LYS_STATUS_MASK)) { |
| 2583 | if (parent && (parent->flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT))) { |
| 2584 | LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.", |
| 2585 | (parent->flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 2586 | node->flags |= parent->flags & LYS_STATUS_MASK; |
| 2587 | } else { |
| 2588 | node->flags |= LYS_STATUS_CURR; |
| 2589 | } |
| 2590 | } else if (parent) { |
| 2591 | /* check status compatibility with the parent */ |
| 2592 | if ((parent->flags & LYS_STATUS_MASK) > (node->flags & LYS_STATUS_MASK)) { |
| 2593 | if (node->flags & LYS_STATUS_CURR) { |
| 2594 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2595 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
| 2596 | (parent->flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 2597 | } else { /* LYS_STATUS_DEPRC */ |
| 2598 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2599 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 2600 | } |
| 2601 | goto error; |
| 2602 | } |
| 2603 | } |
| 2604 | |
| 2605 | if (!(options & LYSC_OPT_FREE_SP)) { |
| 2606 | node->sp = node_p; |
| 2607 | } |
| 2608 | DUP_STRING(ctx->ctx, node_p->name, node->name); |
| 2609 | COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, options, u, lys_compile_ext, ret, error); |
| 2610 | |
| 2611 | /* nodetype-specific part */ |
| 2612 | LY_CHECK_GOTO(node_compile_spec(ctx, node_p, options, node), error); |
| 2613 | |
| 2614 | /* insert into parent's children */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2615 | if (parent) { |
| 2616 | if (parent->nodetype == LYS_CHOICE) { |
| 2617 | /* TODO exception for cases */ |
| 2618 | } else if ((children = lysc_node_children(parent))) { |
| 2619 | if (!(*children)) { |
| 2620 | /* first child */ |
| 2621 | *children = node; |
| 2622 | } else { |
| 2623 | /* insert at the end of the parent's children list */ |
| 2624 | (*children)->prev->next = node; |
| 2625 | node->prev = (*children)->prev; |
| 2626 | (*children)->prev = node; |
| 2627 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2628 | } |
| 2629 | } else { |
| 2630 | /* top-level element */ |
| 2631 | if (!ctx->mod->compiled->data) { |
| 2632 | ctx->mod->compiled->data = node; |
| 2633 | } else { |
| 2634 | /* insert at the end of the module's top-level nodes list */ |
| 2635 | ctx->mod->compiled->data->prev->next = node; |
| 2636 | node->prev = ctx->mod->compiled->data->prev; |
| 2637 | ctx->mod->compiled->data->prev = node; |
| 2638 | } |
| 2639 | } |
| 2640 | |
| 2641 | return LY_SUCCESS; |
| 2642 | |
| 2643 | error: |
| 2644 | lysc_node_free(ctx->ctx, node); |
| 2645 | return ret; |
| 2646 | } |
| 2647 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2648 | /** |
| 2649 | * @brief Compile the given YANG module. |
| 2650 | * @param[in] mod Module structure where the parsed schema is expected and the compiled schema will be placed. |
| 2651 | * @param[in] options Various options to modify compiler behavior, see [compile flags](@ref scflags). |
| 2652 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2653 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2654 | LY_ERR |
| 2655 | lys_compile(struct lys_module *mod, int options) |
| 2656 | { |
| 2657 | struct lysc_ctx ctx = {0}; |
| 2658 | struct lysc_module *mod_c; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame^] | 2659 | struct lysc_type *type, *typeiter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2660 | struct lysp_module *sp; |
| 2661 | struct lysp_node *node_p; |
| 2662 | unsigned int u; |
| 2663 | LY_ERR ret; |
| 2664 | |
| 2665 | LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->parsed->ctx, LY_EINVAL); |
| 2666 | sp = mod->parsed; |
| 2667 | |
| 2668 | if (sp->submodule) { |
| 2669 | LOGERR(sp->ctx, LY_EINVAL, "Submodules (%s) are not supposed to be compiled, compile only the main modules.", sp->name); |
| 2670 | return LY_EINVAL; |
| 2671 | } |
| 2672 | |
| 2673 | ctx.ctx = sp->ctx; |
| 2674 | ctx.mod = mod; |
| 2675 | |
| 2676 | mod->compiled = mod_c = calloc(1, sizeof *mod_c); |
| 2677 | LY_CHECK_ERR_RET(!mod_c, LOGMEM(sp->ctx), LY_EMEM); |
| 2678 | mod_c->ctx = sp->ctx; |
| 2679 | mod_c->implemented = sp->implemented; |
| 2680 | mod_c->latest_revision = sp->latest_revision; |
| 2681 | mod_c->version = sp->version; |
| 2682 | |
| 2683 | DUP_STRING(sp->ctx, sp->name, mod_c->name); |
| 2684 | DUP_STRING(sp->ctx, sp->ns, mod_c->ns); |
| 2685 | DUP_STRING(sp->ctx, sp->prefix, mod_c->prefix); |
| 2686 | if (sp->revs) { |
| 2687 | DUP_STRING(sp->ctx, sp->revs[0].date, mod_c->revision); |
| 2688 | } |
| 2689 | COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, options, u, lys_compile_import, ret, error); |
| 2690 | COMPILE_ARRAY_GOTO(&ctx, sp->features, mod_c->features, options, u, lys_compile_feature, ret, error); |
| 2691 | COMPILE_ARRAY_GOTO(&ctx, sp->identities, mod_c->identities, options, u, lys_compile_identity, ret, error); |
| 2692 | if (sp->identities) { |
| 2693 | LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities)); |
| 2694 | } |
| 2695 | |
| 2696 | LY_LIST_FOR(sp->data, node_p) { |
| 2697 | ret = lys_compile_node(&ctx, node_p, options, NULL); |
| 2698 | LY_CHECK_GOTO(ret, error); |
| 2699 | } |
| 2700 | //COMPILE_ARRAY_GOTO(ctx, sp->rpcs, mod_c->rpcs, options, u, lys_compile_action, ret, error); |
| 2701 | //COMPILE_ARRAY_GOTO(ctx, sp->notifs, mod_c->notifs, options, u, lys_compile_notif, ret, error); |
| 2702 | |
| 2703 | COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, options, u, lys_compile_ext, ret, error); |
| 2704 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2705 | /* validate leafref's paths and when/must xpaths */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame^] | 2706 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 2707 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 2708 | * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2709 | for (u = 0; u < ctx.unres.count; ++u) { |
| 2710 | if (((struct lysc_node*)ctx.unres.objs[u])->nodetype == LYS_LEAF) { |
| 2711 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 2712 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 2713 | /* validate the path */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame^] | 2714 | ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), (struct lysc_type_leafref*)type); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2715 | LY_CHECK_GOTO(ret, error); |
| 2716 | } |
| 2717 | } |
| 2718 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame^] | 2719 | for (u = 0; u < ctx.unres.count; ++u) { |
| 2720 | if (((struct lysc_node*)ctx.unres.objs[u])->nodetype == LYS_LEAF) { |
| 2721 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 2722 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 2723 | /* store pointer to the real type */ |
| 2724 | for (typeiter = ((struct lysc_type_leafref*)type)->realtype; |
| 2725 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 2726 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 2727 | ((struct lysc_type_leafref*)type)->realtype = typeiter; |
| 2728 | } |
| 2729 | } |
| 2730 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2731 | ly_set_erase(&ctx.unres, NULL); |
| 2732 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2733 | if (options & LYSC_OPT_FREE_SP) { |
| 2734 | lysp_module_free(mod->parsed); |
| 2735 | ((struct lys_module*)mod)->parsed = NULL; |
| 2736 | } |
| 2737 | |
| 2738 | ((struct lys_module*)mod)->compiled = mod_c; |
| 2739 | return LY_SUCCESS; |
| 2740 | |
| 2741 | error: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2742 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2743 | lysc_module_free(mod_c, NULL); |
| 2744 | ((struct lys_module*)mod)->compiled = NULL; |
| 2745 | return ret; |
| 2746 | } |