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 | |
| 59 | const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "bits", "boolean", "decimal64", "empty", "enumeration", |
| 60 | "identityref", "instance-identifier", "leafref", "string", "union", "8bit integer", "8bit unsigned integer", "16bit integer", |
| 61 | "16bit unsigned integer", "32bit integer", "32bit unsigned integer", "64bit integer", "64bit unsigned integer" |
| 62 | }; |
| 63 | |
| 64 | static struct lysc_ext_instance * |
| 65 | lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig) |
| 66 | { |
| 67 | /* TODO */ |
| 68 | (void) ctx; |
| 69 | (void) orig; |
| 70 | return NULL; |
| 71 | } |
| 72 | |
| 73 | static struct lysc_pattern* |
| 74 | lysc_pattern_dup(struct lysc_pattern *orig) |
| 75 | { |
| 76 | ++orig->refcount; |
| 77 | return orig; |
| 78 | } |
| 79 | |
| 80 | static struct lysc_pattern** |
| 81 | lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig) |
| 82 | { |
| 83 | struct lysc_pattern **dup; |
| 84 | unsigned int u; |
| 85 | |
| 86 | LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL); |
| 87 | LY_ARRAY_FOR(orig, u) { |
| 88 | dup[u] = lysc_pattern_dup(orig[u]); |
| 89 | LY_ARRAY_INCREMENT(dup); |
| 90 | } |
| 91 | return dup; |
| 92 | } |
| 93 | |
| 94 | struct lysc_range* |
| 95 | lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig) |
| 96 | { |
| 97 | struct lysc_range *dup; |
| 98 | LY_ERR ret; |
| 99 | |
| 100 | dup = calloc(1, sizeof *dup); |
| 101 | LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL); |
| 102 | if (orig->parts) { |
| 103 | LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup); |
| 104 | LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts); |
| 105 | memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts); |
| 106 | } |
| 107 | DUP_STRING(ctx, orig->eapptag, dup->eapptag); |
| 108 | DUP_STRING(ctx, orig->emsg, dup->emsg); |
| 109 | dup->exts = lysc_ext_instance_dup(ctx, orig->exts); |
| 110 | |
| 111 | return dup; |
| 112 | cleanup: |
| 113 | free(dup); |
| 114 | (void) ret; /* set but not used due to the return type */ |
| 115 | return NULL; |
| 116 | } |
| 117 | |
| 118 | struct iff_stack { |
| 119 | int size; |
| 120 | int index; /* first empty item */ |
| 121 | uint8_t *stack; |
| 122 | }; |
| 123 | |
| 124 | static LY_ERR |
| 125 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 126 | { |
| 127 | if (stack->index == stack->size) { |
| 128 | stack->size += 4; |
| 129 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 130 | LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM); |
| 131 | } |
| 132 | stack->stack[stack->index++] = value; |
| 133 | return LY_SUCCESS; |
| 134 | } |
| 135 | |
| 136 | static uint8_t |
| 137 | iff_stack_pop(struct iff_stack *stack) |
| 138 | { |
| 139 | stack->index--; |
| 140 | return stack->stack[stack->index]; |
| 141 | } |
| 142 | |
| 143 | static void |
| 144 | iff_stack_clean(struct iff_stack *stack) |
| 145 | { |
| 146 | stack->size = 0; |
| 147 | free(stack->stack); |
| 148 | } |
| 149 | |
| 150 | static void |
| 151 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 152 | { |
| 153 | uint8_t *item; |
| 154 | uint8_t mask = 3; |
| 155 | |
| 156 | assert(pos >= 0); |
| 157 | assert(op <= 3); /* max 2 bits */ |
| 158 | |
| 159 | item = &list[pos / 4]; |
| 160 | mask = mask << 2 * (pos % 4); |
| 161 | *item = (*item) & ~mask; |
| 162 | *item = (*item) | (op << 2 * (pos % 4)); |
| 163 | } |
| 164 | |
| 165 | #define LYS_IFF_LP 0x04 /* ( */ |
| 166 | #define LYS_IFF_RP 0x08 /* ) */ |
| 167 | |
| 168 | static struct lysc_feature * |
| 169 | lysc_feature_find(struct lysc_module *mod, const char *name, size_t len) |
| 170 | { |
| 171 | size_t i; |
| 172 | struct lysc_feature *f; |
| 173 | |
| 174 | for (i = 0; i < len; ++i) { |
| 175 | if (name[i] == ':') { |
| 176 | /* we have a prefixed feature */ |
| 177 | mod = lysc_module_find_prefix(mod, name, i); |
| 178 | LY_CHECK_RET(!mod, NULL); |
| 179 | |
| 180 | name = &name[i + 1]; |
| 181 | len = len - i - 1; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /* we have the correct module, get the feature */ |
| 186 | LY_ARRAY_FOR(mod->features, i) { |
| 187 | f = &mod->features[i]; |
| 188 | if (!strncmp(f->name, name, len) && f->name[len] == '\0') { |
| 189 | return f; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | return NULL; |
| 194 | } |
| 195 | |
| 196 | static LY_ERR |
| 197 | lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, int UNUSED(options), struct lysc_ext_instance *ext) |
| 198 | { |
| 199 | const char *name; |
| 200 | unsigned int u; |
| 201 | const struct lys_module *mod; |
| 202 | struct lysp_ext *edef = NULL; |
| 203 | |
| 204 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument); |
| 205 | ext->insubstmt = ext_p->insubstmt; |
| 206 | ext->insubstmt_index = ext_p->insubstmt_index; |
| 207 | |
| 208 | /* get module where the extension definition should be placed */ |
| 209 | for (u = 0; ext_p->name[u] != ':'; ++u); |
| 210 | mod = lys_module_find_prefix(ctx->mod, ext_p->name, u); |
| 211 | LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 212 | "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name), |
| 213 | LY_EVALID); |
| 214 | LY_CHECK_ERR_RET(!mod->parsed->extensions, |
| 215 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 216 | "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.", |
| 217 | ext_p->name, mod->parsed->name), |
| 218 | LY_EVALID); |
| 219 | name = &ext_p->name[u + 1]; |
| 220 | /* find the extension definition there */ |
| 221 | for (ext = NULL, u = 0; u < LY_ARRAY_SIZE(mod->parsed->extensions); ++u) { |
| 222 | if (!strcmp(name, mod->parsed->extensions[u].name)) { |
| 223 | edef = &mod->parsed->extensions[u]; |
| 224 | break; |
| 225 | } |
| 226 | } |
| 227 | LY_CHECK_ERR_RET(!edef, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 228 | "Extension definition of extension instance \"%s\" not found.", ext_p->name), |
| 229 | LY_EVALID); |
| 230 | /* TODO plugins */ |
| 231 | |
| 232 | return LY_SUCCESS; |
| 233 | } |
| 234 | |
| 235 | static LY_ERR |
| 236 | lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, int UNUSED(options), struct lysc_iffeature *iff) |
| 237 | { |
| 238 | const char *c = *value; |
| 239 | int r, rc = EXIT_FAILURE; |
| 240 | int i, j, last_not, checkversion = 0; |
| 241 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
| 242 | uint8_t op; |
| 243 | struct iff_stack stack = {0, 0, NULL}; |
| 244 | struct lysc_feature *f; |
| 245 | |
| 246 | assert(c); |
| 247 | |
| 248 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 249 | for (i = j = last_not = 0; c[i]; i++) { |
| 250 | if (c[i] == '(') { |
| 251 | j++; |
| 252 | checkversion = 1; |
| 253 | continue; |
| 254 | } else if (c[i] == ')') { |
| 255 | j--; |
| 256 | continue; |
| 257 | } else if (isspace(c[i])) { |
| 258 | checkversion = 1; |
| 259 | continue; |
| 260 | } |
| 261 | |
| 262 | if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) { |
| 263 | if (c[i + r] == '\0') { |
| 264 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 265 | "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value); |
| 266 | return LY_EVALID; |
| 267 | } else if (!isspace(c[i + r])) { |
| 268 | /* feature name starting with the not/and/or */ |
| 269 | last_not = 0; |
| 270 | f_size++; |
| 271 | } else if (c[i] == 'n') { /* not operation */ |
| 272 | if (last_not) { |
| 273 | /* double not */ |
| 274 | expr_size = expr_size - 2; |
| 275 | last_not = 0; |
| 276 | } else { |
| 277 | last_not = 1; |
| 278 | } |
| 279 | } else { /* and, or */ |
| 280 | f_exp++; |
| 281 | /* not a not operation */ |
| 282 | last_not = 0; |
| 283 | } |
| 284 | i += r; |
| 285 | } else { |
| 286 | f_size++; |
| 287 | last_not = 0; |
| 288 | } |
| 289 | expr_size++; |
| 290 | |
| 291 | while (!isspace(c[i])) { |
| 292 | if (!c[i] || c[i] == ')') { |
| 293 | i--; |
| 294 | break; |
| 295 | } |
| 296 | i++; |
| 297 | } |
| 298 | } |
| 299 | if (j || f_exp != f_size) { |
| 300 | /* not matching count of ( and ) */ |
| 301 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 302 | "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value); |
| 303 | return LY_EVALID; |
| 304 | } |
| 305 | |
| 306 | if (checkversion || expr_size > 1) { |
| 307 | /* check that we have 1.1 module */ |
| 308 | if (ctx->mod->compiled->version != LYS_VERSION_1_1) { |
| 309 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 310 | "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value); |
| 311 | return LY_EVALID; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | /* allocate the memory */ |
| 316 | LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM); |
| 317 | iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr); |
| 318 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 319 | LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error); |
| 320 | |
| 321 | stack.size = expr_size; |
| 322 | f_size--; expr_size--; /* used as indexes from now */ |
| 323 | |
| 324 | for (i--; i >= 0; i--) { |
| 325 | if (c[i] == ')') { |
| 326 | /* push it on stack */ |
| 327 | iff_stack_push(&stack, LYS_IFF_RP); |
| 328 | continue; |
| 329 | } else if (c[i] == '(') { |
| 330 | /* pop from the stack into result all operators until ) */ |
| 331 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 332 | iff_setop(iff->expr, op, expr_size--); |
| 333 | } |
| 334 | continue; |
| 335 | } else if (isspace(c[i])) { |
| 336 | continue; |
| 337 | } |
| 338 | |
| 339 | /* end of operator or operand -> find beginning and get what is it */ |
| 340 | j = i + 1; |
| 341 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 342 | i--; |
| 343 | } |
| 344 | i++; /* go back by one step */ |
| 345 | |
| 346 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
| 347 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 348 | /* double not */ |
| 349 | iff_stack_pop(&stack); |
| 350 | } else { |
| 351 | /* not has the highest priority, so do not pop from the stack |
| 352 | * as in case of AND and OR */ |
| 353 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 354 | } |
| 355 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
| 356 | /* as for OR - pop from the stack all operators with the same or higher |
| 357 | * priority and store them to the result, then push the AND to the stack */ |
| 358 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 359 | op = iff_stack_pop(&stack); |
| 360 | iff_setop(iff->expr, op, expr_size--); |
| 361 | } |
| 362 | iff_stack_push(&stack, LYS_IFF_AND); |
| 363 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
| 364 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 365 | op = iff_stack_pop(&stack); |
| 366 | iff_setop(iff->expr, op, expr_size--); |
| 367 | } |
| 368 | iff_stack_push(&stack, LYS_IFF_OR); |
| 369 | } else { |
| 370 | /* feature name, length is j - i */ |
| 371 | |
| 372 | /* add it to the expression */ |
| 373 | iff_setop(iff->expr, LYS_IFF_F, expr_size--); |
| 374 | |
| 375 | /* now get the link to the feature definition */ |
| 376 | f = lysc_feature_find(ctx->mod->compiled, &c[i], j - i); |
| 377 | LY_CHECK_ERR_GOTO(!f, |
| 378 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 379 | "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]); |
| 380 | rc = LY_EVALID, |
| 381 | error) |
| 382 | iff->features[f_size] = f; |
| 383 | LY_ARRAY_INCREMENT(iff->features); |
| 384 | f_size--; |
| 385 | } |
| 386 | } |
| 387 | while (stack.index) { |
| 388 | op = iff_stack_pop(&stack); |
| 389 | iff_setop(iff->expr, op, expr_size--); |
| 390 | } |
| 391 | |
| 392 | if (++expr_size || ++f_size) { |
| 393 | /* not all expected operators and operands found */ |
| 394 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 395 | "Invalid value \"%s\" of if-feature - processing error.", *value); |
| 396 | rc = LY_EINT; |
| 397 | } else { |
| 398 | rc = LY_SUCCESS; |
| 399 | } |
| 400 | |
| 401 | error: |
| 402 | /* cleanup */ |
| 403 | iff_stack_clean(&stack); |
| 404 | |
| 405 | return rc; |
| 406 | } |
| 407 | |
| 408 | static LY_ERR |
| 409 | lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, int options, struct lysc_when *when) |
| 410 | { |
| 411 | unsigned int u; |
| 412 | LY_ERR ret = LY_SUCCESS; |
| 413 | |
| 414 | when->cond = lyxp_expr_parse(ctx->ctx, when_p->cond); |
| 415 | LY_CHECK_ERR_GOTO(!when->cond, ret = ly_errcode(ctx->ctx), done); |
| 416 | COMPILE_ARRAY_GOTO(ctx, when_p->exts, when->exts, options, u, lys_compile_ext, ret, done); |
| 417 | |
| 418 | done: |
| 419 | return ret; |
| 420 | } |
| 421 | |
| 422 | static LY_ERR |
| 423 | lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, int options, struct lysc_must *must) |
| 424 | { |
| 425 | unsigned int u; |
| 426 | LY_ERR ret = LY_SUCCESS; |
| 427 | |
| 428 | must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg); |
| 429 | LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done); |
| 430 | |
| 431 | DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag); |
| 432 | DUP_STRING(ctx->ctx, must_p->emsg, must->emsg); |
| 433 | COMPILE_ARRAY_GOTO(ctx, must_p->exts, must->exts, options, u, lys_compile_ext, ret, done); |
| 434 | |
| 435 | done: |
| 436 | return ret; |
| 437 | } |
| 438 | |
| 439 | static LY_ERR |
| 440 | lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, int options, struct lysc_import *imp) |
| 441 | { |
| 442 | unsigned int u; |
| 443 | struct lys_module *mod = NULL; |
| 444 | struct lysc_module *comp; |
| 445 | LY_ERR ret = LY_SUCCESS; |
| 446 | |
| 447 | DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix); |
| 448 | COMPILE_ARRAY_GOTO(ctx, imp_p->exts, imp->exts, options, u, lys_compile_ext, ret, done); |
| 449 | imp->module = imp_p->module; |
| 450 | |
| 451 | /* make sure that we have both versions (lysp_ and lysc_) of the imported module. To import groupings or |
| 452 | * typedefs, the lysp_ is needed. To augment or deviate imported module, we need the lysc_ structure */ |
| 453 | if (!imp->module->parsed) { |
| 454 | comp = imp->module->compiled; |
| 455 | /* try to get filepath from the compiled version */ |
| 456 | if (comp->filepath) { |
| 457 | mod = (struct lys_module*)lys_parse_path(ctx->ctx, comp->filepath, |
| 458 | !strcmp(&comp->filepath[strlen(comp->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG); |
| 459 | if (mod != imp->module) { |
| 460 | LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.", |
| 461 | comp->filepath, comp->name); |
| 462 | mod = NULL; |
| 463 | } |
| 464 | } |
| 465 | if (!mod) { |
| 466 | if (lysp_load_module(ctx->ctx, comp->name, comp->revision, 0, 1, &mod)) { |
| 467 | LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.", |
| 468 | comp->name, ctx->mod->compiled->name); |
| 469 | return LY_ENOTFOUND; |
| 470 | } |
| 471 | } |
| 472 | } else if (!imp->module->compiled) { |
| 473 | return lys_compile(imp->module, options); |
| 474 | } |
| 475 | |
| 476 | done: |
| 477 | return ret; |
| 478 | } |
| 479 | |
| 480 | static LY_ERR |
| 481 | lys_compile_identity(struct lysc_ctx *ctx, struct lysp_ident *ident_p, int options, struct lysc_ident *ident) |
| 482 | { |
| 483 | unsigned int u; |
| 484 | LY_ERR ret = LY_SUCCESS; |
| 485 | |
| 486 | DUP_STRING(ctx->ctx, ident_p->name, ident->name); |
| 487 | COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, options, u, lys_compile_iffeature, ret, done); |
| 488 | /* backlings (derived) can be added no sooner than when all the identities in the current module are present */ |
| 489 | COMPILE_ARRAY_GOTO(ctx, ident_p->exts, ident->exts, options, u, lys_compile_ext, ret, done); |
| 490 | ident->flags = ident_p->flags; |
| 491 | |
| 492 | done: |
| 493 | return ret; |
| 494 | } |
| 495 | |
| 496 | static LY_ERR |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame^] | 497 | 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] | 498 | { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame^] | 499 | unsigned int u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 500 | const char *s, *name; |
| 501 | struct lysc_module *mod; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame^] | 502 | struct lysc_ident **idref; |
| 503 | |
| 504 | assert(ident || bases); |
| 505 | |
| 506 | if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod->compiled->version < 2) { |
| 507 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 508 | "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type"); |
| 509 | return LY_EVALID; |
| 510 | } |
| 511 | |
| 512 | for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) { |
| 513 | s = strchr(bases_p[u], ':'); |
| 514 | if (s) { |
| 515 | /* prefixed identity */ |
| 516 | name = &s[1]; |
| 517 | mod = lysc_module_find_prefix(ctx->mod->compiled, bases_p[u], s - bases_p[u]); |
| 518 | } else { |
| 519 | name = bases_p[u]; |
| 520 | mod = ctx->mod->compiled; |
| 521 | } |
| 522 | if (!mod) { |
| 523 | if (ident) { |
| 524 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 525 | "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 526 | } else { |
| 527 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 528 | "Invalid prefix used for base (%s) of identityref.", bases_p[u]); |
| 529 | } |
| 530 | return LY_EVALID; |
| 531 | } |
| 532 | idref = NULL; |
| 533 | if (mod->identities) { |
| 534 | for (v = 0; v < LY_ARRAY_SIZE(mod->identities); ++v) { |
| 535 | if (!strcmp(name, mod->identities[v].name)) { |
| 536 | if (ident) { |
| 537 | /* we have match! store the backlink */ |
| 538 | LY_ARRAY_NEW_RET(ctx->ctx, mod->identities[v].derived, idref, LY_EMEM); |
| 539 | *idref = ident; |
| 540 | } else { |
| 541 | /* we have match! store the found identity */ |
| 542 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
| 543 | *idref = &mod->identities[v]; |
| 544 | } |
| 545 | break; |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | if (!idref || !(*idref)) { |
| 550 | if (ident) { |
| 551 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 552 | "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 553 | } else { |
| 554 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 555 | "Unable to find base (%s) of identityref.", bases_p[u]); |
| 556 | } |
| 557 | return LY_EVALID; |
| 558 | } |
| 559 | } |
| 560 | return LY_SUCCESS; |
| 561 | } |
| 562 | |
| 563 | static LY_ERR |
| 564 | lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents) |
| 565 | { |
| 566 | unsigned int i; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 567 | |
| 568 | for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) { |
| 569 | if (!idents_p[i].bases) { |
| 570 | continue; |
| 571 | } |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame^] | 572 | 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] | 573 | } |
| 574 | return LY_SUCCESS; |
| 575 | } |
| 576 | |
| 577 | static LY_ERR |
| 578 | lys_compile_feature(struct lysc_ctx *ctx, struct lysp_feature *feature_p, int options, struct lysc_feature *feature) |
| 579 | { |
| 580 | unsigned int u, v; |
| 581 | LY_ERR ret = LY_SUCCESS; |
| 582 | struct lysc_feature **df; |
| 583 | |
| 584 | DUP_STRING(ctx->ctx, feature_p->name, feature->name); |
| 585 | feature->flags = feature_p->flags; |
| 586 | |
| 587 | COMPILE_ARRAY_GOTO(ctx, feature_p->exts, feature->exts, options, u, lys_compile_ext, ret, done); |
| 588 | COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, options, u, lys_compile_iffeature, ret, done); |
| 589 | if (feature->iffeatures) { |
| 590 | for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) { |
| 591 | if (feature->iffeatures[u].features) { |
| 592 | for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) { |
| 593 | /* add itself into the dependants list */ |
| 594 | LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM); |
| 595 | *df = feature; |
| 596 | } |
| 597 | /* TODO check for circular dependency */ |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | done: |
| 602 | return ret; |
| 603 | } |
| 604 | |
| 605 | static LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 606 | 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] | 607 | { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 608 | size_t fraction = 0, size; |
| 609 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 610 | *len = 0; |
| 611 | |
| 612 | assert(value); |
| 613 | /* parse value */ |
| 614 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 615 | return LY_EVALID; |
| 616 | } |
| 617 | |
| 618 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 619 | ++(*len); |
| 620 | } |
| 621 | |
| 622 | while (isdigit(value[*len])) { |
| 623 | ++(*len); |
| 624 | } |
| 625 | |
| 626 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 627 | if (basetype == LY_TYPE_DEC64) { |
| 628 | goto decimal; |
| 629 | } else { |
| 630 | *valcopy = strndup(value, *len); |
| 631 | return LY_SUCCESS; |
| 632 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 633 | } |
| 634 | fraction = *len; |
| 635 | |
| 636 | ++(*len); |
| 637 | while (isdigit(value[*len])) { |
| 638 | ++(*len); |
| 639 | } |
| 640 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 641 | if (basetype == LY_TYPE_DEC64) { |
| 642 | decimal: |
| 643 | assert(frdigits); |
| 644 | if (*len - 1 - fraction > frdigits) { |
| 645 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 646 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
| 647 | *len, value, frdigits); |
| 648 | return LY_EINVAL; |
| 649 | } |
| 650 | if (fraction) { |
| 651 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 652 | } else { |
| 653 | size = (*len) + frdigits + 1; |
| 654 | } |
| 655 | *valcopy = malloc(size * sizeof **valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 656 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 657 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 658 | (*valcopy)[size - 1] = '\0'; |
| 659 | if (fraction) { |
| 660 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 661 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 662 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 663 | } else { |
| 664 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 665 | memset(&(*valcopy)[*len], '0', frdigits); |
| 666 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 667 | } |
| 668 | return LY_SUCCESS; |
| 669 | } |
| 670 | |
| 671 | static LY_ERR |
| 672 | range_part_check_ascendance(int unsigned_value, int64_t value, int64_t prev_value) |
| 673 | { |
| 674 | if (unsigned_value) { |
| 675 | if ((uint64_t)prev_value >= (uint64_t)value) { |
| 676 | return LY_EEXIST; |
| 677 | } |
| 678 | } else { |
| 679 | if (prev_value >= value) { |
| 680 | return LY_EEXIST; |
| 681 | } |
| 682 | } |
| 683 | return LY_SUCCESS; |
| 684 | } |
| 685 | |
| 686 | static LY_ERR |
| 687 | 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 | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 688 | uint8_t frdigits, const char **value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 689 | { |
| 690 | LY_ERR ret = LY_SUCCESS; |
| 691 | char *valcopy = NULL; |
| 692 | size_t len; |
| 693 | |
| 694 | if (value) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 695 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 696 | LY_CHECK_GOTO(ret, error); |
| 697 | } |
| 698 | |
| 699 | switch (basetype) { |
| 700 | case LY_TYPE_BINARY: /* length */ |
| 701 | if (valcopy) { |
| 702 | ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64); |
| 703 | } else if (max) { |
| 704 | part->max_u64 = UINT64_C(18446744073709551615); |
| 705 | } else { |
| 706 | part->min_u64 = UINT64_C(0); |
| 707 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 708 | if (!ret && !first) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 709 | ret = range_part_check_ascendance(1, max ? part->max_64 : part->min_64, prev); |
| 710 | } |
| 711 | break; |
| 712 | case LY_TYPE_DEC64: /* range */ |
| 713 | if (valcopy) { |
| 714 | ret = ly_parse_int(valcopy, INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10, |
| 715 | max ? &part->max_64 : &part->min_64); |
| 716 | } else if (max) { |
| 717 | part->max_64 = INT64_C(9223372036854775807); |
| 718 | } else { |
| 719 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 720 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 721 | if (!ret && !first) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 722 | ret = range_part_check_ascendance(0, max ? part->max_64 : part->min_64, prev); |
| 723 | } |
| 724 | break; |
| 725 | case LY_TYPE_INT8: /* range */ |
| 726 | if (valcopy) { |
| 727 | ret = ly_parse_int(valcopy, INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64); |
| 728 | } else if (max) { |
| 729 | part->max_64 = INT64_C(127); |
| 730 | } else { |
| 731 | part->min_64 = INT64_C(-128); |
| 732 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 733 | if (!ret && !first) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 734 | ret = range_part_check_ascendance(0, max ? part->max_64 : part->min_64, prev); |
| 735 | } |
| 736 | break; |
| 737 | case LY_TYPE_INT16: /* range */ |
| 738 | if (valcopy) { |
| 739 | ret = ly_parse_int(valcopy, INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64); |
| 740 | } else if (max) { |
| 741 | part->max_64 = INT64_C(32767); |
| 742 | } else { |
| 743 | part->min_64 = INT64_C(-32768); |
| 744 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 745 | if (!ret && !first) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 746 | ret = range_part_check_ascendance(0, max ? part->max_64 : part->min_64, prev); |
| 747 | } |
| 748 | break; |
| 749 | case LY_TYPE_INT32: /* range */ |
| 750 | if (valcopy) { |
| 751 | ret = ly_parse_int(valcopy, INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64); |
| 752 | } else if (max) { |
| 753 | part->max_64 = INT64_C(2147483647); |
| 754 | } else { |
| 755 | part->min_64 = INT64_C(-2147483648); |
| 756 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 757 | if (!ret && !first) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 758 | ret = range_part_check_ascendance(0, max ? part->max_64 : part->min_64, prev); |
| 759 | } |
| 760 | break; |
| 761 | case LY_TYPE_INT64: /* range */ |
| 762 | if (valcopy) { |
| 763 | ret = ly_parse_int(valcopy, INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10, |
| 764 | max ? &part->max_64 : &part->min_64); |
| 765 | } else if (max) { |
| 766 | part->max_64 = INT64_C(9223372036854775807); |
| 767 | } else { |
| 768 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 769 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 770 | if (!ret && !first) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 771 | ret = range_part_check_ascendance(0, max ? part->max_64 : part->min_64, prev); |
| 772 | } |
| 773 | break; |
| 774 | case LY_TYPE_UINT8: /* range */ |
| 775 | if (valcopy) { |
| 776 | ret = ly_parse_uint(valcopy, UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64); |
| 777 | } else if (max) { |
| 778 | part->max_u64 = UINT64_C(255); |
| 779 | } else { |
| 780 | part->min_u64 = UINT64_C(0); |
| 781 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 782 | if (!ret && !first) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 783 | ret = range_part_check_ascendance(1, max ? part->max_64 : part->min_64, prev); |
| 784 | } |
| 785 | break; |
| 786 | case LY_TYPE_UINT16: /* range */ |
| 787 | if (valcopy) { |
| 788 | ret = ly_parse_uint(valcopy, UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64); |
| 789 | } else if (max) { |
| 790 | part->max_u64 = UINT64_C(65535); |
| 791 | } else { |
| 792 | part->min_u64 = UINT64_C(0); |
| 793 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 794 | if (!ret && !first) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 795 | ret = range_part_check_ascendance(1, max ? part->max_64 : part->min_64, prev); |
| 796 | } |
| 797 | break; |
| 798 | case LY_TYPE_UINT32: /* range */ |
| 799 | if (valcopy) { |
| 800 | ret = ly_parse_uint(valcopy, UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64); |
| 801 | } else if (max) { |
| 802 | part->max_u64 = UINT64_C(4294967295); |
| 803 | } else { |
| 804 | part->min_u64 = UINT64_C(0); |
| 805 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 806 | if (!ret && !first) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 807 | ret = range_part_check_ascendance(1, max ? part->max_64 : part->min_64, prev); |
| 808 | } |
| 809 | break; |
| 810 | case LY_TYPE_UINT64: /* range */ |
| 811 | if (valcopy) { |
| 812 | ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64); |
| 813 | } else if (max) { |
| 814 | part->max_u64 = UINT64_C(18446744073709551615); |
| 815 | } else { |
| 816 | part->min_u64 = UINT64_C(0); |
| 817 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 818 | if (!ret && !first) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 819 | ret = range_part_check_ascendance(1, max ? part->max_64 : part->min_64, prev); |
| 820 | } |
| 821 | break; |
| 822 | case LY_TYPE_STRING: /* length */ |
| 823 | if (valcopy) { |
| 824 | ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64); |
| 825 | } else if (max) { |
| 826 | part->max_u64 = UINT64_C(18446744073709551615); |
| 827 | } else { |
| 828 | part->min_u64 = UINT64_C(0); |
| 829 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 830 | if (!ret && !first) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 831 | ret = range_part_check_ascendance(1, max ? part->max_64 : part->min_64, prev); |
| 832 | } |
| 833 | break; |
| 834 | default: |
| 835 | LOGINT(ctx->ctx); |
| 836 | ret = LY_EINT; |
| 837 | } |
| 838 | |
| 839 | error: |
| 840 | if (ret == LY_EDENIED) { |
| 841 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 842 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 843 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 844 | } else if (ret == LY_EVALID) { |
| 845 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 846 | "Invalid %s restriction - invalid value \"%s\".", |
| 847 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 848 | } else if (ret == LY_EEXIST) { |
| 849 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 850 | "Invalid %s restriction - values are not in ascending order (%s).", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 851 | length_restr ? "length" : "range", |
| 852 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : *value); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 853 | } else if (!ret && value) { |
| 854 | *value = *value + len; |
| 855 | } |
| 856 | free(valcopy); |
| 857 | return ret; |
| 858 | } |
| 859 | |
| 860 | static LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 861 | 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] | 862 | struct lysc_range *base_range, struct lysc_range **range) |
| 863 | { |
| 864 | LY_ERR ret = LY_EVALID; |
| 865 | const char *expr; |
| 866 | struct lysc_range_part *parts = NULL, *part; |
| 867 | int range_expected = 0, uns; |
| 868 | unsigned int parts_done = 0, u, v; |
| 869 | |
| 870 | assert(range); |
| 871 | assert(range_p); |
| 872 | |
| 873 | expr = range_p->arg; |
| 874 | while(1) { |
| 875 | if (isspace(*expr)) { |
| 876 | ++expr; |
| 877 | } else if (*expr == '\0') { |
| 878 | if (range_expected) { |
| 879 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 880 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
| 881 | length_restr ? "length" : "range", range_p->arg); |
| 882 | goto cleanup; |
| 883 | } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) { |
| 884 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 885 | "Invalid %s restriction - unexpected end of the expression (%s).", |
| 886 | length_restr ? "length" : "range", range_p->arg); |
| 887 | goto cleanup; |
| 888 | } |
| 889 | parts_done++; |
| 890 | break; |
| 891 | } else if (!strncmp(expr, "min", 3)) { |
| 892 | if (parts) { |
| 893 | /* min cannot be used elsewhere than in the first part */ |
| 894 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 895 | "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
| 896 | expr - range_p->arg, range_p->arg); |
| 897 | goto cleanup; |
| 898 | } |
| 899 | expr += 3; |
| 900 | |
| 901 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 902 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 903 | part->max_64 = part->min_64; |
| 904 | } else if (*expr == '|') { |
| 905 | if (!parts || range_expected) { |
| 906 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 907 | "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr); |
| 908 | goto cleanup; |
| 909 | } |
| 910 | expr++; |
| 911 | parts_done++; |
| 912 | /* process next part of the expression */ |
| 913 | } else if (!strncmp(expr, "..", 2)) { |
| 914 | expr += 2; |
| 915 | while (isspace(*expr)) { |
| 916 | expr++; |
| 917 | } |
| 918 | if (!parts || LY_ARRAY_SIZE(parts) == parts_done) { |
| 919 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 920 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
| 921 | goto cleanup; |
| 922 | } |
| 923 | /* continue expecting the upper boundary */ |
| 924 | range_expected = 1; |
| 925 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 926 | /* number */ |
| 927 | if (range_expected) { |
| 928 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 929 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 930 | range_expected = 0; |
| 931 | } else { |
| 932 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 933 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0, |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 934 | basetype, parts_done ? 0 : 1, length_restr, frdigits, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 935 | part->max_64 = part->min_64; |
| 936 | } |
| 937 | |
| 938 | /* continue with possible another expression part */ |
| 939 | } else if (!strncmp(expr, "max", 3)) { |
| 940 | expr += 3; |
| 941 | while (isspace(*expr)) { |
| 942 | expr++; |
| 943 | } |
| 944 | if (*expr != '\0') { |
| 945 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).", |
| 946 | length_restr ? "length" : "range", expr); |
| 947 | goto cleanup; |
| 948 | } |
| 949 | if (range_expected) { |
| 950 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 951 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 952 | range_expected = 0; |
| 953 | } else { |
| 954 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 955 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0, |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 956 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 957 | part->min_64 = part->max_64; |
| 958 | } |
| 959 | } else { |
| 960 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).", |
| 961 | length_restr ? "length" : "range", expr); |
| 962 | goto cleanup; |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | /* check with the previous range/length restriction */ |
| 967 | if (base_range) { |
| 968 | switch (basetype) { |
| 969 | case LY_TYPE_BINARY: |
| 970 | case LY_TYPE_UINT8: |
| 971 | case LY_TYPE_UINT16: |
| 972 | case LY_TYPE_UINT32: |
| 973 | case LY_TYPE_UINT64: |
| 974 | case LY_TYPE_STRING: |
| 975 | uns = 1; |
| 976 | break; |
| 977 | case LY_TYPE_DEC64: |
| 978 | case LY_TYPE_INT8: |
| 979 | case LY_TYPE_INT16: |
| 980 | case LY_TYPE_INT32: |
| 981 | case LY_TYPE_INT64: |
| 982 | uns = 0; |
| 983 | break; |
| 984 | default: |
| 985 | LOGINT(ctx->ctx); |
| 986 | ret = LY_EINT; |
| 987 | goto cleanup; |
| 988 | } |
| 989 | for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) { |
| 990 | if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) { |
| 991 | goto baseerror; |
| 992 | } |
| 993 | /* current lower bound is not lower than the base */ |
| 994 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 995 | /* base has single value */ |
| 996 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 997 | /* both lower bounds are the same */ |
| 998 | if (parts[u].min_64 != parts[u].max_64) { |
| 999 | /* current continues with a range */ |
| 1000 | goto baseerror; |
| 1001 | } else { |
| 1002 | /* equal single values, move both forward */ |
| 1003 | ++v; |
| 1004 | continue; |
| 1005 | } |
| 1006 | } else { |
| 1007 | /* base is single value lower than current range, so the |
| 1008 | * value from base range is removed in the current, |
| 1009 | * move only base and repeat checking */ |
| 1010 | ++v; |
| 1011 | --u; |
| 1012 | continue; |
| 1013 | } |
| 1014 | } else { |
| 1015 | /* base is the range */ |
| 1016 | if (parts[u].min_64 == parts[u].max_64) { |
| 1017 | /* current is a single value */ |
| 1018 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1019 | /* current is behind the base range, so base range is omitted, |
| 1020 | * move the base and keep the current for further check */ |
| 1021 | ++v; |
| 1022 | --u; |
| 1023 | } /* else it is within the base range, so move the current, but keep the base */ |
| 1024 | continue; |
| 1025 | } else { |
| 1026 | /* both are ranges - check the higher bound, the lower was already checked */ |
| 1027 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1028 | /* higher bound is higher than the current higher bound */ |
| 1029 | if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) { |
| 1030 | /* but the current lower bound is also higher, so the base range is omitted, |
| 1031 | * continue with the same current, but move the base */ |
| 1032 | --u; |
| 1033 | ++v; |
| 1034 | continue; |
| 1035 | } |
| 1036 | /* current range starts within the base range but end behind it */ |
| 1037 | goto baseerror; |
| 1038 | } else { |
| 1039 | /* current range is smaller than the base, |
| 1040 | * move current, but stay with the base */ |
| 1041 | continue; |
| 1042 | } |
| 1043 | } |
| 1044 | } |
| 1045 | } |
| 1046 | if (u != parts_done) { |
| 1047 | baseerror: |
| 1048 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1049 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
| 1050 | length_restr ? "length" : "range", range_p->arg); |
| 1051 | goto cleanup; |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | if (!(*range)) { |
| 1056 | *range = calloc(1, sizeof **range); |
| 1057 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 1058 | } |
| 1059 | |
| 1060 | if (range_p->eapptag) { |
| 1061 | lydict_remove(ctx->ctx, (*range)->eapptag); |
| 1062 | (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0); |
| 1063 | } |
| 1064 | if (range_p->emsg) { |
| 1065 | lydict_remove(ctx->ctx, (*range)->emsg); |
| 1066 | (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0); |
| 1067 | } |
| 1068 | /* extensions are taken only from the last range by the caller */ |
| 1069 | |
| 1070 | (*range)->parts = parts; |
| 1071 | parts = NULL; |
| 1072 | ret = LY_SUCCESS; |
| 1073 | cleanup: |
| 1074 | /* TODO clean up */ |
| 1075 | LY_ARRAY_FREE(parts); |
| 1076 | |
| 1077 | return ret; |
| 1078 | } |
| 1079 | |
| 1080 | /** |
| 1081 | * @brief Checks pattern syntax. |
| 1082 | * |
| 1083 | * @param[in] pattern Pattern to check. |
| 1084 | * @param[out] pcre_precomp Precompiled PCRE pattern. Can be NULL. |
| 1085 | * @return EXIT_SUCCESS on success, EXIT_FAILURE otherwise. |
| 1086 | */ |
| 1087 | static LY_ERR |
| 1088 | lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre **pcre_precomp) |
| 1089 | { |
| 1090 | int idx, idx2, start, end, err_offset, count; |
| 1091 | char *perl_regex, *ptr; |
| 1092 | const char *err_msg, *orig_ptr; |
| 1093 | pcre *precomp; |
| 1094 | #define URANGE_LEN 19 |
| 1095 | char *ublock2urange[][2] = { |
| 1096 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 1097 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 1098 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 1099 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 1100 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 1101 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 1102 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 1103 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 1104 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 1105 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 1106 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 1107 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 1108 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 1109 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 1110 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 1111 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 1112 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 1113 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 1114 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 1115 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 1116 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 1117 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 1118 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 1119 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 1120 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 1121 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 1122 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 1123 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 1124 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 1125 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 1126 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 1127 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 1128 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 1129 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 1130 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 1131 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 1132 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 1133 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 1134 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 1135 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 1136 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 1137 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 1138 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 1139 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 1140 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 1141 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 1142 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 1143 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 1144 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 1145 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 1146 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 1147 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 1148 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 1149 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 1150 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 1151 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 1152 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 1153 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 1154 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 1155 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 1156 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 1157 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 1158 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 1159 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 1160 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 1161 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 1162 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 1163 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 1164 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 1165 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 1166 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 1167 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 1168 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 1169 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 1170 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 1171 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 1172 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 1173 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 1174 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 1175 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 1176 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 1177 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 1178 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
| 1179 | {NULL, NULL} |
| 1180 | }; |
| 1181 | |
| 1182 | /* adjust the expression to a Perl equivalent |
| 1183 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 1184 | |
| 1185 | /* we need to replace all "$" with "\$", count them now */ |
| 1186 | for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$")); |
| 1187 | |
| 1188 | perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char)); |
| 1189 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM); |
| 1190 | perl_regex[0] = '\0'; |
| 1191 | |
| 1192 | ptr = perl_regex; |
| 1193 | |
| 1194 | if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) { |
| 1195 | /* we will add line-end anchoring */ |
| 1196 | ptr[0] = '('; |
| 1197 | ++ptr; |
| 1198 | } |
| 1199 | |
| 1200 | for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) { |
| 1201 | if (orig_ptr[0] == '$') { |
| 1202 | ptr += sprintf(ptr, "\\$"); |
| 1203 | } else if (orig_ptr[0] == '^') { |
| 1204 | ptr += sprintf(ptr, "\\^"); |
| 1205 | } else { |
| 1206 | ptr[0] = orig_ptr[0]; |
| 1207 | ++ptr; |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) { |
| 1212 | ptr += sprintf(ptr, ")$"); |
| 1213 | } else { |
| 1214 | ptr[0] = '\0'; |
| 1215 | ++ptr; |
| 1216 | } |
| 1217 | |
| 1218 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 1219 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 1220 | start = ptr - perl_regex; |
| 1221 | |
| 1222 | ptr = strchr(ptr, '}'); |
| 1223 | if (!ptr) { |
| 1224 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, |
| 1225 | pattern, perl_regex + start + 2, "unterminated character property"); |
| 1226 | free(perl_regex); |
| 1227 | return LY_EVALID; |
| 1228 | } |
| 1229 | end = (ptr - perl_regex) + 1; |
| 1230 | |
| 1231 | /* need more space */ |
| 1232 | if (end - start < URANGE_LEN) { |
| 1233 | perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1); |
| 1234 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM); |
| 1235 | } |
| 1236 | |
| 1237 | /* find our range */ |
| 1238 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 1239 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 1240 | break; |
| 1241 | } |
| 1242 | } |
| 1243 | if (!ublock2urange[idx][0]) { |
| 1244 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, |
| 1245 | pattern, perl_regex + start + 5, "unknown block name"); |
| 1246 | free(perl_regex); |
| 1247 | return LY_EVALID; |
| 1248 | } |
| 1249 | |
| 1250 | /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */ |
| 1251 | for (idx2 = 0, count = 0; idx2 < start; ++idx2) { |
| 1252 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 1253 | ++count; |
| 1254 | } |
| 1255 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 1256 | --count; |
| 1257 | } |
| 1258 | } |
| 1259 | if (count) { |
| 1260 | /* skip brackets */ |
| 1261 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 1262 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 1263 | } else { |
| 1264 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 1265 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | /* must return 0, already checked during parsing */ |
| 1270 | precomp = pcre_compile(perl_regex, PCRE_UTF8 | PCRE_ANCHORED | PCRE_DOLLAR_ENDONLY | PCRE_NO_AUTO_CAPTURE, |
| 1271 | &err_msg, &err_offset, NULL); |
| 1272 | if (!precomp) { |
| 1273 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg); |
| 1274 | free(perl_regex); |
| 1275 | return LY_EVALID; |
| 1276 | } |
| 1277 | free(perl_regex); |
| 1278 | |
| 1279 | if (pcre_precomp) { |
| 1280 | *pcre_precomp = precomp; |
| 1281 | } else { |
| 1282 | free(precomp); |
| 1283 | } |
| 1284 | |
| 1285 | return LY_SUCCESS; |
| 1286 | |
| 1287 | #undef URANGE_LEN |
| 1288 | } |
| 1289 | |
| 1290 | static LY_ERR |
| 1291 | lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p, int options, |
| 1292 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
| 1293 | { |
| 1294 | struct lysc_pattern **pattern; |
| 1295 | unsigned int u, v; |
| 1296 | const char *err_msg; |
| 1297 | LY_ERR ret = LY_SUCCESS; |
| 1298 | |
| 1299 | /* first, copy the patterns from the base type */ |
| 1300 | if (base_patterns) { |
| 1301 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 1302 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 1303 | } |
| 1304 | |
| 1305 | LY_ARRAY_FOR(patterns_p, u) { |
| 1306 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 1307 | *pattern = calloc(1, sizeof **pattern); |
| 1308 | ++(*pattern)->refcount; |
| 1309 | |
| 1310 | ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->expr); |
| 1311 | LY_CHECK_RET(ret); |
| 1312 | (*pattern)->expr_extra = pcre_study((*pattern)->expr, 0, &err_msg); |
| 1313 | if (err_msg) { |
| 1314 | LOGWRN(ctx->ctx, "Studying pattern \"%s\" failed (%s).", pattern, err_msg); |
| 1315 | } |
| 1316 | |
| 1317 | if (patterns_p[u].arg[0] == 0x15) { |
| 1318 | (*pattern)->inverted = 1; |
| 1319 | } |
| 1320 | DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag); |
| 1321 | DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg); |
| 1322 | COMPILE_ARRAY_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, |
| 1323 | options, v, lys_compile_ext, ret, done); |
| 1324 | } |
| 1325 | done: |
| 1326 | return ret; |
| 1327 | } |
| 1328 | |
| 1329 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 1330 | 0 /* LY_TYPE_UNKNOWN */, |
| 1331 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
| 1332 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 1333 | 0 /* LY_TYPE_BOOL */, |
| 1334 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 1335 | 0 /* LY_TYPE_EMPTY */, |
| 1336 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 1337 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 1338 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 1339 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
| 1340 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
| 1341 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 1342 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
| 1343 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 1344 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
| 1345 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 1346 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
| 1347 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 1348 | LYS_SET_RANGE /* LY_TYPE_INT64 */, |
| 1349 | LYS_SET_RANGE /* LY_TYPE_UINT64 */ |
| 1350 | }; |
| 1351 | |
| 1352 | static LY_ERR |
| 1353 | lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype, int options, |
| 1354 | struct lysc_type_enum_item *base_enums, struct lysc_type_enum_item **enums) |
| 1355 | { |
| 1356 | LY_ERR ret = LY_SUCCESS; |
| 1357 | unsigned int u, v, match; |
| 1358 | int32_t value = 0; |
| 1359 | uint32_t position = 0; |
| 1360 | struct lysc_type_enum_item *e, storage; |
| 1361 | |
| 1362 | if (base_enums && ctx->mod->compiled->version < 2) { |
| 1363 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
| 1364 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
| 1365 | return LY_EVALID; |
| 1366 | } |
| 1367 | |
| 1368 | LY_ARRAY_FOR(enums_p, u) { |
| 1369 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
| 1370 | DUP_STRING(ctx->ctx, enums_p[u].name, e->name); |
| 1371 | if (base_enums) { |
| 1372 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 1373 | LY_ARRAY_FOR(base_enums, v) { |
| 1374 | if (!strcmp(e->name, base_enums[v].name)) { |
| 1375 | break; |
| 1376 | } |
| 1377 | } |
| 1378 | if (v == LY_ARRAY_SIZE(base_enums)) { |
| 1379 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1380 | "Invalid %s - derived type adds new item \"%s\".", |
| 1381 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
| 1382 | return LY_EVALID; |
| 1383 | } |
| 1384 | match = v; |
| 1385 | } |
| 1386 | |
| 1387 | if (basetype == LY_TYPE_ENUM) { |
| 1388 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 1389 | e->value = (int32_t)enums_p[u].value; |
| 1390 | if (!u || e->value >= value) { |
| 1391 | value = e->value + 1; |
| 1392 | } |
| 1393 | /* check collision with other values */ |
| 1394 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 1395 | if (e->value == (*enums)[v].value) { |
| 1396 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1397 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 1398 | e->value, e->name, (*enums)[v].name); |
| 1399 | return LY_EVALID; |
| 1400 | } |
| 1401 | } |
| 1402 | } else if (base_enums) { |
| 1403 | /* inherit the assigned value */ |
| 1404 | e->value = base_enums[match].value; |
| 1405 | if (!u || e->value >= value) { |
| 1406 | value = e->value + 1; |
| 1407 | } |
| 1408 | } else { |
| 1409 | /* assign value automatically */ |
| 1410 | if (u && value == INT32_MIN) { |
| 1411 | /* counter overflow */ |
| 1412 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1413 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 1414 | "\"%s\" since the highest value is already 2147483647.", e->name); |
| 1415 | return LY_EVALID; |
| 1416 | } |
| 1417 | e->value = value++; |
| 1418 | } |
| 1419 | } else { /* LY_TYPE_BITS */ |
| 1420 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 1421 | e->value = (int32_t)enums_p[u].value; |
| 1422 | if (!u || (uint32_t)e->value >= position) { |
| 1423 | position = (uint32_t)e->value + 1; |
| 1424 | } |
| 1425 | /* check collision with other values */ |
| 1426 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 1427 | if (e->value == (*enums)[v].value) { |
| 1428 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1429 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
| 1430 | (uint32_t)e->value, e->name, (*enums)[v].name); |
| 1431 | return LY_EVALID; |
| 1432 | } |
| 1433 | } |
| 1434 | } else if (base_enums) { |
| 1435 | /* inherit the assigned value */ |
| 1436 | e->value = base_enums[match].value; |
| 1437 | if (!u || (uint32_t)e->value >= position) { |
| 1438 | position = (uint32_t)e->value + 1; |
| 1439 | } |
| 1440 | } else { |
| 1441 | /* assign value automatically */ |
| 1442 | if (u && position == 0) { |
| 1443 | /* counter overflow */ |
| 1444 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1445 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 1446 | "\"%s\" since the highest value is already 4294967295.", e->name); |
| 1447 | return LY_EVALID; |
| 1448 | } |
| 1449 | e->value = position++; |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | if (base_enums) { |
| 1454 | /* the assigned values must not change from the derived type */ |
| 1455 | if (e->value != base_enums[match].value) { |
| 1456 | if (basetype == LY_TYPE_ENUM) { |
| 1457 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1458 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
| 1459 | e->name, base_enums[match].value, e->value); |
| 1460 | } else { |
| 1461 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1462 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 1463 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
| 1464 | } |
| 1465 | return LY_EVALID; |
| 1466 | } |
| 1467 | } |
| 1468 | |
| 1469 | COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, options, v, lys_compile_iffeature, ret, done); |
| 1470 | COMPILE_ARRAY_GOTO(ctx, enums_p[u].exts, e->exts, options, v, lys_compile_ext, ret, done); |
| 1471 | |
| 1472 | if (basetype == LY_TYPE_BITS) { |
| 1473 | /* keep bits ordered by position */ |
| 1474 | for (v = u; v && (*enums)[v - 1].value > e->value; --v); |
| 1475 | if (v != u) { |
| 1476 | memcpy(&storage, e, sizeof *e); |
| 1477 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 1478 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 1479 | } |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | done: |
| 1484 | return ret; |
| 1485 | } |
| 1486 | |
| 1487 | static LY_ERR |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame^] | 1488 | lys_compile_type_(struct lysc_ctx *ctx, struct lysp_type *type_p, LY_DATA_TYPE basetype, int options, const char *tpdfname, |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 1489 | struct lysc_type *base, struct lysc_type **type) |
| 1490 | { |
| 1491 | LY_ERR ret = LY_SUCCESS; |
| 1492 | unsigned int u; |
| 1493 | struct lysc_type_bin *bin; |
| 1494 | struct lysc_type_num *num; |
| 1495 | struct lysc_type_str *str; |
| 1496 | struct lysc_type_bits *bits; |
| 1497 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1498 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame^] | 1499 | struct lysc_type_identityref *idref; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 1500 | |
| 1501 | switch (basetype) { |
| 1502 | case LY_TYPE_BINARY: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 1503 | bin = (struct lysc_type_bin*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame^] | 1504 | |
| 1505 | /* 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] | 1506 | if (type_p->length) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1507 | ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 1508 | base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length); |
| 1509 | LY_CHECK_RET(ret); |
| 1510 | if (!tpdfname) { |
| 1511 | COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts, |
| 1512 | options, u, lys_compile_ext, ret, done); |
| 1513 | } |
| 1514 | } |
| 1515 | |
| 1516 | if (tpdfname) { |
| 1517 | type_p->compiled = *type; |
| 1518 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 1519 | } |
| 1520 | break; |
| 1521 | case LY_TYPE_BITS: |
| 1522 | /* RFC 7950 9.7 - bits */ |
| 1523 | bits = (struct lysc_type_bits*)(*type); |
| 1524 | if (type_p->bits) { |
| 1525 | ret = lys_compile_type_enums(ctx, type_p->bits, basetype, options, |
| 1526 | base ? (struct lysc_type_enum_item*)((struct lysc_type_bits*)base)->bits : NULL, |
| 1527 | (struct lysc_type_enum_item**)&bits->bits); |
| 1528 | LY_CHECK_RET(ret); |
| 1529 | } |
| 1530 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame^] | 1531 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 1532 | /* 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] | 1533 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame^] | 1534 | 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] | 1535 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame^] | 1536 | 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] | 1537 | free(*type); |
| 1538 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 1539 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1540 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | if (tpdfname) { |
| 1544 | type_p->compiled = *type; |
| 1545 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 1546 | } |
| 1547 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1548 | case LY_TYPE_DEC64: |
| 1549 | dec = (struct lysc_type_dec*)(*type); |
| 1550 | |
| 1551 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame^] | 1552 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 1553 | if (!type_p->fraction_digits) { |
| 1554 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame^] | 1555 | 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] | 1556 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame^] | 1557 | 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] | 1558 | free(*type); |
| 1559 | *type = NULL; |
| 1560 | } |
| 1561 | return LY_EVALID; |
| 1562 | } |
| 1563 | } else if (type_p->fraction_digits) { |
| 1564 | /* 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] | 1565 | if (tpdfname) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 1566 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1567 | "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] | 1568 | tpdfname); |
| 1569 | } else { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 1570 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1571 | "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] | 1572 | free(*type); |
| 1573 | *type = NULL; |
| 1574 | } |
| 1575 | return LY_EVALID; |
| 1576 | } |
| 1577 | dec->fraction_digits = type_p->fraction_digits; |
| 1578 | |
| 1579 | /* RFC 7950 9.2.4 - range */ |
| 1580 | if (type_p->range) { |
| 1581 | ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits, |
| 1582 | base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range); |
| 1583 | LY_CHECK_RET(ret); |
| 1584 | if (!tpdfname) { |
| 1585 | COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts, |
| 1586 | options, u, lys_compile_ext, ret, done); |
| 1587 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1588 | } |
| 1589 | |
| 1590 | if (tpdfname) { |
| 1591 | type_p->compiled = *type; |
| 1592 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 1593 | } |
| 1594 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 1595 | case LY_TYPE_STRING: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 1596 | str = (struct lysc_type_str*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame^] | 1597 | |
| 1598 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 1599 | if (type_p->length) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1600 | ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 1601 | base ? ((struct lysc_type_str*)base)->length : NULL, &str->length); |
| 1602 | LY_CHECK_RET(ret); |
| 1603 | if (!tpdfname) { |
| 1604 | COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts, |
| 1605 | options, u, lys_compile_ext, ret, done); |
| 1606 | } |
| 1607 | } else if (base && ((struct lysc_type_str*)base)->length) { |
| 1608 | str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length); |
| 1609 | } |
| 1610 | |
| 1611 | /* RFC 7950 9.4.5 - pattern */ |
| 1612 | if (type_p->patterns) { |
| 1613 | ret = lys_compile_type_patterns(ctx, type_p->patterns, options, |
| 1614 | base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns); |
| 1615 | LY_CHECK_RET(ret); |
| 1616 | } else if (base && ((struct lysc_type_str*)base)->patterns) { |
| 1617 | str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns); |
| 1618 | } |
| 1619 | |
| 1620 | if (tpdfname) { |
| 1621 | type_p->compiled = *type; |
| 1622 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 1623 | } |
| 1624 | break; |
| 1625 | case LY_TYPE_ENUM: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 1626 | enumeration = (struct lysc_type_enum*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame^] | 1627 | |
| 1628 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 1629 | if (type_p->enums) { |
| 1630 | ret = lys_compile_type_enums(ctx, type_p->enums, basetype, options, |
| 1631 | base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums); |
| 1632 | LY_CHECK_RET(ret); |
| 1633 | } |
| 1634 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame^] | 1635 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 1636 | /* 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] | 1637 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame^] | 1638 | 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] | 1639 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame^] | 1640 | 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] | 1641 | free(*type); |
| 1642 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 1643 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1644 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 1645 | } |
| 1646 | |
| 1647 | if (tpdfname) { |
| 1648 | type_p->compiled = *type; |
| 1649 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 1650 | } |
| 1651 | break; |
| 1652 | case LY_TYPE_INT8: |
| 1653 | case LY_TYPE_UINT8: |
| 1654 | case LY_TYPE_INT16: |
| 1655 | case LY_TYPE_UINT16: |
| 1656 | case LY_TYPE_INT32: |
| 1657 | case LY_TYPE_UINT32: |
| 1658 | case LY_TYPE_INT64: |
| 1659 | case LY_TYPE_UINT64: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 1660 | num = (struct lysc_type_num*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame^] | 1661 | |
| 1662 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 1663 | if (type_p->range) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1664 | ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, 0, |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 1665 | base ? ((struct lysc_type_num*)base)->range : NULL, &num->range); |
| 1666 | LY_CHECK_RET(ret); |
| 1667 | if (!tpdfname) { |
| 1668 | COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts, |
| 1669 | options, u, lys_compile_ext, ret, done); |
| 1670 | } |
| 1671 | } |
| 1672 | |
| 1673 | if (tpdfname) { |
| 1674 | type_p->compiled = *type; |
| 1675 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 1676 | } |
| 1677 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame^] | 1678 | case LY_TYPE_IDENT: |
| 1679 | idref = (struct lysc_type_identityref*)(*type); |
| 1680 | |
| 1681 | /* RFC 7950 9.10.2 - base */ |
| 1682 | if (type_p->bases) { |
| 1683 | if (base) { |
| 1684 | /* only the directly derived identityrefs can contain base specification */ |
| 1685 | if (tpdfname) { |
| 1686 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1687 | "Invalid base substatement for type \"%s\" not directly derived from identityref built-in type.", |
| 1688 | tpdfname); |
| 1689 | } else { |
| 1690 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1691 | "Invalid base substatement for type not directly derived from identityref built-in type."); |
| 1692 | free(*type); |
| 1693 | *type = NULL; |
| 1694 | } |
| 1695 | return LY_EVALID; |
| 1696 | } |
| 1697 | ret = lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases); |
| 1698 | LY_CHECK_RET(ret); |
| 1699 | } |
| 1700 | |
| 1701 | if (!base && !type_p->flags) { |
| 1702 | /* type derived from identityref built-in type must contain at least one base */ |
| 1703 | if (tpdfname) { |
| 1704 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 1705 | } else { |
| 1706 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
| 1707 | free(*type); |
| 1708 | *type = NULL; |
| 1709 | } |
| 1710 | return LY_EVALID; |
| 1711 | } |
| 1712 | |
| 1713 | if (tpdfname) { |
| 1714 | type_p->compiled = *type; |
| 1715 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 1716 | } |
| 1717 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 1718 | case LY_TYPE_INST: |
| 1719 | /* RFC 7950 9.9.3 - require-instance */ |
| 1720 | if (type_p->flags & LYS_SET_REQINST) { |
| 1721 | ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance; |
| 1722 | } else { |
| 1723 | /* default is true */ |
| 1724 | ((struct lysc_type_instanceid*)(*type))->require_instance = 1; |
| 1725 | } |
| 1726 | |
| 1727 | if (tpdfname) { |
| 1728 | type_p->compiled = *type; |
| 1729 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 1730 | } |
| 1731 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 1732 | case LY_TYPE_BOOL: |
| 1733 | case LY_TYPE_EMPTY: |
| 1734 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 1735 | break; |
| 1736 | } |
| 1737 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
| 1738 | done: |
| 1739 | return ret; |
| 1740 | } |
| 1741 | |
| 1742 | static LY_ERR |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1743 | lys_compile_type(struct lysc_ctx *ctx, struct lysp_node_leaf *leaf_p, int options, struct lysc_type **type) |
| 1744 | { |
| 1745 | LY_ERR ret = LY_SUCCESS; |
| 1746 | unsigned int u; |
| 1747 | struct lysp_type *type_p = &leaf_p->type; |
| 1748 | struct type_context { |
| 1749 | const struct lysp_tpdf *tpdf; |
| 1750 | struct lysp_node *node; |
| 1751 | struct lysp_module *mod; |
| 1752 | } *tctx, *tctx_prev = NULL; |
| 1753 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 1754 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1755 | struct ly_set tpdf_chain = {0}; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1756 | |
| 1757 | (*type) = NULL; |
| 1758 | |
| 1759 | tctx = calloc(1, sizeof *tctx); |
| 1760 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 1761 | for (ret = lysp_type_find(type_p->name, (struct lysp_node*)leaf_p, ctx->mod->parsed, |
| 1762 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
| 1763 | ret == LY_SUCCESS; |
| 1764 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
| 1765 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
| 1766 | if (basetype) { |
| 1767 | break; |
| 1768 | } |
| 1769 | |
| 1770 | /* check status */ |
| 1771 | ret = lysc_check_status(ctx, leaf_p->flags, ctx->mod->parsed, leaf_p->name, |
| 1772 | tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->mod->name); |
| 1773 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 1774 | |
| 1775 | if (tctx->tpdf->type.compiled) { |
| 1776 | /* it is not necessary to continue, the rest of the chain was already compiled */ |
| 1777 | basetype = tctx->tpdf->type.compiled->basetype; |
| 1778 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 1779 | tctx = NULL; |
| 1780 | break; |
| 1781 | } |
| 1782 | |
| 1783 | /* store information for the following processing */ |
| 1784 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 1785 | |
| 1786 | /* prepare next loop */ |
| 1787 | tctx_prev = tctx; |
| 1788 | tctx = calloc(1, sizeof *tctx); |
| 1789 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 1790 | } |
| 1791 | free(tctx); |
| 1792 | |
| 1793 | /* allocate type according to the basetype */ |
| 1794 | switch (basetype) { |
| 1795 | case LY_TYPE_BINARY: |
| 1796 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1797 | break; |
| 1798 | case LY_TYPE_BITS: |
| 1799 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1800 | break; |
| 1801 | case LY_TYPE_BOOL: |
| 1802 | case LY_TYPE_EMPTY: |
| 1803 | *type = calloc(1, sizeof(struct lysc_type)); |
| 1804 | break; |
| 1805 | case LY_TYPE_DEC64: |
| 1806 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 1807 | break; |
| 1808 | case LY_TYPE_ENUM: |
| 1809 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1810 | break; |
| 1811 | case LY_TYPE_IDENT: |
| 1812 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 1813 | break; |
| 1814 | case LY_TYPE_INST: |
| 1815 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 1816 | break; |
| 1817 | case LY_TYPE_LEAFREF: |
| 1818 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 1819 | break; |
| 1820 | case LY_TYPE_STRING: |
| 1821 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1822 | break; |
| 1823 | case LY_TYPE_UNION: |
| 1824 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 1825 | break; |
| 1826 | case LY_TYPE_INT8: |
| 1827 | case LY_TYPE_UINT8: |
| 1828 | case LY_TYPE_INT16: |
| 1829 | case LY_TYPE_UINT16: |
| 1830 | case LY_TYPE_INT32: |
| 1831 | case LY_TYPE_UINT32: |
| 1832 | case LY_TYPE_INT64: |
| 1833 | case LY_TYPE_UINT64: |
| 1834 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1835 | break; |
| 1836 | case LY_TYPE_UNKNOWN: |
| 1837 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1838 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 1839 | ret = LY_EVALID; |
| 1840 | goto cleanup; |
| 1841 | } |
| 1842 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
| 1843 | if (~type_substmt_map[basetype] & leaf_p->type.flags) { |
| 1844 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
| 1845 | ly_data_type2str[basetype]); |
| 1846 | free(*type); |
| 1847 | (*type) = NULL; |
| 1848 | ret = LY_EVALID; |
| 1849 | goto cleanup; |
| 1850 | } |
| 1851 | |
| 1852 | /* get restrictions from the referred typedefs */ |
| 1853 | for (u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
| 1854 | tctx = (struct type_context*)tpdf_chain.objs[u]; |
| 1855 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 1856 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
| 1857 | tctx->tpdf->name, ly_data_type2str[basetype]); |
| 1858 | ret = LY_EVALID; |
| 1859 | goto cleanup; |
| 1860 | } else if (tctx->tpdf->type.compiled) { |
| 1861 | base = tctx->tpdf->type.compiled; |
| 1862 | continue; |
| 1863 | } else if ((u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) { |
| 1864 | /* no change, just use the type information from the base */ |
| 1865 | base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled; |
| 1866 | ++base->refcount; |
| 1867 | continue; |
| 1868 | } |
| 1869 | |
| 1870 | ++(*type)->refcount; |
| 1871 | (*type)->basetype = basetype; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 1872 | prev_type = *type; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame^] | 1873 | ret = lys_compile_type_(ctx, &((struct lysp_tpdf*)tctx->tpdf)->type, basetype, options, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 1874 | LY_CHECK_GOTO(ret, cleanup); |
| 1875 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1876 | } |
| 1877 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 1878 | /* process the type definition in leaf */ |
| 1879 | if (leaf_p->type.flags || !base) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1880 | /* get restrictions from the node itself, finalize the type structure */ |
| 1881 | (*type)->basetype = basetype; |
| 1882 | ++(*type)->refcount; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame^] | 1883 | ret = lys_compile_type_(ctx, &leaf_p->type, basetype, options, NULL, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 1884 | LY_CHECK_GOTO(ret, cleanup); |
| 1885 | } else { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1886 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 1887 | free(*type); |
| 1888 | (*type) = base; |
| 1889 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, options, u, lys_compile_ext, ret, cleanup); |
| 1893 | |
| 1894 | cleanup: |
| 1895 | ly_set_erase(&tpdf_chain, free); |
| 1896 | return ret; |
| 1897 | } |
| 1898 | |
| 1899 | static LY_ERR lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *parent); |
| 1900 | |
| 1901 | static LY_ERR |
| 1902 | lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node) |
| 1903 | { |
| 1904 | struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p; |
| 1905 | struct lysc_node_container *cont = (struct lysc_node_container*)node; |
| 1906 | struct lysp_node *child_p; |
| 1907 | unsigned int u; |
| 1908 | LY_ERR ret = LY_SUCCESS; |
| 1909 | |
| 1910 | COMPILE_MEMBER_GOTO(ctx, cont_p->when, cont->when, options, lys_compile_when, ret, done); |
| 1911 | COMPILE_ARRAY_GOTO(ctx, cont_p->iffeatures, cont->iffeatures, options, u, lys_compile_iffeature, ret, done); |
| 1912 | |
| 1913 | LY_LIST_FOR(cont_p->child, child_p) { |
| 1914 | LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node)); |
| 1915 | } |
| 1916 | |
| 1917 | COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, options, u, lys_compile_must, ret, done); |
| 1918 | //COMPILE_ARRAY_GOTO(ctx, cont_p->actions, cont->actions, options, u, lys_compile_action, ret, done); |
| 1919 | //COMPILE_ARRAY_GOTO(ctx, cont_p->notifs, cont->notifs, options, u, lys_compile_notif, ret, done); |
| 1920 | |
| 1921 | done: |
| 1922 | return ret; |
| 1923 | } |
| 1924 | |
| 1925 | static LY_ERR |
| 1926 | lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node) |
| 1927 | { |
| 1928 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p; |
| 1929 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 1930 | unsigned int u; |
| 1931 | LY_ERR ret = LY_SUCCESS; |
| 1932 | |
| 1933 | COMPILE_MEMBER_GOTO(ctx, leaf_p->when, leaf->when, options, lys_compile_when, ret, done); |
| 1934 | COMPILE_ARRAY_GOTO(ctx, leaf_p->iffeatures, leaf->iffeatures, options, u, lys_compile_iffeature, ret, done); |
| 1935 | |
| 1936 | COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, options, u, lys_compile_must, ret, done); |
| 1937 | ret = lys_compile_type(ctx, leaf_p, options, &leaf->type); |
| 1938 | LY_CHECK_GOTO(ret, done); |
| 1939 | |
| 1940 | DUP_STRING(ctx->ctx, leaf_p->units, leaf->units); |
| 1941 | DUP_STRING(ctx->ctx, leaf_p->dflt, leaf->dflt); |
| 1942 | done: |
| 1943 | return ret; |
| 1944 | } |
| 1945 | |
| 1946 | static LY_ERR |
| 1947 | lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *parent) |
| 1948 | { |
| 1949 | LY_ERR ret = LY_EVALID; |
| 1950 | struct lysc_node *node, **children; |
| 1951 | unsigned int u; |
| 1952 | LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, int, struct lysc_node*); |
| 1953 | |
| 1954 | switch (node_p->nodetype) { |
| 1955 | case LYS_CONTAINER: |
| 1956 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container)); |
| 1957 | node_compile_spec = lys_compile_node_container; |
| 1958 | break; |
| 1959 | case LYS_LEAF: |
| 1960 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf)); |
| 1961 | node_compile_spec = lys_compile_node_leaf; |
| 1962 | break; |
| 1963 | case LYS_LIST: |
| 1964 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list)); |
| 1965 | break; |
| 1966 | case LYS_LEAFLIST: |
| 1967 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist)); |
| 1968 | break; |
| 1969 | case LYS_CASE: |
| 1970 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_case)); |
| 1971 | break; |
| 1972 | case LYS_CHOICE: |
| 1973 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice)); |
| 1974 | break; |
| 1975 | case LYS_USES: |
| 1976 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_uses)); |
| 1977 | break; |
| 1978 | case LYS_ANYXML: |
| 1979 | case LYS_ANYDATA: |
| 1980 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata)); |
| 1981 | break; |
| 1982 | default: |
| 1983 | LOGINT(ctx->ctx); |
| 1984 | return LY_EINT; |
| 1985 | } |
| 1986 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 1987 | node->nodetype = node_p->nodetype; |
| 1988 | node->module = ctx->mod; |
| 1989 | node->prev = node; |
| 1990 | node->flags = node_p->flags; |
| 1991 | |
| 1992 | /* config */ |
| 1993 | if (!(node->flags & LYS_CONFIG_MASK)) { |
| 1994 | /* config not explicitely set, inherit it from parent */ |
| 1995 | if (parent) { |
| 1996 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 1997 | } else { |
| 1998 | /* default is config true */ |
| 1999 | node->flags |= LYS_CONFIG_W; |
| 2000 | } |
| 2001 | } |
| 2002 | |
| 2003 | /* status - it is not inherited by specification, but it does not make sense to have |
| 2004 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 2005 | if (!(node->flags & LYS_STATUS_MASK)) { |
| 2006 | if (parent && (parent->flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT))) { |
| 2007 | LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.", |
| 2008 | (parent->flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 2009 | node->flags |= parent->flags & LYS_STATUS_MASK; |
| 2010 | } else { |
| 2011 | node->flags |= LYS_STATUS_CURR; |
| 2012 | } |
| 2013 | } else if (parent) { |
| 2014 | /* check status compatibility with the parent */ |
| 2015 | if ((parent->flags & LYS_STATUS_MASK) > (node->flags & LYS_STATUS_MASK)) { |
| 2016 | if (node->flags & LYS_STATUS_CURR) { |
| 2017 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2018 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
| 2019 | (parent->flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 2020 | } else { /* LYS_STATUS_DEPRC */ |
| 2021 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2022 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 2023 | } |
| 2024 | goto error; |
| 2025 | } |
| 2026 | } |
| 2027 | |
| 2028 | if (!(options & LYSC_OPT_FREE_SP)) { |
| 2029 | node->sp = node_p; |
| 2030 | } |
| 2031 | DUP_STRING(ctx->ctx, node_p->name, node->name); |
| 2032 | COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, options, u, lys_compile_ext, ret, error); |
| 2033 | |
| 2034 | /* nodetype-specific part */ |
| 2035 | LY_CHECK_GOTO(node_compile_spec(ctx, node_p, options, node), error); |
| 2036 | |
| 2037 | /* insert into parent's children */ |
| 2038 | if (parent && (children = lysc_node_children(parent))) { |
| 2039 | if (!(*children)) { |
| 2040 | /* first child */ |
| 2041 | *children = node; |
| 2042 | } else { |
| 2043 | /* insert at the end of the parent's children list */ |
| 2044 | (*children)->prev->next = node; |
| 2045 | node->prev = (*children)->prev; |
| 2046 | (*children)->prev = node; |
| 2047 | } |
| 2048 | } else { |
| 2049 | /* top-level element */ |
| 2050 | if (!ctx->mod->compiled->data) { |
| 2051 | ctx->mod->compiled->data = node; |
| 2052 | } else { |
| 2053 | /* insert at the end of the module's top-level nodes list */ |
| 2054 | ctx->mod->compiled->data->prev->next = node; |
| 2055 | node->prev = ctx->mod->compiled->data->prev; |
| 2056 | ctx->mod->compiled->data->prev = node; |
| 2057 | } |
| 2058 | } |
| 2059 | |
| 2060 | return LY_SUCCESS; |
| 2061 | |
| 2062 | error: |
| 2063 | lysc_node_free(ctx->ctx, node); |
| 2064 | return ret; |
| 2065 | } |
| 2066 | |
| 2067 | LY_ERR |
| 2068 | lys_compile(struct lys_module *mod, int options) |
| 2069 | { |
| 2070 | struct lysc_ctx ctx = {0}; |
| 2071 | struct lysc_module *mod_c; |
| 2072 | struct lysp_module *sp; |
| 2073 | struct lysp_node *node_p; |
| 2074 | unsigned int u; |
| 2075 | LY_ERR ret; |
| 2076 | |
| 2077 | LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->parsed->ctx, LY_EINVAL); |
| 2078 | sp = mod->parsed; |
| 2079 | |
| 2080 | if (sp->submodule) { |
| 2081 | LOGERR(sp->ctx, LY_EINVAL, "Submodules (%s) are not supposed to be compiled, compile only the main modules.", sp->name); |
| 2082 | return LY_EINVAL; |
| 2083 | } |
| 2084 | |
| 2085 | ctx.ctx = sp->ctx; |
| 2086 | ctx.mod = mod; |
| 2087 | |
| 2088 | mod->compiled = mod_c = calloc(1, sizeof *mod_c); |
| 2089 | LY_CHECK_ERR_RET(!mod_c, LOGMEM(sp->ctx), LY_EMEM); |
| 2090 | mod_c->ctx = sp->ctx; |
| 2091 | mod_c->implemented = sp->implemented; |
| 2092 | mod_c->latest_revision = sp->latest_revision; |
| 2093 | mod_c->version = sp->version; |
| 2094 | |
| 2095 | DUP_STRING(sp->ctx, sp->name, mod_c->name); |
| 2096 | DUP_STRING(sp->ctx, sp->ns, mod_c->ns); |
| 2097 | DUP_STRING(sp->ctx, sp->prefix, mod_c->prefix); |
| 2098 | if (sp->revs) { |
| 2099 | DUP_STRING(sp->ctx, sp->revs[0].date, mod_c->revision); |
| 2100 | } |
| 2101 | COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, options, u, lys_compile_import, ret, error); |
| 2102 | COMPILE_ARRAY_GOTO(&ctx, sp->features, mod_c->features, options, u, lys_compile_feature, ret, error); |
| 2103 | COMPILE_ARRAY_GOTO(&ctx, sp->identities, mod_c->identities, options, u, lys_compile_identity, ret, error); |
| 2104 | if (sp->identities) { |
| 2105 | LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities)); |
| 2106 | } |
| 2107 | |
| 2108 | LY_LIST_FOR(sp->data, node_p) { |
| 2109 | ret = lys_compile_node(&ctx, node_p, options, NULL); |
| 2110 | LY_CHECK_GOTO(ret, error); |
| 2111 | } |
| 2112 | //COMPILE_ARRAY_GOTO(ctx, sp->rpcs, mod_c->rpcs, options, u, lys_compile_action, ret, error); |
| 2113 | //COMPILE_ARRAY_GOTO(ctx, sp->notifs, mod_c->notifs, options, u, lys_compile_notif, ret, error); |
| 2114 | |
| 2115 | COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, options, u, lys_compile_ext, ret, error); |
| 2116 | |
| 2117 | if (options & LYSC_OPT_FREE_SP) { |
| 2118 | lysp_module_free(mod->parsed); |
| 2119 | ((struct lys_module*)mod)->parsed = NULL; |
| 2120 | } |
| 2121 | |
| 2122 | ((struct lys_module*)mod)->compiled = mod_c; |
| 2123 | return LY_SUCCESS; |
| 2124 | |
| 2125 | error: |
| 2126 | lysc_module_free(mod_c, NULL); |
| 2127 | ((struct lys_module*)mod)->compiled = NULL; |
| 2128 | return ret; |
| 2129 | } |