blob: f2b4bf2919933f53a25aa3dbb901fb007b2878ab [file] [log] [blame]
Radek Krejci19a96102018-11-15 13:38:09 +01001/**
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
59const 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
64static struct lysc_ext_instance *
65lysc_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
73static struct lysc_pattern*
74lysc_pattern_dup(struct lysc_pattern *orig)
75{
76 ++orig->refcount;
77 return orig;
78}
79
80static struct lysc_pattern**
81lysc_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
94struct lysc_range*
95lysc_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;
112cleanup:
113 free(dup);
114 (void) ret; /* set but not used due to the return type */
115 return NULL;
116}
117
118struct iff_stack {
119 int size;
120 int index; /* first empty item */
121 uint8_t *stack;
122};
123
124static LY_ERR
125iff_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
136static uint8_t
137iff_stack_pop(struct iff_stack *stack)
138{
139 stack->index--;
140 return stack->stack[stack->index];
141}
142
143static void
144iff_stack_clean(struct iff_stack *stack)
145{
146 stack->size = 0;
147 free(stack->stack);
148}
149
150static void
151iff_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
168static struct lysc_feature *
169lysc_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
196static LY_ERR
197lys_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
235static LY_ERR
236lys_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
401error:
402 /* cleanup */
403 iff_stack_clean(&stack);
404
405 return rc;
406}
407
408static LY_ERR
409lys_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
418done:
419 return ret;
420}
421
422static LY_ERR
423lys_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
435done:
436 return ret;
437}
438
439static LY_ERR
440lys_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
476done:
477 return ret;
478}
479
480static LY_ERR
481lys_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
492done:
493 return ret;
494}
495
496static LY_ERR
497lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
498{
499 unsigned int i, u, v;
500 const char *s, *name;
501 struct lysc_module *mod;
502 struct lysc_ident **dident;
503
504 for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
505 if (!idents_p[i].bases) {
506 continue;
507 }
508 for (u = 0; u < LY_ARRAY_SIZE(idents_p[i].bases); ++u) {
509 s = strchr(idents_p[i].bases[u], ':');
510 if (s) {
511 /* prefixed identity */
512 name = &s[1];
513 mod = lysc_module_find_prefix(ctx->mod->compiled, idents_p[i].bases[u], s - idents_p[i].bases[u]);
514 } else {
515 name = idents_p[i].bases[u];
516 mod = ctx->mod->compiled;
517 }
518 LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
519 "Invalid prefix used for base (%s) of identity \"%s\".", idents_p[i].bases[u], idents[i].name),
520 LY_EVALID);
521 if (mod->identities) {
522 for (v = 0; v < LY_ARRAY_SIZE(mod->identities); ++v) {
523 if (!strcmp(name, mod->identities[v].name)) {
524 /* we have match! store the backlink */
525 LY_ARRAY_NEW_RET(ctx->ctx, mod->identities[v].derived, dident, LY_EMEM);
526 *dident = &idents[i];
527 break;
528 }
529 }
530 }
531 LY_CHECK_ERR_RET(!dident, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
532 "Unable to find base (%s) of identity \"%s\".", idents_p[i].bases[u], idents[i].name),
533 LY_EVALID);
534 }
535 }
536 return LY_SUCCESS;
537}
538
539static LY_ERR
540lys_compile_feature(struct lysc_ctx *ctx, struct lysp_feature *feature_p, int options, struct lysc_feature *feature)
541{
542 unsigned int u, v;
543 LY_ERR ret = LY_SUCCESS;
544 struct lysc_feature **df;
545
546 DUP_STRING(ctx->ctx, feature_p->name, feature->name);
547 feature->flags = feature_p->flags;
548
549 COMPILE_ARRAY_GOTO(ctx, feature_p->exts, feature->exts, options, u, lys_compile_ext, ret, done);
550 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, options, u, lys_compile_iffeature, ret, done);
551 if (feature->iffeatures) {
552 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
553 if (feature->iffeatures[u].features) {
554 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
555 /* add itself into the dependants list */
556 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
557 *df = feature;
558 }
559 /* TODO check for circular dependency */
560 }
561 }
562 }
563done:
564 return ret;
565}
566
567static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +0100568range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value, size_t *len, char **valcopy)
Radek Krejci19a96102018-11-15 13:38:09 +0100569{
Radek Krejci6cba4292018-11-15 17:33:29 +0100570 size_t fraction = 0, size;
571
Radek Krejci19a96102018-11-15 13:38:09 +0100572 *len = 0;
573
574 assert(value);
575 /* parse value */
576 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
577 return LY_EVALID;
578 }
579
580 if ((value[*len] == '-') || (value[*len] == '+')) {
581 ++(*len);
582 }
583
584 while (isdigit(value[*len])) {
585 ++(*len);
586 }
587
588 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +0100589 if (basetype == LY_TYPE_DEC64) {
590 goto decimal;
591 } else {
592 *valcopy = strndup(value, *len);
593 return LY_SUCCESS;
594 }
Radek Krejci19a96102018-11-15 13:38:09 +0100595 }
596 fraction = *len;
597
598 ++(*len);
599 while (isdigit(value[*len])) {
600 ++(*len);
601 }
602
Radek Krejci6cba4292018-11-15 17:33:29 +0100603 if (basetype == LY_TYPE_DEC64) {
604decimal:
605 assert(frdigits);
606 if (*len - 1 - fraction > frdigits) {
607 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
608 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
609 *len, value, frdigits);
610 return LY_EINVAL;
611 }
612 if (fraction) {
613 size = (*len) + (frdigits - ((*len) - 1 - fraction));
614 } else {
615 size = (*len) + frdigits + 1;
616 }
617 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +0100618 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
619
Radek Krejci6cba4292018-11-15 17:33:29 +0100620 (*valcopy)[size - 1] = '\0';
621 if (fraction) {
622 memcpy(&(*valcopy)[0], &value[0], fraction);
623 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
624 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
625 } else {
626 memcpy(&(*valcopy)[0], &value[0], *len);
627 memset(&(*valcopy)[*len], '0', frdigits);
628 }
Radek Krejci19a96102018-11-15 13:38:09 +0100629 }
630 return LY_SUCCESS;
631}
632
633static LY_ERR
634range_part_check_ascendance(int unsigned_value, int64_t value, int64_t prev_value)
635{
636 if (unsigned_value) {
637 if ((uint64_t)prev_value >= (uint64_t)value) {
638 return LY_EEXIST;
639 }
640 } else {
641 if (prev_value >= value) {
642 return LY_EEXIST;
643 }
644 }
645 return LY_SUCCESS;
646}
647
648static LY_ERR
649range_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 Krejci6cba4292018-11-15 17:33:29 +0100650 uint8_t frdigits, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +0100651{
652 LY_ERR ret = LY_SUCCESS;
653 char *valcopy = NULL;
654 size_t len;
655
656 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +0100657 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +0100658 LY_CHECK_GOTO(ret, error);
659 }
660
661 switch (basetype) {
662 case LY_TYPE_BINARY: /* length */
663 if (valcopy) {
664 ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64);
665 } else if (max) {
666 part->max_u64 = UINT64_C(18446744073709551615);
667 } else {
668 part->min_u64 = UINT64_C(0);
669 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100670 if (!ret && !first) {
Radek Krejci19a96102018-11-15 13:38:09 +0100671 ret = range_part_check_ascendance(1, max ? part->max_64 : part->min_64, prev);
672 }
673 break;
674 case LY_TYPE_DEC64: /* range */
675 if (valcopy) {
676 ret = ly_parse_int(valcopy, INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
677 max ? &part->max_64 : &part->min_64);
678 } else if (max) {
679 part->max_64 = INT64_C(9223372036854775807);
680 } else {
681 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
682 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100683 if (!ret && !first) {
Radek Krejci19a96102018-11-15 13:38:09 +0100684 ret = range_part_check_ascendance(0, max ? part->max_64 : part->min_64, prev);
685 }
686 break;
687 case LY_TYPE_INT8: /* range */
688 if (valcopy) {
689 ret = ly_parse_int(valcopy, INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64);
690 } else if (max) {
691 part->max_64 = INT64_C(127);
692 } else {
693 part->min_64 = INT64_C(-128);
694 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100695 if (!ret && !first) {
Radek Krejci19a96102018-11-15 13:38:09 +0100696 ret = range_part_check_ascendance(0, max ? part->max_64 : part->min_64, prev);
697 }
698 break;
699 case LY_TYPE_INT16: /* range */
700 if (valcopy) {
701 ret = ly_parse_int(valcopy, INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64);
702 } else if (max) {
703 part->max_64 = INT64_C(32767);
704 } else {
705 part->min_64 = INT64_C(-32768);
706 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100707 if (!ret && !first) {
Radek Krejci19a96102018-11-15 13:38:09 +0100708 ret = range_part_check_ascendance(0, max ? part->max_64 : part->min_64, prev);
709 }
710 break;
711 case LY_TYPE_INT32: /* range */
712 if (valcopy) {
713 ret = ly_parse_int(valcopy, INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64);
714 } else if (max) {
715 part->max_64 = INT64_C(2147483647);
716 } else {
717 part->min_64 = INT64_C(-2147483648);
718 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100719 if (!ret && !first) {
Radek Krejci19a96102018-11-15 13:38:09 +0100720 ret = range_part_check_ascendance(0, max ? part->max_64 : part->min_64, prev);
721 }
722 break;
723 case LY_TYPE_INT64: /* range */
724 if (valcopy) {
725 ret = ly_parse_int(valcopy, INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
726 max ? &part->max_64 : &part->min_64);
727 } else if (max) {
728 part->max_64 = INT64_C(9223372036854775807);
729 } else {
730 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
731 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100732 if (!ret && !first) {
Radek Krejci19a96102018-11-15 13:38:09 +0100733 ret = range_part_check_ascendance(0, max ? part->max_64 : part->min_64, prev);
734 }
735 break;
736 case LY_TYPE_UINT8: /* range */
737 if (valcopy) {
738 ret = ly_parse_uint(valcopy, UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64);
739 } else if (max) {
740 part->max_u64 = UINT64_C(255);
741 } else {
742 part->min_u64 = UINT64_C(0);
743 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100744 if (!ret && !first) {
Radek Krejci19a96102018-11-15 13:38:09 +0100745 ret = range_part_check_ascendance(1, max ? part->max_64 : part->min_64, prev);
746 }
747 break;
748 case LY_TYPE_UINT16: /* range */
749 if (valcopy) {
750 ret = ly_parse_uint(valcopy, UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64);
751 } else if (max) {
752 part->max_u64 = UINT64_C(65535);
753 } else {
754 part->min_u64 = UINT64_C(0);
755 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100756 if (!ret && !first) {
Radek Krejci19a96102018-11-15 13:38:09 +0100757 ret = range_part_check_ascendance(1, max ? part->max_64 : part->min_64, prev);
758 }
759 break;
760 case LY_TYPE_UINT32: /* range */
761 if (valcopy) {
762 ret = ly_parse_uint(valcopy, UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64);
763 } else if (max) {
764 part->max_u64 = UINT64_C(4294967295);
765 } else {
766 part->min_u64 = UINT64_C(0);
767 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100768 if (!ret && !first) {
Radek Krejci19a96102018-11-15 13:38:09 +0100769 ret = range_part_check_ascendance(1, max ? part->max_64 : part->min_64, prev);
770 }
771 break;
772 case LY_TYPE_UINT64: /* range */
773 if (valcopy) {
774 ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64);
775 } else if (max) {
776 part->max_u64 = UINT64_C(18446744073709551615);
777 } else {
778 part->min_u64 = UINT64_C(0);
779 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100780 if (!ret && !first) {
Radek Krejci19a96102018-11-15 13:38:09 +0100781 ret = range_part_check_ascendance(1, max ? part->max_64 : part->min_64, prev);
782 }
783 break;
784 case LY_TYPE_STRING: /* length */
785 if (valcopy) {
786 ret = ly_parse_uint(valcopy, UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64);
787 } else if (max) {
788 part->max_u64 = UINT64_C(18446744073709551615);
789 } else {
790 part->min_u64 = UINT64_C(0);
791 }
Radek Krejci6cba4292018-11-15 17:33:29 +0100792 if (!ret && !first) {
Radek Krejci19a96102018-11-15 13:38:09 +0100793 ret = range_part_check_ascendance(1, max ? part->max_64 : part->min_64, prev);
794 }
795 break;
796 default:
797 LOGINT(ctx->ctx);
798 ret = LY_EINT;
799 }
800
801error:
802 if (ret == LY_EDENIED) {
803 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
804 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
805 length_restr ? "length" : "range", valcopy ? valcopy : *value);
806 } else if (ret == LY_EVALID) {
807 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
808 "Invalid %s restriction - invalid value \"%s\".",
809 length_restr ? "length" : "range", valcopy ? valcopy : *value);
810 } else if (ret == LY_EEXIST) {
811 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
812 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +0100813 length_restr ? "length" : "range",
814 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : *value);
Radek Krejci19a96102018-11-15 13:38:09 +0100815 } else if (!ret && value) {
816 *value = *value + len;
817 }
818 free(valcopy);
819 return ret;
820}
821
822static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +0100823lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, int length_restr, uint8_t frdigits,
Radek Krejci19a96102018-11-15 13:38:09 +0100824 struct lysc_range *base_range, struct lysc_range **range)
825{
826 LY_ERR ret = LY_EVALID;
827 const char *expr;
828 struct lysc_range_part *parts = NULL, *part;
829 int range_expected = 0, uns;
830 unsigned int parts_done = 0, u, v;
831
832 assert(range);
833 assert(range_p);
834
835 expr = range_p->arg;
836 while(1) {
837 if (isspace(*expr)) {
838 ++expr;
839 } else if (*expr == '\0') {
840 if (range_expected) {
841 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
842 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
843 length_restr ? "length" : "range", range_p->arg);
844 goto cleanup;
845 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
846 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
847 "Invalid %s restriction - unexpected end of the expression (%s).",
848 length_restr ? "length" : "range", range_p->arg);
849 goto cleanup;
850 }
851 parts_done++;
852 break;
853 } else if (!strncmp(expr, "min", 3)) {
854 if (parts) {
855 /* min cannot be used elsewhere than in the first part */
856 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
857 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
858 expr - range_p->arg, range_p->arg);
859 goto cleanup;
860 }
861 expr += 3;
862
863 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci6cba4292018-11-15 17:33:29 +0100864 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +0100865 part->max_64 = part->min_64;
866 } else if (*expr == '|') {
867 if (!parts || range_expected) {
868 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
869 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
870 goto cleanup;
871 }
872 expr++;
873 parts_done++;
874 /* process next part of the expression */
875 } else if (!strncmp(expr, "..", 2)) {
876 expr += 2;
877 while (isspace(*expr)) {
878 expr++;
879 }
880 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
881 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
882 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
883 goto cleanup;
884 }
885 /* continue expecting the upper boundary */
886 range_expected = 1;
887 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
888 /* number */
889 if (range_expected) {
890 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci6cba4292018-11-15 17:33:29 +0100891 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +0100892 range_expected = 0;
893 } else {
894 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
895 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0,
Radek Krejci6cba4292018-11-15 17:33:29 +0100896 basetype, parts_done ? 0 : 1, length_restr, frdigits, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +0100897 part->max_64 = part->min_64;
898 }
899
900 /* continue with possible another expression part */
901 } else if (!strncmp(expr, "max", 3)) {
902 expr += 3;
903 while (isspace(*expr)) {
904 expr++;
905 }
906 if (*expr != '\0') {
907 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
908 length_restr ? "length" : "range", expr);
909 goto cleanup;
910 }
911 if (range_expected) {
912 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci6cba4292018-11-15 17:33:29 +0100913 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +0100914 range_expected = 0;
915 } else {
916 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
917 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0,
Radek Krejci6cba4292018-11-15 17:33:29 +0100918 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +0100919 part->min_64 = part->max_64;
920 }
921 } else {
922 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
923 length_restr ? "length" : "range", expr);
924 goto cleanup;
925 }
926 }
927
928 /* check with the previous range/length restriction */
929 if (base_range) {
930 switch (basetype) {
931 case LY_TYPE_BINARY:
932 case LY_TYPE_UINT8:
933 case LY_TYPE_UINT16:
934 case LY_TYPE_UINT32:
935 case LY_TYPE_UINT64:
936 case LY_TYPE_STRING:
937 uns = 1;
938 break;
939 case LY_TYPE_DEC64:
940 case LY_TYPE_INT8:
941 case LY_TYPE_INT16:
942 case LY_TYPE_INT32:
943 case LY_TYPE_INT64:
944 uns = 0;
945 break;
946 default:
947 LOGINT(ctx->ctx);
948 ret = LY_EINT;
949 goto cleanup;
950 }
951 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
952 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
953 goto baseerror;
954 }
955 /* current lower bound is not lower than the base */
956 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
957 /* base has single value */
958 if (base_range->parts[v].min_64 == parts[u].min_64) {
959 /* both lower bounds are the same */
960 if (parts[u].min_64 != parts[u].max_64) {
961 /* current continues with a range */
962 goto baseerror;
963 } else {
964 /* equal single values, move both forward */
965 ++v;
966 continue;
967 }
968 } else {
969 /* base is single value lower than current range, so the
970 * value from base range is removed in the current,
971 * move only base and repeat checking */
972 ++v;
973 --u;
974 continue;
975 }
976 } else {
977 /* base is the range */
978 if (parts[u].min_64 == parts[u].max_64) {
979 /* current is a single value */
980 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
981 /* current is behind the base range, so base range is omitted,
982 * move the base and keep the current for further check */
983 ++v;
984 --u;
985 } /* else it is within the base range, so move the current, but keep the base */
986 continue;
987 } else {
988 /* both are ranges - check the higher bound, the lower was already checked */
989 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
990 /* higher bound is higher than the current higher bound */
991 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
992 /* but the current lower bound is also higher, so the base range is omitted,
993 * continue with the same current, but move the base */
994 --u;
995 ++v;
996 continue;
997 }
998 /* current range starts within the base range but end behind it */
999 goto baseerror;
1000 } else {
1001 /* current range is smaller than the base,
1002 * move current, but stay with the base */
1003 continue;
1004 }
1005 }
1006 }
1007 }
1008 if (u != parts_done) {
1009baseerror:
1010 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1011 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1012 length_restr ? "length" : "range", range_p->arg);
1013 goto cleanup;
1014 }
1015 }
1016
1017 if (!(*range)) {
1018 *range = calloc(1, sizeof **range);
1019 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1020 }
1021
1022 if (range_p->eapptag) {
1023 lydict_remove(ctx->ctx, (*range)->eapptag);
1024 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1025 }
1026 if (range_p->emsg) {
1027 lydict_remove(ctx->ctx, (*range)->emsg);
1028 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1029 }
1030 /* extensions are taken only from the last range by the caller */
1031
1032 (*range)->parts = parts;
1033 parts = NULL;
1034 ret = LY_SUCCESS;
1035cleanup:
1036 /* TODO clean up */
1037 LY_ARRAY_FREE(parts);
1038
1039 return ret;
1040}
1041
1042/**
1043 * @brief Checks pattern syntax.
1044 *
1045 * @param[in] pattern Pattern to check.
1046 * @param[out] pcre_precomp Precompiled PCRE pattern. Can be NULL.
1047 * @return EXIT_SUCCESS on success, EXIT_FAILURE otherwise.
1048 */
1049static LY_ERR
1050lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre **pcre_precomp)
1051{
1052 int idx, idx2, start, end, err_offset, count;
1053 char *perl_regex, *ptr;
1054 const char *err_msg, *orig_ptr;
1055 pcre *precomp;
1056#define URANGE_LEN 19
1057 char *ublock2urange[][2] = {
1058 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1059 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1060 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1061 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1062 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1063 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1064 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1065 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1066 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1067 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1068 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1069 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1070 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1071 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1072 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1073 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1074 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1075 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1076 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1077 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1078 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1079 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1080 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1081 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1082 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1083 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1084 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1085 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1086 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1087 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1088 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1089 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1090 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1091 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1092 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1093 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1094 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1095 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1096 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1097 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1098 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1099 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1100 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1101 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1102 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1103 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1104 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1105 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1106 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1107 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1108 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1109 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1110 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1111 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1112 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1113 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1114 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1115 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1116 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1117 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1118 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1119 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1120 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1121 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1122 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1123 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1124 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1125 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1126 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1127 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1128 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1129 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1130 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1131 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1132 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1133 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1134 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1135 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1136 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1137 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1138 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1139 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1140 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1141 {NULL, NULL}
1142 };
1143
1144 /* adjust the expression to a Perl equivalent
1145 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1146
1147 /* we need to replace all "$" with "\$", count them now */
1148 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1149
1150 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
1151 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM);
1152 perl_regex[0] = '\0';
1153
1154 ptr = perl_regex;
1155
1156 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1157 /* we will add line-end anchoring */
1158 ptr[0] = '(';
1159 ++ptr;
1160 }
1161
1162 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1163 if (orig_ptr[0] == '$') {
1164 ptr += sprintf(ptr, "\\$");
1165 } else if (orig_ptr[0] == '^') {
1166 ptr += sprintf(ptr, "\\^");
1167 } else {
1168 ptr[0] = orig_ptr[0];
1169 ++ptr;
1170 }
1171 }
1172
1173 if (strncmp(pattern + strlen(pattern) - 2, ".*", 2)) {
1174 ptr += sprintf(ptr, ")$");
1175 } else {
1176 ptr[0] = '\0';
1177 ++ptr;
1178 }
1179
1180 /* substitute Unicode Character Blocks with exact Character Ranges */
1181 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1182 start = ptr - perl_regex;
1183
1184 ptr = strchr(ptr, '}');
1185 if (!ptr) {
1186 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1187 pattern, perl_regex + start + 2, "unterminated character property");
1188 free(perl_regex);
1189 return LY_EVALID;
1190 }
1191 end = (ptr - perl_regex) + 1;
1192
1193 /* need more space */
1194 if (end - start < URANGE_LEN) {
1195 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1196 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM);
1197 }
1198
1199 /* find our range */
1200 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1201 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1202 break;
1203 }
1204 }
1205 if (!ublock2urange[idx][0]) {
1206 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1207 pattern, perl_regex + start + 5, "unknown block name");
1208 free(perl_regex);
1209 return LY_EVALID;
1210 }
1211
1212 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1213 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
1214 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1215 ++count;
1216 }
1217 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1218 --count;
1219 }
1220 }
1221 if (count) {
1222 /* skip brackets */
1223 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1224 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1225 } else {
1226 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1227 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1228 }
1229 }
1230
1231 /* must return 0, already checked during parsing */
1232 precomp = pcre_compile(perl_regex, PCRE_UTF8 | PCRE_ANCHORED | PCRE_DOLLAR_ENDONLY | PCRE_NO_AUTO_CAPTURE,
1233 &err_msg, &err_offset, NULL);
1234 if (!precomp) {
1235 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
1236 free(perl_regex);
1237 return LY_EVALID;
1238 }
1239 free(perl_regex);
1240
1241 if (pcre_precomp) {
1242 *pcre_precomp = precomp;
1243 } else {
1244 free(precomp);
1245 }
1246
1247 return LY_SUCCESS;
1248
1249#undef URANGE_LEN
1250}
1251
1252static LY_ERR
1253lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p, int options,
1254 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1255{
1256 struct lysc_pattern **pattern;
1257 unsigned int u, v;
1258 const char *err_msg;
1259 LY_ERR ret = LY_SUCCESS;
1260
1261 /* first, copy the patterns from the base type */
1262 if (base_patterns) {
1263 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1264 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1265 }
1266
1267 LY_ARRAY_FOR(patterns_p, u) {
1268 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1269 *pattern = calloc(1, sizeof **pattern);
1270 ++(*pattern)->refcount;
1271
1272 ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->expr);
1273 LY_CHECK_RET(ret);
1274 (*pattern)->expr_extra = pcre_study((*pattern)->expr, 0, &err_msg);
1275 if (err_msg) {
1276 LOGWRN(ctx->ctx, "Studying pattern \"%s\" failed (%s).", pattern, err_msg);
1277 }
1278
1279 if (patterns_p[u].arg[0] == 0x15) {
1280 (*pattern)->inverted = 1;
1281 }
1282 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
1283 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
1284 COMPILE_ARRAY_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts,
1285 options, v, lys_compile_ext, ret, done);
1286 }
1287done:
1288 return ret;
1289}
1290
1291static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1292 0 /* LY_TYPE_UNKNOWN */,
1293 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
1294 LYS_SET_BIT /* LY_TYPE_BITS */,
1295 0 /* LY_TYPE_BOOL */,
1296 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1297 0 /* LY_TYPE_EMPTY */,
1298 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1299 LYS_SET_BASE /* LY_TYPE_IDENT */,
1300 LYS_SET_REQINST /* LY_TYPE_INST */,
1301 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
1302 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
1303 LYS_SET_TYPE /* LY_TYPE_UNION */,
1304 LYS_SET_RANGE /* LY_TYPE_INT8 */,
1305 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1306 LYS_SET_RANGE /* LY_TYPE_INT16 */,
1307 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1308 LYS_SET_RANGE /* LY_TYPE_INT32 */,
1309 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1310 LYS_SET_RANGE /* LY_TYPE_INT64 */,
1311 LYS_SET_RANGE /* LY_TYPE_UINT64 */
1312};
1313
1314static LY_ERR
1315lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype, int options,
1316 struct lysc_type_enum_item *base_enums, struct lysc_type_enum_item **enums)
1317{
1318 LY_ERR ret = LY_SUCCESS;
1319 unsigned int u, v, match;
1320 int32_t value = 0;
1321 uint32_t position = 0;
1322 struct lysc_type_enum_item *e, storage;
1323
1324 if (base_enums && ctx->mod->compiled->version < 2) {
1325 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
1326 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1327 return LY_EVALID;
1328 }
1329
1330 LY_ARRAY_FOR(enums_p, u) {
1331 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
1332 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
1333 if (base_enums) {
1334 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1335 LY_ARRAY_FOR(base_enums, v) {
1336 if (!strcmp(e->name, base_enums[v].name)) {
1337 break;
1338 }
1339 }
1340 if (v == LY_ARRAY_SIZE(base_enums)) {
1341 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1342 "Invalid %s - derived type adds new item \"%s\".",
1343 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
1344 return LY_EVALID;
1345 }
1346 match = v;
1347 }
1348
1349 if (basetype == LY_TYPE_ENUM) {
1350 if (enums_p[u].flags & LYS_SET_VALUE) {
1351 e->value = (int32_t)enums_p[u].value;
1352 if (!u || e->value >= value) {
1353 value = e->value + 1;
1354 }
1355 /* check collision with other values */
1356 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1357 if (e->value == (*enums)[v].value) {
1358 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1359 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
1360 e->value, e->name, (*enums)[v].name);
1361 return LY_EVALID;
1362 }
1363 }
1364 } else if (base_enums) {
1365 /* inherit the assigned value */
1366 e->value = base_enums[match].value;
1367 if (!u || e->value >= value) {
1368 value = e->value + 1;
1369 }
1370 } else {
1371 /* assign value automatically */
1372 if (u && value == INT32_MIN) {
1373 /* counter overflow */
1374 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1375 "Invalid enumeration - it is not possible to auto-assign enum value for "
1376 "\"%s\" since the highest value is already 2147483647.", e->name);
1377 return LY_EVALID;
1378 }
1379 e->value = value++;
1380 }
1381 } else { /* LY_TYPE_BITS */
1382 if (enums_p[u].flags & LYS_SET_VALUE) {
1383 e->value = (int32_t)enums_p[u].value;
1384 if (!u || (uint32_t)e->value >= position) {
1385 position = (uint32_t)e->value + 1;
1386 }
1387 /* check collision with other values */
1388 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1389 if (e->value == (*enums)[v].value) {
1390 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1391 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
1392 (uint32_t)e->value, e->name, (*enums)[v].name);
1393 return LY_EVALID;
1394 }
1395 }
1396 } else if (base_enums) {
1397 /* inherit the assigned value */
1398 e->value = base_enums[match].value;
1399 if (!u || (uint32_t)e->value >= position) {
1400 position = (uint32_t)e->value + 1;
1401 }
1402 } else {
1403 /* assign value automatically */
1404 if (u && position == 0) {
1405 /* counter overflow */
1406 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1407 "Invalid bits - it is not possible to auto-assign bit position for "
1408 "\"%s\" since the highest value is already 4294967295.", e->name);
1409 return LY_EVALID;
1410 }
1411 e->value = position++;
1412 }
1413 }
1414
1415 if (base_enums) {
1416 /* the assigned values must not change from the derived type */
1417 if (e->value != base_enums[match].value) {
1418 if (basetype == LY_TYPE_ENUM) {
1419 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1420 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
1421 e->name, base_enums[match].value, e->value);
1422 } else {
1423 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1424 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
1425 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
1426 }
1427 return LY_EVALID;
1428 }
1429 }
1430
1431 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, options, v, lys_compile_iffeature, ret, done);
1432 COMPILE_ARRAY_GOTO(ctx, enums_p[u].exts, e->exts, options, v, lys_compile_ext, ret, done);
1433
1434 if (basetype == LY_TYPE_BITS) {
1435 /* keep bits ordered by position */
1436 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
1437 if (v != u) {
1438 memcpy(&storage, e, sizeof *e);
1439 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
1440 memcpy(&(*enums)[v], &storage, sizeof storage);
1441 }
1442 }
1443 }
1444
1445done:
1446 return ret;
1447}
1448
1449static LY_ERR
Radek Krejcic5c27e52018-11-15 14:38:11 +01001450lys_compile_type_(struct lysc_ctx *ctx, struct lysp_type *type_p, LY_DATA_TYPE basetype, int options, int builtin, const char *tpdfname,
1451 struct lysc_type *base, struct lysc_type **type)
1452{
1453 LY_ERR ret = LY_SUCCESS;
1454 unsigned int u;
1455 struct lysc_type_bin *bin;
1456 struct lysc_type_num *num;
1457 struct lysc_type_str *str;
1458 struct lysc_type_bits *bits;
1459 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01001460 struct lysc_type_dec *dec;
Radek Krejcic5c27e52018-11-15 14:38:11 +01001461
1462 switch (basetype) {
1463 case LY_TYPE_BINARY:
1464 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
1465 bin = (struct lysc_type_bin*)(*type);
1466 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001467 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01001468 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length);
1469 LY_CHECK_RET(ret);
1470 if (!tpdfname) {
1471 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts,
1472 options, u, lys_compile_ext, ret, done);
1473 }
1474 }
1475
1476 if (tpdfname) {
1477 type_p->compiled = *type;
1478 *type = calloc(1, sizeof(struct lysc_type_bin));
1479 }
1480 break;
1481 case LY_TYPE_BITS:
1482 /* RFC 7950 9.7 - bits */
1483 bits = (struct lysc_type_bits*)(*type);
1484 if (type_p->bits) {
1485 ret = lys_compile_type_enums(ctx, type_p->bits, basetype, options,
1486 base ? (struct lysc_type_enum_item*)((struct lysc_type_bits*)base)->bits : NULL,
1487 (struct lysc_type_enum_item**)&bits->bits);
1488 LY_CHECK_RET(ret);
1489 }
1490
1491 if (builtin && !type_p->flags) {
1492 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01001493 if (tpdfname) {
1494 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing bit substatement for bits type \"%s\".",
1495 tpdfname);
1496 } else {
1497 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing bit substatement for bits type.");
1498 free(*type);
1499 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01001500 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001501 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01001502 }
1503
1504 if (tpdfname) {
1505 type_p->compiled = *type;
1506 *type = calloc(1, sizeof(struct lysc_type_bits));
1507 }
1508 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01001509 case LY_TYPE_DEC64:
1510 dec = (struct lysc_type_dec*)(*type);
1511
1512 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci643c8242018-11-15 17:51:11 +01001513 if (builtin) {
1514 if (!type_p->fraction_digits) {
1515 if (tpdfname) {
1516 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing fraction-digits substatement for decimal64 type \"%s\".",
1517 tpdfname);
1518 } else {
1519 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing fraction-digits substatement for decimal64 type.");
1520 free(*type);
1521 *type = NULL;
1522 }
1523 return LY_EVALID;
1524 }
1525 } else if (type_p->fraction_digits) {
1526 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01001527 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01001528 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1529 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01001530 tpdfname);
1531 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01001532 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1533 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01001534 free(*type);
1535 *type = NULL;
1536 }
1537 return LY_EVALID;
1538 }
1539 dec->fraction_digits = type_p->fraction_digits;
1540
1541 /* RFC 7950 9.2.4 - range */
1542 if (type_p->range) {
1543 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
1544 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range);
1545 LY_CHECK_RET(ret);
1546 if (!tpdfname) {
1547 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts,
1548 options, u, lys_compile_ext, ret, done);
1549 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001550 }
1551
1552 if (tpdfname) {
1553 type_p->compiled = *type;
1554 *type = calloc(1, sizeof(struct lysc_type_dec));
1555 }
1556 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01001557 case LY_TYPE_STRING:
1558 /* RFC 7950 9.4.4 - length */
1559 str = (struct lysc_type_str*)(*type);
1560 if (type_p->length) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001561 ret = lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01001562 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length);
1563 LY_CHECK_RET(ret);
1564 if (!tpdfname) {
1565 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts,
1566 options, u, lys_compile_ext, ret, done);
1567 }
1568 } else if (base && ((struct lysc_type_str*)base)->length) {
1569 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
1570 }
1571
1572 /* RFC 7950 9.4.5 - pattern */
1573 if (type_p->patterns) {
1574 ret = lys_compile_type_patterns(ctx, type_p->patterns, options,
1575 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns);
1576 LY_CHECK_RET(ret);
1577 } else if (base && ((struct lysc_type_str*)base)->patterns) {
1578 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
1579 }
1580
1581 if (tpdfname) {
1582 type_p->compiled = *type;
1583 *type = calloc(1, sizeof(struct lysc_type_str));
1584 }
1585 break;
1586 case LY_TYPE_ENUM:
1587 /* RFC 7950 9.6 - enum */
1588 enumeration = (struct lysc_type_enum*)(*type);
1589 if (type_p->enums) {
1590 ret = lys_compile_type_enums(ctx, type_p->enums, basetype, options,
1591 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums);
1592 LY_CHECK_RET(ret);
1593 }
1594
1595 if (builtin && !type_p->flags) {
1596 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01001597 if (tpdfname) {
1598 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1599 "Missing enum substatement for enumeration type \"%s\".", tpdfname);
1600 } else {
1601 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing enum substatement for enumeration type.");
1602 free(*type);
1603 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01001604 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001605 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01001606 }
1607
1608 if (tpdfname) {
1609 type_p->compiled = *type;
1610 *type = calloc(1, sizeof(struct lysc_type_enum));
1611 }
1612 break;
1613 case LY_TYPE_INT8:
1614 case LY_TYPE_UINT8:
1615 case LY_TYPE_INT16:
1616 case LY_TYPE_UINT16:
1617 case LY_TYPE_INT32:
1618 case LY_TYPE_UINT32:
1619 case LY_TYPE_INT64:
1620 case LY_TYPE_UINT64:
1621 /* RFC 6020 9.2.4 - range */
1622 num = (struct lysc_type_num*)(*type);
1623 if (type_p->range) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001624 ret = lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
Radek Krejcic5c27e52018-11-15 14:38:11 +01001625 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range);
1626 LY_CHECK_RET(ret);
1627 if (!tpdfname) {
1628 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts,
1629 options, u, lys_compile_ext, ret, done);
1630 }
1631 }
1632
1633 if (tpdfname) {
1634 type_p->compiled = *type;
1635 *type = calloc(1, sizeof(struct lysc_type_num));
1636 }
1637 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01001638 case LY_TYPE_INST:
1639 /* RFC 7950 9.9.3 - require-instance */
1640 if (type_p->flags & LYS_SET_REQINST) {
1641 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
1642 } else {
1643 /* default is true */
1644 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
1645 }
1646
1647 if (tpdfname) {
1648 type_p->compiled = *type;
1649 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1650 }
1651 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01001652 case LY_TYPE_BOOL:
1653 case LY_TYPE_EMPTY:
1654 case LY_TYPE_UNKNOWN: /* just to complete switch */
1655 break;
1656 }
1657 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
1658done:
1659 return ret;
1660}
1661
1662static LY_ERR
Radek Krejci19a96102018-11-15 13:38:09 +01001663lys_compile_type(struct lysc_ctx *ctx, struct lysp_node_leaf *leaf_p, int options, struct lysc_type **type)
1664{
1665 LY_ERR ret = LY_SUCCESS;
1666 unsigned int u;
1667 struct lysp_type *type_p = &leaf_p->type;
1668 struct type_context {
1669 const struct lysp_tpdf *tpdf;
1670 struct lysp_node *node;
1671 struct lysp_module *mod;
1672 } *tctx, *tctx_prev = NULL;
1673 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01001674 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01001675 struct ly_set tpdf_chain = {0};
Radek Krejci19a96102018-11-15 13:38:09 +01001676
1677 (*type) = NULL;
1678
1679 tctx = calloc(1, sizeof *tctx);
1680 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
1681 for (ret = lysp_type_find(type_p->name, (struct lysp_node*)leaf_p, ctx->mod->parsed,
1682 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
1683 ret == LY_SUCCESS;
1684 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
1685 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
1686 if (basetype) {
1687 break;
1688 }
1689
1690 /* check status */
1691 ret = lysc_check_status(ctx, leaf_p->flags, ctx->mod->parsed, leaf_p->name,
1692 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->mod->name);
1693 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1694
1695 if (tctx->tpdf->type.compiled) {
1696 /* it is not necessary to continue, the rest of the chain was already compiled */
1697 basetype = tctx->tpdf->type.compiled->basetype;
1698 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
1699 tctx = NULL;
1700 break;
1701 }
1702
1703 /* store information for the following processing */
1704 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
1705
1706 /* prepare next loop */
1707 tctx_prev = tctx;
1708 tctx = calloc(1, sizeof *tctx);
1709 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
1710 }
1711 free(tctx);
1712
1713 /* allocate type according to the basetype */
1714 switch (basetype) {
1715 case LY_TYPE_BINARY:
1716 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01001717 break;
1718 case LY_TYPE_BITS:
1719 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01001720 break;
1721 case LY_TYPE_BOOL:
1722 case LY_TYPE_EMPTY:
1723 *type = calloc(1, sizeof(struct lysc_type));
1724 break;
1725 case LY_TYPE_DEC64:
1726 *type = calloc(1, sizeof(struct lysc_type_dec));
1727 break;
1728 case LY_TYPE_ENUM:
1729 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01001730 break;
1731 case LY_TYPE_IDENT:
1732 *type = calloc(1, sizeof(struct lysc_type_identityref));
1733 break;
1734 case LY_TYPE_INST:
1735 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1736 break;
1737 case LY_TYPE_LEAFREF:
1738 *type = calloc(1, sizeof(struct lysc_type_leafref));
1739 break;
1740 case LY_TYPE_STRING:
1741 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01001742 break;
1743 case LY_TYPE_UNION:
1744 *type = calloc(1, sizeof(struct lysc_type_union));
1745 break;
1746 case LY_TYPE_INT8:
1747 case LY_TYPE_UINT8:
1748 case LY_TYPE_INT16:
1749 case LY_TYPE_UINT16:
1750 case LY_TYPE_INT32:
1751 case LY_TYPE_UINT32:
1752 case LY_TYPE_INT64:
1753 case LY_TYPE_UINT64:
1754 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01001755 break;
1756 case LY_TYPE_UNKNOWN:
1757 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1758 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
1759 ret = LY_EVALID;
1760 goto cleanup;
1761 }
1762 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
1763 if (~type_substmt_map[basetype] & leaf_p->type.flags) {
1764 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
1765 ly_data_type2str[basetype]);
1766 free(*type);
1767 (*type) = NULL;
1768 ret = LY_EVALID;
1769 goto cleanup;
1770 }
1771
1772 /* get restrictions from the referred typedefs */
1773 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
1774 tctx = (struct type_context*)tpdf_chain.objs[u];
1775 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
1776 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
1777 tctx->tpdf->name, ly_data_type2str[basetype]);
1778 ret = LY_EVALID;
1779 goto cleanup;
1780 } else if (tctx->tpdf->type.compiled) {
1781 base = tctx->tpdf->type.compiled;
1782 continue;
1783 } else if ((u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
1784 /* no change, just use the type information from the base */
1785 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
1786 ++base->refcount;
1787 continue;
1788 }
1789
1790 ++(*type)->refcount;
1791 (*type)->basetype = basetype;
Radek Krejcic5c27e52018-11-15 14:38:11 +01001792 prev_type = *type;
1793 ret = lys_compile_type_(ctx, &((struct lysp_tpdf*)tctx->tpdf)->type, basetype, options, (u == tpdf_chain.count - 1) ? 1 : 0, tctx->tpdf->name, base, type);
1794 LY_CHECK_GOTO(ret, cleanup);
1795 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01001796 }
1797
Radek Krejcic5c27e52018-11-15 14:38:11 +01001798 /* process the type definition in leaf */
1799 if (leaf_p->type.flags || !base) {
Radek Krejci19a96102018-11-15 13:38:09 +01001800 /* get restrictions from the node itself, finalize the type structure */
1801 (*type)->basetype = basetype;
1802 ++(*type)->refcount;
Radek Krejcic5c27e52018-11-15 14:38:11 +01001803 ret = lys_compile_type_(ctx, &leaf_p->type, basetype, options, base ? 0 : 1, NULL, base, type);
1804 LY_CHECK_GOTO(ret, cleanup);
1805 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01001806 /* no specific restriction in leaf's type definition, copy from the base */
1807 free(*type);
1808 (*type) = base;
1809 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01001810 }
1811
1812 COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, options, u, lys_compile_ext, ret, cleanup);
1813
1814cleanup:
1815 ly_set_erase(&tpdf_chain, free);
1816 return ret;
1817}
1818
1819static LY_ERR lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *parent);
1820
1821static LY_ERR
1822lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
1823{
1824 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
1825 struct lysc_node_container *cont = (struct lysc_node_container*)node;
1826 struct lysp_node *child_p;
1827 unsigned int u;
1828 LY_ERR ret = LY_SUCCESS;
1829
1830 COMPILE_MEMBER_GOTO(ctx, cont_p->when, cont->when, options, lys_compile_when, ret, done);
1831 COMPILE_ARRAY_GOTO(ctx, cont_p->iffeatures, cont->iffeatures, options, u, lys_compile_iffeature, ret, done);
1832
1833 LY_LIST_FOR(cont_p->child, child_p) {
1834 LY_CHECK_RET(lys_compile_node(ctx, child_p, options, node));
1835 }
1836
1837 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, options, u, lys_compile_must, ret, done);
1838 //COMPILE_ARRAY_GOTO(ctx, cont_p->actions, cont->actions, options, u, lys_compile_action, ret, done);
1839 //COMPILE_ARRAY_GOTO(ctx, cont_p->notifs, cont->notifs, options, u, lys_compile_notif, ret, done);
1840
1841done:
1842 return ret;
1843}
1844
1845static LY_ERR
1846lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *node)
1847{
1848 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
1849 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
1850 unsigned int u;
1851 LY_ERR ret = LY_SUCCESS;
1852
1853 COMPILE_MEMBER_GOTO(ctx, leaf_p->when, leaf->when, options, lys_compile_when, ret, done);
1854 COMPILE_ARRAY_GOTO(ctx, leaf_p->iffeatures, leaf->iffeatures, options, u, lys_compile_iffeature, ret, done);
1855
1856 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, options, u, lys_compile_must, ret, done);
1857 ret = lys_compile_type(ctx, leaf_p, options, &leaf->type);
1858 LY_CHECK_GOTO(ret, done);
1859
1860 DUP_STRING(ctx->ctx, leaf_p->units, leaf->units);
1861 DUP_STRING(ctx->ctx, leaf_p->dflt, leaf->dflt);
1862done:
1863 return ret;
1864}
1865
1866static LY_ERR
1867lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, int options, struct lysc_node *parent)
1868{
1869 LY_ERR ret = LY_EVALID;
1870 struct lysc_node *node, **children;
1871 unsigned int u;
1872 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, int, struct lysc_node*);
1873
1874 switch (node_p->nodetype) {
1875 case LYS_CONTAINER:
1876 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
1877 node_compile_spec = lys_compile_node_container;
1878 break;
1879 case LYS_LEAF:
1880 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
1881 node_compile_spec = lys_compile_node_leaf;
1882 break;
1883 case LYS_LIST:
1884 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
1885 break;
1886 case LYS_LEAFLIST:
1887 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
1888 break;
1889 case LYS_CASE:
1890 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_case));
1891 break;
1892 case LYS_CHOICE:
1893 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
1894 break;
1895 case LYS_USES:
1896 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_uses));
1897 break;
1898 case LYS_ANYXML:
1899 case LYS_ANYDATA:
1900 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
1901 break;
1902 default:
1903 LOGINT(ctx->ctx);
1904 return LY_EINT;
1905 }
1906 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
1907 node->nodetype = node_p->nodetype;
1908 node->module = ctx->mod;
1909 node->prev = node;
1910 node->flags = node_p->flags;
1911
1912 /* config */
1913 if (!(node->flags & LYS_CONFIG_MASK)) {
1914 /* config not explicitely set, inherit it from parent */
1915 if (parent) {
1916 node->flags |= parent->flags & LYS_CONFIG_MASK;
1917 } else {
1918 /* default is config true */
1919 node->flags |= LYS_CONFIG_W;
1920 }
1921 }
1922
1923 /* status - it is not inherited by specification, but it does not make sense to have
1924 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
1925 if (!(node->flags & LYS_STATUS_MASK)) {
1926 if (parent && (parent->flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT))) {
1927 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
1928 (parent->flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
1929 node->flags |= parent->flags & LYS_STATUS_MASK;
1930 } else {
1931 node->flags |= LYS_STATUS_CURR;
1932 }
1933 } else if (parent) {
1934 /* check status compatibility with the parent */
1935 if ((parent->flags & LYS_STATUS_MASK) > (node->flags & LYS_STATUS_MASK)) {
1936 if (node->flags & LYS_STATUS_CURR) {
1937 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
1938 "A \"current\" status is in conflict with the parent's \"%s\" status.",
1939 (parent->flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
1940 } else { /* LYS_STATUS_DEPRC */
1941 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
1942 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
1943 }
1944 goto error;
1945 }
1946 }
1947
1948 if (!(options & LYSC_OPT_FREE_SP)) {
1949 node->sp = node_p;
1950 }
1951 DUP_STRING(ctx->ctx, node_p->name, node->name);
1952 COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, options, u, lys_compile_ext, ret, error);
1953
1954 /* nodetype-specific part */
1955 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, options, node), error);
1956
1957 /* insert into parent's children */
1958 if (parent && (children = lysc_node_children(parent))) {
1959 if (!(*children)) {
1960 /* first child */
1961 *children = node;
1962 } else {
1963 /* insert at the end of the parent's children list */
1964 (*children)->prev->next = node;
1965 node->prev = (*children)->prev;
1966 (*children)->prev = node;
1967 }
1968 } else {
1969 /* top-level element */
1970 if (!ctx->mod->compiled->data) {
1971 ctx->mod->compiled->data = node;
1972 } else {
1973 /* insert at the end of the module's top-level nodes list */
1974 ctx->mod->compiled->data->prev->next = node;
1975 node->prev = ctx->mod->compiled->data->prev;
1976 ctx->mod->compiled->data->prev = node;
1977 }
1978 }
1979
1980 return LY_SUCCESS;
1981
1982error:
1983 lysc_node_free(ctx->ctx, node);
1984 return ret;
1985}
1986
1987LY_ERR
1988lys_compile(struct lys_module *mod, int options)
1989{
1990 struct lysc_ctx ctx = {0};
1991 struct lysc_module *mod_c;
1992 struct lysp_module *sp;
1993 struct lysp_node *node_p;
1994 unsigned int u;
1995 LY_ERR ret;
1996
1997 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->parsed->ctx, LY_EINVAL);
1998 sp = mod->parsed;
1999
2000 if (sp->submodule) {
2001 LOGERR(sp->ctx, LY_EINVAL, "Submodules (%s) are not supposed to be compiled, compile only the main modules.", sp->name);
2002 return LY_EINVAL;
2003 }
2004
2005 ctx.ctx = sp->ctx;
2006 ctx.mod = mod;
2007
2008 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
2009 LY_CHECK_ERR_RET(!mod_c, LOGMEM(sp->ctx), LY_EMEM);
2010 mod_c->ctx = sp->ctx;
2011 mod_c->implemented = sp->implemented;
2012 mod_c->latest_revision = sp->latest_revision;
2013 mod_c->version = sp->version;
2014
2015 DUP_STRING(sp->ctx, sp->name, mod_c->name);
2016 DUP_STRING(sp->ctx, sp->ns, mod_c->ns);
2017 DUP_STRING(sp->ctx, sp->prefix, mod_c->prefix);
2018 if (sp->revs) {
2019 DUP_STRING(sp->ctx, sp->revs[0].date, mod_c->revision);
2020 }
2021 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, options, u, lys_compile_import, ret, error);
2022 COMPILE_ARRAY_GOTO(&ctx, sp->features, mod_c->features, options, u, lys_compile_feature, ret, error);
2023 COMPILE_ARRAY_GOTO(&ctx, sp->identities, mod_c->identities, options, u, lys_compile_identity, ret, error);
2024 if (sp->identities) {
2025 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
2026 }
2027
2028 LY_LIST_FOR(sp->data, node_p) {
2029 ret = lys_compile_node(&ctx, node_p, options, NULL);
2030 LY_CHECK_GOTO(ret, error);
2031 }
2032 //COMPILE_ARRAY_GOTO(ctx, sp->rpcs, mod_c->rpcs, options, u, lys_compile_action, ret, error);
2033 //COMPILE_ARRAY_GOTO(ctx, sp->notifs, mod_c->notifs, options, u, lys_compile_notif, ret, error);
2034
2035 COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, options, u, lys_compile_ext, ret, error);
2036
2037 if (options & LYSC_OPT_FREE_SP) {
2038 lysp_module_free(mod->parsed);
2039 ((struct lys_module*)mod)->parsed = NULL;
2040 }
2041
2042 ((struct lys_module*)mod)->compiled = mod_c;
2043 return LY_SUCCESS;
2044
2045error:
2046 lysc_module_free(mod_c, NULL);
2047 ((struct lys_module*)mod)->compiled = NULL;
2048 return ret;
2049}