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 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 17 | #include <assert.h> |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 18 | #include <ctype.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 19 | #include <stddef.h> |
| 20 | #include <stdint.h> |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 21 | #include <stdio.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 24 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 25 | #include "dict.h" |
| 26 | #include "log.h" |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 27 | #include "plugins_exts.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 28 | #include "set.h" |
| 29 | #include "plugins_types.h" |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 30 | #include "plugins_exts_internal.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 31 | #include "tree.h" |
| 32 | #include "tree_schema.h" |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 33 | #include "tree_schema_internal.h" |
| 34 | #include "xpath.h" |
| 35 | |
| 36 | /** |
| 37 | * @brief Duplicate string into dictionary |
| 38 | * @param[in] CTX libyang context of the dictionary. |
| 39 | * @param[in] ORIG String to duplicate. |
| 40 | * @param[out] DUP Where to store the result. |
| 41 | */ |
| 42 | #define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);} |
| 43 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 44 | #define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 45 | if (ARRAY_P) { \ |
| 46 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 47 | size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 48 | for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \ |
| 49 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 50 | RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 51 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 52 | } \ |
| 53 | } |
| 54 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 55 | #define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 56 | if (ARRAY_P) { \ |
| 57 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \ |
| 58 | size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
| 59 | for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \ |
| 60 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 61 | RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 62 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 63 | } \ |
| 64 | } |
| 65 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 66 | #define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \ |
| 67 | if (EXTS_P) { \ |
| 68 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_SIZE(EXTS_P), RET, GOTO); \ |
| 69 | for (uint32_t __exts_iter = 0, __array_offset = LY_ARRAY_SIZE(EXT_C); __exts_iter < LY_ARRAY_SIZE(EXTS_P); ++__exts_iter) { \ |
| 70 | LY_ARRAY_INCREMENT(EXT_C); \ |
| 71 | RET = lys_compile_ext(CTX, &(EXTS_P)[__exts_iter], &(EXT_C)[__exts_iter + __array_offset], PARENT, PARENT_TYPE); \ |
| 72 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 73 | } \ |
| 74 | } |
| 75 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 76 | #define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 77 | if (ARRAY_P) { \ |
| 78 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \ |
| 79 | size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
| 80 | for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \ |
| 81 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 82 | RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 83 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 84 | } \ |
| 85 | } |
| 86 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 87 | #define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 88 | if (MEMBER_P) { \ |
| 89 | MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \ |
| 90 | LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 91 | RET = FUNC(CTX, MEMBER_P, MEMBER_C); \ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 92 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 93 | } |
| 94 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 95 | #define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 96 | if (MEMBER_P) { \ |
| 97 | LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \ |
| 98 | size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \ |
| 99 | LY_ARRAY_INCREMENT(ARRAY_C); \ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 100 | RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 101 | LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \ |
| 102 | } |
| 103 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 104 | #define COMPILE_CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \ |
| 105 | if (ARRAY) { \ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 106 | for (unsigned int u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \ |
| 107 | if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \ |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 108 | LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \ |
| 109 | return LY_EVALID; \ |
| 110 | } \ |
| 111 | } \ |
| 112 | } |
| 113 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 114 | static struct lysc_ext_instance * |
| 115 | lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig) |
| 116 | { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 117 | /* TODO - extensions */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 118 | (void) ctx; |
| 119 | (void) orig; |
| 120 | return NULL; |
| 121 | } |
| 122 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 123 | /** |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 124 | * @brief Add record into the compile context's list of incomplete default values. |
| 125 | * @param[in] ctx Compile context with the incomplete default values list. |
| 126 | * @param[in] context_node Context schema node to store in the record. |
| 127 | * @param[in] dflt Incomplete default value to store in the record. |
| 128 | * @param[in] dflt_mod Module of the default value definition to store in the record. |
| 129 | * @return LY_EMEM in case of memory allocation failure. |
| 130 | * @return LY_SUCCESS |
| 131 | */ |
| 132 | static LY_ERR |
| 133 | lysc_incomplete_dflts_add(struct lysc_ctx *ctx, struct lysc_node *context_node, struct lyd_value *dflt, struct lys_module *dflt_mod) |
| 134 | { |
| 135 | struct lysc_incomplete_dflt *r; |
| 136 | r = malloc(sizeof *r); |
| 137 | LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM); |
| 138 | r->context_node = context_node; |
| 139 | r->dflt = dflt; |
| 140 | r->dflt_mod = dflt_mod; |
| 141 | ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST); |
| 142 | |
| 143 | return LY_SUCCESS; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * @brief Remove record of the given default value from the compile context's list of incomplete default values. |
| 148 | * @param[in] ctx Compile context with the incomplete default values list. |
| 149 | * @param[in] dflt Incomplete default values identifying the record to remove. |
| 150 | */ |
| 151 | static void |
| 152 | lysc_incomplete_dflts_remove(struct lysc_ctx *ctx, struct lyd_value *dflt) |
| 153 | { |
| 154 | unsigned int u; |
| 155 | struct lysc_incomplete_dflt *r; |
| 156 | |
| 157 | for (u = 0; u < ctx->dflts.count; ++u) { |
| 158 | r = (struct lysc_incomplete_dflt*)ctx->dflts.objs[u]; |
| 159 | if (r->dflt == dflt) { |
| 160 | free(ctx->dflts.objs[u]); |
| 161 | memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs); |
| 162 | --ctx->dflts.count; |
| 163 | return; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
Radek Krejci | 0e59c31 | 2019-08-15 15:34:15 +0200 | [diff] [blame] | 168 | void |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 169 | lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name) |
| 170 | { |
| 171 | int len; |
| 172 | int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */ |
| 173 | |
| 174 | if (!name) { |
| 175 | /* removing last path segment */ |
| 176 | if (ctx->path[ctx->path_len - 1] == '}') { |
| 177 | for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len); |
| 178 | if (ctx->path[ctx->path_len] == '=') { |
| 179 | ctx->path[ctx->path_len++] = '}'; |
| 180 | } else { |
| 181 | /* not a top-level special tag, remove also preceiding '/' */ |
| 182 | goto remove_nodelevel; |
| 183 | } |
| 184 | } else { |
| 185 | remove_nodelevel: |
| 186 | for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len); |
| 187 | if (ctx->path_len == 0) { |
| 188 | /* top-level (last segment) */ |
Radek Krejci | acc7904 | 2019-07-25 14:14:57 +0200 | [diff] [blame] | 189 | ctx->path_len = 1; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | /* set new terminating NULL-byte */ |
| 193 | ctx->path[ctx->path_len] = '\0'; |
| 194 | } else { |
| 195 | if (ctx->path_len > 1) { |
| 196 | if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') { |
| 197 | /* extension of the special tag */ |
| 198 | nextlevel = 2; |
| 199 | --ctx->path_len; |
| 200 | } else { |
| 201 | /* there is already some path, so add next level */ |
| 202 | nextlevel = 1; |
| 203 | } |
| 204 | } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */ |
| 205 | |
| 206 | if (nextlevel != 2) { |
| 207 | if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) { |
| 208 | /* module not changed, print the name unprefixed */ |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 209 | len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 210 | } else { |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 211 | len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s:%s", nextlevel ? "/" : "", ctx->mod->name, name); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 212 | } |
| 213 | } else { |
Radek Krejci | 70ee915 | 2019-07-25 11:27:27 +0200 | [diff] [blame] | 214 | len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 215 | } |
Radek Krejci | acc7904 | 2019-07-25 14:14:57 +0200 | [diff] [blame] | 216 | if (len >= LYSC_CTX_BUFSIZE - ctx->path_len) { |
| 217 | /* output truncated */ |
| 218 | ctx->path_len = LYSC_CTX_BUFSIZE - 1; |
| 219 | } else { |
| 220 | ctx->path_len += len; |
| 221 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
| 225 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 226 | * @brief Duplicate the compiled pattern structure. |
| 227 | * |
| 228 | * Instead of duplicating memory, the reference counter in the @p orig is increased. |
| 229 | * |
| 230 | * @param[in] orig The pattern structure to duplicate. |
| 231 | * @return The duplicated structure to use. |
| 232 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 233 | static struct lysc_pattern* |
| 234 | lysc_pattern_dup(struct lysc_pattern *orig) |
| 235 | { |
| 236 | ++orig->refcount; |
| 237 | return orig; |
| 238 | } |
| 239 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 240 | /** |
| 241 | * @brief Duplicate the array of compiled patterns. |
| 242 | * |
| 243 | * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter. |
| 244 | * |
| 245 | * @param[in] ctx Libyang context for logging. |
| 246 | * @param[in] orig The patterns sized array to duplicate. |
| 247 | * @return New sized array as a copy of @p orig. |
| 248 | * @return NULL in case of memory allocation error. |
| 249 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 250 | static struct lysc_pattern** |
| 251 | lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig) |
| 252 | { |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 253 | struct lysc_pattern **dup = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 254 | unsigned int u; |
| 255 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 256 | assert(orig); |
| 257 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 258 | LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL); |
| 259 | LY_ARRAY_FOR(orig, u) { |
| 260 | dup[u] = lysc_pattern_dup(orig[u]); |
| 261 | LY_ARRAY_INCREMENT(dup); |
| 262 | } |
| 263 | return dup; |
| 264 | } |
| 265 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 266 | /** |
| 267 | * @brief Duplicate compiled range structure. |
| 268 | * |
| 269 | * @param[in] ctx Libyang context for logging. |
| 270 | * @param[in] orig The range structure to be duplicated. |
| 271 | * @return New compiled range structure as a copy of @p orig. |
| 272 | * @return NULL in case of memory allocation error. |
| 273 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 274 | struct lysc_range* |
| 275 | lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig) |
| 276 | { |
| 277 | struct lysc_range *dup; |
| 278 | LY_ERR ret; |
| 279 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 280 | assert(orig); |
| 281 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 282 | dup = calloc(1, sizeof *dup); |
| 283 | LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL); |
| 284 | if (orig->parts) { |
| 285 | LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup); |
| 286 | LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts); |
| 287 | memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts); |
| 288 | } |
| 289 | DUP_STRING(ctx, orig->eapptag, dup->eapptag); |
| 290 | DUP_STRING(ctx, orig->emsg, dup->emsg); |
| 291 | dup->exts = lysc_ext_instance_dup(ctx, orig->exts); |
| 292 | |
| 293 | return dup; |
| 294 | cleanup: |
| 295 | free(dup); |
| 296 | (void) ret; /* set but not used due to the return type */ |
| 297 | return NULL; |
| 298 | } |
| 299 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 300 | /** |
| 301 | * @brief Stack for processing if-feature expressions. |
| 302 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 303 | struct iff_stack { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 304 | int size; /**< number of items in the stack */ |
| 305 | int index; /**< first empty item */ |
| 306 | uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 307 | }; |
| 308 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 309 | /** |
| 310 | * @brief Add @ref ifftokens into the stack. |
| 311 | * @param[in] stack The if-feature stack to use. |
| 312 | * @param[in] value One of the @ref ifftokens to store in the stack. |
| 313 | * @return LY_EMEM in case of memory allocation error |
| 314 | * @return LY_ESUCCESS if the value successfully stored. |
| 315 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 316 | static LY_ERR |
| 317 | iff_stack_push(struct iff_stack *stack, uint8_t value) |
| 318 | { |
| 319 | if (stack->index == stack->size) { |
| 320 | stack->size += 4; |
| 321 | stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack); |
| 322 | LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM); |
| 323 | } |
| 324 | stack->stack[stack->index++] = value; |
| 325 | return LY_SUCCESS; |
| 326 | } |
| 327 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 328 | /** |
| 329 | * @brief Get (and remove) the last item form the stack. |
| 330 | * @param[in] stack The if-feature stack to use. |
| 331 | * @return The value from the top of the stack. |
| 332 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 333 | static uint8_t |
| 334 | iff_stack_pop(struct iff_stack *stack) |
| 335 | { |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 336 | assert(stack && stack->index); |
| 337 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 338 | stack->index--; |
| 339 | return stack->stack[stack->index]; |
| 340 | } |
| 341 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 342 | /** |
| 343 | * @brief Clean up the stack. |
| 344 | * @param[in] stack The if-feature stack to use. |
| 345 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 346 | static void |
| 347 | iff_stack_clean(struct iff_stack *stack) |
| 348 | { |
| 349 | stack->size = 0; |
| 350 | free(stack->stack); |
| 351 | } |
| 352 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 353 | /** |
| 354 | * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array |
| 355 | * (libyang format of the if-feature expression). |
| 356 | * @param[in,out] list The 2bits array to modify. |
| 357 | * @param[in] op The operand (@ref ifftokens) to store. |
| 358 | * @param[in] pos Position (0-based) where to store the given @p op. |
| 359 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 360 | static void |
| 361 | iff_setop(uint8_t *list, uint8_t op, int pos) |
| 362 | { |
| 363 | uint8_t *item; |
| 364 | uint8_t mask = 3; |
| 365 | |
| 366 | assert(pos >= 0); |
| 367 | assert(op <= 3); /* max 2 bits */ |
| 368 | |
| 369 | item = &list[pos / 4]; |
| 370 | mask = mask << 2 * (pos % 4); |
| 371 | *item = (*item) & ~mask; |
| 372 | *item = (*item) | (op << 2 * (pos % 4)); |
| 373 | } |
| 374 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 375 | #define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */ |
| 376 | #define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 377 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 378 | /** |
| 379 | * @brief Find a feature of the given name and referenced in the given module. |
| 380 | * |
| 381 | * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is |
| 382 | * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such |
| 383 | * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema |
| 384 | * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via |
| 385 | * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers |
| 386 | * assigned till that time will be still valid. |
| 387 | * |
| 388 | * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature). |
| 389 | * @param[in] name Name of the feature including possible prefix. |
| 390 | * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!). |
| 391 | * @return Pointer to the feature structure if found, NULL otherwise. |
| 392 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 393 | static struct lysc_feature * |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 394 | lys_feature_find(struct lys_module *mod, const char *name, size_t len) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 395 | { |
| 396 | size_t i; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 397 | struct lysc_feature *f, *flist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 398 | |
| 399 | for (i = 0; i < len; ++i) { |
| 400 | if (name[i] == ':') { |
| 401 | /* we have a prefixed feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 402 | mod = lys_module_find_prefix(mod, name, i); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 403 | LY_CHECK_RET(!mod, NULL); |
| 404 | |
| 405 | name = &name[i + 1]; |
| 406 | len = len - i - 1; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | /* we have the correct module, get the feature */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 411 | if (mod->implemented) { |
| 412 | /* module is implemented so there is already the compiled schema */ |
| 413 | flist = mod->compiled->features; |
| 414 | } else { |
| 415 | flist = mod->off_features; |
| 416 | } |
| 417 | LY_ARRAY_FOR(flist, i) { |
| 418 | f = &flist[i]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 419 | if (!strncmp(f->name, name, len) && f->name[len] == '\0') { |
| 420 | return f; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | return NULL; |
| 425 | } |
| 426 | |
| 427 | static LY_ERR |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 428 | lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent, LYEXT_PARENT parent_type) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 429 | { |
| 430 | const char *name; |
| 431 | unsigned int u; |
| 432 | const struct lys_module *mod; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 433 | struct lysc_ext *elist = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 434 | |
| 435 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument); |
| 436 | ext->insubstmt = ext_p->insubstmt; |
| 437 | ext->insubstmt_index = ext_p->insubstmt_index; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 438 | ext->parent = parent; |
| 439 | ext->parent_type = parent_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 440 | |
| 441 | /* get module where the extension definition should be placed */ |
| 442 | for (u = 0; ext_p->name[u] != ':'; ++u); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 443 | mod = lys_module_find_prefix(ctx->mod_def, ext_p->name, u); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 444 | LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 445 | "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name), |
| 446 | LY_EVALID); |
| 447 | LY_CHECK_ERR_RET(!mod->parsed->extensions, |
| 448 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 449 | "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 450 | ext_p->name, mod->name), |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 451 | LY_EVALID); |
| 452 | name = &ext_p->name[u + 1]; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 453 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 454 | /* find the extension definition there */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 455 | if (mod->off_extensions) { |
| 456 | elist = mod->off_extensions; |
| 457 | } else { |
| 458 | elist = mod->compiled->extensions; |
| 459 | } |
| 460 | LY_ARRAY_FOR(elist, u) { |
| 461 | if (!strcmp(name, elist[u].name)) { |
| 462 | ext->def = &elist[u]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 463 | break; |
| 464 | } |
| 465 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 466 | LY_CHECK_ERR_RET(!ext->def, |
| 467 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 468 | "Extension definition of extension instance \"%s\" not found.", ext_p->name), |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 469 | LY_EVALID); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 470 | |
| 471 | if (ext->def->plugin && ext->def->plugin->compile) { |
| 472 | LY_CHECK_RET(ext->def->plugin->compile(ctx, ext_p, ext),LY_EVALID); |
| 473 | } |
| 474 | |
| 475 | return LY_SUCCESS; |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition. |
| 480 | */ |
| 481 | static LY_ERR |
| 482 | lys_compile_extension(struct lysc_ctx *ctx, struct lysp_ext *ext_p, struct lysc_ext *ext) |
| 483 | { |
| 484 | LY_ERR ret = LY_SUCCESS; |
| 485 | |
| 486 | DUP_STRING(ctx->ctx, ext_p->name, ext->name); |
| 487 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument); |
| 488 | ext->module = ctx->mod_def; |
| 489 | COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext->exts, ext, LYEXT_PAR_EXT, ret, done); |
| 490 | |
| 491 | done: |
| 492 | return ret; |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * @brief Link the extensions definitions with the available extension plugins. |
| 497 | * |
| 498 | * This is done only in the compiled (implemented) module. Extensions of a non-implemented modules |
| 499 | * are not connected with even available extension plugins. |
| 500 | * |
| 501 | * @param[in] extensions List of extensions to be processed ([sized array](@ref sizedarrays)). |
| 502 | */ |
| 503 | static void |
| 504 | lys_compile_extension_plugins(struct lysc_ext *extensions) |
| 505 | { |
| 506 | unsigned int u; |
| 507 | |
| 508 | LY_ARRAY_FOR(extensions, u) { |
| 509 | extensions[u].plugin = lyext_get_plugin(&extensions[u]); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | LY_ERR |
| 514 | lys_extension_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, |
| 515 | struct lysp_ext *extensions_p, struct lysc_ext **extensions) |
| 516 | { |
| 517 | unsigned int offset = 0, u; |
| 518 | struct lysc_ctx context = {0}; |
| 519 | |
| 520 | assert(ctx_sc || ctx); |
| 521 | |
| 522 | if (!ctx_sc) { |
| 523 | context.ctx = ctx; |
| 524 | context.mod = module; |
| 525 | context.path_len = 1; |
| 526 | context.path[0] = '/'; |
| 527 | ctx_sc = &context; |
| 528 | } |
| 529 | |
| 530 | if (!extensions_p) { |
| 531 | return LY_SUCCESS; |
| 532 | } |
| 533 | if (*extensions) { |
| 534 | offset = LY_ARRAY_SIZE(*extensions); |
| 535 | } |
| 536 | |
| 537 | lysc_update_path(ctx_sc, NULL, "{extension}"); |
| 538 | LY_ARRAY_CREATE_RET(ctx_sc->ctx, *extensions, LY_ARRAY_SIZE(extensions_p), LY_EMEM); |
| 539 | LY_ARRAY_FOR(extensions_p, u) { |
| 540 | lysc_update_path(ctx_sc, NULL, extensions_p[u].name); |
| 541 | LY_ARRAY_INCREMENT(*extensions); |
| 542 | COMPILE_CHECK_UNIQUENESS(ctx_sc, *extensions, name, &(*extensions)[offset + u], "extension", extensions_p[u].name); |
| 543 | LY_CHECK_RET(lys_compile_extension(ctx_sc, &extensions_p[u], &(*extensions)[offset + u])); |
| 544 | lysc_update_path(ctx_sc, NULL, NULL); |
| 545 | } |
| 546 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 547 | |
| 548 | return LY_SUCCESS; |
| 549 | } |
| 550 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 551 | /** |
| 552 | * @brief Compile information from the if-feature statement |
| 553 | * @param[in] ctx Compile context. |
| 554 | * @param[in] value The if-feature argument to process. It is pointer-to-pointer-to-char just to unify the compile functions. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 555 | * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill. |
| 556 | * @return LY_ERR value. |
| 557 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 558 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 559 | lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 560 | { |
| 561 | const char *c = *value; |
| 562 | int r, rc = EXIT_FAILURE; |
| 563 | int i, j, last_not, checkversion = 0; |
| 564 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
| 565 | uint8_t op; |
| 566 | struct iff_stack stack = {0, 0, NULL}; |
| 567 | struct lysc_feature *f; |
| 568 | |
| 569 | assert(c); |
| 570 | |
| 571 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 572 | for (i = j = last_not = 0; c[i]; i++) { |
| 573 | if (c[i] == '(') { |
| 574 | j++; |
| 575 | checkversion = 1; |
| 576 | continue; |
| 577 | } else if (c[i] == ')') { |
| 578 | j--; |
| 579 | continue; |
| 580 | } else if (isspace(c[i])) { |
| 581 | checkversion = 1; |
| 582 | continue; |
| 583 | } |
| 584 | |
| 585 | if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) { |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 586 | int sp; |
| 587 | for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++); |
| 588 | if (c[i + r + sp] == '\0') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 589 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 590 | "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value); |
| 591 | return LY_EVALID; |
| 592 | } else if (!isspace(c[i + r])) { |
| 593 | /* feature name starting with the not/and/or */ |
| 594 | last_not = 0; |
| 595 | f_size++; |
| 596 | } else if (c[i] == 'n') { /* not operation */ |
| 597 | if (last_not) { |
| 598 | /* double not */ |
| 599 | expr_size = expr_size - 2; |
| 600 | last_not = 0; |
| 601 | } else { |
| 602 | last_not = 1; |
| 603 | } |
| 604 | } else { /* and, or */ |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 605 | if (f_exp != f_size) { |
| 606 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 607 | "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]); |
| 608 | return LY_EVALID; |
| 609 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 610 | f_exp++; |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 611 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 612 | /* not a not operation */ |
| 613 | last_not = 0; |
| 614 | } |
| 615 | i += r; |
| 616 | } else { |
| 617 | f_size++; |
| 618 | last_not = 0; |
| 619 | } |
| 620 | expr_size++; |
| 621 | |
| 622 | while (!isspace(c[i])) { |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 623 | if (!c[i] || c[i] == ')' || c[i] == '(') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 624 | i--; |
| 625 | break; |
| 626 | } |
| 627 | i++; |
| 628 | } |
| 629 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 630 | if (j) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 631 | /* not matching count of ( and ) */ |
| 632 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 633 | "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value); |
| 634 | return LY_EVALID; |
| 635 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 636 | if (f_exp != f_size) { |
| 637 | /* features do not match the needed arguments for the logical operations */ |
| 638 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 639 | "Invalid value \"%s\" of if-feature - number of features in expression does not match " |
| 640 | "the required number of operands for the operations.", *value); |
| 641 | return LY_EVALID; |
| 642 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 643 | |
| 644 | if (checkversion || expr_size > 1) { |
| 645 | /* check that we have 1.1 module */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 646 | if (ctx->mod_def->version != LYS_VERSION_1_1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 647 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 648 | "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value); |
| 649 | return LY_EVALID; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | /* allocate the memory */ |
| 654 | LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM); |
| 655 | iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr); |
| 656 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 657 | LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error); |
| 658 | |
| 659 | stack.size = expr_size; |
| 660 | f_size--; expr_size--; /* used as indexes from now */ |
| 661 | |
| 662 | for (i--; i >= 0; i--) { |
| 663 | if (c[i] == ')') { |
| 664 | /* push it on stack */ |
| 665 | iff_stack_push(&stack, LYS_IFF_RP); |
| 666 | continue; |
| 667 | } else if (c[i] == '(') { |
| 668 | /* pop from the stack into result all operators until ) */ |
| 669 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 670 | iff_setop(iff->expr, op, expr_size--); |
| 671 | } |
| 672 | continue; |
| 673 | } else if (isspace(c[i])) { |
| 674 | continue; |
| 675 | } |
| 676 | |
| 677 | /* end of operator or operand -> find beginning and get what is it */ |
| 678 | j = i + 1; |
| 679 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 680 | i--; |
| 681 | } |
| 682 | i++; /* go back by one step */ |
| 683 | |
| 684 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
| 685 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 686 | /* double not */ |
| 687 | iff_stack_pop(&stack); |
| 688 | } else { |
| 689 | /* not has the highest priority, so do not pop from the stack |
| 690 | * as in case of AND and OR */ |
| 691 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 692 | } |
| 693 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
| 694 | /* as for OR - pop from the stack all operators with the same or higher |
| 695 | * priority and store them to the result, then push the AND to the stack */ |
| 696 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 697 | op = iff_stack_pop(&stack); |
| 698 | iff_setop(iff->expr, op, expr_size--); |
| 699 | } |
| 700 | iff_stack_push(&stack, LYS_IFF_AND); |
| 701 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
| 702 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 703 | op = iff_stack_pop(&stack); |
| 704 | iff_setop(iff->expr, op, expr_size--); |
| 705 | } |
| 706 | iff_stack_push(&stack, LYS_IFF_OR); |
| 707 | } else { |
| 708 | /* feature name, length is j - i */ |
| 709 | |
| 710 | /* add it to the expression */ |
| 711 | iff_setop(iff->expr, LYS_IFF_F, expr_size--); |
| 712 | |
| 713 | /* now get the link to the feature definition */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 714 | f = lys_feature_find(ctx->mod_def, &c[i], j - i); |
| 715 | LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 716 | "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]); |
| 717 | rc = LY_EVALID, error) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 718 | iff->features[f_size] = f; |
| 719 | LY_ARRAY_INCREMENT(iff->features); |
| 720 | f_size--; |
| 721 | } |
| 722 | } |
| 723 | while (stack.index) { |
| 724 | op = iff_stack_pop(&stack); |
| 725 | iff_setop(iff->expr, op, expr_size--); |
| 726 | } |
| 727 | |
| 728 | if (++expr_size || ++f_size) { |
| 729 | /* not all expected operators and operands found */ |
| 730 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 731 | "Invalid value \"%s\" of if-feature - processing error.", *value); |
| 732 | rc = LY_EINT; |
| 733 | } else { |
| 734 | rc = LY_SUCCESS; |
| 735 | } |
| 736 | |
| 737 | error: |
| 738 | /* cleanup */ |
| 739 | iff_stack_clean(&stack); |
| 740 | |
| 741 | return rc; |
| 742 | } |
| 743 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 744 | /** |
| 745 | * @brief Compile information from the when statement |
| 746 | * @param[in] ctx Compile context. |
| 747 | * @param[in] when_p The parsed when statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 748 | * @param[out] when Pointer where to store pointer to the created compiled when structure. |
| 749 | * @return LY_ERR value. |
| 750 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 751 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 752 | lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, struct lysc_when **when) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 753 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 754 | LY_ERR ret = LY_SUCCESS; |
| 755 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 756 | *when = calloc(1, sizeof **when); |
| 757 | (*when)->refcount = 1; |
| 758 | (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond); |
| 759 | DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc); |
| 760 | DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref); |
| 761 | LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 762 | COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 763 | |
| 764 | done: |
| 765 | return ret; |
| 766 | } |
| 767 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 768 | /** |
| 769 | * @brief Compile information from the must statement |
| 770 | * @param[in] ctx Compile context. |
| 771 | * @param[in] must_p The parsed must statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 772 | * @param[in,out] must Prepared (empty) compiled must structure to fill. |
| 773 | * @return LY_ERR value. |
| 774 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 775 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 776 | lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 777 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 778 | LY_ERR ret = LY_SUCCESS; |
| 779 | |
| 780 | must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg); |
| 781 | LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 462cf25 | 2019-07-24 09:49:08 +0200 | [diff] [blame] | 782 | must->module = ctx->mod_def; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 783 | DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag); |
| 784 | DUP_STRING(ctx->ctx, must_p->emsg, must->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 785 | DUP_STRING(ctx->ctx, must_p->dsc, must->dsc); |
| 786 | DUP_STRING(ctx->ctx, must_p->ref, must->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 787 | COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 788 | |
| 789 | done: |
| 790 | return ret; |
| 791 | } |
| 792 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 793 | /** |
| 794 | * @brief Compile information from the import statement |
| 795 | * @param[in] ctx Compile context. |
| 796 | * @param[in] imp_p The parsed import statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 797 | * @param[in,out] imp Prepared (empty) compiled import structure to fill. |
| 798 | * @return LY_ERR value. |
| 799 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 800 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 801 | lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, struct lysc_import *imp) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 802 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 803 | struct lys_module *mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 804 | LY_ERR ret = LY_SUCCESS; |
| 805 | |
| 806 | DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 807 | COMPILE_EXTS_GOTO(ctx, imp_p->exts, imp->exts, imp, LYEXT_PAR_IMPORT, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 808 | imp->module = imp_p->module; |
| 809 | |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 810 | /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs. |
| 811 | * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added, |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 812 | * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 813 | if (!imp->module->parsed) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 814 | /* try to use filepath if present */ |
| 815 | if (imp->module->filepath) { |
| 816 | mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath, |
| 817 | !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 818 | if (mod != imp->module) { |
| 819 | LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 820 | imp->module->filepath, imp->module->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 821 | mod = NULL; |
| 822 | } |
| 823 | } |
| 824 | if (!mod) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 825 | if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 826 | LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 827 | imp->module->name, ctx->mod->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 828 | return LY_ENOTFOUND; |
| 829 | } |
| 830 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | done: |
| 834 | return ret; |
| 835 | } |
| 836 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 837 | /** |
| 838 | * @brief Compile information from the identity statement |
| 839 | * |
| 840 | * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases(). |
| 841 | * |
| 842 | * @param[in] ctx Compile context. |
| 843 | * @param[in] ident_p The parsed identity statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 844 | * @param[in] idents List of so far compiled identities to check the name uniqueness. |
| 845 | * @param[in,out] ident Prepared (empty) compiled identity structure to fill. |
| 846 | * @return LY_ERR value. |
| 847 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 848 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 849 | lys_compile_identity(struct lysc_ctx *ctx, struct lysp_ident *ident_p, struct lysc_ident *idents, struct lysc_ident *ident) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 850 | { |
| 851 | unsigned int u; |
| 852 | LY_ERR ret = LY_SUCCESS; |
| 853 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 854 | lysc_update_path(ctx, NULL, ident_p->name); |
| 855 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 856 | COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 857 | DUP_STRING(ctx->ctx, ident_p->name, ident->name); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 858 | DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc); |
| 859 | DUP_STRING(ctx->ctx, ident_p->ref, ident->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 860 | ident->module = ctx->mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 861 | COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, u, lys_compile_iffeature, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 862 | /* backlings (derived) can be added no sooner than when all the identities in the current module are present */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 863 | COMPILE_EXTS_GOTO(ctx, ident_p->exts, ident->exts, ident, LYEXT_PAR_IDENT, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 864 | ident->flags = ident_p->flags; |
| 865 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 866 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 867 | done: |
| 868 | return ret; |
| 869 | } |
| 870 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 871 | /** |
| 872 | * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement). |
| 873 | * |
| 874 | * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages. |
| 875 | * |
| 876 | * @param[in] ctx Compile context for logging. |
| 877 | * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed). |
| 878 | * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident) |
| 879 | * @return LY_SUCCESS if everything is ok. |
| 880 | * @return LY_EVALID if the identity is derived from itself. |
| 881 | */ |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 882 | static LY_ERR |
| 883 | lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived) |
| 884 | { |
| 885 | LY_ERR ret = LY_EVALID; |
| 886 | unsigned int u, v; |
| 887 | struct ly_set recursion = {0}; |
| 888 | struct lysc_ident *drv; |
| 889 | |
| 890 | if (!derived) { |
| 891 | return LY_SUCCESS; |
| 892 | } |
| 893 | |
| 894 | for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) { |
| 895 | if (ident == derived[u]) { |
| 896 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 897 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 898 | goto cleanup; |
| 899 | } |
| 900 | ly_set_add(&recursion, derived[u], 0); |
| 901 | } |
| 902 | |
| 903 | for (v = 0; v < recursion.count; ++v) { |
| 904 | drv = recursion.objs[v]; |
| 905 | if (!drv->derived) { |
| 906 | continue; |
| 907 | } |
| 908 | for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) { |
| 909 | if (ident == drv->derived[u]) { |
| 910 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 911 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 912 | goto cleanup; |
| 913 | } |
| 914 | ly_set_add(&recursion, drv->derived[u], 0); |
| 915 | } |
| 916 | } |
| 917 | ret = LY_SUCCESS; |
| 918 | |
| 919 | cleanup: |
| 920 | ly_set_erase(&recursion, NULL); |
| 921 | return ret; |
| 922 | } |
| 923 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 924 | /** |
| 925 | * @brief Find and process the referenced base identities from another identity or identityref |
| 926 | * |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 927 | * For bases in identity set backlinks to them from the base identities. For identityref, store |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 928 | * the array of pointers to the base identities. So one of the ident or bases parameter must be set |
| 929 | * to distinguish these two use cases. |
| 930 | * |
| 931 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 932 | * @param[in] bases_p Array of names (including prefix if necessary) of base identities. |
| 933 | * @param[in] ident Referencing identity to work with. |
| 934 | * @param[in] bases Array of bases of identityref to fill in. |
| 935 | * @return LY_ERR value. |
| 936 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 937 | static LY_ERR |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 938 | 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] | 939 | { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 940 | unsigned int u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 941 | const char *s, *name; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 942 | struct lys_module *mod; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 943 | struct lysc_ident **idref; |
| 944 | |
| 945 | assert(ident || bases); |
| 946 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 947 | if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 948 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 949 | "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type"); |
| 950 | return LY_EVALID; |
| 951 | } |
| 952 | |
| 953 | for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) { |
| 954 | s = strchr(bases_p[u], ':'); |
| 955 | if (s) { |
| 956 | /* prefixed identity */ |
| 957 | name = &s[1]; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 958 | mod = lys_module_find_prefix(ctx->mod_def, bases_p[u], s - bases_p[u]); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 959 | } else { |
| 960 | name = bases_p[u]; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 961 | mod = ctx->mod_def; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 962 | } |
| 963 | if (!mod) { |
| 964 | if (ident) { |
| 965 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 966 | "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 967 | } else { |
| 968 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 969 | "Invalid prefix used for base (%s) of identityref.", bases_p[u]); |
| 970 | } |
| 971 | return LY_EVALID; |
| 972 | } |
| 973 | idref = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 974 | if (mod->compiled && mod->compiled->identities) { |
| 975 | for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) { |
| 976 | if (!strcmp(name, mod->compiled->identities[v].name)) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 977 | if (ident) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 978 | if (ident == &mod->compiled->identities[v]) { |
| 979 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 980 | "Identity \"%s\" is derived from itself.", ident->name); |
| 981 | return LY_EVALID; |
| 982 | } |
| 983 | LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->compiled->identities[v], ident->derived)); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 984 | /* we have match! store the backlink */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 985 | LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 986 | *idref = ident; |
| 987 | } else { |
| 988 | /* we have match! store the found identity */ |
| 989 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 990 | *idref = &mod->compiled->identities[v]; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 991 | } |
| 992 | break; |
| 993 | } |
| 994 | } |
| 995 | } |
| 996 | if (!idref || !(*idref)) { |
| 997 | if (ident) { |
| 998 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 999 | "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1000 | } else { |
| 1001 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1002 | "Unable to find base (%s) of identityref.", bases_p[u]); |
| 1003 | } |
| 1004 | return LY_EVALID; |
| 1005 | } |
| 1006 | } |
| 1007 | return LY_SUCCESS; |
| 1008 | } |
| 1009 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1010 | /** |
| 1011 | * @brief For the given array of identities, set the backlinks from all their base identities. |
| 1012 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1013 | * @param[in] idents_p Array of identities definitions from the parsed schema structure. |
| 1014 | * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set. |
| 1015 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1016 | */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1017 | static LY_ERR |
| 1018 | lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents) |
| 1019 | { |
| 1020 | unsigned int i; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1021 | |
| 1022 | for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) { |
| 1023 | if (!idents_p[i].bases) { |
| 1024 | continue; |
| 1025 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1026 | lysc_update_path(ctx, NULL, idents[i].name); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1027 | LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL)); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1028 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1029 | } |
| 1030 | return LY_SUCCESS; |
| 1031 | } |
| 1032 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1033 | LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1034 | lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, struct lysp_feature *features_p, struct lysc_feature **features) |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1035 | { |
| 1036 | unsigned int offset = 0, u; |
| 1037 | struct lysc_ctx context = {0}; |
| 1038 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1039 | assert(ctx_sc || ctx); |
| 1040 | |
| 1041 | if (!ctx_sc) { |
| 1042 | context.ctx = ctx; |
| 1043 | context.mod = module; |
| 1044 | context.path_len = 1; |
| 1045 | context.path[0] = '/'; |
| 1046 | ctx_sc = &context; |
| 1047 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1048 | |
| 1049 | if (!features_p) { |
| 1050 | return LY_SUCCESS; |
| 1051 | } |
| 1052 | if (*features) { |
| 1053 | offset = LY_ARRAY_SIZE(*features); |
| 1054 | } |
| 1055 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1056 | lysc_update_path(ctx_sc, NULL, "{feature}"); |
| 1057 | LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1058 | LY_ARRAY_FOR(features_p, u) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1059 | lysc_update_path(ctx_sc, NULL, features_p[u].name); |
| 1060 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1061 | LY_ARRAY_INCREMENT(*features); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1062 | COMPILE_CHECK_UNIQUENESS(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name); |
| 1063 | DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name); |
| 1064 | DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc); |
| 1065 | DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1066 | (*features)[offset + u].flags = features_p[u].flags; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1067 | (*features)[offset + u].module = ctx_sc->mod; |
| 1068 | |
| 1069 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1070 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1071 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1072 | |
| 1073 | return LY_SUCCESS; |
| 1074 | } |
| 1075 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1076 | /** |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1077 | * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement). |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1078 | * |
| 1079 | * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages. |
| 1080 | * |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1081 | * @param[in] ctx Compile context for logging. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1082 | * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature |
| 1083 | * being currently processed). |
| 1084 | * @param[in] depfeatures The list of depending features of the feature being currently processed (not the one provided as @p feature) |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1085 | * @return LY_SUCCESS if everything is ok. |
| 1086 | * @return LY_EVALID if the feature references indirectly itself. |
| 1087 | */ |
| 1088 | static LY_ERR |
| 1089 | lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures) |
| 1090 | { |
| 1091 | LY_ERR ret = LY_EVALID; |
| 1092 | unsigned int u, v; |
| 1093 | struct ly_set recursion = {0}; |
| 1094 | struct lysc_feature *drv; |
| 1095 | |
| 1096 | if (!depfeatures) { |
| 1097 | return LY_SUCCESS; |
| 1098 | } |
| 1099 | |
| 1100 | for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) { |
| 1101 | if (feature == depfeatures[u]) { |
| 1102 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1103 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1104 | goto cleanup; |
| 1105 | } |
| 1106 | ly_set_add(&recursion, depfeatures[u], 0); |
| 1107 | } |
| 1108 | |
| 1109 | for (v = 0; v < recursion.count; ++v) { |
| 1110 | drv = recursion.objs[v]; |
| 1111 | if (!drv->depfeatures) { |
| 1112 | continue; |
| 1113 | } |
| 1114 | for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) { |
| 1115 | if (feature == drv->depfeatures[u]) { |
| 1116 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1117 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1118 | goto cleanup; |
| 1119 | } |
| 1120 | ly_set_add(&recursion, drv->depfeatures[u], 0); |
| 1121 | } |
| 1122 | } |
| 1123 | ret = LY_SUCCESS; |
| 1124 | |
| 1125 | cleanup: |
| 1126 | ly_set_erase(&recursion, NULL); |
| 1127 | return ret; |
| 1128 | } |
| 1129 | |
| 1130 | /** |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1131 | * @brief Create pre-compiled features array. |
| 1132 | * |
| 1133 | * See lys_feature_precompile() for more details. |
| 1134 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1135 | * @param[in] ctx Compile context. |
| 1136 | * @param[in] feature_p Parsed feature definition to compile. |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1137 | * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1138 | * @return LY_ERR value. |
| 1139 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1140 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1141 | lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1142 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1143 | unsigned int u, v, x; |
| 1144 | struct lysc_feature *feature, **df; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1145 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1146 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1147 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1148 | /* find the preprecompiled feature */ |
| 1149 | LY_ARRAY_FOR(features, x) { |
| 1150 | if (strcmp(features[x].name, feature_p->name)) { |
| 1151 | continue; |
| 1152 | } |
| 1153 | feature = &features[x]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1154 | lysc_update_path(ctx, NULL, "{feature}"); |
| 1155 | lysc_update_path(ctx, NULL, feature_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1156 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1157 | /* finish compilation started in lys_feature_precompile() */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1158 | COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1159 | COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1160 | if (feature->iffeatures) { |
| 1161 | for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) { |
| 1162 | if (feature->iffeatures[u].features) { |
| 1163 | for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1164 | /* check for circular dependency - direct reference first,... */ |
| 1165 | if (feature == feature->iffeatures[u].features[v]) { |
| 1166 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1167 | "Feature \"%s\" is referenced from itself.", feature->name); |
| 1168 | return LY_EVALID; |
| 1169 | } |
| 1170 | /* ... and indirect circular reference */ |
| 1171 | LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures)); |
| 1172 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1173 | /* add itself into the dependants list */ |
| 1174 | LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM); |
| 1175 | *df = feature; |
| 1176 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1177 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1178 | } |
| 1179 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1180 | lysc_update_path(ctx, NULL, NULL); |
| 1181 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1182 | done: |
| 1183 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1184 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1185 | |
| 1186 | LOGINT(ctx->ctx); |
| 1187 | return LY_EINT; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1188 | } |
| 1189 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1190 | /** |
| 1191 | * @brief Revert compiled list of features back to the precompiled state. |
| 1192 | * |
| 1193 | * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status. |
| 1194 | * The features are supposed to be stored again as off_features in ::lys_module structure. |
| 1195 | * |
| 1196 | * @param[in] ctx Compilation context. |
| 1197 | * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema |
| 1198 | * and supposed to hold the off_features list. |
| 1199 | */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1200 | static void |
| 1201 | lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod) |
| 1202 | { |
| 1203 | unsigned int u, v; |
| 1204 | |
| 1205 | /* keep the off_features list until the complete lys_module is freed */ |
| 1206 | mod->off_features = mod->compiled->features; |
| 1207 | mod->compiled->features = NULL; |
| 1208 | |
| 1209 | /* in the off_features list, remove all the parts (from finished compiling process) |
| 1210 | * which may points into the data being freed here */ |
| 1211 | LY_ARRAY_FOR(mod->off_features, u) { |
| 1212 | LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) { |
| 1213 | lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]); |
| 1214 | } |
| 1215 | LY_ARRAY_FREE(mod->off_features[u].iffeatures); |
| 1216 | mod->off_features[u].iffeatures = NULL; |
| 1217 | |
| 1218 | LY_ARRAY_FOR(mod->off_features[u].exts, v) { |
| 1219 | lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]); |
| 1220 | } |
| 1221 | LY_ARRAY_FREE(mod->off_features[u].exts); |
| 1222 | mod->off_features[u].exts = NULL; |
| 1223 | } |
| 1224 | } |
| 1225 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1226 | /** |
| 1227 | * @brief Validate and normalize numeric value from a range definition. |
| 1228 | * @param[in] ctx Compile context. |
| 1229 | * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to |
| 1230 | * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into |
| 1231 | * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from |
| 1232 | * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100. |
| 1233 | * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64. |
| 1234 | * @param[in] value String value of the range boundary. |
| 1235 | * @param[out] len Number of the processed bytes from the value. Processing stops on the first character which is not part of the number boundary. |
| 1236 | * @param[out] valcopy NULL-terminated string with the numeric value to parse and store. |
| 1237 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value). |
| 1238 | */ |
Radek Krejci | e88beef | 2019-05-30 15:47:19 +0200 | [diff] [blame] | 1239 | LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1240 | 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] | 1241 | { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1242 | size_t fraction = 0, size; |
| 1243 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1244 | *len = 0; |
| 1245 | |
| 1246 | assert(value); |
| 1247 | /* parse value */ |
| 1248 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 1249 | return LY_EVALID; |
| 1250 | } |
| 1251 | |
| 1252 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 1253 | ++(*len); |
| 1254 | } |
| 1255 | |
| 1256 | while (isdigit(value[*len])) { |
| 1257 | ++(*len); |
| 1258 | } |
| 1259 | |
| 1260 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1261 | if (basetype == LY_TYPE_DEC64) { |
| 1262 | goto decimal; |
| 1263 | } else { |
| 1264 | *valcopy = strndup(value, *len); |
| 1265 | return LY_SUCCESS; |
| 1266 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1267 | } |
| 1268 | fraction = *len; |
| 1269 | |
| 1270 | ++(*len); |
| 1271 | while (isdigit(value[*len])) { |
| 1272 | ++(*len); |
| 1273 | } |
| 1274 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1275 | if (basetype == LY_TYPE_DEC64) { |
| 1276 | decimal: |
| 1277 | assert(frdigits); |
Radek Krejci | 943177f | 2019-06-14 16:32:43 +0200 | [diff] [blame] | 1278 | if (fraction && (*len - 1 - fraction > frdigits)) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1279 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1280 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
| 1281 | *len, value, frdigits); |
| 1282 | return LY_EINVAL; |
| 1283 | } |
| 1284 | if (fraction) { |
| 1285 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 1286 | } else { |
| 1287 | size = (*len) + frdigits + 1; |
| 1288 | } |
| 1289 | *valcopy = malloc(size * sizeof **valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1290 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 1291 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1292 | (*valcopy)[size - 1] = '\0'; |
| 1293 | if (fraction) { |
| 1294 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 1295 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 1296 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 1297 | } else { |
| 1298 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 1299 | memset(&(*valcopy)[*len], '0', frdigits); |
| 1300 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1301 | } |
| 1302 | return LY_SUCCESS; |
| 1303 | } |
| 1304 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1305 | /** |
| 1306 | * @brief Check that values in range are in ascendant order. |
| 1307 | * @param[in] unsigned_value Flag to note that we are working with unsigned values. |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1308 | * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous, |
| 1309 | * max can be also equal. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1310 | * @param[in] value Current value to check. |
| 1311 | * @param[in] prev_value The last seen value. |
| 1312 | * @return LY_SUCCESS or LY_EEXIST for invalid order. |
| 1313 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1314 | static LY_ERR |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1315 | range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1316 | { |
| 1317 | if (unsigned_value) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1318 | if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1319 | return LY_EEXIST; |
| 1320 | } |
| 1321 | } else { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1322 | if ((max && prev_value > value) || (!max && prev_value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1323 | return LY_EEXIST; |
| 1324 | } |
| 1325 | } |
| 1326 | return LY_SUCCESS; |
| 1327 | } |
| 1328 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1329 | /** |
| 1330 | * @brief Set min/max value of the range part. |
| 1331 | * @param[in] ctx Compile context. |
| 1332 | * @param[in] part Range part structure to fill. |
| 1333 | * @param[in] max Flag to distinguish if storing min or max value. |
| 1334 | * @param[in] prev The last seen value to check that all values in range are specified in ascendant order. |
| 1335 | * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules. |
| 1336 | * @param[in] first Flag for the first value of the range to avoid ascendancy order. |
| 1337 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1338 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1339 | * @param[in] base_range Range from the type from which the current type is derived (if not built-in) to get type's min and max values. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1340 | * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set. |
| 1341 | * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number), |
| 1342 | * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the |
| 1343 | * frdigits value), LY_EMEM. |
| 1344 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1345 | static LY_ERR |
| 1346 | range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, int max, int64_t prev, LY_DATA_TYPE basetype, int first, int length_restr, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1347 | uint8_t frdigits, struct lysc_range *base_range, const char **value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1348 | { |
| 1349 | LY_ERR ret = LY_SUCCESS; |
| 1350 | char *valcopy = NULL; |
| 1351 | size_t len; |
| 1352 | |
| 1353 | if (value) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1354 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1355 | LY_CHECK_GOTO(ret, finalize); |
| 1356 | } |
| 1357 | if (!valcopy && base_range) { |
| 1358 | if (max) { |
| 1359 | part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64; |
| 1360 | } else { |
| 1361 | part->min_64 = base_range->parts[0].min_64; |
| 1362 | } |
| 1363 | if (!first) { |
| 1364 | ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev); |
| 1365 | } |
| 1366 | goto finalize; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | switch (basetype) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1370 | case LY_TYPE_INT8: /* range */ |
| 1371 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1372 | ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1373 | } else if (max) { |
| 1374 | part->max_64 = INT64_C(127); |
| 1375 | } else { |
| 1376 | part->min_64 = INT64_C(-128); |
| 1377 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1378 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1379 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1380 | } |
| 1381 | break; |
| 1382 | case LY_TYPE_INT16: /* range */ |
| 1383 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1384 | ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1385 | } else if (max) { |
| 1386 | part->max_64 = INT64_C(32767); |
| 1387 | } else { |
| 1388 | part->min_64 = INT64_C(-32768); |
| 1389 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1390 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1391 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1392 | } |
| 1393 | break; |
| 1394 | case LY_TYPE_INT32: /* range */ |
| 1395 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1396 | ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1397 | } else if (max) { |
| 1398 | part->max_64 = INT64_C(2147483647); |
| 1399 | } else { |
| 1400 | part->min_64 = INT64_C(-2147483648); |
| 1401 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1402 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1403 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1404 | } |
| 1405 | break; |
| 1406 | case LY_TYPE_INT64: /* range */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1407 | case LY_TYPE_DEC64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1408 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1409 | ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1410 | max ? &part->max_64 : &part->min_64); |
| 1411 | } else if (max) { |
| 1412 | part->max_64 = INT64_C(9223372036854775807); |
| 1413 | } else { |
| 1414 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 1415 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1416 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1417 | ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1418 | } |
| 1419 | break; |
| 1420 | case LY_TYPE_UINT8: /* range */ |
| 1421 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1422 | ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1423 | } else if (max) { |
| 1424 | part->max_u64 = UINT64_C(255); |
| 1425 | } else { |
| 1426 | part->min_u64 = UINT64_C(0); |
| 1427 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1428 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1429 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1430 | } |
| 1431 | break; |
| 1432 | case LY_TYPE_UINT16: /* range */ |
| 1433 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1434 | ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1435 | } else if (max) { |
| 1436 | part->max_u64 = UINT64_C(65535); |
| 1437 | } else { |
| 1438 | part->min_u64 = UINT64_C(0); |
| 1439 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1440 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1441 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1442 | } |
| 1443 | break; |
| 1444 | case LY_TYPE_UINT32: /* range */ |
| 1445 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1446 | ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1447 | } else if (max) { |
| 1448 | part->max_u64 = UINT64_C(4294967295); |
| 1449 | } else { |
| 1450 | part->min_u64 = UINT64_C(0); |
| 1451 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1452 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1453 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1454 | } |
| 1455 | break; |
| 1456 | case LY_TYPE_UINT64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1457 | case LY_TYPE_STRING: /* length */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1458 | case LY_TYPE_BINARY: /* length */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1459 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1460 | ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1461 | } else if (max) { |
| 1462 | part->max_u64 = UINT64_C(18446744073709551615); |
| 1463 | } else { |
| 1464 | part->min_u64 = UINT64_C(0); |
| 1465 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1466 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1467 | ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1468 | } |
| 1469 | break; |
| 1470 | default: |
| 1471 | LOGINT(ctx->ctx); |
| 1472 | ret = LY_EINT; |
| 1473 | } |
| 1474 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1475 | finalize: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1476 | if (ret == LY_EDENIED) { |
| 1477 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1478 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 1479 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1480 | } else if (ret == LY_EVALID) { |
| 1481 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1482 | "Invalid %s restriction - invalid value \"%s\".", |
| 1483 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1484 | } else if (ret == LY_EEXIST) { |
| 1485 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1486 | "Invalid %s restriction - values are not in ascending order (%s).", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1487 | length_restr ? "length" : "range", |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1488 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1489 | } else if (!ret && value) { |
| 1490 | *value = *value + len; |
| 1491 | } |
| 1492 | free(valcopy); |
| 1493 | return ret; |
| 1494 | } |
| 1495 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1496 | /** |
| 1497 | * @brief Compile the parsed range restriction. |
| 1498 | * @param[in] ctx Compile context. |
| 1499 | * @param[in] range_p Parsed range structure to compile. |
| 1500 | * @param[in] basetype Base YANG built-in type of the node with the range restriction. |
| 1501 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1502 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 1503 | * @param[in] base_range Range restriction of the type from which the current type is derived. The current |
| 1504 | * range restriction must be more restrictive than the base_range. |
| 1505 | * @param[in,out] range Pointer to the created current range structure. |
| 1506 | * @return LY_ERR value. |
| 1507 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1508 | static LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1509 | 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] | 1510 | struct lysc_range *base_range, struct lysc_range **range) |
| 1511 | { |
| 1512 | LY_ERR ret = LY_EVALID; |
| 1513 | const char *expr; |
| 1514 | struct lysc_range_part *parts = NULL, *part; |
| 1515 | int range_expected = 0, uns; |
| 1516 | unsigned int parts_done = 0, u, v; |
| 1517 | |
| 1518 | assert(range); |
| 1519 | assert(range_p); |
| 1520 | |
| 1521 | expr = range_p->arg; |
| 1522 | while(1) { |
| 1523 | if (isspace(*expr)) { |
| 1524 | ++expr; |
| 1525 | } else if (*expr == '\0') { |
| 1526 | if (range_expected) { |
| 1527 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1528 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
| 1529 | length_restr ? "length" : "range", range_p->arg); |
| 1530 | goto cleanup; |
| 1531 | } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) { |
| 1532 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1533 | "Invalid %s restriction - unexpected end of the expression (%s).", |
| 1534 | length_restr ? "length" : "range", range_p->arg); |
| 1535 | goto cleanup; |
| 1536 | } |
| 1537 | parts_done++; |
| 1538 | break; |
| 1539 | } else if (!strncmp(expr, "min", 3)) { |
| 1540 | if (parts) { |
| 1541 | /* min cannot be used elsewhere than in the first part */ |
| 1542 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1543 | "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
| 1544 | expr - range_p->arg, range_p->arg); |
| 1545 | goto cleanup; |
| 1546 | } |
| 1547 | expr += 3; |
| 1548 | |
| 1549 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1550 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1551 | part->max_64 = part->min_64; |
| 1552 | } else if (*expr == '|') { |
| 1553 | if (!parts || range_expected) { |
| 1554 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1555 | "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr); |
| 1556 | goto cleanup; |
| 1557 | } |
| 1558 | expr++; |
| 1559 | parts_done++; |
| 1560 | /* process next part of the expression */ |
| 1561 | } else if (!strncmp(expr, "..", 2)) { |
| 1562 | expr += 2; |
| 1563 | while (isspace(*expr)) { |
| 1564 | expr++; |
| 1565 | } |
| 1566 | if (!parts || LY_ARRAY_SIZE(parts) == parts_done) { |
| 1567 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1568 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
| 1569 | goto cleanup; |
| 1570 | } |
| 1571 | /* continue expecting the upper boundary */ |
| 1572 | range_expected = 1; |
| 1573 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 1574 | /* number */ |
| 1575 | if (range_expected) { |
| 1576 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1577 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1578 | range_expected = 0; |
| 1579 | } else { |
| 1580 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1581 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1582 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1583 | part->max_64 = part->min_64; |
| 1584 | } |
| 1585 | |
| 1586 | /* continue with possible another expression part */ |
| 1587 | } else if (!strncmp(expr, "max", 3)) { |
| 1588 | expr += 3; |
| 1589 | while (isspace(*expr)) { |
| 1590 | expr++; |
| 1591 | } |
| 1592 | if (*expr != '\0') { |
| 1593 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).", |
| 1594 | length_restr ? "length" : "range", expr); |
| 1595 | goto cleanup; |
| 1596 | } |
| 1597 | if (range_expected) { |
| 1598 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1599 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1600 | range_expected = 0; |
| 1601 | } else { |
| 1602 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1603 | LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1604 | basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1605 | part->min_64 = part->max_64; |
| 1606 | } |
| 1607 | } else { |
| 1608 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).", |
| 1609 | length_restr ? "length" : "range", expr); |
| 1610 | goto cleanup; |
| 1611 | } |
| 1612 | } |
| 1613 | |
| 1614 | /* check with the previous range/length restriction */ |
| 1615 | if (base_range) { |
| 1616 | switch (basetype) { |
| 1617 | case LY_TYPE_BINARY: |
| 1618 | case LY_TYPE_UINT8: |
| 1619 | case LY_TYPE_UINT16: |
| 1620 | case LY_TYPE_UINT32: |
| 1621 | case LY_TYPE_UINT64: |
| 1622 | case LY_TYPE_STRING: |
| 1623 | uns = 1; |
| 1624 | break; |
| 1625 | case LY_TYPE_DEC64: |
| 1626 | case LY_TYPE_INT8: |
| 1627 | case LY_TYPE_INT16: |
| 1628 | case LY_TYPE_INT32: |
| 1629 | case LY_TYPE_INT64: |
| 1630 | uns = 0; |
| 1631 | break; |
| 1632 | default: |
| 1633 | LOGINT(ctx->ctx); |
| 1634 | ret = LY_EINT; |
| 1635 | goto cleanup; |
| 1636 | } |
| 1637 | for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) { |
| 1638 | if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) { |
| 1639 | goto baseerror; |
| 1640 | } |
| 1641 | /* current lower bound is not lower than the base */ |
| 1642 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 1643 | /* base has single value */ |
| 1644 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 1645 | /* both lower bounds are the same */ |
| 1646 | if (parts[u].min_64 != parts[u].max_64) { |
| 1647 | /* current continues with a range */ |
| 1648 | goto baseerror; |
| 1649 | } else { |
| 1650 | /* equal single values, move both forward */ |
| 1651 | ++v; |
| 1652 | continue; |
| 1653 | } |
| 1654 | } else { |
| 1655 | /* base is single value lower than current range, so the |
| 1656 | * value from base range is removed in the current, |
| 1657 | * move only base and repeat checking */ |
| 1658 | ++v; |
| 1659 | --u; |
| 1660 | continue; |
| 1661 | } |
| 1662 | } else { |
| 1663 | /* base is the range */ |
| 1664 | if (parts[u].min_64 == parts[u].max_64) { |
| 1665 | /* current is a single value */ |
| 1666 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1667 | /* current is behind the base range, so base range is omitted, |
| 1668 | * move the base and keep the current for further check */ |
| 1669 | ++v; |
| 1670 | --u; |
| 1671 | } /* else it is within the base range, so move the current, but keep the base */ |
| 1672 | continue; |
| 1673 | } else { |
| 1674 | /* both are ranges - check the higher bound, the lower was already checked */ |
| 1675 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1676 | /* higher bound is higher than the current higher bound */ |
| 1677 | if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) { |
| 1678 | /* but the current lower bound is also higher, so the base range is omitted, |
| 1679 | * continue with the same current, but move the base */ |
| 1680 | --u; |
| 1681 | ++v; |
| 1682 | continue; |
| 1683 | } |
| 1684 | /* current range starts within the base range but end behind it */ |
| 1685 | goto baseerror; |
| 1686 | } else { |
| 1687 | /* current range is smaller than the base, |
| 1688 | * move current, but stay with the base */ |
| 1689 | continue; |
| 1690 | } |
| 1691 | } |
| 1692 | } |
| 1693 | } |
| 1694 | if (u != parts_done) { |
| 1695 | baseerror: |
| 1696 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1697 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
| 1698 | length_restr ? "length" : "range", range_p->arg); |
| 1699 | goto cleanup; |
| 1700 | } |
| 1701 | } |
| 1702 | |
| 1703 | if (!(*range)) { |
| 1704 | *range = calloc(1, sizeof **range); |
| 1705 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 1706 | } |
| 1707 | |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1708 | /* we rewrite the following values as the types chain is being processed */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1709 | if (range_p->eapptag) { |
| 1710 | lydict_remove(ctx->ctx, (*range)->eapptag); |
| 1711 | (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0); |
| 1712 | } |
| 1713 | if (range_p->emsg) { |
| 1714 | lydict_remove(ctx->ctx, (*range)->emsg); |
| 1715 | (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0); |
| 1716 | } |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1717 | if (range_p->dsc) { |
| 1718 | lydict_remove(ctx->ctx, (*range)->dsc); |
| 1719 | (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0); |
| 1720 | } |
| 1721 | if (range_p->ref) { |
| 1722 | lydict_remove(ctx->ctx, (*range)->ref); |
| 1723 | (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0); |
| 1724 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1725 | /* extensions are taken only from the last range by the caller */ |
| 1726 | |
| 1727 | (*range)->parts = parts; |
| 1728 | parts = NULL; |
| 1729 | ret = LY_SUCCESS; |
| 1730 | cleanup: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1731 | LY_ARRAY_FREE(parts); |
| 1732 | |
| 1733 | return ret; |
| 1734 | } |
| 1735 | |
| 1736 | /** |
| 1737 | * @brief Checks pattern syntax. |
| 1738 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1739 | * @param[in] ctx Compile context. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1740 | * @param[in] pattern Pattern to check. |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1741 | * @param[in,out] pcre2_code Compiled PCRE2 pattern. If NULL, the compiled information used to validate pattern are freed. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1742 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1743 | */ |
| 1744 | static LY_ERR |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1745 | lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre2_code **code) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1746 | { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1747 | int idx, idx2, start, end, count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1748 | char *perl_regex, *ptr; |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1749 | int err_code; |
| 1750 | const char *orig_ptr; |
| 1751 | PCRE2_SIZE err_offset; |
| 1752 | pcre2_code *code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1753 | #define URANGE_LEN 19 |
| 1754 | char *ublock2urange[][2] = { |
| 1755 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 1756 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 1757 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 1758 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 1759 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 1760 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 1761 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 1762 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 1763 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 1764 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 1765 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 1766 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 1767 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 1768 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 1769 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 1770 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 1771 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 1772 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 1773 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 1774 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 1775 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 1776 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 1777 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 1778 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 1779 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 1780 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 1781 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 1782 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 1783 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 1784 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 1785 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 1786 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 1787 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 1788 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 1789 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 1790 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 1791 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 1792 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 1793 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 1794 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 1795 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 1796 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 1797 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 1798 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 1799 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 1800 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 1801 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 1802 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 1803 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 1804 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 1805 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 1806 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 1807 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 1808 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 1809 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 1810 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 1811 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 1812 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 1813 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 1814 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 1815 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 1816 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 1817 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 1818 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 1819 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 1820 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 1821 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 1822 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 1823 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 1824 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 1825 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 1826 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 1827 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 1828 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 1829 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 1830 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 1831 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 1832 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 1833 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 1834 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 1835 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 1836 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 1837 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
| 1838 | {NULL, NULL} |
| 1839 | }; |
| 1840 | |
| 1841 | /* adjust the expression to a Perl equivalent |
| 1842 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 1843 | |
| 1844 | /* we need to replace all "$" with "\$", count them now */ |
| 1845 | for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$")); |
| 1846 | |
| 1847 | perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char)); |
| 1848 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM); |
| 1849 | perl_regex[0] = '\0'; |
| 1850 | |
| 1851 | ptr = perl_regex; |
| 1852 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1853 | for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) { |
| 1854 | if (orig_ptr[0] == '$') { |
| 1855 | ptr += sprintf(ptr, "\\$"); |
| 1856 | } else if (orig_ptr[0] == '^') { |
| 1857 | ptr += sprintf(ptr, "\\^"); |
| 1858 | } else { |
| 1859 | ptr[0] = orig_ptr[0]; |
| 1860 | ++ptr; |
| 1861 | } |
| 1862 | } |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 1863 | ptr[0] = '\0'; |
| 1864 | ++ptr; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1865 | |
| 1866 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 1867 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 1868 | start = ptr - perl_regex; |
| 1869 | |
| 1870 | ptr = strchr(ptr, '}'); |
| 1871 | if (!ptr) { |
| 1872 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, |
| 1873 | pattern, perl_regex + start + 2, "unterminated character property"); |
| 1874 | free(perl_regex); |
| 1875 | return LY_EVALID; |
| 1876 | } |
| 1877 | end = (ptr - perl_regex) + 1; |
| 1878 | |
| 1879 | /* need more space */ |
| 1880 | if (end - start < URANGE_LEN) { |
| 1881 | perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1); |
| 1882 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM); |
| 1883 | } |
| 1884 | |
| 1885 | /* find our range */ |
| 1886 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 1887 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 1888 | break; |
| 1889 | } |
| 1890 | } |
| 1891 | if (!ublock2urange[idx][0]) { |
| 1892 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, |
| 1893 | pattern, perl_regex + start + 5, "unknown block name"); |
| 1894 | free(perl_regex); |
| 1895 | return LY_EVALID; |
| 1896 | } |
| 1897 | |
| 1898 | /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */ |
| 1899 | for (idx2 = 0, count = 0; idx2 < start; ++idx2) { |
| 1900 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 1901 | ++count; |
| 1902 | } |
| 1903 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 1904 | --count; |
| 1905 | } |
| 1906 | } |
| 1907 | if (count) { |
| 1908 | /* skip brackets */ |
| 1909 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 1910 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 1911 | } else { |
| 1912 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 1913 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 1914 | } |
| 1915 | } |
| 1916 | |
| 1917 | /* must return 0, already checked during parsing */ |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 1918 | code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, |
| 1919 | PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE, |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1920 | &err_code, &err_offset, NULL); |
| 1921 | if (!code_local) { |
| 1922 | PCRE2_UCHAR err_msg[256] = {0}; |
| 1923 | pcre2_get_error_message(err_code, err_msg, 256); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1924 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg); |
| 1925 | free(perl_regex); |
| 1926 | return LY_EVALID; |
| 1927 | } |
| 1928 | free(perl_regex); |
| 1929 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1930 | if (code) { |
| 1931 | *code = code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1932 | } else { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1933 | free(code_local); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1934 | } |
| 1935 | |
| 1936 | return LY_SUCCESS; |
| 1937 | |
| 1938 | #undef URANGE_LEN |
| 1939 | } |
| 1940 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1941 | /** |
| 1942 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 1943 | * @param[in] ctx Compile context. |
| 1944 | * @param[in] patterns_p Array of parsed patterns from the current type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1945 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 1946 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 1947 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 1948 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 1949 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1950 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1951 | lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1952 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
| 1953 | { |
| 1954 | struct lysc_pattern **pattern; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1955 | unsigned int u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1956 | LY_ERR ret = LY_SUCCESS; |
| 1957 | |
| 1958 | /* first, copy the patterns from the base type */ |
| 1959 | if (base_patterns) { |
| 1960 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 1961 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 1962 | } |
| 1963 | |
| 1964 | LY_ARRAY_FOR(patterns_p, u) { |
| 1965 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 1966 | *pattern = calloc(1, sizeof **pattern); |
| 1967 | ++(*pattern)->refcount; |
| 1968 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1969 | ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->code); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1970 | LY_CHECK_RET(ret); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1971 | |
| 1972 | if (patterns_p[u].arg[0] == 0x15) { |
| 1973 | (*pattern)->inverted = 1; |
| 1974 | } |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1975 | DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1976 | DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag); |
| 1977 | DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1978 | DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc); |
| 1979 | DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1980 | COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1981 | } |
| 1982 | done: |
| 1983 | return ret; |
| 1984 | } |
| 1985 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1986 | /** |
| 1987 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 1988 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1989 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 1990 | 0 /* LY_TYPE_UNKNOWN */, |
| 1991 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1992 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 1993 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 1994 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 1995 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 1996 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1997 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 1998 | 0 /* LY_TYPE_BOOL */, |
| 1999 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 2000 | 0 /* LY_TYPE_EMPTY */, |
| 2001 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 2002 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 2003 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 2004 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2005 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 2006 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2007 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2008 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2009 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 2010 | }; |
| 2011 | |
| 2012 | /** |
| 2013 | * @brief stringification of the YANG built-in data types |
| 2014 | */ |
| 2015 | const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer", |
| 2016 | "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration", |
| 2017 | "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer" |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2018 | }; |
| 2019 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2020 | /** |
| 2021 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 2022 | * @param[in] ctx Compile context. |
| 2023 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 2024 | * @param[in] basetype Base YANG built-in type from which the current type is derived. Only LY_TYPE_ENUM and LY_TYPE_BITS are expected. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2025 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
| 2026 | * @param[out] enums Newly created array of the compiled enums information for the current type. |
| 2027 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2028 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2029 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2030 | lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype, |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2031 | struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2032 | { |
| 2033 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 4ad42aa | 2019-07-23 16:55:58 +0200 | [diff] [blame] | 2034 | unsigned int u, v, match = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2035 | int32_t value = 0; |
| 2036 | uint32_t position = 0; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2037 | struct lysc_type_bitenum_item *e, storage; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2038 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2039 | if (base_enums && ctx->mod_def->version < 2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2040 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
| 2041 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
| 2042 | return LY_EVALID; |
| 2043 | } |
| 2044 | |
| 2045 | LY_ARRAY_FOR(enums_p, u) { |
| 2046 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
| 2047 | DUP_STRING(ctx->ctx, enums_p[u].name, e->name); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2048 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc); |
| 2049 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2050 | e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2051 | if (base_enums) { |
| 2052 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 2053 | LY_ARRAY_FOR(base_enums, v) { |
| 2054 | if (!strcmp(e->name, base_enums[v].name)) { |
| 2055 | break; |
| 2056 | } |
| 2057 | } |
| 2058 | if (v == LY_ARRAY_SIZE(base_enums)) { |
| 2059 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2060 | "Invalid %s - derived type adds new item \"%s\".", |
| 2061 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
| 2062 | return LY_EVALID; |
| 2063 | } |
| 2064 | match = v; |
| 2065 | } |
| 2066 | |
| 2067 | if (basetype == LY_TYPE_ENUM) { |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2068 | e->flags |= LYS_ISENUM; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2069 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2070 | e->value = (int32_t)enums_p[u].value; |
| 2071 | if (!u || e->value >= value) { |
| 2072 | value = e->value + 1; |
| 2073 | } |
| 2074 | /* check collision with other values */ |
| 2075 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 2076 | if (e->value == (*enums)[v].value) { |
| 2077 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2078 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 2079 | e->value, e->name, (*enums)[v].name); |
| 2080 | return LY_EVALID; |
| 2081 | } |
| 2082 | } |
| 2083 | } else if (base_enums) { |
| 2084 | /* inherit the assigned value */ |
| 2085 | e->value = base_enums[match].value; |
| 2086 | if (!u || e->value >= value) { |
| 2087 | value = e->value + 1; |
| 2088 | } |
| 2089 | } else { |
| 2090 | /* assign value automatically */ |
| 2091 | if (u && value == INT32_MIN) { |
| 2092 | /* counter overflow */ |
| 2093 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2094 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 2095 | "\"%s\" since the highest value is already 2147483647.", e->name); |
| 2096 | return LY_EVALID; |
| 2097 | } |
| 2098 | e->value = value++; |
| 2099 | } |
| 2100 | } else { /* LY_TYPE_BITS */ |
| 2101 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2102 | e->value = (int32_t)enums_p[u].value; |
| 2103 | if (!u || (uint32_t)e->value >= position) { |
| 2104 | position = (uint32_t)e->value + 1; |
| 2105 | } |
| 2106 | /* check collision with other values */ |
| 2107 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 2108 | if (e->value == (*enums)[v].value) { |
| 2109 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2110 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
| 2111 | (uint32_t)e->value, e->name, (*enums)[v].name); |
| 2112 | return LY_EVALID; |
| 2113 | } |
| 2114 | } |
| 2115 | } else if (base_enums) { |
| 2116 | /* inherit the assigned value */ |
| 2117 | e->value = base_enums[match].value; |
| 2118 | if (!u || (uint32_t)e->value >= position) { |
| 2119 | position = (uint32_t)e->value + 1; |
| 2120 | } |
| 2121 | } else { |
| 2122 | /* assign value automatically */ |
| 2123 | if (u && position == 0) { |
| 2124 | /* counter overflow */ |
| 2125 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2126 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 2127 | "\"%s\" since the highest value is already 4294967295.", e->name); |
| 2128 | return LY_EVALID; |
| 2129 | } |
| 2130 | e->value = position++; |
| 2131 | } |
| 2132 | } |
| 2133 | |
| 2134 | if (base_enums) { |
| 2135 | /* the assigned values must not change from the derived type */ |
| 2136 | if (e->value != base_enums[match].value) { |
| 2137 | if (basetype == LY_TYPE_ENUM) { |
| 2138 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2139 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
| 2140 | e->name, base_enums[match].value, e->value); |
| 2141 | } else { |
| 2142 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2143 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 2144 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
| 2145 | } |
| 2146 | return LY_EVALID; |
| 2147 | } |
| 2148 | } |
| 2149 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2150 | COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2151 | COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, basetype == LY_TYPE_ENUM ? LYEXT_PAR_TYPE_ENUM : LYEXT_PAR_TYPE_BIT, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2152 | |
| 2153 | if (basetype == LY_TYPE_BITS) { |
| 2154 | /* keep bits ordered by position */ |
| 2155 | for (v = u; v && (*enums)[v - 1].value > e->value; --v); |
| 2156 | if (v != u) { |
| 2157 | memcpy(&storage, e, sizeof *e); |
| 2158 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 2159 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 2160 | } |
| 2161 | } |
| 2162 | } |
| 2163 | |
| 2164 | done: |
| 2165 | return ret; |
| 2166 | } |
| 2167 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2168 | #define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \ |
| 2169 | for ((NODE) = (NODE)->parent; \ |
| 2170 | (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \ |
| 2171 | (NODE) = (NODE)->parent); \ |
| 2172 | if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \ |
| 2173 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \ |
| 2174 | TERM; \ |
| 2175 | } |
| 2176 | |
| 2177 | /** |
| 2178 | * @brief Validate the predicate(s) from the leafref path. |
| 2179 | * @param[in] ctx Compile context |
| 2180 | * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s). |
| 2181 | * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated. |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2182 | * @param[in] start_node Path context node (where the path is instantiated). |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2183 | * @param[in] context_node Predicate context node (where the predicate is placed). |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2184 | * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2185 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2186 | */ |
| 2187 | static LY_ERR |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2188 | lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2189 | const struct lysc_node_list *context_node, const struct lys_module *path_context) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2190 | { |
| 2191 | LY_ERR ret = LY_EVALID; |
| 2192 | const struct lys_module *mod; |
| 2193 | const struct lysc_node *src_node, *dst_node; |
| 2194 | const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix; |
| 2195 | size_t src_len, src_prefix_len, dst_len, dst_prefix_len; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 2196 | unsigned int dest_parent_times, c; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2197 | const char *start, *end, *pke_end; |
| 2198 | struct ly_set keys = {0}; |
| 2199 | int i; |
| 2200 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2201 | assert(path_context); |
| 2202 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2203 | while (**predicate == '[') { |
| 2204 | start = (*predicate)++; |
| 2205 | |
| 2206 | while (isspace(**predicate)) { |
| 2207 | ++(*predicate); |
| 2208 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2209 | LY_CHECK_GOTO(ly_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2210 | while (isspace(**predicate)) { |
| 2211 | ++(*predicate); |
| 2212 | } |
| 2213 | if (**predicate != '=') { |
| 2214 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2215 | "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.", |
| 2216 | *predicate - start + 1, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2217 | goto cleanup; |
| 2218 | } |
| 2219 | ++(*predicate); |
| 2220 | while (isspace(**predicate)) { |
| 2221 | ++(*predicate); |
| 2222 | } |
| 2223 | |
| 2224 | if ((end = pke_end = strchr(*predicate, ']')) == NULL) { |
| 2225 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2226 | "Invalid leafref path predicate \"%s\" - missing predicate termination.", start); |
| 2227 | goto cleanup; |
| 2228 | } |
| 2229 | --pke_end; |
| 2230 | while (isspace(*pke_end)) { |
| 2231 | --pke_end; |
| 2232 | } |
Radek Krejci | 7adf4ff | 2018-12-05 08:55:00 +0100 | [diff] [blame] | 2233 | ++pke_end; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2234 | /* localize path-key-expr */ |
| 2235 | pke_start = path_key_expr = *predicate; |
| 2236 | /* move after the current predicate */ |
| 2237 | *predicate = end + 1; |
| 2238 | |
| 2239 | /* source (must be leaf or leaf-list) */ |
| 2240 | if (src_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2241 | mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2242 | if (!mod) { |
| 2243 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2244 | "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2245 | *predicate - start, start, src_prefix_len, src_prefix, path_context->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2246 | goto cleanup; |
| 2247 | } |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2248 | if (!mod->implemented) { |
| 2249 | /* make the module implemented */ |
| 2250 | ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2); |
| 2251 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2252 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2253 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2254 | } |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2255 | src_node = NULL; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 2256 | if (!(context_node->flags & LYS_KEYLESS)) { |
| 2257 | struct lysc_node *key; |
| 2258 | for (key = context_node->child; key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY); key = key->next) { |
| 2259 | if (!strncmp(src, key->name, src_len) && key->name[src_len] == '\0') { |
| 2260 | src_node = key; |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2261 | break; |
| 2262 | } |
| 2263 | } |
| 2264 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2265 | if (!src_node) { |
| 2266 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2267 | "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2268 | *predicate - start, start, src_len, src, mod->name); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2269 | goto cleanup; |
| 2270 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2271 | |
| 2272 | /* check that there is only one predicate for the */ |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2273 | c = keys.count; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2274 | i = ly_set_add(&keys, (void*)src_node, 0); |
| 2275 | LY_CHECK_GOTO(i == -1, cleanup); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2276 | if (keys.count == c) { /* node was already present in the set */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2277 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2278 | "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".", |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2279 | *predicate - start, start, src_node->name); |
| 2280 | goto cleanup; |
| 2281 | } |
| 2282 | |
| 2283 | /* destination */ |
| 2284 | dest_parent_times = 0; |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2285 | dst_node = start_node; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2286 | |
| 2287 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2288 | if (strncmp(path_key_expr, "current", 7)) { |
| 2289 | error_current_function_invocation: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2290 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2291 | "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.", |
| 2292 | *predicate - start, start); |
| 2293 | goto cleanup; |
| 2294 | } |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2295 | for (path_key_expr += 7; isspace(*path_key_expr); ++path_key_expr); |
| 2296 | if (*path_key_expr != '(') { |
| 2297 | goto error_current_function_invocation; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2298 | } |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2299 | for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr); |
| 2300 | if (*path_key_expr != ')') { |
| 2301 | goto error_current_function_invocation; |
| 2302 | } |
| 2303 | for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2304 | |
| 2305 | if (*path_key_expr != '/') { |
| 2306 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2307 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.", |
| 2308 | *predicate - start, start); |
| 2309 | goto cleanup; |
| 2310 | } |
| 2311 | ++path_key_expr; |
| 2312 | while (isspace(*path_key_expr)) { |
| 2313 | ++path_key_expr; |
| 2314 | } |
| 2315 | |
| 2316 | /* rel-path-keyexpr: |
| 2317 | * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */ |
| 2318 | while (!strncmp(path_key_expr, "..", 2)) { |
| 2319 | ++dest_parent_times; |
| 2320 | path_key_expr += 2; |
| 2321 | while (isspace(*path_key_expr)) { |
| 2322 | ++path_key_expr; |
| 2323 | } |
| 2324 | if (*path_key_expr != '/') { |
| 2325 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2326 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.", |
| 2327 | *predicate - start, start); |
| 2328 | goto cleanup; |
| 2329 | } |
| 2330 | ++path_key_expr; |
| 2331 | while (isspace(*path_key_expr)) { |
| 2332 | ++path_key_expr; |
| 2333 | } |
| 2334 | |
| 2335 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2336 | * all schema nodes that cannot be instantiated in data tree */ |
| 2337 | MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup, |
| 2338 | "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.", |
| 2339 | *predicate - start, start); |
| 2340 | } |
| 2341 | if (!dest_parent_times) { |
| 2342 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2343 | "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.", |
| 2344 | *predicate - start, start); |
| 2345 | goto cleanup; |
| 2346 | } |
| 2347 | if (path_key_expr == pke_end) { |
| 2348 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2349 | "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.", |
| 2350 | *predicate - start, start); |
| 2351 | goto cleanup; |
| 2352 | } |
| 2353 | |
| 2354 | while(path_key_expr != pke_end) { |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2355 | for (;*path_key_expr == '/' || isspace(*path_key_expr); ++path_key_expr); |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2356 | if (ly_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2357 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2358 | "Invalid node identifier in leafref path predicate - character %d (of %.*s).", |
| 2359 | path_key_expr - start + 1, *predicate - start, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2360 | goto cleanup; |
| 2361 | } |
| 2362 | |
| 2363 | if (dst_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2364 | mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2365 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2366 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2367 | } |
| 2368 | if (!mod) { |
| 2369 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2370 | "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path-keyexpr.", |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2371 | *predicate - start, start, dst_len, dst); |
| 2372 | goto cleanup; |
| 2373 | } |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2374 | if (!mod->implemented) { |
| 2375 | /* make the module implemented */ |
| 2376 | ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2); |
| 2377 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2378 | |
| 2379 | dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK); |
| 2380 | if (!dst_node) { |
| 2381 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2382 | "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path-keyexpr.", |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2383 | *predicate - start, start, path_key_expr - pke_start, pke_start); |
| 2384 | goto cleanup; |
| 2385 | } |
| 2386 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2387 | if (!(dst_node->nodetype & (dst_node->module->version < LYS_VERSION_1_1 ? LYS_LEAF : LYS_LEAF | LYS_LEAFLIST))) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2388 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2389 | "Invalid leafref path predicate \"%.*s\" - rel-path-keyexpr \"%.*s\" refers %s instead of leaf.", |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2390 | *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype)); |
| 2391 | goto cleanup; |
| 2392 | } |
| 2393 | } |
| 2394 | |
| 2395 | ret = LY_SUCCESS; |
| 2396 | cleanup: |
| 2397 | ly_set_erase(&keys, NULL); |
| 2398 | return ret; |
| 2399 | } |
| 2400 | |
| 2401 | /** |
| 2402 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 2403 | * |
| 2404 | * path-arg = absolute-path / relative-path |
| 2405 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 2406 | * relative-path = 1*(".." "/") descendant-path |
| 2407 | * |
| 2408 | * @param[in,out] path Path to parse. |
| 2409 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 2410 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 2411 | * @param[out] name Name of the token. |
| 2412 | * @param[out] nam_len Length of the name. |
| 2413 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 2414 | * must not be changed between consecutive calls. -1 if the |
| 2415 | * path is absolute. |
| 2416 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 2417 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 2418 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2419 | LY_ERR |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2420 | lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len, |
| 2421 | int *parent_times, int *has_predicate) |
| 2422 | { |
| 2423 | int par_times = 0; |
| 2424 | |
| 2425 | assert(path && *path); |
| 2426 | assert(parent_times); |
| 2427 | assert(prefix); |
| 2428 | assert(prefix_len); |
| 2429 | assert(name); |
| 2430 | assert(name_len); |
| 2431 | assert(has_predicate); |
| 2432 | |
| 2433 | *prefix = NULL; |
| 2434 | *prefix_len = 0; |
| 2435 | *name = NULL; |
| 2436 | *name_len = 0; |
| 2437 | *has_predicate = 0; |
| 2438 | |
| 2439 | if (!*parent_times) { |
| 2440 | if (!strncmp(*path, "..", 2)) { |
| 2441 | *path += 2; |
| 2442 | ++par_times; |
| 2443 | while (!strncmp(*path, "/..", 3)) { |
| 2444 | *path += 3; |
| 2445 | ++par_times; |
| 2446 | } |
| 2447 | } |
| 2448 | if (par_times) { |
| 2449 | *parent_times = par_times; |
| 2450 | } else { |
| 2451 | *parent_times = -1; |
| 2452 | } |
| 2453 | } |
| 2454 | |
| 2455 | if (**path != '/') { |
| 2456 | return LY_EINVAL; |
| 2457 | } |
| 2458 | /* skip '/' */ |
| 2459 | ++(*path); |
| 2460 | |
| 2461 | /* node-identifier ([prefix:]name) */ |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2462 | LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2463 | |
| 2464 | if ((**path == '/' && (*path)[1]) || !**path) { |
| 2465 | /* path continues by another token or this is the last token */ |
| 2466 | return LY_SUCCESS; |
| 2467 | } else if ((*path)[0] != '[') { |
| 2468 | /* unexpected character */ |
| 2469 | return LY_EINVAL; |
| 2470 | } else { |
| 2471 | /* predicate starting with [ */ |
| 2472 | *has_predicate = 1; |
| 2473 | return LY_SUCCESS; |
| 2474 | } |
| 2475 | } |
| 2476 | |
| 2477 | /** |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2478 | * @brief Check the features used in if-feature statements applicable to the leafref and its target. |
| 2479 | * |
| 2480 | * The set of features used for target must be a subset of features used for the leafref. |
| 2481 | * This is not a perfect, we should compare the truth tables but it could require too much resources |
| 2482 | * and RFC 7950 does not require it explicitely, so we simplify that. |
| 2483 | * |
| 2484 | * @param[in] refnode The leafref node. |
| 2485 | * @param[in] target Tha target node of the leafref. |
| 2486 | * @return LY_SUCCESS or LY_EVALID; |
| 2487 | */ |
| 2488 | static LY_ERR |
| 2489 | lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target) |
| 2490 | { |
| 2491 | LY_ERR ret = LY_EVALID; |
| 2492 | const struct lysc_node *iter; |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2493 | unsigned int u, v, count; |
| 2494 | struct ly_set features = {0}; |
| 2495 | |
| 2496 | for (iter = refnode; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2497 | if (iter->iffeatures) { |
| 2498 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2499 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2500 | LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup); |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2501 | } |
| 2502 | } |
| 2503 | } |
| 2504 | } |
| 2505 | |
| 2506 | /* we should have, in features set, a superset of features applicable to the target node. |
| 2507 | * So when adding features applicable to the target into the features set, we should not be |
| 2508 | * able to actually add any new feature, otherwise it is not a subset of features applicable |
| 2509 | * to the leafref itself. */ |
| 2510 | count = features.count; |
| 2511 | for (iter = target; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2512 | if (iter->iffeatures) { |
| 2513 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2514 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2515 | if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) { |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2516 | /* new feature was added (or LY_EMEM) */ |
| 2517 | goto cleanup; |
| 2518 | } |
| 2519 | } |
| 2520 | } |
| 2521 | } |
| 2522 | } |
| 2523 | ret = LY_SUCCESS; |
| 2524 | |
| 2525 | cleanup: |
| 2526 | ly_set_erase(&features, NULL); |
| 2527 | return ret; |
| 2528 | } |
| 2529 | |
| 2530 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2531 | * @brief Validate the leafref path. |
| 2532 | * @param[in] ctx Compile context |
| 2533 | * @param[in] startnode Path context node (where the leafref path begins/is placed). |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2534 | * @param[in] leafref Leafref to validate. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2535 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2536 | */ |
| 2537 | static LY_ERR |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2538 | lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2539 | { |
| 2540 | const struct lysc_node *node = NULL, *parent = NULL; |
| 2541 | const struct lys_module *mod; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2542 | struct lysc_type *type; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2543 | const char *id, *prefix, *name; |
| 2544 | size_t prefix_len, name_len; |
| 2545 | int parent_times = 0, has_predicate; |
| 2546 | unsigned int iter, u; |
| 2547 | LY_ERR ret = LY_SUCCESS; |
| 2548 | |
| 2549 | assert(ctx); |
| 2550 | assert(startnode); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2551 | assert(leafref); |
| 2552 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 2553 | ctx->path[0] = '\0'; |
| 2554 | lysc_path(startnode, LY_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 2555 | ctx->path_len = strlen(ctx->path); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 2556 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2557 | iter = 0; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2558 | id = leafref->path; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2559 | while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) { |
| 2560 | if (!iter) { /* first iteration */ |
| 2561 | /* precess ".." in relative paths */ |
| 2562 | if (parent_times > 0) { |
| 2563 | /* move from the context node */ |
| 2564 | for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) { |
| 2565 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2566 | * all schema nodes that cannot be instantiated in data tree */ |
| 2567 | MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2568 | "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2569 | } |
| 2570 | } |
| 2571 | } |
| 2572 | |
| 2573 | if (prefix) { |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2574 | mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2575 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2576 | mod = startnode->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2577 | } |
| 2578 | if (!mod) { |
| 2579 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2580 | "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".", |
| 2581 | id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2582 | return LY_EVALID; |
| 2583 | } |
Radek Krejci | 3d92956 | 2019-02-21 11:25:55 +0100 | [diff] [blame] | 2584 | if (!mod->implemented) { |
| 2585 | /* make the module implemented */ |
| 2586 | ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2); |
| 2587 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2588 | |
| 2589 | node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK); |
| 2590 | if (!node) { |
| 2591 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2592 | "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2593 | return LY_EVALID; |
| 2594 | } |
| 2595 | parent = node; |
| 2596 | |
| 2597 | if (has_predicate) { |
| 2598 | /* we have predicate, so the current result must be list */ |
| 2599 | if (node->nodetype != LYS_LIST) { |
| 2600 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2601 | "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.", |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2602 | id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2603 | return LY_EVALID; |
| 2604 | } |
| 2605 | |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2606 | LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context), |
| 2607 | LY_EVALID); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2608 | } |
| 2609 | |
| 2610 | ++iter; |
| 2611 | } |
| 2612 | if (ret) { |
| 2613 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2614 | "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2615 | return LY_EVALID; |
| 2616 | } |
| 2617 | |
| 2618 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 2619 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2620 | "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.", |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2621 | leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2622 | return LY_EVALID; |
| 2623 | } |
| 2624 | |
| 2625 | /* check status */ |
| 2626 | if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) { |
| 2627 | return LY_EVALID; |
| 2628 | } |
| 2629 | |
Radek Krejci | b57cf1e | 2018-11-23 14:07:05 +0100 | [diff] [blame] | 2630 | /* check config */ |
| 2631 | if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) { |
| 2632 | if (node->flags & LYS_CONFIG_R) { |
| 2633 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2634 | "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.", |
| 2635 | leafref->path); |
| 2636 | return LY_EVALID; |
| 2637 | } |
| 2638 | } |
| 2639 | |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2640 | /* store the target's type and check for circular chain of leafrefs */ |
| 2641 | leafref->realtype = ((struct lysc_node_leaf*)node)->type; |
| 2642 | for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) { |
| 2643 | if (type == (struct lysc_type*)leafref) { |
| 2644 | /* circular chain detected */ |
| 2645 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2646 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path); |
| 2647 | return LY_EVALID; |
| 2648 | } |
| 2649 | } |
| 2650 | |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2651 | /* check if leafref and its target are under common if-features */ |
| 2652 | if (lys_compile_leafref_features_validate(startnode, node)) { |
| 2653 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2654 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.", |
| 2655 | leafref->path); |
| 2656 | return LY_EVALID; |
| 2657 | } |
| 2658 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 2659 | ctx->path_len = 1; |
| 2660 | ctx->path[1] = '\0'; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2661 | return LY_SUCCESS; |
| 2662 | } |
| 2663 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2664 | static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, struct lysp_module *context_mod, const char *context_name, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2665 | struct lysp_type *type_p, struct lysc_type **type, const char **units); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2666 | /** |
| 2667 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 2668 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2669 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2670 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2671 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2672 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2673 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2674 | * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path) |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2675 | * @param[in] basetype Base YANG built-in type of the type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2676 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 2677 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 2678 | * @param[out] type Newly created type structure with the filled information about the type. |
| 2679 | * @return LY_ERR value. |
| 2680 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2681 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2682 | lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, struct lysp_module *context_mod, const char *context_name, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2683 | struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2684 | struct lysc_type *base, struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2685 | { |
| 2686 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2687 | unsigned int u, v, additional; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2688 | struct lysc_type_bin *bin; |
| 2689 | struct lysc_type_num *num; |
| 2690 | struct lysc_type_str *str; |
| 2691 | struct lysc_type_bits *bits; |
| 2692 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2693 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2694 | struct lysc_type_identityref *idref; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2695 | struct lysc_type_union *un, *un_aux; |
| 2696 | void *p; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2697 | |
| 2698 | switch (basetype) { |
| 2699 | case LY_TYPE_BINARY: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2700 | bin = (struct lysc_type_bin*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2701 | |
| 2702 | /* 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] | 2703 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2704 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2705 | base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2706 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2707 | COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, LYEXT_PAR_LENGTH, ret, done); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2708 | } |
| 2709 | } |
| 2710 | |
| 2711 | if (tpdfname) { |
| 2712 | type_p->compiled = *type; |
| 2713 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2714 | } |
| 2715 | break; |
| 2716 | case LY_TYPE_BITS: |
| 2717 | /* RFC 7950 9.7 - bits */ |
| 2718 | bits = (struct lysc_type_bits*)(*type); |
| 2719 | if (type_p->bits) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2720 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2721 | base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL, |
| 2722 | (struct lysc_type_bitenum_item**)&bits->bits)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2723 | } |
| 2724 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2725 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2726 | /* 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] | 2727 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2728 | 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] | 2729 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2730 | 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] | 2731 | free(*type); |
| 2732 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2733 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2734 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2735 | } |
| 2736 | |
| 2737 | if (tpdfname) { |
| 2738 | type_p->compiled = *type; |
| 2739 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2740 | } |
| 2741 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2742 | case LY_TYPE_DEC64: |
| 2743 | dec = (struct lysc_type_dec*)(*type); |
| 2744 | |
| 2745 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2746 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2747 | if (!type_p->fraction_digits) { |
| 2748 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2749 | 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] | 2750 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2751 | 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] | 2752 | free(*type); |
| 2753 | *type = NULL; |
| 2754 | } |
| 2755 | return LY_EVALID; |
| 2756 | } |
| 2757 | } else if (type_p->fraction_digits) { |
| 2758 | /* 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] | 2759 | if (tpdfname) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2760 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2761 | "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] | 2762 | tpdfname); |
| 2763 | } else { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2764 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2765 | "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] | 2766 | free(*type); |
| 2767 | *type = NULL; |
| 2768 | } |
| 2769 | return LY_EVALID; |
| 2770 | } |
| 2771 | dec->fraction_digits = type_p->fraction_digits; |
| 2772 | |
| 2773 | /* RFC 7950 9.2.4 - range */ |
| 2774 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2775 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits, |
| 2776 | base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2777 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2778 | COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, LYEXT_PAR_RANGE, ret, done); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2779 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2780 | } |
| 2781 | |
| 2782 | if (tpdfname) { |
| 2783 | type_p->compiled = *type; |
| 2784 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2785 | } |
| 2786 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2787 | case LY_TYPE_STRING: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2788 | str = (struct lysc_type_str*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2789 | |
| 2790 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2791 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2792 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2793 | base ? ((struct lysc_type_str*)base)->length : NULL, &str->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2794 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2795 | COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, LYEXT_PAR_LENGTH, ret, done); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2796 | } |
| 2797 | } else if (base && ((struct lysc_type_str*)base)->length) { |
| 2798 | str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length); |
| 2799 | } |
| 2800 | |
| 2801 | /* RFC 7950 9.4.5 - pattern */ |
| 2802 | if (type_p->patterns) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2803 | LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2804 | base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2805 | } else if (base && ((struct lysc_type_str*)base)->patterns) { |
| 2806 | str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns); |
| 2807 | } |
| 2808 | |
| 2809 | if (tpdfname) { |
| 2810 | type_p->compiled = *type; |
| 2811 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2812 | } |
| 2813 | break; |
| 2814 | case LY_TYPE_ENUM: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2815 | enumeration = (struct lysc_type_enum*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2816 | |
| 2817 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2818 | if (type_p->enums) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2819 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2820 | base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2821 | } |
| 2822 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2823 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2824 | /* 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] | 2825 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2826 | 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] | 2827 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2828 | 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] | 2829 | free(*type); |
| 2830 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2831 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2832 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2833 | } |
| 2834 | |
| 2835 | if (tpdfname) { |
| 2836 | type_p->compiled = *type; |
| 2837 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2838 | } |
| 2839 | break; |
| 2840 | case LY_TYPE_INT8: |
| 2841 | case LY_TYPE_UINT8: |
| 2842 | case LY_TYPE_INT16: |
| 2843 | case LY_TYPE_UINT16: |
| 2844 | case LY_TYPE_INT32: |
| 2845 | case LY_TYPE_UINT32: |
| 2846 | case LY_TYPE_INT64: |
| 2847 | case LY_TYPE_UINT64: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2848 | num = (struct lysc_type_num*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2849 | |
| 2850 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2851 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2852 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0, |
| 2853 | base ? ((struct lysc_type_num*)base)->range : NULL, &num->range)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2854 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2855 | COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, LYEXT_PAR_RANGE, ret, done); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2856 | } |
| 2857 | } |
| 2858 | |
| 2859 | if (tpdfname) { |
| 2860 | type_p->compiled = *type; |
| 2861 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2862 | } |
| 2863 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2864 | case LY_TYPE_IDENT: |
| 2865 | idref = (struct lysc_type_identityref*)(*type); |
| 2866 | |
| 2867 | /* RFC 7950 9.10.2 - base */ |
| 2868 | if (type_p->bases) { |
| 2869 | if (base) { |
| 2870 | /* only the directly derived identityrefs can contain base specification */ |
| 2871 | if (tpdfname) { |
| 2872 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2873 | "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.", |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2874 | tpdfname); |
| 2875 | } else { |
| 2876 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2877 | "Invalid base substatement for the type not directly derived from identityref built-in type."); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2878 | free(*type); |
| 2879 | *type = NULL; |
| 2880 | } |
| 2881 | return LY_EVALID; |
| 2882 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2883 | LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases)); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2884 | } |
| 2885 | |
| 2886 | if (!base && !type_p->flags) { |
| 2887 | /* type derived from identityref built-in type must contain at least one base */ |
| 2888 | if (tpdfname) { |
| 2889 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 2890 | } else { |
| 2891 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
| 2892 | free(*type); |
| 2893 | *type = NULL; |
| 2894 | } |
| 2895 | return LY_EVALID; |
| 2896 | } |
| 2897 | |
| 2898 | if (tpdfname) { |
| 2899 | type_p->compiled = *type; |
| 2900 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2901 | } |
| 2902 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2903 | case LY_TYPE_LEAFREF: |
| 2904 | /* RFC 7950 9.9.3 - require-instance */ |
| 2905 | if (type_p->flags & LYS_SET_REQINST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2906 | if (context_mod->mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2907 | if (tpdfname) { |
| 2908 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2909 | "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname); |
| 2910 | } else { |
| 2911 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2912 | "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
| 2913 | free(*type); |
| 2914 | *type = NULL; |
| 2915 | } |
| 2916 | return LY_EVALID; |
| 2917 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2918 | ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2919 | } else if (base) { |
| 2920 | /* inherit */ |
| 2921 | ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2922 | } else { |
| 2923 | /* default is true */ |
| 2924 | ((struct lysc_type_leafref*)(*type))->require_instance = 1; |
| 2925 | } |
| 2926 | if (type_p->path) { |
| 2927 | DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path); |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2928 | ((struct lysc_type_leafref*)(*type))->path_context = module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2929 | } else if (base) { |
| 2930 | DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path); |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2931 | ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2932 | } else if (tpdfname) { |
| 2933 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 2934 | return LY_EVALID; |
| 2935 | } else { |
| 2936 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
| 2937 | free(*type); |
| 2938 | *type = NULL; |
| 2939 | return LY_EVALID; |
| 2940 | } |
| 2941 | if (tpdfname) { |
| 2942 | type_p->compiled = *type; |
| 2943 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2944 | } |
| 2945 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2946 | case LY_TYPE_INST: |
| 2947 | /* RFC 7950 9.9.3 - require-instance */ |
| 2948 | if (type_p->flags & LYS_SET_REQINST) { |
| 2949 | ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance; |
| 2950 | } else { |
| 2951 | /* default is true */ |
| 2952 | ((struct lysc_type_instanceid*)(*type))->require_instance = 1; |
| 2953 | } |
| 2954 | |
| 2955 | if (tpdfname) { |
| 2956 | type_p->compiled = *type; |
| 2957 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2958 | } |
| 2959 | break; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2960 | case LY_TYPE_UNION: |
| 2961 | un = (struct lysc_type_union*)(*type); |
| 2962 | |
| 2963 | /* RFC 7950 7.4 - type */ |
| 2964 | if (type_p->types) { |
| 2965 | if (base) { |
| 2966 | /* only the directly derived union can contain types specification */ |
| 2967 | if (tpdfname) { |
| 2968 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2969 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 2970 | tpdfname); |
| 2971 | } else { |
| 2972 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2973 | "Invalid type substatement for the type not directly derived from union built-in type."); |
| 2974 | free(*type); |
| 2975 | *type = NULL; |
| 2976 | } |
| 2977 | return LY_EVALID; |
| 2978 | } |
| 2979 | /* compile the type */ |
| 2980 | additional = 0; |
| 2981 | LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID); |
| 2982 | for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2983 | LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name, &type_p->types[u], &un->types[u + additional], NULL)); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2984 | if (un->types[u + additional]->basetype == LY_TYPE_UNION) { |
| 2985 | /* add space for additional types from the union subtype */ |
| 2986 | un_aux = (struct lysc_type_union *)un->types[u + additional]; |
| 2987 | p = ly_realloc(((uint32_t*)(un->types) - 1), sizeof(uint32_t) + ((LY_ARRAY_SIZE(type_p->types) + additional + LY_ARRAY_SIZE(un_aux->types) - 1) * sizeof *(un->types))); |
| 2988 | LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
| 2989 | un->types = (void*)((uint32_t*)(p) + 1); |
| 2990 | |
| 2991 | /* copy subtypes of the subtype union */ |
| 2992 | for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) { |
| 2993 | if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 2994 | /* duplicate the whole structure because of the instance-specific path resolving for realtype */ |
| 2995 | un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2996 | LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
| 2997 | ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF; |
| 2998 | DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path); |
| 2999 | ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1; |
| 3000 | ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance; |
| 3001 | ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context; |
| 3002 | /* TODO extensions */ |
| 3003 | |
| 3004 | } else { |
| 3005 | un->types[u + additional] = un_aux->types[v]; |
| 3006 | ++un_aux->types[v]->refcount; |
| 3007 | } |
| 3008 | ++additional; |
| 3009 | LY_ARRAY_INCREMENT(un->types); |
| 3010 | } |
| 3011 | /* compensate u increment in main loop */ |
| 3012 | --additional; |
| 3013 | |
| 3014 | /* free the replaced union subtype */ |
| 3015 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux); |
| 3016 | } else { |
| 3017 | LY_ARRAY_INCREMENT(un->types); |
| 3018 | } |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3019 | } |
| 3020 | } |
| 3021 | |
| 3022 | if (!base && !type_p->flags) { |
| 3023 | /* type derived from union built-in type must contain at least one type */ |
| 3024 | if (tpdfname) { |
| 3025 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname); |
| 3026 | } else { |
| 3027 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", ""); |
| 3028 | free(*type); |
| 3029 | *type = NULL; |
| 3030 | } |
| 3031 | return LY_EVALID; |
| 3032 | } |
| 3033 | |
| 3034 | if (tpdfname) { |
| 3035 | type_p->compiled = *type; |
| 3036 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3037 | } |
| 3038 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3039 | case LY_TYPE_BOOL: |
| 3040 | case LY_TYPE_EMPTY: |
| 3041 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 3042 | break; |
| 3043 | } |
| 3044 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
| 3045 | done: |
| 3046 | return ret; |
| 3047 | } |
| 3048 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3049 | /** |
| 3050 | * @brief Compile information about the leaf/leaf-list's type. |
| 3051 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3052 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 3053 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 3054 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 3055 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 3056 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3057 | * @param[out] type Newly created (or reused with increased refcount) type structure with the filled information about the type. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3058 | * @param[out] units Storage for inheriting units value from the typedefs the current type derives from. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3059 | * @return LY_ERR value. |
| 3060 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3061 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3062 | lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, struct lysp_module *context_mod, const char *context_name, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3063 | struct lysp_type *type_p, struct lysc_type **type, const char **units) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3064 | { |
| 3065 | LY_ERR ret = LY_SUCCESS; |
| 3066 | unsigned int u; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3067 | int dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3068 | struct type_context { |
| 3069 | const struct lysp_tpdf *tpdf; |
| 3070 | struct lysp_node *node; |
| 3071 | struct lysp_module *mod; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3072 | } *tctx, *tctx_prev = NULL, *tctx_iter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3073 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3074 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3075 | struct ly_set tpdf_chain = {0}; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3076 | const char *dflt = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3077 | struct lys_module *dflt_mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3078 | |
| 3079 | (*type) = NULL; |
| 3080 | |
| 3081 | tctx = calloc(1, sizeof *tctx); |
| 3082 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3083 | for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3084 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
| 3085 | ret == LY_SUCCESS; |
| 3086 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
| 3087 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
| 3088 | if (basetype) { |
| 3089 | break; |
| 3090 | } |
| 3091 | |
| 3092 | /* check status */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3093 | ret = lysc_check_status(ctx, context_flags, context_mod, context_name, |
| 3094 | tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3095 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 3096 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3097 | if (units && !*units) { |
| 3098 | /* inherit units */ |
| 3099 | DUP_STRING(ctx->ctx, tctx->tpdf->units, *units); |
| 3100 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3101 | if (!dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3102 | /* inherit default */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3103 | dflt = tctx->tpdf->dflt; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3104 | dflt_mod = tctx->mod->mod; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3105 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3106 | if (dummyloops && (!units || *units) && dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3107 | basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype; |
| 3108 | break; |
| 3109 | } |
| 3110 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3111 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3112 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 3113 | * but we still may need to inherit default and units values, so start dummy loops */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3114 | basetype = tctx->tpdf->type.compiled->basetype; |
| 3115 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3116 | if ((units && !*units) || !dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3117 | dummyloops = 1; |
| 3118 | goto preparenext; |
| 3119 | } else { |
| 3120 | tctx = NULL; |
| 3121 | break; |
| 3122 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3123 | } |
| 3124 | |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3125 | /* circular typedef reference detection */ |
| 3126 | for (u = 0; u < tpdf_chain.count; u++) { |
| 3127 | /* local part */ |
| 3128 | tctx_iter = (struct type_context*)tpdf_chain.objs[u]; |
| 3129 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 3130 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3131 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 3132 | free(tctx); |
| 3133 | ret = LY_EVALID; |
| 3134 | goto cleanup; |
| 3135 | } |
| 3136 | } |
| 3137 | for (u = 0; u < ctx->tpdf_chain.count; u++) { |
| 3138 | /* global part for unions corner case */ |
| 3139 | tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u]; |
| 3140 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 3141 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3142 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 3143 | free(tctx); |
| 3144 | ret = LY_EVALID; |
| 3145 | goto cleanup; |
| 3146 | } |
| 3147 | } |
| 3148 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3149 | /* store information for the following processing */ |
| 3150 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3151 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3152 | preparenext: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3153 | /* prepare next loop */ |
| 3154 | tctx_prev = tctx; |
| 3155 | tctx = calloc(1, sizeof *tctx); |
| 3156 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 3157 | } |
| 3158 | free(tctx); |
| 3159 | |
| 3160 | /* allocate type according to the basetype */ |
| 3161 | switch (basetype) { |
| 3162 | case LY_TYPE_BINARY: |
| 3163 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3164 | break; |
| 3165 | case LY_TYPE_BITS: |
| 3166 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3167 | break; |
| 3168 | case LY_TYPE_BOOL: |
| 3169 | case LY_TYPE_EMPTY: |
| 3170 | *type = calloc(1, sizeof(struct lysc_type)); |
| 3171 | break; |
| 3172 | case LY_TYPE_DEC64: |
| 3173 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 3174 | break; |
| 3175 | case LY_TYPE_ENUM: |
| 3176 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3177 | break; |
| 3178 | case LY_TYPE_IDENT: |
| 3179 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 3180 | break; |
| 3181 | case LY_TYPE_INST: |
| 3182 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 3183 | break; |
| 3184 | case LY_TYPE_LEAFREF: |
| 3185 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3186 | break; |
| 3187 | case LY_TYPE_STRING: |
| 3188 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3189 | break; |
| 3190 | case LY_TYPE_UNION: |
| 3191 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3192 | break; |
| 3193 | case LY_TYPE_INT8: |
| 3194 | case LY_TYPE_UINT8: |
| 3195 | case LY_TYPE_INT16: |
| 3196 | case LY_TYPE_UINT16: |
| 3197 | case LY_TYPE_INT32: |
| 3198 | case LY_TYPE_UINT32: |
| 3199 | case LY_TYPE_INT64: |
| 3200 | case LY_TYPE_UINT64: |
| 3201 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3202 | break; |
| 3203 | case LY_TYPE_UNKNOWN: |
| 3204 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3205 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 3206 | ret = LY_EVALID; |
| 3207 | goto cleanup; |
| 3208 | } |
| 3209 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3210 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3211 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
| 3212 | ly_data_type2str[basetype]); |
| 3213 | free(*type); |
| 3214 | (*type) = NULL; |
| 3215 | ret = LY_EVALID; |
| 3216 | goto cleanup; |
| 3217 | } |
| 3218 | |
| 3219 | /* get restrictions from the referred typedefs */ |
| 3220 | for (u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
| 3221 | tctx = (struct type_context*)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3222 | |
| 3223 | /* remember the typedef context for circular check */ |
| 3224 | ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3225 | |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3226 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3227 | base = tctx->tpdf->type.compiled; |
| 3228 | continue; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3229 | } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3230 | /* no change, just use the type information from the base */ |
| 3231 | base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled; |
| 3232 | ++base->refcount; |
| 3233 | continue; |
| 3234 | } |
| 3235 | |
| 3236 | ++(*type)->refcount; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3237 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 3238 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
| 3239 | tctx->tpdf->name, ly_data_type2str[basetype]); |
| 3240 | ret = LY_EVALID; |
| 3241 | goto cleanup; |
| 3242 | } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) { |
| 3243 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3244 | "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).", |
| 3245 | tctx->tpdf->name, tctx->tpdf->dflt); |
| 3246 | ret = LY_EVALID; |
| 3247 | goto cleanup; |
| 3248 | } |
| 3249 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3250 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3251 | /* TODO user type plugins */ |
| 3252 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3253 | prev_type = *type; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3254 | ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->mod, tctx->tpdf->name, &((struct lysp_tpdf*)tctx->tpdf)->type, |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 3255 | basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3256 | basetype, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3257 | LY_CHECK_GOTO(ret, cleanup); |
| 3258 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3259 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3260 | /* remove the processed typedef contexts from the stack for circular check */ |
| 3261 | ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3262 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3263 | /* process the type definition in leaf */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3264 | if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3265 | /* get restrictions from the node itself */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3266 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3267 | /* TODO user type plugins */ |
| 3268 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3269 | ++(*type)->refcount; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3270 | ret = lys_compile_type_(ctx, context_node_p, context_flags, context_mod, context_name, type_p, ctx->mod_def, basetype, NULL, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3271 | LY_CHECK_GOTO(ret, cleanup); |
| 3272 | } else { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3273 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 3274 | free(*type); |
| 3275 | (*type) = base; |
| 3276 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3277 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3278 | if (dflt && !(*type)->dflt) { |
| 3279 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3280 | (*type)->dflt_mod = dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3281 | (*type)->dflt = calloc(1, sizeof *(*type)->dflt); |
| 3282 | (*type)->dflt->realtype = (*type); |
| 3283 | ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt), |
| 3284 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3285 | lys_resolve_prefix, (void*)(*type)->dflt_mod, LYD_XML, NULL, NULL, (*type)->dflt, NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3286 | if (err) { |
| 3287 | ly_err_print(err); |
| 3288 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3289 | "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 3290 | ly_err_free(err); |
| 3291 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3292 | if (ret == LY_EINCOMPLETE) { |
| 3293 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3294 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, NULL, (*type)->dflt, (*type)->dflt_mod), cleanup); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3295 | |
| 3296 | /* but in general result is so far ok */ |
| 3297 | ret = LY_SUCCESS; |
| 3298 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3299 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3300 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3301 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3302 | COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3303 | |
| 3304 | cleanup: |
| 3305 | ly_set_erase(&tpdf_chain, free); |
| 3306 | return ret; |
| 3307 | } |
| 3308 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3309 | /** |
| 3310 | * @brief Compile status information of the given node. |
| 3311 | * |
| 3312 | * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes |
| 3313 | * has the status correctly set during the compilation. |
| 3314 | * |
| 3315 | * @param[in] ctx Compile context |
| 3316 | * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved. |
| 3317 | * If the status was set explicitly on the node, it is already set in the flags value and we just check |
| 3318 | * the compatibility with the parent's status value. |
| 3319 | * @param[in] parent_flags Flags of the parent node to check/inherit the status value. |
| 3320 | * @return LY_ERR value. |
| 3321 | */ |
| 3322 | static LY_ERR |
| 3323 | lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags) |
| 3324 | { |
| 3325 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3326 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3327 | if (!((*node_flags) & LYS_STATUS_MASK)) { |
| 3328 | if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) { |
| 3329 | if ((parent_flags & 0x3) != 0x3) { |
| 3330 | /* do not print the warning when inheriting status from uses - the uses_status value has a special |
| 3331 | * combination of bits (0x3) which marks the uses_status value */ |
| 3332 | LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.", |
| 3333 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3334 | } |
| 3335 | (*node_flags) |= parent_flags & LYS_STATUS_MASK; |
| 3336 | } else { |
| 3337 | (*node_flags) |= LYS_STATUS_CURR; |
| 3338 | } |
| 3339 | } else if (parent_flags & LYS_STATUS_MASK) { |
| 3340 | /* check status compatibility with the parent */ |
| 3341 | if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) { |
| 3342 | if ((*node_flags) & LYS_STATUS_CURR) { |
| 3343 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3344 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
| 3345 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3346 | } else { /* LYS_STATUS_DEPRC */ |
| 3347 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3348 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 3349 | } |
| 3350 | return LY_EVALID; |
| 3351 | } |
| 3352 | } |
| 3353 | return LY_SUCCESS; |
| 3354 | } |
| 3355 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3356 | /** |
| 3357 | * @brief Check uniqness of the node/action/notification name. |
| 3358 | * |
| 3359 | * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema |
| 3360 | * structures, but they share the namespace so we need to check their name collisions. |
| 3361 | * |
| 3362 | * @param[in] ctx Compile context. |
| 3363 | * @param[in] children List (linked list) of data nodes to go through. |
| 3364 | * @param[in] actions List (sized array) of actions or RPCs to go through. |
| 3365 | * @param[in] notifs List (sized array) of Notifications to go through. |
| 3366 | * @param[in] name Name of the item to find in the given lists. |
| 3367 | * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object |
| 3368 | * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity. |
| 3369 | * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise. |
| 3370 | */ |
| 3371 | static LY_ERR |
| 3372 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, |
| 3373 | const struct lysc_action *actions, const struct lysc_notif *notifs, |
| 3374 | const char *name, void *exclude) |
| 3375 | { |
| 3376 | const struct lysc_node *iter; |
| 3377 | unsigned int u; |
| 3378 | |
| 3379 | LY_LIST_FOR(children, iter) { |
| 3380 | if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) { |
| 3381 | goto error; |
| 3382 | } |
| 3383 | } |
| 3384 | LY_ARRAY_FOR(actions, u) { |
| 3385 | if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) { |
| 3386 | goto error; |
| 3387 | } |
| 3388 | } |
| 3389 | LY_ARRAY_FOR(notifs, u) { |
| 3390 | if (¬ifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) { |
| 3391 | goto error; |
| 3392 | } |
| 3393 | } |
| 3394 | return LY_SUCCESS; |
| 3395 | error: |
| 3396 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification"); |
| 3397 | return LY_EEXIST; |
| 3398 | } |
| 3399 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3400 | static LY_ERR lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *parent, uint16_t uses_status); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3401 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3402 | /** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3403 | * @brief Compile parsed RPC/action schema node information. |
| 3404 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3405 | * @param[in] action_p Parsed RPC/action schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3406 | * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action) |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3407 | * @param[in,out] action Prepared (empty) compiled action structure to fill. |
| 3408 | * @param[in] uses_status If the RPC/action is being placed instead of uses, here we have the uses's status value (as node's flags). |
| 3409 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3410 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3411 | */ |
| 3412 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3413 | lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3414 | struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status) |
| 3415 | { |
| 3416 | LY_ERR ret = LY_SUCCESS; |
| 3417 | struct lysp_node *child_p; |
| 3418 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3419 | int opt_prev = ctx->options; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3420 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3421 | lysc_update_path(ctx, parent, action_p->name); |
| 3422 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3423 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3424 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3425 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3426 | action_p->name, action)) { |
| 3427 | return LY_EVALID; |
| 3428 | } |
| 3429 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3430 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3431 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3432 | "Action \"%s\" is placed inside %s.", action_p->name, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3433 | ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification"); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3434 | return LY_EVALID; |
| 3435 | } |
| 3436 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3437 | action->nodetype = LYS_ACTION; |
| 3438 | action->module = ctx->mod; |
| 3439 | action->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3440 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3441 | action->sp = action_p; |
| 3442 | } |
| 3443 | action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3444 | |
| 3445 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3446 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3447 | LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3448 | |
| 3449 | DUP_STRING(ctx->ctx, action_p->name, action->name); |
| 3450 | DUP_STRING(ctx->ctx, action_p->dsc, action->dsc); |
| 3451 | DUP_STRING(ctx->ctx, action_p->ref, action->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3452 | COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3453 | COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3454 | |
| 3455 | /* input */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3456 | lysc_update_path(ctx, (struct lysc_node*)action, "input"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3457 | COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3458 | COMPILE_EXTS_GOTO(ctx, action_p->input.exts, action->input_exts, &action->input, LYEXT_PAR_INPUT, ret, cleanup); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3459 | ctx->options |= LYSC_OPT_RPC_INPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3460 | LY_LIST_FOR(action_p->input.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3461 | LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status)); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3462 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3463 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3464 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3465 | |
| 3466 | /* output */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3467 | lysc_update_path(ctx, (struct lysc_node*)action, "output"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3468 | COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3469 | COMPILE_EXTS_GOTO(ctx, action_p->output.exts, action->output_exts, &action->output, LYEXT_PAR_OUTPUT, ret, cleanup); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3470 | ctx->options |= LYSC_OPT_RPC_OUTPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3471 | LY_LIST_FOR(action_p->output.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3472 | LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status)); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3473 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3474 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3475 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3476 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3477 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3478 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3479 | return ret; |
| 3480 | } |
| 3481 | |
| 3482 | /** |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3483 | * @brief Compile parsed Notification schema node information. |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3484 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3485 | * @param[in] notif_p Parsed Notification schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3486 | * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification |
| 3487 | * @param[in,out] notif Prepared (empty) compiled notification structure to fill. |
| 3488 | * @param[in] uses_status If the Notification is being placed instead of uses, here we have the uses's status value (as node's flags). |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3489 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3490 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3491 | */ |
| 3492 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3493 | lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3494 | struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status) |
| 3495 | { |
| 3496 | LY_ERR ret = LY_SUCCESS; |
| 3497 | struct lysp_node *child_p; |
| 3498 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3499 | int opt_prev = ctx->options; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3500 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3501 | lysc_update_path(ctx, parent, notif_p->name); |
| 3502 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3503 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3504 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3505 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3506 | notif_p->name, notif)) { |
| 3507 | return LY_EVALID; |
| 3508 | } |
| 3509 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3510 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3511 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3512 | "Notification \"%s\" is placed inside %s.", notif_p->name, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3513 | ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3514 | return LY_EVALID; |
| 3515 | } |
| 3516 | |
| 3517 | notif->nodetype = LYS_NOTIF; |
| 3518 | notif->module = ctx->mod; |
| 3519 | notif->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3520 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3521 | notif->sp = notif_p; |
| 3522 | } |
| 3523 | notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3524 | |
| 3525 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3526 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3527 | LY_CHECK_RET(lys_compile_status(ctx, ¬if->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3528 | |
| 3529 | DUP_STRING(ctx->ctx, notif_p->name, notif->name); |
| 3530 | DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc); |
| 3531 | DUP_STRING(ctx->ctx, notif_p->ref, notif->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3532 | COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3533 | COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3534 | COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3535 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3536 | ctx->options |= LYSC_OPT_NOTIFICATION; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3537 | LY_LIST_FOR(notif_p->data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3538 | LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3539 | } |
| 3540 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3541 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3542 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3543 | ctx->options = opt_prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3544 | return ret; |
| 3545 | } |
| 3546 | |
| 3547 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3548 | * @brief Compile parsed container node information. |
| 3549 | * @param[in] ctx Compile context |
| 3550 | * @param[in] node_p Parsed container node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3551 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3552 | * is enriched with the container-specific information. |
| 3553 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3554 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3555 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3556 | lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3557 | { |
| 3558 | struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p; |
| 3559 | struct lysc_node_container *cont = (struct lysc_node_container*)node; |
| 3560 | struct lysp_node *child_p; |
| 3561 | unsigned int u; |
| 3562 | LY_ERR ret = LY_SUCCESS; |
| 3563 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3564 | if (cont_p->presence) { |
| 3565 | cont->flags |= LYS_PRESENCE; |
| 3566 | } |
| 3567 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3568 | LY_LIST_FOR(cont_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3569 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3570 | } |
| 3571 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3572 | COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done); |
| 3573 | COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done); |
| 3574 | COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3575 | |
| 3576 | done: |
| 3577 | return ret; |
| 3578 | } |
| 3579 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3580 | /* |
| 3581 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 3582 | * @param[in] ctx Compile context. |
| 3583 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 3584 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3585 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 3586 | * @return LY_ERR value. |
| 3587 | */ |
| 3588 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3589 | lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p, struct lysc_node_leaf *leaf) |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3590 | { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3591 | unsigned int u; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3592 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf; |
| 3593 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3594 | LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, ctx->mod_def->parsed, leaf->name, type_p, &leaf->type, |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3595 | leaf->units ? NULL : &leaf->units)); |
| 3596 | if (leaf->nodetype == LYS_LEAFLIST) { |
| 3597 | if (llist->type->dflt && !llist->dflts && !llist->min) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3598 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM); |
| 3599 | llist->dflts_mods[0] = llist->type->dflt_mod; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3600 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3601 | llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]); |
| 3602 | llist->dflts[0]->realtype = llist->type->dflt->realtype; |
| 3603 | llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]); |
| 3604 | llist->dflts[0]->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3605 | LY_ARRAY_INCREMENT(llist->dflts); |
| 3606 | } |
| 3607 | } else { |
| 3608 | if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3609 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3610 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3611 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 3612 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 3613 | leaf->dflt->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3614 | } |
| 3615 | } |
| 3616 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3617 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 3618 | ly_set_add(&ctx->unres, leaf, 0); |
| 3619 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
| 3620 | LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) { |
| 3621 | if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) { |
| 3622 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 3623 | ly_set_add(&ctx->unres, leaf, 0); |
| 3624 | } |
| 3625 | } |
| 3626 | } else if (leaf->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3627 | if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) { |
| 3628 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3629 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 3630 | return LY_EVALID; |
| 3631 | } |
| 3632 | } |
| 3633 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3634 | return LY_SUCCESS; |
| 3635 | } |
| 3636 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3637 | /** |
| 3638 | * @brief Compile parsed leaf node information. |
| 3639 | * @param[in] ctx Compile context |
| 3640 | * @param[in] node_p Parsed leaf node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3641 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3642 | * is enriched with the leaf-specific information. |
| 3643 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3644 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3645 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3646 | lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3647 | { |
| 3648 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p; |
| 3649 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 3650 | unsigned int u; |
| 3651 | LY_ERR ret = LY_SUCCESS; |
| 3652 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3653 | COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3654 | if (leaf_p->units) { |
| 3655 | leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0); |
| 3656 | leaf->flags |= LYS_SET_UNITS; |
| 3657 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3658 | |
| 3659 | /* the dflt member is just filled to avoid getting the default value from the type */ |
| 3660 | leaf->dflt = (void*)leaf_p->dflt; |
| 3661 | ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf); |
| 3662 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3663 | if (leaf_p->dflt) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3664 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3665 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3666 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3667 | leaf->dflt->realtype = leaf->type; |
| 3668 | ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt), |
| 3669 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3670 | lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3671 | leaf->dflt->realtype->refcount++; |
| 3672 | if (err) { |
| 3673 | ly_err_print(err); |
| 3674 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3675 | "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg); |
| 3676 | ly_err_free(err); |
| 3677 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3678 | if (ret == LY_EINCOMPLETE) { |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3679 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3680 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), done); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3681 | |
| 3682 | /* but in general result is so far ok */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3683 | ret = LY_SUCCESS; |
| 3684 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3685 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3686 | leaf->flags |= LYS_SET_DFLT; |
| 3687 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3688 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 3689 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3690 | done: |
| 3691 | return ret; |
| 3692 | } |
| 3693 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3694 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3695 | * @brief Compile parsed leaf-list node information. |
| 3696 | * @param[in] ctx Compile context |
| 3697 | * @param[in] node_p Parsed leaf-list node. |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3698 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3699 | * is enriched with the leaf-list-specific information. |
| 3700 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3701 | */ |
| 3702 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3703 | lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node) |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3704 | { |
| 3705 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p; |
| 3706 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3707 | unsigned int u, v; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3708 | LY_ERR ret = LY_SUCCESS; |
| 3709 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3710 | COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3711 | if (llist_p->units) { |
| 3712 | llist->units = lydict_insert(ctx->ctx, llist_p->units, 0); |
| 3713 | llist->flags |= LYS_SET_UNITS; |
| 3714 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3715 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3716 | /* the dflts member is just filled to avoid getting the default value from the type */ |
| 3717 | llist->dflts = (void*)llist_p->dflts; |
| 3718 | ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3719 | if (llist_p->dflts) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3720 | llist->dflts = NULL; /* reset the temporary llist_p->dflts */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3721 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(llist_p->dflts), ret, done); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3722 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done); |
| 3723 | LY_ARRAY_FOR(llist_p->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3724 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3725 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 3726 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3727 | LY_ARRAY_INCREMENT(llist->dflts); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3728 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 3729 | llist->dflts[u]->realtype = llist->type; |
| 3730 | ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]), |
| 3731 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3732 | lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3733 | llist->dflts[u]->realtype->refcount++; |
| 3734 | if (err) { |
| 3735 | ly_err_print(err); |
| 3736 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3737 | "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg); |
| 3738 | ly_err_free(err); |
| 3739 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3740 | if (ret == LY_EINCOMPLETE) { |
| 3741 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3742 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), done); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3743 | |
| 3744 | /* but in general result is so far ok */ |
| 3745 | ret = LY_SUCCESS; |
| 3746 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3747 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3748 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3749 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3750 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3751 | if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) { |
| 3752 | /* configuration data values must be unique - so check the default values */ |
| 3753 | LY_ARRAY_FOR(llist->dflts, u) { |
| 3754 | for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) { |
| 3755 | if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) { |
| 3756 | int dynamic = 0; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3757 | const char *val = llist->type->plugin->print(llist->dflts[v], LYD_XML, lys_get_prefix, llist->dflts_mods[v], &dynamic); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3758 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3759 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", val); |
| 3760 | if (dynamic) { |
| 3761 | free((char*)val); |
| 3762 | } |
| 3763 | return LY_EVALID; |
| 3764 | } |
| 3765 | } |
| 3766 | } |
| 3767 | } |
| 3768 | |
| 3769 | /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */ |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3770 | |
| 3771 | llist->min = llist_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3772 | if (llist->min) { |
| 3773 | llist->flags |= LYS_MAND_TRUE; |
| 3774 | } |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 3775 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3776 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3777 | done: |
| 3778 | return ret; |
| 3779 | } |
| 3780 | |
| 3781 | /** |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3782 | * @brief Compile information about list's uniques. |
| 3783 | * @param[in] ctx Compile context. |
| 3784 | * @param[in] context_module Module where the prefixes are going to be resolved. |
| 3785 | * @param[in] uniques Sized array list of unique statements. |
| 3786 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3787 | * @return LY_ERR value. |
| 3788 | */ |
| 3789 | static LY_ERR |
| 3790 | lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list) |
| 3791 | { |
| 3792 | LY_ERR ret = LY_SUCCESS; |
| 3793 | struct lysc_node_leaf **key, ***unique; |
| 3794 | const char *keystr, *delim; |
| 3795 | size_t len; |
| 3796 | unsigned int v; |
| 3797 | int config; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3798 | uint16_t flags; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3799 | |
| 3800 | for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) { |
| 3801 | config = -1; |
| 3802 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 3803 | keystr = uniques[v]; |
| 3804 | while (keystr) { |
| 3805 | delim = strpbrk(keystr, " \t\n"); |
| 3806 | if (delim) { |
| 3807 | len = delim - keystr; |
| 3808 | while (isspace(*delim)) { |
| 3809 | ++delim; |
| 3810 | } |
| 3811 | } else { |
| 3812 | len = strlen(keystr); |
| 3813 | } |
| 3814 | |
| 3815 | /* unique node must be present */ |
| 3816 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3817 | ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0, |
| 3818 | (const struct lysc_node**)key, &flags); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3819 | if (ret != LY_SUCCESS) { |
| 3820 | if (ret == LY_EDENIED) { |
| 3821 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3822 | "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.", |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3823 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
| 3824 | } |
| 3825 | return LY_EVALID; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3826 | } else if (flags) { |
| 3827 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3828 | "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.", |
| 3829 | len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action"); |
| 3830 | return LY_EVALID; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3831 | } |
| 3832 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3833 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3834 | /* all referenced leafs must be of the same config type */ |
| 3835 | if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) { |
| 3836 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3837 | "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]); |
| 3838 | return LY_EVALID; |
| 3839 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 3840 | config = 1; |
| 3841 | } else { /* LYS_CONFIG_R */ |
| 3842 | config = 0; |
| 3843 | } |
| 3844 | |
| 3845 | /* check status */ |
| 3846 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 3847 | (*key)->flags, (*key)->module, (*key)->name)); |
| 3848 | |
| 3849 | /* mark leaf as unique */ |
| 3850 | (*key)->flags |= LYS_UNIQUE; |
| 3851 | |
| 3852 | /* next unique value in line */ |
| 3853 | keystr = delim; |
| 3854 | } |
| 3855 | /* next unique definition */ |
| 3856 | } |
| 3857 | |
| 3858 | return LY_SUCCESS; |
| 3859 | } |
| 3860 | |
| 3861 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3862 | * @brief Compile parsed list node information. |
| 3863 | * @param[in] ctx Compile context |
| 3864 | * @param[in] node_p Parsed list node. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3865 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3866 | * is enriched with the list-specific information. |
| 3867 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3868 | */ |
| 3869 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3870 | lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node) |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3871 | { |
| 3872 | struct lysp_node_list *list_p = (struct lysp_node_list*)node_p; |
| 3873 | struct lysc_node_list *list = (struct lysc_node_list*)node; |
| 3874 | struct lysp_node *child_p; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3875 | struct lysc_node_leaf *key, *prev_key = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3876 | size_t len; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3877 | unsigned int u; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3878 | const char *keystr, *delim; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3879 | LY_ERR ret = LY_SUCCESS; |
| 3880 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3881 | list->min = list_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3882 | if (list->min) { |
| 3883 | list->flags |= LYS_MAND_TRUE; |
| 3884 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3885 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 3886 | |
| 3887 | LY_LIST_FOR(list_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3888 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3889 | } |
| 3890 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3891 | COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3892 | |
| 3893 | /* keys */ |
| 3894 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 3895 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 3896 | return LY_EVALID; |
| 3897 | } |
| 3898 | |
| 3899 | /* find all the keys (must be direct children) */ |
| 3900 | keystr = list_p->key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3901 | if (!keystr) { |
| 3902 | /* keyless list */ |
| 3903 | list->flags |= LYS_KEYLESS; |
| 3904 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3905 | while (keystr) { |
| 3906 | delim = strpbrk(keystr, " \t\n"); |
| 3907 | if (delim) { |
| 3908 | len = delim - keystr; |
| 3909 | while (isspace(*delim)) { |
| 3910 | ++delim; |
| 3911 | } |
| 3912 | } else { |
| 3913 | len = strlen(keystr); |
| 3914 | } |
| 3915 | |
| 3916 | /* key node must be present */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3917 | key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK); |
| 3918 | if (!(key)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3919 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3920 | "The list's key \"%.*s\" not found.", len, keystr); |
| 3921 | return LY_EVALID; |
| 3922 | } |
| 3923 | /* keys must be unique */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3924 | if (key->flags & LYS_KEY) { |
| 3925 | /* the node was already marked as a key */ |
| 3926 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3927 | "Duplicated key identifier \"%.*s\".", len, keystr); |
| 3928 | return LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3929 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3930 | |
| 3931 | lysc_update_path(ctx, (struct lysc_node*)list, key->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3932 | /* key must have the same config flag as the list itself */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3933 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3934 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 3935 | return LY_EVALID; |
| 3936 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3937 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3938 | /* YANG 1.0 denies key to be of empty type */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3939 | if (key->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3940 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3941 | "List's key cannot be of \"empty\" type until it is in YANG 1.1 module."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3942 | return LY_EVALID; |
| 3943 | } |
| 3944 | } else { |
| 3945 | /* when and if-feature are illegal on list keys */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3946 | if (key->when) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3947 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3948 | "List's key must not have any \"when\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3949 | return LY_EVALID; |
| 3950 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3951 | if (key->iffeatures) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3952 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3953 | "List's key must not have any \"if-feature\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3954 | return LY_EVALID; |
| 3955 | } |
| 3956 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3957 | |
| 3958 | /* check status */ |
| 3959 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3960 | key->flags, key->module, key->name)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3961 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3962 | /* ignore default values of the key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3963 | if (key->dflt) { |
| 3964 | key->dflt->realtype->plugin->free(ctx->ctx, key->dflt); |
| 3965 | lysc_type_free(ctx->ctx, key->dflt->realtype); |
| 3966 | free(key->dflt); |
| 3967 | key->dflt = NULL; |
| 3968 | key->dflt_mod = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3969 | } |
| 3970 | /* mark leaf as key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3971 | key->flags |= LYS_KEY; |
| 3972 | |
| 3973 | /* move it to the correct position */ |
| 3974 | if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) { |
| 3975 | /* fix links in closest previous siblings of the key */ |
| 3976 | if (key->next) { |
| 3977 | key->next->prev = key->prev; |
| 3978 | } else { |
| 3979 | /* last child */ |
| 3980 | list->child->prev = key->prev; |
| 3981 | } |
| 3982 | if (key->prev->next) { |
| 3983 | key->prev->next = key->next; |
| 3984 | } |
| 3985 | /* fix links in the key */ |
| 3986 | if (prev_key) { |
| 3987 | key->prev = (struct lysc_node*)prev_key; |
| 3988 | key->next = prev_key->next; |
| 3989 | } else { |
| 3990 | key->prev = list->child->prev; |
| 3991 | key->next = list->child; |
| 3992 | } |
| 3993 | /* fix links in closes future siblings of the key */ |
| 3994 | if (prev_key) { |
| 3995 | if (prev_key->next) { |
| 3996 | prev_key->next->prev = (struct lysc_node*)key; |
| 3997 | } else { |
| 3998 | list->child->prev = (struct lysc_node*)key; |
| 3999 | } |
| 4000 | prev_key->next = (struct lysc_node*)key; |
| 4001 | } else { |
| 4002 | list->child->prev = (struct lysc_node*)key; |
| 4003 | } |
| 4004 | /* fix links in parent */ |
| 4005 | if (!key->prev->next) { |
| 4006 | list->child = (struct lysc_node*)key; |
| 4007 | } |
| 4008 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4009 | |
| 4010 | /* next key value */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4011 | prev_key = key; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4012 | keystr = delim; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4013 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4014 | } |
| 4015 | |
| 4016 | /* uniques */ |
| 4017 | if (list_p->uniques) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4018 | LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4019 | } |
| 4020 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4021 | COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done); |
| 4022 | COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4023 | |
| 4024 | done: |
| 4025 | return ret; |
| 4026 | } |
| 4027 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4028 | /** |
| 4029 | * @brief Do some checks and set the default choice's case. |
| 4030 | * |
| 4031 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 4032 | * |
| 4033 | * @param[in] ctx Compile context. |
| 4034 | * @param[in] dflt Name of the default branch. Can contain even the prefix, but it make sense only in case it is the prefix of the module itself, |
| 4035 | * not the reference to the imported module. |
| 4036 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 4037 | * @return LY_ERR value. |
| 4038 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4039 | static LY_ERR |
| 4040 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
| 4041 | { |
| 4042 | struct lysc_node *iter, *node = (struct lysc_node*)ch; |
| 4043 | const char *prefix = NULL, *name; |
| 4044 | size_t prefix_len = 0; |
| 4045 | |
| 4046 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 4047 | name = strchr(dflt, ':'); |
| 4048 | if (name) { |
| 4049 | prefix = dflt; |
| 4050 | prefix_len = name - prefix; |
| 4051 | ++name; |
| 4052 | } else { |
| 4053 | name = dflt; |
| 4054 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4055 | if (prefix && (strncmp(prefix, node->module->prefix, prefix_len) || node->module->prefix[prefix_len] != '\0')) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4056 | /* prefixed default case make sense only for the prefix of the schema itself */ |
| 4057 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4058 | "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").", |
| 4059 | prefix_len, prefix); |
| 4060 | return LY_EVALID; |
| 4061 | } |
| 4062 | ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
| 4063 | if (!ch->dflt) { |
| 4064 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4065 | "Default case \"%s\" not found.", dflt); |
| 4066 | return LY_EVALID; |
| 4067 | } |
| 4068 | /* no mandatory nodes directly under the default case */ |
| 4069 | LY_LIST_FOR(ch->dflt->child, iter) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4070 | if (iter->parent != (struct lysc_node*)ch->dflt) { |
| 4071 | break; |
| 4072 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4073 | if (iter->flags & LYS_MAND_TRUE) { |
| 4074 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4075 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt); |
| 4076 | return LY_EVALID; |
| 4077 | } |
| 4078 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4079 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4080 | return LY_SUCCESS; |
| 4081 | } |
| 4082 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4083 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4084 | lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4085 | { |
| 4086 | struct lys_module *mod; |
| 4087 | const char *prefix = NULL, *name; |
| 4088 | size_t prefix_len = 0; |
| 4089 | struct lysc_node_case *cs; |
| 4090 | struct lysc_node *node; |
| 4091 | |
| 4092 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 4093 | name = strchr(dflt, ':'); |
| 4094 | if (name) { |
| 4095 | prefix = dflt; |
| 4096 | prefix_len = name - prefix; |
| 4097 | ++name; |
| 4098 | } else { |
| 4099 | name = dflt; |
| 4100 | } |
| 4101 | /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */ |
| 4102 | if (prefix) { |
| 4103 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 4104 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4105 | "Invalid deviation adding \"default\" property \"%s\" of choice. " |
| 4106 | "The prefix does not match any imported module of the deviation module.", dflt); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4107 | return LY_EVALID; |
| 4108 | } |
| 4109 | } else { |
| 4110 | mod = ctx->mod; |
| 4111 | } |
| 4112 | /* get the default case */ |
| 4113 | cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
| 4114 | if (!cs) { |
| 4115 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4116 | "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4117 | return LY_EVALID; |
| 4118 | } |
| 4119 | |
| 4120 | /* check that there is no mandatory node */ |
| 4121 | LY_LIST_FOR(cs->child, node) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4122 | if (node->parent != (struct lysc_node*)cs) { |
| 4123 | break; |
| 4124 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4125 | if (node->flags & LYS_MAND_TRUE) { |
| 4126 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4127 | "Invalid deviation adding \"default\" property \"%s\" of choice - " |
| 4128 | "mandatory node \"%s\" under the default case.", dflt, node->name); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4129 | return LY_EVALID; |
| 4130 | } |
| 4131 | } |
| 4132 | |
| 4133 | /* set the default case in choice */ |
| 4134 | ch->dflt = cs; |
| 4135 | cs->flags |= LYS_SET_DFLT; |
| 4136 | |
| 4137 | return LY_SUCCESS; |
| 4138 | } |
| 4139 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4140 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4141 | * @brief Compile parsed choice node information. |
| 4142 | * @param[in] ctx Compile context |
| 4143 | * @param[in] node_p Parsed choice node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4144 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4145 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4146 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4147 | */ |
| 4148 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4149 | lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node) |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4150 | { |
| 4151 | struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p; |
| 4152 | struct lysc_node_choice *ch = (struct lysc_node_choice*)node; |
| 4153 | struct lysp_node *child_p, *case_child_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4154 | LY_ERR ret = LY_SUCCESS; |
| 4155 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4156 | LY_LIST_FOR(ch_p->child, child_p) { |
| 4157 | if (child_p->nodetype == LYS_CASE) { |
| 4158 | LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4159 | LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4160 | } |
| 4161 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4162 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4163 | } |
| 4164 | } |
| 4165 | |
| 4166 | /* default branch */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 4167 | if (ch_p->dflt) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4168 | LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch)); |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 4169 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4170 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4171 | return ret; |
| 4172 | } |
| 4173 | |
| 4174 | /** |
| 4175 | * @brief Compile parsed anydata or anyxml node information. |
| 4176 | * @param[in] ctx Compile context |
| 4177 | * @param[in] node_p Parsed anydata or anyxml node. |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4178 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 4179 | * is enriched with the any-specific information. |
| 4180 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4181 | */ |
| 4182 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4183 | lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node) |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4184 | { |
| 4185 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p; |
| 4186 | struct lysc_node_anydata *any = (struct lysc_node_anydata*)node; |
| 4187 | unsigned int u; |
| 4188 | LY_ERR ret = LY_SUCCESS; |
| 4189 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4190 | COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4191 | |
| 4192 | if (any->flags & LYS_CONFIG_W) { |
| 4193 | LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.", |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 4194 | ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4195 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4196 | done: |
| 4197 | return ret; |
| 4198 | } |
| 4199 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4200 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4201 | * @brief Connect the node into the siblings list and check its name uniqueness. |
| 4202 | * |
| 4203 | * @param[in] ctx Compile context |
| 4204 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 4205 | * the choice itself is expected instead of a specific case node. |
| 4206 | * @param[in] node Schema node to connect into the list. |
| 4207 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
| 4208 | */ |
| 4209 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4210 | lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node) |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4211 | { |
| 4212 | struct lysc_node **children; |
| 4213 | |
| 4214 | if (node->nodetype == LYS_CASE) { |
| 4215 | children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases; |
| 4216 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4217 | children = lysc_node_children_p(parent, ctx->options); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4218 | } |
| 4219 | if (children) { |
| 4220 | if (!(*children)) { |
| 4221 | /* first child */ |
| 4222 | *children = node; |
| 4223 | } else if (*children != node) { |
| 4224 | /* by the condition in previous branch we cover the choice/case children |
| 4225 | * - the children list is shared by the choice and the the first case, in addition |
| 4226 | * the first child of each case must be referenced from the case node. So the node is |
| 4227 | * actually always already inserted in case it is the first children - so here such |
| 4228 | * a situation actually corresponds to the first branch */ |
| 4229 | /* insert at the end of the parent's children list */ |
| 4230 | (*children)->prev->next = node; |
| 4231 | node->prev = (*children)->prev; |
| 4232 | (*children)->prev = node; |
| 4233 | |
| 4234 | /* check the name uniqueness */ |
| 4235 | if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent), |
| 4236 | lysc_node_notifs(parent), node->name, node)) { |
| 4237 | return LY_EEXIST; |
| 4238 | } |
| 4239 | } |
| 4240 | } |
| 4241 | return LY_SUCCESS; |
| 4242 | } |
| 4243 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4244 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4245 | * @brief Get the XPath context node for the given schema node. |
| 4246 | * @param[in] start The schema node where the XPath expression appears. |
| 4247 | * @return The context node to evaluate XPath expression in given schema node. |
| 4248 | * @return NULL in case the context node is the root node. |
| 4249 | */ |
| 4250 | static struct lysc_node * |
| 4251 | lysc_xpath_context(struct lysc_node *start) |
| 4252 | { |
| 4253 | for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF)); |
| 4254 | start = start->parent); |
| 4255 | return start; |
| 4256 | } |
| 4257 | |
| 4258 | /** |
| 4259 | * @brief Prepare the case structure in choice node for the new data node. |
| 4260 | * |
| 4261 | * It is able to handle implicit as well as explicit cases and the situation when the case has multiple data nodes and the case was already |
| 4262 | * created in the choice when the first child was processed. |
| 4263 | * |
| 4264 | * @param[in] ctx Compile context. |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4265 | * @param[in] node_p Node image from the parsed tree. If the case is explicit, it is the LYS_CASE node, but in case of implicit case, |
| 4266 | * it is the LYS_CHOICE node or LYS_AUGMENT node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4267 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 4268 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 4269 | * @return The case structure where the child node belongs to, NULL in case of error. Note that the child is not connected into the siblings list, |
| 4270 | * it is linked from the case structure only in case it is its first child. |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4271 | */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4272 | static struct lysc_node_case* |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4273 | lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node_choice *ch, struct lysc_node *child) |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4274 | { |
| 4275 | struct lysc_node *iter; |
Radek Krejci | 98b1a66 | 2019-04-23 10:25:50 +0200 | [diff] [blame] | 4276 | struct lysc_node_case *cs = NULL; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4277 | struct lysc_when **when; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4278 | unsigned int u; |
| 4279 | LY_ERR ret; |
| 4280 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4281 | #define UNIQUE_CHECK(NAME, MOD) \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4282 | LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4283 | if (iter->module == MOD && !strcmp(iter->name, NAME)) { \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4284 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \ |
| 4285 | return NULL; \ |
| 4286 | } \ |
| 4287 | } |
| 4288 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4289 | if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) { |
| 4290 | UNIQUE_CHECK(child->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4291 | |
| 4292 | /* we have to add an implicit case node into the parent choice */ |
| 4293 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4294 | DUP_STRING(ctx->ctx, child->name, cs->name); |
| 4295 | cs->flags = ch->flags & LYS_STATUS_MASK; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4296 | } else if (node_p->nodetype == LYS_CASE) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4297 | if (ch->cases && (node_p == ch->cases->prev->sp)) { |
| 4298 | /* the case is already present since the child is not its first children */ |
| 4299 | return (struct lysc_node_case*)ch->cases->prev; |
| 4300 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4301 | UNIQUE_CHECK(node_p->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4302 | |
| 4303 | /* explicit parent case is not present (this is its first child) */ |
| 4304 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4305 | DUP_STRING(ctx->ctx, node_p->name, cs->name); |
| 4306 | cs->flags = LYS_STATUS_MASK & node_p->flags; |
| 4307 | cs->sp = node_p; |
| 4308 | |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4309 | /* check the case's status (don't need to solve uses_status since case statement cannot be directly in grouping statement */ |
Radek Krejci | bae745f | 2019-04-09 16:28:00 +0200 | [diff] [blame] | 4310 | LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4311 | |
| 4312 | if (node_p->when) { |
| 4313 | LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4314 | ret = lys_compile_when(ctx, node_p->when, when); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4315 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4316 | (*when)->context = lysc_xpath_context(ch->parent); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4317 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4318 | COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4319 | } else { |
| 4320 | LOGINT(ctx->ctx); |
| 4321 | goto error; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4322 | } |
| 4323 | cs->module = ctx->mod; |
| 4324 | cs->prev = (struct lysc_node*)cs; |
| 4325 | cs->nodetype = LYS_CASE; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4326 | lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4327 | cs->parent = (struct lysc_node*)ch; |
| 4328 | cs->child = child; |
| 4329 | |
| 4330 | return cs; |
| 4331 | error: |
Radek Krejci | 1b1e925 | 2019-04-24 08:45:50 +0200 | [diff] [blame] | 4332 | if (cs) { |
| 4333 | lysc_node_free(ctx->ctx, (struct lysc_node*)cs); |
| 4334 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4335 | return NULL; |
| 4336 | |
| 4337 | #undef UNIQUE_CHECK |
| 4338 | } |
| 4339 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4340 | /** |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4341 | * @brief Apply refined or deviated config to the target node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4342 | * |
| 4343 | * @param[in] ctx Compile context. |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4344 | * @param[in] node Target node where the config is supposed to be changed. |
| 4345 | * @param[in] config_flag Node's config flag to be applied to the @p node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4346 | * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is |
| 4347 | * done only on the node for which the refine was created. The function applies also recursively to apply the config change |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4348 | * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes. |
| 4349 | * @param[in] refine_flag Flag to distinguish if the change is caused by refine (flag set) or deviation (for logging). |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4350 | * @return LY_ERR value. |
| 4351 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4352 | static LY_ERR |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4353 | lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4354 | int inheriting, int refine_flag) |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4355 | { |
| 4356 | struct lysc_node *child; |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4357 | uint16_t config = config_flag & LYS_CONFIG_MASK; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4358 | |
| 4359 | if (config == (node->flags & LYS_CONFIG_MASK)) { |
| 4360 | /* nothing to do */ |
| 4361 | return LY_SUCCESS; |
| 4362 | } |
| 4363 | |
| 4364 | if (!inheriting) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4365 | /* explicit change */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4366 | if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 4367 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4368 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4369 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4370 | return LY_EVALID; |
| 4371 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4372 | node->flags |= LYS_SET_CONFIG; |
| 4373 | } else { |
| 4374 | if (node->flags & LYS_SET_CONFIG) { |
| 4375 | if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) { |
| 4376 | /* setting config flags, but have node with explicit config true */ |
| 4377 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4378 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4379 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4380 | return LY_EVALID; |
| 4381 | } |
| 4382 | /* do not change config on nodes where the config is explicitely set, this does not apply to |
| 4383 | * nodes, which are being changed explicitly (targets of refine or deviation) */ |
| 4384 | return LY_SUCCESS; |
| 4385 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4386 | } |
| 4387 | node->flags &= ~LYS_CONFIG_MASK; |
| 4388 | node->flags |= config; |
| 4389 | |
| 4390 | /* inherit the change into the children */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4391 | LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4392 | LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4393 | } |
| 4394 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4395 | return LY_SUCCESS; |
| 4396 | } |
| 4397 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4398 | /** |
| 4399 | * @brief Set LYS_MAND_TRUE flag for the non-presence container parents. |
| 4400 | * |
| 4401 | * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate |
| 4402 | * the flag to such parents from a mandatory children. |
| 4403 | * |
| 4404 | * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory. |
| 4405 | * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag |
| 4406 | * (mandatory children was removed). |
| 4407 | */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4408 | void |
| 4409 | lys_compile_mandatory_parents(struct lysc_node *parent, int add) |
| 4410 | { |
| 4411 | struct lysc_node *iter; |
| 4412 | |
| 4413 | if (add) { /* set flag */ |
| 4414 | for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE); |
| 4415 | parent = parent->parent) { |
| 4416 | parent->flags |= LYS_MAND_TRUE; |
| 4417 | } |
| 4418 | } else { /* unset flag */ |
| 4419 | for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4420 | for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) { |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4421 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4422 | /* there is another mandatory node */ |
| 4423 | return; |
| 4424 | } |
| 4425 | } |
| 4426 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 4427 | parent->flags &= ~LYS_MAND_TRUE; |
| 4428 | } |
| 4429 | } |
| 4430 | } |
| 4431 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4432 | /** |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4433 | * @brief Internal sorting process for the lys_compile_augment_sort(). |
| 4434 | * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result. |
| 4435 | * @param[in,out] result Sized array to store the sorted list of augments. The array is expected |
| 4436 | * to be allocated to hold the complete list, its size is just incremented by adding another item. |
| 4437 | */ |
| 4438 | static void |
| 4439 | lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result) |
| 4440 | { |
| 4441 | unsigned int v; |
| 4442 | size_t len; |
| 4443 | |
| 4444 | len = strlen(aug_p->nodeid); |
| 4445 | LY_ARRAY_FOR(result, v) { |
| 4446 | if (strlen(result[v]->nodeid) <= len) { |
| 4447 | continue; |
| 4448 | } |
| 4449 | if (v < LY_ARRAY_SIZE(result)) { |
| 4450 | /* move the rest of array */ |
| 4451 | memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result); |
| 4452 | break; |
| 4453 | } |
| 4454 | } |
| 4455 | result[v] = aug_p; |
| 4456 | LY_ARRAY_INCREMENT(result); |
| 4457 | } |
| 4458 | |
| 4459 | /** |
| 4460 | * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment). |
| 4461 | * |
| 4462 | * The sorting is based only on the length of the augment's path since it guarantee the correct order |
| 4463 | * (it doesn't matter the /a/x is done before /a/b/c from the example above). |
| 4464 | * |
| 4465 | * @param[in] ctx Compile context. |
| 4466 | * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken). |
| 4467 | * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's) |
| 4468 | * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules. |
| 4469 | * @param[out] augments Resulting sorted sized array of pointers to the augments. |
| 4470 | * @return LY_ERR value. |
| 4471 | */ |
| 4472 | LY_ERR |
| 4473 | lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments) |
| 4474 | { |
| 4475 | struct lysp_augment **result = NULL; |
| 4476 | unsigned int u, v; |
| 4477 | size_t count = 0; |
| 4478 | |
| 4479 | assert(augments); |
| 4480 | |
| 4481 | /* get count of the augments in module and all its submodules */ |
| 4482 | if (aug_p) { |
| 4483 | count += LY_ARRAY_SIZE(aug_p); |
| 4484 | } |
| 4485 | LY_ARRAY_FOR(inc_p, u) { |
| 4486 | if (inc_p[u].submodule->augments) { |
| 4487 | count += LY_ARRAY_SIZE(inc_p[u].submodule->augments); |
| 4488 | } |
| 4489 | } |
| 4490 | |
| 4491 | if (!count) { |
| 4492 | *augments = NULL; |
| 4493 | return LY_SUCCESS; |
| 4494 | } |
| 4495 | LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM); |
| 4496 | |
| 4497 | /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them |
| 4498 | * together, so there can be even /z/y betwwen them. */ |
| 4499 | LY_ARRAY_FOR(aug_p, u) { |
| 4500 | lys_compile_augment_sort_(&aug_p[u], result); |
| 4501 | } |
| 4502 | LY_ARRAY_FOR(inc_p, u) { |
| 4503 | LY_ARRAY_FOR(inc_p[u].submodule->augments, v) { |
| 4504 | lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result); |
| 4505 | } |
| 4506 | } |
| 4507 | |
| 4508 | *augments = result; |
| 4509 | return LY_SUCCESS; |
| 4510 | } |
| 4511 | |
| 4512 | /** |
| 4513 | * @brief Compile the parsed augment connecting it into its target. |
| 4514 | * |
| 4515 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 4516 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 4517 | * are already implemented and compiled. |
| 4518 | * |
| 4519 | * @param[in] ctx Compile context. |
| 4520 | * @param[in] aug_p Parsed augment to compile. |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4521 | * @param[in] parent Parent node to provide the augment's context. It is NULL for the top level augments and a node holding uses's |
| 4522 | * children in case of the augmenting uses data. |
| 4523 | * @return LY_SUCCESS on success. |
| 4524 | * @return LY_EVALID on failure. |
| 4525 | */ |
| 4526 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4527 | lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent) |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4528 | { |
| 4529 | LY_ERR ret = LY_SUCCESS; |
| 4530 | struct lysp_node *node_p, *case_node_p; |
| 4531 | struct lysc_node *target; /* target target of the augment */ |
| 4532 | struct lysc_node *node; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4533 | struct lysc_when **when, *when_shared; |
| 4534 | int allow_mandatory = 0; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4535 | uint16_t flags = 0; |
| 4536 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4537 | int opt_prev = ctx->options; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4538 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4539 | lysc_update_path(ctx, NULL, "{augment}"); |
| 4540 | lysc_update_path(ctx, NULL, aug_p->nodeid); |
| 4541 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4542 | ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def, |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4543 | LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4544 | 1, (const struct lysc_node**)&target, &flags); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4545 | if (ret != LY_SUCCESS) { |
| 4546 | if (ret == LY_EDENIED) { |
| 4547 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4548 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 4549 | parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 4550 | } |
| 4551 | return LY_EVALID; |
| 4552 | } |
| 4553 | |
| 4554 | /* check for mandatory nodes |
| 4555 | * - new cases augmenting some choice can have mandatory nodes |
| 4556 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 4557 | */ |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4558 | if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4559 | allow_mandatory = 1; |
| 4560 | } |
| 4561 | |
| 4562 | when_shared = NULL; |
| 4563 | LY_LIST_FOR(aug_p->child, node_p) { |
| 4564 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 4565 | if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE) |
| 4566 | && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF))) |
Radek Krejci | 2d56a89 | 2019-02-19 09:05:26 +0100 | [diff] [blame] | 4567 | && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES) |
| 4568 | && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4569 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4570 | "Invalid augment of %s node which is not allowed to contain %s node \"%s\".", |
| 4571 | lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4572 | return LY_EVALID; |
| 4573 | } |
| 4574 | |
| 4575 | /* compile the children */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4576 | ctx->options |= flags; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4577 | if (node_p->nodetype != LYS_CASE) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4578 | LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4579 | } else { |
| 4580 | LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4581 | LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4582 | } |
| 4583 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4584 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4585 | |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4586 | /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children, |
| 4587 | * here we gets the last created node as last children of our parent */ |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4588 | if (target->nodetype == LYS_CASE) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4589 | /* the compiled node is the last child of the target (but it is a case, so we have to be careful and stop) */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4590 | for (node = (struct lysc_node*)lysc_node_children(target, flags); node->next && node->next->parent == node->parent; node = node->next); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4591 | } else if (target->nodetype == LYS_CHOICE) { |
| 4592 | /* to pass when statement, we need the last case no matter if it is explicit or implicit case */ |
| 4593 | node = ((struct lysc_node_choice*)target)->cases->prev; |
| 4594 | } else { |
| 4595 | /* the compiled node is the last child of the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4596 | node = lysc_node_children(target, flags)->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4597 | } |
| 4598 | |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4599 | if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4600 | node->flags &= ~LYS_MAND_TRUE; |
| 4601 | lys_compile_mandatory_parents(target, 0); |
| 4602 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4603 | "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4604 | return LY_EVALID; |
| 4605 | } |
| 4606 | |
| 4607 | /* pass augment's when to all the children */ |
| 4608 | if (aug_p->when) { |
| 4609 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
| 4610 | if (!when_shared) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4611 | ret = lys_compile_when(ctx, aug_p->when, when); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4612 | LY_CHECK_GOTO(ret, error); |
| 4613 | (*when)->context = lysc_xpath_context(target); |
| 4614 | when_shared = *when; |
| 4615 | } else { |
| 4616 | ++when_shared->refcount; |
| 4617 | (*when) = when_shared; |
| 4618 | } |
| 4619 | } |
| 4620 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4621 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4622 | ctx->options |= flags; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4623 | switch (target->nodetype) { |
| 4624 | case LYS_CONTAINER: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4625 | COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4626 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4627 | COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4628 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4629 | break; |
| 4630 | case LYS_LIST: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4631 | COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4632 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4633 | COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4634 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4635 | break; |
| 4636 | default: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4637 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4638 | if (aug_p->actions) { |
| 4639 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4640 | "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 4641 | lys_nodetype2str(target->nodetype), aug_p->actions[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4642 | return LY_EVALID; |
| 4643 | } |
| 4644 | if (aug_p->notifs) { |
| 4645 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4646 | "Invalid augment of %s node which is not allowed to contain Notification node \"%s\".", |
| 4647 | lys_nodetype2str(target->nodetype), aug_p->notifs[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4648 | return LY_EVALID; |
| 4649 | } |
| 4650 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4651 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4652 | lysc_update_path(ctx, NULL, NULL); |
| 4653 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4654 | error: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4655 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4656 | return ret; |
| 4657 | } |
| 4658 | |
| 4659 | /** |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4660 | * @brief Apply refined or deviated mandatory flag to the target node. |
| 4661 | * |
| 4662 | * @param[in] ctx Compile context. |
| 4663 | * @param[in] node Target node where the mandatory property is supposed to be changed. |
| 4664 | * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node. |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4665 | * @param[in] refine_flag Flag to distinguish if the change is caused by refine (flag set) or deviation (for logging). |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4666 | * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations, |
| 4667 | * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure, |
| 4668 | * the tests are skipped here, but they are supposed to be performed after all the deviations are applied. |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4669 | * @return LY_ERR value. |
| 4670 | */ |
| 4671 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4672 | lys_compile_change_mandatory(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t mandatory_flag, int refine_flag) |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4673 | { |
| 4674 | if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) { |
| 4675 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4676 | "Invalid %s of mandatory - %s cannot hold mandatory statement.", |
| 4677 | refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype)); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4678 | return LY_EVALID; |
| 4679 | } |
| 4680 | |
| 4681 | if (mandatory_flag & LYS_MAND_TRUE) { |
| 4682 | /* check if node has default value */ |
| 4683 | if (node->nodetype & LYS_LEAF) { |
| 4684 | if (node->flags & LYS_SET_DFLT) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4685 | if (refine_flag) { |
| 4686 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4687 | "Invalid refine of mandatory - leaf already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4688 | return LY_EVALID; |
| 4689 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4690 | } else if (((struct lysc_node_leaf*)node)->dflt) { |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4691 | /* remove the default value taken from the leaf's type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4692 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4693 | |
| 4694 | /* update the list of incomplete default values if needed */ |
| 4695 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 4696 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4697 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4698 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4699 | free(leaf->dflt); |
| 4700 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4701 | leaf->dflt_mod = NULL; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4702 | } |
| 4703 | } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4704 | if (refine_flag) { |
| 4705 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4706 | "Invalid refine of mandatory - choice already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4707 | return LY_EVALID; |
| 4708 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4709 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4710 | if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4711 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid refine of mandatory under the default case."); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4712 | return LY_EVALID; |
| 4713 | } |
| 4714 | |
| 4715 | node->flags &= ~LYS_MAND_FALSE; |
| 4716 | node->flags |= LYS_MAND_TRUE; |
| 4717 | lys_compile_mandatory_parents(node->parent, 1); |
| 4718 | } else { |
| 4719 | /* make mandatory false */ |
| 4720 | node->flags &= ~LYS_MAND_TRUE; |
| 4721 | node->flags |= LYS_MAND_FALSE; |
| 4722 | lys_compile_mandatory_parents(node->parent, 0); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4723 | if ((node->nodetype & LYS_LEAF) && !((struct lysc_node_leaf*)node)->dflt && ((struct lysc_node_leaf*)node)->type->dflt) { |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4724 | /* get the type's default value if any */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4725 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4726 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4727 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4728 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 4729 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 4730 | leaf->dflt->realtype->refcount++; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4731 | } |
| 4732 | } |
| 4733 | return LY_SUCCESS; |
| 4734 | } |
| 4735 | |
| 4736 | /** |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4737 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 4738 | * If present, also apply uses's modificators. |
| 4739 | * |
| 4740 | * @param[in] ctx Compile context |
| 4741 | * @param[in] uses_p Parsed uses schema node. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4742 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 4743 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4744 | * the compile context. |
| 4745 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4746 | */ |
| 4747 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4748 | lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent) |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4749 | { |
| 4750 | struct lysp_node *node_p; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4751 | struct lysc_node *node, *child; |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4752 | /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4753 | struct lysc_node_container context_node_fake = |
| 4754 | {.nodetype = LYS_CONTAINER, |
| 4755 | .module = ctx->mod, |
| 4756 | .flags = parent ? parent->flags : 0, |
| 4757 | .child = NULL, .next = NULL, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4758 | .prev = (struct lysc_node*)&context_node_fake, |
| 4759 | .actions = NULL, .notifs = NULL}; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4760 | struct lysp_grp *grp = NULL; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4761 | unsigned int u, v, grp_stack_count; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4762 | int found; |
| 4763 | const char *id, *name, *prefix; |
| 4764 | size_t prefix_len, name_len; |
| 4765 | struct lys_module *mod, *mod_old; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4766 | struct lysp_refine *rfn; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4767 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4768 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4769 | uint16_t flags; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4770 | struct ly_set refined = {0}; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4771 | struct lysc_when **when, *when_shared; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4772 | struct lysp_augment **augments = NULL; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4773 | unsigned int actions_index, notifs_index; |
| 4774 | struct lysc_notif **notifs = NULL; |
| 4775 | struct lysc_action **actions = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4776 | |
| 4777 | /* search for the grouping definition */ |
| 4778 | found = 0; |
| 4779 | id = uses_p->name; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 4780 | LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4781 | if (prefix) { |
| 4782 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 4783 | if (!mod) { |
| 4784 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4785 | "Invalid prefix used for grouping reference.", uses_p->name); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4786 | return LY_EVALID; |
| 4787 | } |
| 4788 | } else { |
| 4789 | mod = ctx->mod_def; |
| 4790 | } |
| 4791 | if (mod == ctx->mod_def) { |
| 4792 | for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4793 | grp = (struct lysp_grp*)lysp_node_groupings(node_p); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4794 | LY_ARRAY_FOR(grp, u) { |
| 4795 | if (!strcmp(grp[u].name, name)) { |
| 4796 | grp = &grp[u]; |
| 4797 | found = 1; |
| 4798 | break; |
| 4799 | } |
| 4800 | } |
| 4801 | } |
| 4802 | } |
| 4803 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4804 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4805 | grp = mod->parsed->groupings; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4806 | if (grp) { |
| 4807 | for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) { |
| 4808 | if (!strcmp(grp[u].name, name)) { |
| 4809 | grp = &grp[u]; |
| 4810 | found = 1; |
| 4811 | } |
| 4812 | } |
| 4813 | } |
| 4814 | if (!found && mod->parsed->includes) { |
| 4815 | /* ... and all the submodules */ |
| 4816 | for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) { |
| 4817 | grp = mod->parsed->includes[u].submodule->groupings; |
| 4818 | if (grp) { |
| 4819 | for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) { |
| 4820 | if (!strcmp(grp[v].name, name)) { |
| 4821 | grp = &grp[v]; |
| 4822 | found = 1; |
| 4823 | } |
| 4824 | } |
| 4825 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4826 | } |
| 4827 | } |
| 4828 | } |
| 4829 | if (!found) { |
| 4830 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4831 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
| 4832 | return LY_EVALID; |
| 4833 | } |
| 4834 | |
| 4835 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 4836 | grp_stack_count = ctx->groupings.count; |
| 4837 | ly_set_add(&ctx->groupings, (void*)grp, 0); |
| 4838 | if (grp_stack_count == ctx->groupings.count) { |
| 4839 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 4840 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4841 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
| 4842 | return LY_EVALID; |
| 4843 | } |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 4844 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4845 | /* remember that the grouping is instantiated to avoid its standalone validation */ |
| 4846 | grp->flags |= LYS_USED_GRP; |
| 4847 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4848 | |
| 4849 | /* switch context's mod_def */ |
| 4850 | mod_old = ctx->mod_def; |
| 4851 | ctx->mod_def = mod; |
| 4852 | |
| 4853 | /* check status */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4854 | LY_CHECK_GOTO(lysc_check_status(ctx, uses_p->flags, mod_old, uses_p->name, grp->flags, mod, grp->name), cleanup); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4855 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4856 | /* compile data nodes */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4857 | LY_LIST_FOR(grp->data, node_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4858 | /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4859 | LY_CHECK_GOTO(lys_compile_node(ctx, node_p, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3), cleanup); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4860 | |
| 4861 | /* some preparation for applying refines */ |
| 4862 | if (grp->data == node_p) { |
| 4863 | /* remember the first child */ |
Radek Krejci | 684faf2 | 2019-05-27 14:31:16 +0200 | [diff] [blame] | 4864 | if (parent) { |
| 4865 | child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 4866 | } else if (ctx->mod->compiled->data) { |
| 4867 | child = ctx->mod->compiled->data; |
| 4868 | } else { |
| 4869 | child = NULL; |
| 4870 | } |
| 4871 | context_node_fake.child = child ? child->prev : NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4872 | } |
| 4873 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4874 | when_shared = NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4875 | LY_LIST_FOR(context_node_fake.child, child) { |
| 4876 | child->parent = (struct lysc_node*)&context_node_fake; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4877 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4878 | /* pass uses's when to all the data children, actions and notifications are ignored */ |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4879 | if (uses_p->when) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4880 | LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4881 | if (!when_shared) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4882 | LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, when), cleanup); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4883 | (*when)->context = lysc_xpath_context(parent); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4884 | when_shared = *when; |
| 4885 | } else { |
| 4886 | ++when_shared->refcount; |
| 4887 | (*when) = when_shared; |
| 4888 | } |
| 4889 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4890 | } |
| 4891 | if (context_node_fake.child) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4892 | /* child is the last data node added by grouping */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4893 | child = context_node_fake.child->prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4894 | /* fix child link of our fake container to point to the first child of the original list */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4895 | context_node_fake.child->prev = parent ? lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK)->prev : ctx->mod->compiled->data->prev; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4896 | } |
| 4897 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4898 | /* compile actions */ |
| 4899 | actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs; |
| 4900 | if (actions) { |
| 4901 | actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4902 | COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4903 | if (*actions && (uses_p->augments || uses_p->refines)) { |
| 4904 | /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */ |
| 4905 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup); |
| 4906 | LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index; |
| 4907 | memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 4908 | } |
| 4909 | } |
| 4910 | |
| 4911 | /* compile notifications */ |
| 4912 | notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs; |
| 4913 | if (notifs) { |
| 4914 | notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4915 | COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4916 | if (*notifs && (uses_p->augments || uses_p->refines)) { |
| 4917 | /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */ |
| 4918 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup); |
| 4919 | LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index; |
| 4920 | memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 4921 | } |
| 4922 | } |
| 4923 | |
| 4924 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4925 | /* sort and apply augments */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4926 | LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4927 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4928 | LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4929 | } |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4930 | |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4931 | /* reload previous context's mod_def */ |
| 4932 | ctx->mod_def = mod_old; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4933 | lysc_update_path(ctx, NULL, "{refine}"); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4934 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4935 | /* apply refine */ |
| 4936 | LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4937 | lysc_update_path(ctx, NULL, rfn->nodeid); |
| 4938 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4939 | LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, rfn->nodeid, 0, (struct lysc_node*)&context_node_fake, ctx->mod, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4940 | 0, 0, (const struct lysc_node**)&node, &flags), |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4941 | cleanup); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4942 | ly_set_add(&refined, node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4943 | |
| 4944 | /* default value */ |
| 4945 | if (rfn->dflts) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4946 | if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4947 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4948 | "Invalid refine of default - %s cannot hold %d default values.", |
| 4949 | lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4950 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4951 | } |
| 4952 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 4953 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4954 | "Invalid refine of default - %s cannot hold default value(s).", |
| 4955 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4956 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4957 | } |
| 4958 | if (node->nodetype == LYS_LEAF) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4959 | struct ly_err_item *err = NULL; |
| 4960 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 4961 | if (leaf->dflt) { |
| 4962 | /* remove the previous default value */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4963 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4964 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4965 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4966 | } else { |
| 4967 | /* prepare a new one */ |
| 4968 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4969 | leaf->dflt->realtype = leaf->type; |
| 4970 | } |
| 4971 | /* parse the new one */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4972 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4973 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]), |
| 4974 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4975 | lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4976 | leaf->dflt->realtype->refcount++; |
| 4977 | if (err) { |
| 4978 | ly_err_print(err); |
| 4979 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4980 | "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg); |
| 4981 | ly_err_free(err); |
| 4982 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4983 | if (rc == LY_EINCOMPLETE) { |
| 4984 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4985 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), cleanup); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4986 | |
| 4987 | /* but in general result is so far ok */ |
| 4988 | rc = LY_SUCCESS; |
| 4989 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4990 | LY_CHECK_GOTO(rc, cleanup); |
| 4991 | leaf->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4992 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4993 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
| 4994 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4995 | if (ctx->mod->version < 2) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4996 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4997 | "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules."); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4998 | goto cleanup; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4999 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5000 | |
| 5001 | /* remove previous set of default values */ |
| 5002 | LY_ARRAY_FOR(llist->dflts, u) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5003 | lysc_incomplete_dflts_remove(ctx, llist->dflts[u]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5004 | llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]); |
| 5005 | lysc_type_free(ctx->ctx, llist->dflts[u]->realtype); |
| 5006 | free(llist->dflts[u]); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5007 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5008 | LY_ARRAY_FREE(llist->dflts); |
| 5009 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5010 | LY_ARRAY_FREE(llist->dflts_mods); |
| 5011 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5012 | |
| 5013 | /* create the new set of the default values */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5014 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5015 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5016 | LY_ARRAY_FOR(rfn->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5017 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5018 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 5019 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5020 | LY_ARRAY_INCREMENT(llist->dflts); |
| 5021 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 5022 | llist->dflts[u]->realtype = llist->type; |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 5023 | rc = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]), |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5024 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 5025 | lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5026 | llist->dflts[u]->realtype->refcount++; |
| 5027 | if (err) { |
| 5028 | ly_err_print(err); |
| 5029 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5030 | "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg); |
| 5031 | ly_err_free(err); |
| 5032 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 5033 | if (rc == LY_EINCOMPLETE) { |
| 5034 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5035 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), cleanup); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 5036 | |
| 5037 | /* but in general result is so far ok */ |
| 5038 | rc = LY_SUCCESS; |
| 5039 | } |
| 5040 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5041 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5042 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5043 | } else if (node->nodetype == LYS_CHOICE) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5044 | if (((struct lysc_node_choice*)node)->dflt) { |
| 5045 | /* unset LYS_SET_DFLT from the current default case */ |
| 5046 | ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT; |
| 5047 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5048 | LY_CHECK_GOTO(lys_compile_node_choice_dflt(ctx, rfn->dflts[0], (struct lysc_node_choice*)node), cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5049 | } |
| 5050 | } |
| 5051 | |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5052 | /* description */ |
| 5053 | if (rfn->dsc) { |
| 5054 | FREE_STRING(ctx->ctx, node->dsc); |
| 5055 | node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0); |
| 5056 | } |
| 5057 | |
| 5058 | /* reference */ |
| 5059 | if (rfn->ref) { |
| 5060 | FREE_STRING(ctx->ctx, node->ref); |
| 5061 | node->ref = lydict_insert(ctx->ctx, rfn->ref, 0); |
| 5062 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5063 | |
| 5064 | /* config */ |
| 5065 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5066 | if (!flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5067 | LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5068 | } else { |
| 5069 | LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).", |
| 5070 | flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path); |
| 5071 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5072 | } |
| 5073 | |
| 5074 | /* mandatory */ |
| 5075 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5076 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5077 | } |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 5078 | |
| 5079 | /* presence */ |
| 5080 | if (rfn->presence) { |
| 5081 | if (node->nodetype != LYS_CONTAINER) { |
| 5082 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5083 | "Invalid refine of presence statement - %s cannot hold the presence statement.", |
| 5084 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5085 | goto cleanup; |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 5086 | } |
| 5087 | node->flags |= LYS_PRESENCE; |
| 5088 | } |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5089 | |
| 5090 | /* must */ |
| 5091 | if (rfn->musts) { |
| 5092 | switch (node->nodetype) { |
| 5093 | case LYS_LEAF: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5094 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaf*)node)->musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5095 | break; |
| 5096 | case LYS_LEAFLIST: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5097 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaflist*)node)->musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5098 | break; |
| 5099 | case LYS_LIST: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5100 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_list*)node)->musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5101 | break; |
| 5102 | case LYS_CONTAINER: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5103 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_container*)node)->musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5104 | break; |
| 5105 | case LYS_ANYXML: |
| 5106 | case LYS_ANYDATA: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5107 | COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_anydata*)node)->musts, u, lys_compile_must, ret, cleanup); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5108 | break; |
| 5109 | default: |
| 5110 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5111 | "Invalid refine of must statement - %s cannot hold any must statement.", |
| 5112 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5113 | goto cleanup; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5114 | } |
| 5115 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5116 | |
| 5117 | /* min/max-elements */ |
| 5118 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5119 | switch (node->nodetype) { |
| 5120 | case LYS_LEAFLIST: |
| 5121 | if (rfn->flags & LYS_SET_MAX) { |
| 5122 | ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5123 | } |
| 5124 | if (rfn->flags & LYS_SET_MIN) { |
| 5125 | ((struct lysc_node_leaflist*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5126 | if (rfn->min) { |
| 5127 | node->flags |= LYS_MAND_TRUE; |
| 5128 | lys_compile_mandatory_parents(node->parent, 1); |
| 5129 | } else { |
| 5130 | node->flags &= ~LYS_MAND_TRUE; |
| 5131 | lys_compile_mandatory_parents(node->parent, 0); |
| 5132 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5133 | } |
| 5134 | break; |
| 5135 | case LYS_LIST: |
| 5136 | if (rfn->flags & LYS_SET_MAX) { |
| 5137 | ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5138 | } |
| 5139 | if (rfn->flags & LYS_SET_MIN) { |
| 5140 | ((struct lysc_node_list*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5141 | if (rfn->min) { |
| 5142 | node->flags |= LYS_MAND_TRUE; |
| 5143 | lys_compile_mandatory_parents(node->parent, 1); |
| 5144 | } else { |
| 5145 | node->flags &= ~LYS_MAND_TRUE; |
| 5146 | lys_compile_mandatory_parents(node->parent, 0); |
| 5147 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5148 | } |
| 5149 | break; |
| 5150 | default: |
| 5151 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5152 | "Invalid refine of %s statement - %s cannot hold this statement.", |
| 5153 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5154 | goto cleanup; |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5155 | } |
| 5156 | } |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5157 | |
| 5158 | /* if-feature */ |
| 5159 | if (rfn->iffeatures) { |
| 5160 | /* any node in compiled tree can get additional if-feature, so do not check nodetype */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5161 | COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5162 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5163 | |
| 5164 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5165 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5166 | |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5167 | /* do some additional checks of the changed nodes when all the refines are applied */ |
| 5168 | for (u = 0; u < refined.count; ++u) { |
| 5169 | node = (struct lysc_node*)refined.objs[u]; |
| 5170 | rfn = &uses_p->refines[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5171 | lysc_update_path(ctx, NULL, rfn->nodeid); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5172 | |
| 5173 | /* check possible conflict with default value (default added, mandatory left true) */ |
| 5174 | if ((node->flags & LYS_MAND_TRUE) && |
| 5175 | (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) || |
| 5176 | ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) { |
| 5177 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5178 | "Invalid refine of default - the node is mandatory."); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5179 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5180 | } |
| 5181 | |
| 5182 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5183 | if (node->nodetype == LYS_LIST) { |
| 5184 | min = ((struct lysc_node_list*)node)->min; |
| 5185 | max = ((struct lysc_node_list*)node)->max; |
| 5186 | } else { |
| 5187 | min = ((struct lysc_node_leaflist*)node)->min; |
| 5188 | max = ((struct lysc_node_leaflist*)node)->max; |
| 5189 | } |
| 5190 | if (min > max) { |
| 5191 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5192 | "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".", |
| 5193 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5194 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5195 | } |
| 5196 | } |
| 5197 | } |
| 5198 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5199 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5200 | ret = LY_SUCCESS; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5201 | |
| 5202 | cleanup: |
| 5203 | /* fix connection of the children nodes from fake context node back into the parent */ |
| 5204 | if (context_node_fake.child) { |
| 5205 | context_node_fake.child->prev = child; |
| 5206 | } |
| 5207 | LY_LIST_FOR(context_node_fake.child, child) { |
| 5208 | child->parent = parent; |
| 5209 | } |
| 5210 | |
| 5211 | if (uses_p->augments || uses_p->refines) { |
| 5212 | /* return back actions and notifications in case they were separated for augment/refine processing */ |
Radek Krejci | 65e20e2 | 2019-04-12 09:44:37 +0200 | [diff] [blame] | 5213 | if (context_node_fake.actions) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5214 | memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 5215 | LY_ARRAY_FREE(context_node_fake.actions); |
| 5216 | } |
Radek Krejci | 65e20e2 | 2019-04-12 09:44:37 +0200 | [diff] [blame] | 5217 | if (context_node_fake.notifs) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5218 | memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 5219 | LY_ARRAY_FREE(context_node_fake.notifs); |
| 5220 | } |
| 5221 | } |
| 5222 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5223 | /* reload previous context's mod_def */ |
| 5224 | ctx->mod_def = mod_old; |
| 5225 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 5226 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 5227 | assert(ctx->groupings.count == grp_stack_count); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5228 | ly_set_erase(&refined, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5229 | LY_ARRAY_FREE(augments); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5230 | |
| 5231 | return ret; |
| 5232 | } |
| 5233 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5234 | static int |
| 5235 | lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path) |
| 5236 | { |
| 5237 | struct lysp_node *iter; |
| 5238 | int len = 0; |
| 5239 | |
| 5240 | *path = NULL; |
| 5241 | for (iter = node; iter && len >= 0; iter = iter->parent) { |
| 5242 | char *s = *path; |
| 5243 | char *id; |
| 5244 | |
| 5245 | switch (iter->nodetype) { |
| 5246 | case LYS_USES: |
| 5247 | asprintf(&id, "{uses='%s'}", iter->name); |
| 5248 | break; |
| 5249 | case LYS_GROUPING: |
| 5250 | asprintf(&id, "{grouping='%s'}", iter->name); |
| 5251 | break; |
| 5252 | case LYS_AUGMENT: |
| 5253 | asprintf(&id, "{augment='%s'}", iter->name); |
| 5254 | break; |
| 5255 | default: |
| 5256 | id = strdup(iter->name); |
| 5257 | break; |
| 5258 | } |
| 5259 | |
| 5260 | if (!iter->parent) { |
| 5261 | /* print prefix */ |
| 5262 | len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : ""); |
| 5263 | } else { |
| 5264 | /* prefix is the same as in parent */ |
| 5265 | len = asprintf(path, "/%s%s", id, s ? s : ""); |
| 5266 | } |
| 5267 | free(s); |
| 5268 | free(id); |
| 5269 | } |
| 5270 | |
| 5271 | if (len < 0) { |
| 5272 | free(*path); |
| 5273 | *path = NULL; |
| 5274 | } else if (len == 0) { |
| 5275 | *path = strdup("/"); |
| 5276 | len = 1; |
| 5277 | } |
| 5278 | return len; |
| 5279 | } |
| 5280 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5281 | /** |
| 5282 | * @brief Validate groupings that were defined but not directly used in the schema itself. |
| 5283 | * |
| 5284 | * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately), |
| 5285 | * but to have the complete result of the schema validity, even such groupings are supposed to be checked. |
| 5286 | */ |
| 5287 | static LY_ERR |
| 5288 | lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp) |
| 5289 | { |
| 5290 | LY_ERR ret; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5291 | char *path; |
| 5292 | int len; |
| 5293 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5294 | struct lysp_node_uses fake_uses = { |
| 5295 | .parent = node_p, |
| 5296 | .nodetype = LYS_USES, |
| 5297 | .flags = 0, .next = NULL, |
| 5298 | .name = grp->name, |
| 5299 | .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL, |
| 5300 | .refines = NULL, .augments = NULL |
| 5301 | }; |
| 5302 | struct lysc_node_container fake_container = { |
| 5303 | .nodetype = LYS_CONTAINER, |
| 5304 | .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0, |
| 5305 | .module = ctx->mod, |
| 5306 | .sp = NULL, .parent = NULL, .next = NULL, |
| 5307 | .prev = (struct lysc_node*)&fake_container, |
| 5308 | .name = "fake", |
| 5309 | .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL, |
| 5310 | .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL |
| 5311 | }; |
| 5312 | |
| 5313 | if (grp->parent) { |
| 5314 | LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name); |
| 5315 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5316 | |
| 5317 | len = lys_compile_grouping_pathlog(ctx, grp->parent, &path); |
| 5318 | if (len < 0) { |
| 5319 | LOGMEM(ctx->ctx); |
| 5320 | return LY_EMEM; |
| 5321 | } |
| 5322 | strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1); |
| 5323 | ctx->path_len = (uint16_t)len; |
| 5324 | free(path); |
| 5325 | |
| 5326 | lysc_update_path(ctx, NULL, "{grouping}"); |
| 5327 | lysc_update_path(ctx, NULL, grp->name); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5328 | ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5329 | lysc_update_path(ctx, NULL, NULL); |
| 5330 | lysc_update_path(ctx, NULL, NULL); |
| 5331 | |
| 5332 | ctx->path_len = 1; |
| 5333 | ctx->path[1] = '\0'; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5334 | |
| 5335 | /* cleanup */ |
| 5336 | lysc_node_container_free(ctx->ctx, &fake_container); |
| 5337 | |
| 5338 | return ret; |
| 5339 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5340 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5341 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5342 | * @brief Compile parsed schema node information. |
| 5343 | * @param[in] ctx Compile context |
| 5344 | * @param[in] node_p Parsed schema node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5345 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 5346 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 5347 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5348 | * @param[in] uses_status If the node is being placed instead of uses, here we have the uses's status value (as node's flags). |
| 5349 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5350 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 5351 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5352 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5353 | lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *parent, uint16_t uses_status) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5354 | { |
| 5355 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5356 | struct lysc_node *node; |
| 5357 | struct lysc_node_case *cs; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5358 | struct lysc_when **when; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5359 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5360 | LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5361 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5362 | if (node_p->nodetype != LYS_USES) { |
| 5363 | lysc_update_path(ctx, parent, node_p->name); |
| 5364 | } else { |
| 5365 | lysc_update_path(ctx, NULL, "{uses}"); |
| 5366 | lysc_update_path(ctx, NULL, node_p->name); |
| 5367 | } |
| 5368 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5369 | switch (node_p->nodetype) { |
| 5370 | case LYS_CONTAINER: |
| 5371 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container)); |
| 5372 | node_compile_spec = lys_compile_node_container; |
| 5373 | break; |
| 5374 | case LYS_LEAF: |
| 5375 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf)); |
| 5376 | node_compile_spec = lys_compile_node_leaf; |
| 5377 | break; |
| 5378 | case LYS_LIST: |
| 5379 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5380 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5381 | break; |
| 5382 | case LYS_LEAFLIST: |
| 5383 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5384 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5385 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5386 | case LYS_CHOICE: |
| 5387 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5388 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5389 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5390 | case LYS_ANYXML: |
| 5391 | case LYS_ANYDATA: |
| 5392 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 5393 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5394 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5395 | case LYS_USES: |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5396 | ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent); |
| 5397 | lysc_update_path(ctx, NULL, NULL); |
| 5398 | lysc_update_path(ctx, NULL, NULL); |
| 5399 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5400 | default: |
| 5401 | LOGINT(ctx->ctx); |
| 5402 | return LY_EINT; |
| 5403 | } |
| 5404 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 5405 | node->nodetype = node_p->nodetype; |
| 5406 | node->module = ctx->mod; |
| 5407 | node->prev = node; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5408 | node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5409 | |
| 5410 | /* config */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5411 | if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5412 | /* ignore config statements inside RPC/action data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5413 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5414 | node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R; |
| 5415 | } else if (ctx->options & LYSC_OPT_NOTIFICATION) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5416 | /* ignore config statements inside Notification data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5417 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5418 | node->flags |= LYS_CONFIG_R; |
| 5419 | } else if (!(node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5420 | /* config not explicitely set, inherit it from parent */ |
| 5421 | if (parent) { |
| 5422 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 5423 | } else { |
| 5424 | /* default is config true */ |
| 5425 | node->flags |= LYS_CONFIG_W; |
| 5426 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5427 | } else { |
| 5428 | /* config set explicitely */ |
| 5429 | node->flags |= LYS_SET_CONFIG; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5430 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5431 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 5432 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5433 | "Configuration node cannot be child of any state data node."); |
| 5434 | goto error; |
| 5435 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5436 | |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5437 | /* *list ordering */ |
| 5438 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 5439 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5440 | LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).", |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5441 | (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" : |
| 5442 | (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5443 | node->flags &= ~LYS_ORDBY_MASK; |
| 5444 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5445 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 5446 | /* default ordering is system */ |
| 5447 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5448 | } |
| 5449 | } |
| 5450 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5451 | /* status - it is not inherited by specification, but it does not make sense to have |
| 5452 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5453 | if (!parent || parent->nodetype != LYS_CHOICE) { |
| 5454 | /* in case of choice/case's children, postpone the check to the moment we know if |
| 5455 | * the parent is choice (parent here) or some case (so we have to get its flags to check) */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5456 | LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, uses_status ? uses_status : (parent ? parent->flags : 0)), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5457 | } |
| 5458 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5459 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5460 | node->sp = node_p; |
| 5461 | } |
| 5462 | DUP_STRING(ctx->ctx, node_p->name, node->name); |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5463 | DUP_STRING(ctx->ctx, node_p->dsc, node->dsc); |
| 5464 | DUP_STRING(ctx->ctx, node_p->ref, node->ref); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5465 | if (node_p->when) { |
| 5466 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5467 | ret = lys_compile_when(ctx, node_p->when, when); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5468 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 5469 | (*when)->context = lysc_xpath_context(node); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5470 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5471 | COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5472 | |
| 5473 | /* nodetype-specific part */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5474 | LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5475 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 5476 | COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error); |
| 5477 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5478 | /* inherit LYS_MAND_TRUE in parent containers */ |
| 5479 | if (node->flags & LYS_MAND_TRUE) { |
| 5480 | lys_compile_mandatory_parents(parent, 1); |
| 5481 | } |
| 5482 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5483 | lysc_update_path(ctx, NULL, NULL); |
| 5484 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5485 | /* insert into parent's children */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5486 | if (parent) { |
| 5487 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5488 | if (node_p->parent->nodetype == LYS_CASE) { |
| 5489 | lysc_update_path(ctx, parent, node_p->parent->name); |
| 5490 | } else { |
| 5491 | lysc_update_path(ctx, parent, node->name); |
| 5492 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5493 | cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5494 | LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5495 | if (uses_status) { |
| 5496 | |
| 5497 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5498 | /* the postponed status check of the node and its real parent - in case of implicit case, |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5499 | * it directly gets the same status flags as the choice; |
| 5500 | * uses_status cannot be applied here since uses cannot be child statement of choice */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5501 | LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5502 | node->parent = (struct lysc_node*)cs; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5503 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5504 | } else { /* other than choice */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5505 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5506 | node->parent = parent; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5507 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5508 | LY_CHECK_RET(lys_compile_node_connect(ctx, parent->nodetype == LYS_CASE ? parent->parent : parent, node), LY_EVALID); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5509 | |
| 5510 | if (parent->nodetype == LYS_CHOICE) { |
| 5511 | lysc_update_path(ctx, NULL, NULL); |
| 5512 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5513 | } else { |
| 5514 | /* top-level element */ |
| 5515 | if (!ctx->mod->compiled->data) { |
| 5516 | ctx->mod->compiled->data = node; |
| 5517 | } else { |
| 5518 | /* insert at the end of the module's top-level nodes list */ |
| 5519 | ctx->mod->compiled->data->prev->next = node; |
| 5520 | node->prev = ctx->mod->compiled->data->prev; |
| 5521 | ctx->mod->compiled->data->prev = node; |
| 5522 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5523 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5524 | if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs, |
| 5525 | ctx->mod->compiled->notifs, node->name, node)) { |
| 5526 | return LY_EVALID; |
| 5527 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5528 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5529 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5530 | |
| 5531 | return LY_SUCCESS; |
| 5532 | |
| 5533 | error: |
| 5534 | lysc_node_free(ctx->ctx, node); |
| 5535 | return ret; |
| 5536 | } |
| 5537 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5538 | static void |
| 5539 | lysc_disconnect(struct lysc_node *node) |
| 5540 | { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5541 | struct lysc_node *parent, *child, *prev = NULL, *next; |
| 5542 | struct lysc_node_case *cs = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5543 | int remove_cs = 0; |
| 5544 | |
| 5545 | parent = node->parent; |
| 5546 | |
| 5547 | /* parent's first child */ |
| 5548 | if (!parent) { |
| 5549 | return; |
| 5550 | } |
| 5551 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5552 | cs = (struct lysc_node_case*)node; |
| 5553 | } else if (parent->nodetype == LYS_CASE) { |
| 5554 | /* disconnecting some node in a case */ |
| 5555 | cs = (struct lysc_node_case*)parent; |
| 5556 | parent = cs->parent; |
| 5557 | for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) { |
| 5558 | if (child == node) { |
| 5559 | if (cs->child == child) { |
| 5560 | if (!child->next || child->next->parent != (struct lysc_node*)cs) { |
| 5561 | /* case with a single child -> remove also the case */ |
| 5562 | child->parent = NULL; |
| 5563 | remove_cs = 1; |
| 5564 | } else { |
| 5565 | cs->child = child->next; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5566 | } |
| 5567 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5568 | break; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5569 | } |
| 5570 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5571 | if (!remove_cs) { |
| 5572 | cs = NULL; |
| 5573 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5574 | } else if (lysc_node_children(parent, node->flags) == node) { |
| 5575 | *lysc_node_children_p(parent, node->flags) = node->next; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5576 | } |
| 5577 | |
| 5578 | if (cs) { |
| 5579 | if (remove_cs) { |
| 5580 | /* cs has only one child which is being also removed */ |
| 5581 | lysc_disconnect((struct lysc_node*)cs); |
| 5582 | lysc_node_free(cs->module->ctx, (struct lysc_node*)cs); |
| 5583 | } else { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5584 | if (((struct lysc_node_choice*)parent)->dflt == cs) { |
| 5585 | /* default case removed */ |
| 5586 | ((struct lysc_node_choice*)parent)->dflt = NULL; |
| 5587 | } |
| 5588 | if (((struct lysc_node_choice*)parent)->cases == cs) { |
| 5589 | /* first case removed */ |
| 5590 | ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next; |
| 5591 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5592 | if (cs->child) { |
| 5593 | /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */ |
| 5594 | if (cs->child->prev->parent != (struct lysc_node*)cs) { |
| 5595 | prev = cs->child->prev; |
| 5596 | } /* else all the children are under a single case */ |
| 5597 | LY_LIST_FOR_SAFE(cs->child, next, child) { |
| 5598 | if (child->parent != (struct lysc_node*)cs) { |
| 5599 | break; |
| 5600 | } |
| 5601 | lysc_node_free(node->module->ctx, child); |
| 5602 | } |
| 5603 | if (prev) { |
| 5604 | if (prev->next) { |
| 5605 | prev->next = child; |
| 5606 | } |
| 5607 | if (child) { |
| 5608 | child->prev = prev; |
| 5609 | } else { |
| 5610 | /* link from the first child under the cases */ |
| 5611 | ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev; |
| 5612 | } |
| 5613 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5614 | } |
| 5615 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5616 | } |
| 5617 | |
| 5618 | /* siblings */ |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5619 | if (node->prev->next) { |
| 5620 | node->prev->next = node->next; |
| 5621 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5622 | if (node->next) { |
| 5623 | node->next->prev = node->prev; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5624 | } else if (node->nodetype != LYS_CASE) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5625 | child = (struct lysc_node*)lysc_node_children(parent, node->flags); |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5626 | if (child) { |
| 5627 | child->prev = node->prev; |
| 5628 | } |
| 5629 | } else if (((struct lysc_node_choice*)parent)->cases) { |
| 5630 | ((struct lysc_node_choice*)parent)->cases->prev = node->prev; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5631 | } |
| 5632 | } |
| 5633 | |
| 5634 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5635 | lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5636 | { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5637 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5638 | struct ly_set devs_p = {0}; |
| 5639 | struct ly_set targets = {0}; |
| 5640 | struct lysc_node *target; /* target target of the deviation */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5641 | struct lysc_node_list *list; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5642 | struct lysc_action *rpcs; |
| 5643 | struct lysc_notif *notifs; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5644 | struct lysp_deviation *dev; |
| 5645 | struct lysp_deviate *d, **dp_new; |
| 5646 | struct lysp_deviate_add *d_add; |
| 5647 | struct lysp_deviate_del *d_del; |
| 5648 | struct lysp_deviate_rpl *d_rpl; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5649 | unsigned int u, v, x, y, z; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5650 | struct lysc_deviation { |
| 5651 | const char *nodeid; |
| 5652 | struct lysc_node *target; /* target node of the deviation */ |
| 5653 | struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5654 | uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5655 | uint8_t not_supported; /* flag if deviates contains not-supported deviate */ |
| 5656 | } **devs = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5657 | int i, changed_type; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5658 | size_t prefix_len, name_len; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5659 | const char *prefix, *name, *nodeid, *dflt; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5660 | struct lys_module *mod; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5661 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5662 | uint16_t flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5663 | |
| 5664 | /* get all deviations from the module and all its submodules ... */ |
| 5665 | LY_ARRAY_FOR(mod_p->deviations, u) { |
| 5666 | ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST); |
| 5667 | } |
| 5668 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 5669 | LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) { |
| 5670 | ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST); |
| 5671 | } |
| 5672 | } |
| 5673 | if (!devs_p.count) { |
| 5674 | /* nothing to do */ |
| 5675 | return LY_SUCCESS; |
| 5676 | } |
| 5677 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5678 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 5679 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5680 | /* ... and group them by the target node */ |
| 5681 | devs = calloc(devs_p.count, sizeof *devs); |
| 5682 | for (u = 0; u < devs_p.count; ++u) { |
| 5683 | dev = devs_p.objs[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5684 | lysc_update_path(ctx, NULL, dev->nodeid); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5685 | |
| 5686 | /* resolve the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5687 | LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1, |
| 5688 | (const struct lysc_node**)&target, &flags), cleanup); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5689 | if (target->nodetype == LYS_ACTION) { |
| 5690 | /* move the target pointer to input/output to make them different from the action and |
| 5691 | * between them. Before the devs[] item is being processed, the target pointer must be fixed |
| 5692 | * back to the RPC/action node due to a better compatibility and decision code in this function. |
| 5693 | * The LYSC_OPT_INTERNAL is used as a flag to this change. */ |
| 5694 | if (flags & LYSC_OPT_RPC_INPUT) { |
| 5695 | target = (struct lysc_node*)&((struct lysc_action*)target)->input; |
| 5696 | flags |= LYSC_OPT_INTERNAL; |
| 5697 | } else if (flags & LYSC_OPT_RPC_OUTPUT) { |
| 5698 | target = (struct lysc_node*)&((struct lysc_action*)target)->output; |
| 5699 | flags |= LYSC_OPT_INTERNAL; |
| 5700 | } |
| 5701 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5702 | /* insert into the set of targets with duplicity detection */ |
| 5703 | i = ly_set_add(&targets, target, 0); |
| 5704 | if (!devs[i]) { |
| 5705 | /* new record */ |
| 5706 | devs[i] = calloc(1, sizeof **devs); |
| 5707 | devs[i]->target = target; |
| 5708 | devs[i]->nodeid = dev->nodeid; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5709 | devs[i]->flags = flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5710 | } |
| 5711 | /* add deviates into the deviation's list of deviates */ |
| 5712 | for (d = dev->deviates; d; d = d->next) { |
| 5713 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup); |
| 5714 | *dp_new = d; |
| 5715 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
| 5716 | devs[i]->not_supported = 1; |
| 5717 | } |
| 5718 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5719 | |
| 5720 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5721 | } |
| 5722 | |
| 5723 | /* MACROS for deviates checking */ |
| 5724 | #define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \ |
| 5725 | if (!(devs[u]->target->nodetype & (NODETYPES))) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5726 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, lys_nodetype2str(devs[u]->target->nodetype), DEVTYPE, PROPERTY);\ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5727 | goto cleanup; \ |
| 5728 | } |
| 5729 | |
| 5730 | #define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \ |
| 5731 | if (LY_ARRAY_SIZE(ARRAY) > MAX) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5732 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%u) %s properties.", \ |
| 5733 | lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5734 | goto cleanup; \ |
| 5735 | } |
| 5736 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5737 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5738 | #define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5739 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
| 5740 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5741 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \ |
| 5742 | PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \ |
| 5743 | goto cleanup; \ |
| 5744 | } |
| 5745 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5746 | #define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5747 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5748 | int dynamic_ = 0; const char *val_; \ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5749 | val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \ |
| 5750 | lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5751 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5752 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \ |
| 5753 | if (dynamic_) {free((void*)val_);} \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5754 | goto cleanup; \ |
| 5755 | } |
| 5756 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5757 | #define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5758 | if (((TYPE)devs[u]->target)->MEMBER COND) { \ |
| 5759 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5760 | "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \ |
| 5761 | PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5762 | goto cleanup; \ |
| 5763 | } |
| 5764 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5765 | #define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 5766 | if (!((TYPE)devs[u]->target)->MEMBER || COND) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5767 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5768 | goto cleanup; \ |
| 5769 | } |
| 5770 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5771 | #define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5772 | if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \ |
| 5773 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5774 | "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5775 | goto cleanup; \ |
| 5776 | } |
| 5777 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5778 | #define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \ |
| 5779 | DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \ |
| 5780 | LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \ |
| 5781 | LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \ |
| 5782 | if (!strcmp(((TYPE)devs[u]->target)->ARRAY_TRG[y]VALMEMBER_CMP, d_del->ARRAY_DEV[x]VALMEMBER)) { break; } \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5783 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5784 | if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5785 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5786 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \ |
| 5787 | PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5788 | goto cleanup; \ |
| 5789 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5790 | LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5791 | DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \ |
| 5792 | memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \ |
| 5793 | &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \ |
| 5794 | (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5795 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5796 | if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
| 5797 | LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5798 | ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5799 | } |
| 5800 | |
| 5801 | /* apply deviations */ |
| 5802 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5803 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target; |
| 5804 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target; |
| 5805 | struct ly_err_item *err = NULL; |
| 5806 | |
| 5807 | dflt = NULL; |
| 5808 | changed_type = 0; |
| 5809 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5810 | lysc_update_path(ctx, NULL, devs[u]->nodeid); |
| 5811 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5812 | if (devs[u]->flags & LYSC_OPT_INTERNAL) { |
| 5813 | /* fix the target pointer in case of RPC's/action's input/output */ |
| 5814 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5815 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input)); |
| 5816 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5817 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output)); |
| 5818 | } |
| 5819 | } |
| 5820 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5821 | /* not-supported */ |
| 5822 | if (devs[u]->not_supported) { |
| 5823 | if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) { |
| 5824 | LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.", |
| 5825 | LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid); |
| 5826 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5827 | |
| 5828 | #define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \ |
| 5829 | if (devs[u]->target->parent) { \ |
| 5830 | ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \ |
| 5831 | } else { \ |
| 5832 | ARRAY = devs[u]->target->module->compiled->ARRAY; \ |
| 5833 | } \ |
| 5834 | LY_ARRAY_FOR(ARRAY, x) { \ |
| 5835 | if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \ |
| 5836 | } \ |
| 5837 | if (x < LY_ARRAY_SIZE(ARRAY)) { \ |
| 5838 | FREEFUNC(ctx->ctx, &ARRAY[x]); \ |
| 5839 | memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \ |
| 5840 | LY_ARRAY_DECREMENT(ARRAY); \ |
| 5841 | } |
| 5842 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5843 | if (devs[u]->target->nodetype == LYS_ACTION) { |
| 5844 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5845 | /* remove RPC's/action's input */ |
| 5846 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input); |
| 5847 | memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5848 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free); |
| 5849 | ((struct lysc_action*)devs[u]->target)->input_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5850 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5851 | /* remove RPC's/action's output */ |
| 5852 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output); |
| 5853 | memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5854 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free); |
| 5855 | ((struct lysc_action*)devs[u]->target)->output_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5856 | } else { |
| 5857 | /* remove RPC/action */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5858 | REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5859 | } |
| 5860 | } else if (devs[u]->target->nodetype == LYS_NOTIF) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5861 | /* remove Notification */ |
| 5862 | REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5863 | } else { |
| 5864 | /* remove the target node */ |
| 5865 | lysc_disconnect(devs[u]->target); |
| 5866 | lysc_node_free(ctx->ctx, devs[u]->target); |
| 5867 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5868 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5869 | /* mark the context for later re-compilation of objects that could reference the curently removed node */ |
| 5870 | ctx->ctx->flags |= LY_CTX_CHANGED_TREE; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5871 | continue; |
| 5872 | } |
| 5873 | |
| 5874 | /* list of deviates (not-supported is not present in the list) */ |
| 5875 | LY_ARRAY_FOR(devs[u]->deviates, v) { |
| 5876 | d = devs[u]->deviates[v]; |
| 5877 | |
| 5878 | switch (d->mod) { |
| 5879 | case LYS_DEV_ADD: |
| 5880 | d_add = (struct lysp_deviate_add*)d; |
| 5881 | /* [units-stmt] */ |
| 5882 | if (d_add->units) { |
| 5883 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units"); |
| 5884 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units); |
| 5885 | |
| 5886 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5887 | DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5888 | } |
| 5889 | |
| 5890 | /* *must-stmt */ |
| 5891 | if (d_add->musts) { |
| 5892 | switch (devs[u]->target->nodetype) { |
| 5893 | case LYS_CONTAINER: |
| 5894 | case LYS_LIST: |
| 5895 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5896 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5897 | break; |
| 5898 | case LYS_LEAF: |
| 5899 | case LYS_LEAFLIST: |
| 5900 | case LYS_ANYDATA: |
| 5901 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5902 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5903 | break; |
| 5904 | case LYS_NOTIF: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5905 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5906 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5907 | break; |
| 5908 | case LYS_ACTION: |
| 5909 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5910 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5911 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5912 | break; |
| 5913 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5914 | COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5915 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5916 | break; |
| 5917 | } |
| 5918 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5919 | default: |
| 5920 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5921 | lys_nodetype2str(devs[u]->target->nodetype), "add", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5922 | goto cleanup; |
| 5923 | } |
| 5924 | } |
| 5925 | |
| 5926 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5927 | if (d_add->uniques) { |
| 5928 | DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique"); |
| 5929 | LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup); |
| 5930 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5931 | |
| 5932 | /* *default-stmt */ |
| 5933 | if (d_add->dflts) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5934 | switch (devs[u]->target->nodetype) { |
| 5935 | case LYS_LEAF: |
| 5936 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5937 | DEV_CHECK_NONPRESENCE_VALUE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_DFLT), dflt, "default", dflt, dflt_mod); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5938 | if (leaf->dflt) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5939 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5940 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5941 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 5942 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 5943 | } else { |
| 5944 | /* prepare new default value storage */ |
| 5945 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5946 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5947 | dflt = d_add->dflts[0]; |
| 5948 | /* parsing is done at the end after possible replace of the leaf's type */ |
| 5949 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5950 | /* mark the new default values as leaf's own */ |
| 5951 | devs[u]->target->flags |= LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5952 | break; |
| 5953 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5954 | if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5955 | /* first, remove the default value taken from the type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5956 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5957 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5958 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 5959 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 5960 | free(llist->dflts[x]); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5961 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5962 | LY_ARRAY_FREE(llist->dflts); |
| 5963 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5964 | LY_ARRAY_FREE(llist->dflts_mods); |
| 5965 | llist->dflts_mods = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5966 | } |
| 5967 | /* add new default value(s) */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5968 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5969 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup); |
| 5970 | for (x = y = LY_ARRAY_SIZE(llist->dflts); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5971 | x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5972 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 5973 | llist->dflts_mods[x] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5974 | LY_ARRAY_INCREMENT(llist->dflts); |
| 5975 | llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]); |
| 5976 | llist->dflts[x]->realtype = llist->type; |
| 5977 | rc = llist->type->plugin->store(ctx->ctx, llist->type, d_add->dflts[x - y], strlen(d_add->dflts[x - y]), |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5978 | LY_TYPE_OPTS_INCOMPLETE_DATA |LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, lys_resolve_prefix, |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 5979 | (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, llist->dflts[x], NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5980 | llist->dflts[x]->realtype->refcount++; |
| 5981 | if (err) { |
| 5982 | ly_err_print(err); |
| 5983 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5984 | "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).", |
| 5985 | d_add->dflts[x - y], err->msg); |
| 5986 | ly_err_free(err); |
| 5987 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5988 | if (rc == LY_EINCOMPLETE) { |
| 5989 | /* postpone default compilation when the tree is complete */ |
| 5990 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 5991 | |
| 5992 | /* but in general result is so far ok */ |
| 5993 | rc = LY_SUCCESS; |
| 5994 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5995 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5996 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5997 | /* mark the new default values as leaf-list's own */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5998 | devs[u]->target->flags |= LYS_SET_DFLT; |
| 5999 | break; |
| 6000 | case LYS_CHOICE: |
| 6001 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
| 6002 | DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name); |
| 6003 | /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation |
| 6004 | * to allow making the default case even the augmented case from the deviating module */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6005 | if (lys_compile_deviation_set_choice_dflt(ctx, d_add->dflts[0], (struct lysc_node_choice*)devs[u]->target)) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6006 | goto cleanup; |
| 6007 | } |
| 6008 | break; |
| 6009 | default: |
| 6010 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6011 | lys_nodetype2str(devs[u]->target->nodetype), "add", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6012 | goto cleanup; |
| 6013 | } |
| 6014 | } |
| 6015 | |
| 6016 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6017 | if (d_add->flags & LYS_CONFIG_MASK) { |
| 6018 | if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6019 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6020 | lys_nodetype2str(devs[u]->target->nodetype), "add", "config"); |
| 6021 | goto cleanup; |
| 6022 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6023 | if (devs[u]->flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6024 | LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.", |
| 6025 | devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6026 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6027 | if (devs[u]->target->flags & LYS_SET_CONFIG) { |
| 6028 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6029 | "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").", |
| 6030 | devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6031 | goto cleanup; |
| 6032 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6033 | LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6034 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6035 | |
| 6036 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6037 | if (d_add->flags & LYS_MAND_MASK) { |
| 6038 | if (devs[u]->target->flags & LYS_MAND_MASK) { |
| 6039 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6040 | "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 6041 | devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false"); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6042 | goto cleanup; |
| 6043 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6044 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6045 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6046 | |
| 6047 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6048 | if (d_add->flags & LYS_SET_MIN) { |
| 6049 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6050 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 6051 | /* change value */ |
| 6052 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min; |
| 6053 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6054 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 6055 | /* change value */ |
| 6056 | ((struct lysc_node_list*)devs[u]->target)->min = d_add->min; |
| 6057 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6058 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6059 | lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements"); |
| 6060 | goto cleanup; |
| 6061 | } |
| 6062 | if (d_add->min) { |
| 6063 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 6064 | } |
| 6065 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6066 | |
| 6067 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6068 | if (d_add->flags & LYS_SET_MAX) { |
| 6069 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6070 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 6071 | /* change value */ |
| 6072 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 6073 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6074 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 6075 | /* change value */ |
| 6076 | ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 6077 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6078 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6079 | lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements"); |
| 6080 | goto cleanup; |
| 6081 | } |
| 6082 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6083 | |
| 6084 | break; |
| 6085 | case LYS_DEV_DELETE: |
| 6086 | d_del = (struct lysp_deviate_del*)d; |
| 6087 | |
| 6088 | /* [units-stmt] */ |
| 6089 | if (d_del->units) { |
| 6090 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units"); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6091 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units); |
| 6092 | if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) { |
| 6093 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6094 | "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6095 | d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6096 | goto cleanup; |
| 6097 | } |
| 6098 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6099 | ((struct lysc_node_leaf*)devs[u]->target)->units = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6100 | } |
| 6101 | |
| 6102 | /* *must-stmt */ |
| 6103 | if (d_del->musts) { |
| 6104 | switch (devs[u]->target->nodetype) { |
| 6105 | case LYS_CONTAINER: |
| 6106 | case LYS_LIST: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6107 | DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6108 | break; |
| 6109 | case LYS_LEAF: |
| 6110 | case LYS_LEAFLIST: |
| 6111 | case LYS_ANYDATA: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6112 | DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6113 | break; |
| 6114 | case LYS_NOTIF: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6115 | DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6116 | break; |
| 6117 | case LYS_ACTION: |
| 6118 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 6119 | DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 6120 | break; |
| 6121 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 6122 | DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 6123 | break; |
| 6124 | } |
| 6125 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6126 | default: |
| 6127 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6128 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6129 | goto cleanup; |
| 6130 | } |
| 6131 | } |
| 6132 | |
| 6133 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6134 | if (d_del->uniques) { |
| 6135 | DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique"); |
| 6136 | list = (struct lysc_node_list*)devs[u]->target; /* shortcut */ |
| 6137 | LY_ARRAY_FOR(d_del->uniques, x) { |
| 6138 | LY_ARRAY_FOR(list->uniques, z) { |
| 6139 | for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) { |
| 6140 | nodeid = strpbrk(name, " \t\n"); |
| 6141 | if (nodeid) { |
| 6142 | if (strncmp(name, list->uniques[z][y]->name, nodeid - name) |
| 6143 | || list->uniques[z][y]->name[nodeid - name] != '\0') { |
| 6144 | break; |
| 6145 | } |
| 6146 | while (isspace(*nodeid)) { |
| 6147 | ++nodeid; |
| 6148 | } |
| 6149 | } else { |
| 6150 | if (strcmp(name, list->uniques[z][y]->name)) { |
| 6151 | break; |
| 6152 | } |
| 6153 | } |
| 6154 | } |
| 6155 | if (!name) { |
| 6156 | /* complete match - remove the unique */ |
| 6157 | LY_ARRAY_DECREMENT(list->uniques); |
| 6158 | LY_ARRAY_FREE(list->uniques[z]); |
| 6159 | memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques)); |
| 6160 | --z; |
| 6161 | break; |
| 6162 | } |
| 6163 | } |
| 6164 | if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) { |
| 6165 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6166 | "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.", |
| 6167 | d_del->uniques[x]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6168 | goto cleanup; |
| 6169 | } |
| 6170 | } |
| 6171 | if (!LY_ARRAY_SIZE(list->uniques)) { |
| 6172 | LY_ARRAY_FREE(list->uniques); |
| 6173 | list->uniques = NULL; |
| 6174 | } |
| 6175 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6176 | |
| 6177 | /* *default-stmt */ |
| 6178 | if (d_del->dflts) { |
| 6179 | switch (devs[u]->target->nodetype) { |
| 6180 | case LYS_LEAF: |
| 6181 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6182 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6183 | dflt, "deleting", "default", d_del->dflts[0]); |
| 6184 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6185 | /* check that the values matches */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6186 | dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &i); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6187 | if (strcmp(dflt, d_del->dflts[0])) { |
| 6188 | if (i) { |
| 6189 | free((char*)dflt); |
| 6190 | } |
| 6191 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6192 | "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6193 | d_del->dflts[0], dflt); |
| 6194 | goto cleanup; |
| 6195 | } |
| 6196 | if (i) { |
| 6197 | free((char*)dflt); |
| 6198 | } |
| 6199 | dflt = NULL; |
| 6200 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6201 | /* update the list of incomplete default values if needed */ |
| 6202 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6203 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6204 | /* remove the default specification */ |
| 6205 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6206 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6207 | free(leaf->dflt); |
| 6208 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6209 | leaf->dflt_mod = NULL; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6210 | devs[u]->target->flags &= ~LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6211 | break; |
| 6212 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6213 | DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]); |
| 6214 | LY_ARRAY_FOR(d_del->dflts, x) { |
| 6215 | LY_ARRAY_FOR(llist->dflts, y) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6216 | dflt = llist->type->plugin->print(llist->dflts[y], LYD_XML, lys_get_prefix, llist->dflts_mods[y], &i); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6217 | if (!strcmp(dflt, d_del->dflts[x])) { |
| 6218 | if (i) { |
| 6219 | free((char*)dflt); |
| 6220 | } |
| 6221 | dflt = NULL; |
| 6222 | break; |
| 6223 | } |
| 6224 | if (i) { |
| 6225 | free((char*)dflt); |
| 6226 | } |
| 6227 | dflt = NULL; |
| 6228 | } |
| 6229 | if (y == LY_ARRAY_SIZE(llist->dflts)) { |
| 6230 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" " |
| 6231 | "which does not match any of the target's property values.", d_del->dflts[x]); |
| 6232 | goto cleanup; |
| 6233 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6234 | |
| 6235 | /* update the list of incomplete default values if needed */ |
| 6236 | lysc_incomplete_dflts_remove(ctx, llist->dflts[y]); |
| 6237 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6238 | LY_ARRAY_DECREMENT(llist->dflts_mods); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6239 | LY_ARRAY_DECREMENT(llist->dflts); |
| 6240 | llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]); |
| 6241 | lysc_type_free(ctx->ctx, llist->dflts[y]->realtype); |
| 6242 | free(llist->dflts[y]); |
| 6243 | memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_SIZE(llist->dflts) - y) * (sizeof *llist->dflts)); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6244 | memmove(&llist->dflts_mods[y], &llist->dflts_mods[y + 1], (LY_ARRAY_SIZE(llist->dflts_mods) - y) * (sizeof *llist->dflts_mods)); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6245 | } |
| 6246 | if (!LY_ARRAY_SIZE(llist->dflts)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6247 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6248 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6249 | LY_ARRAY_FREE(llist->dflts); |
| 6250 | llist->dflts = NULL; |
| 6251 | llist->flags &= ~LYS_SET_DFLT; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6252 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6253 | break; |
| 6254 | case LYS_CHOICE: |
| 6255 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6256 | DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]); |
| 6257 | nodeid = d_del->dflts[0]; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 6258 | LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6259 | if (prefix) { |
| 6260 | /* use module prefixes from the deviation module to match the module of the default case */ |
| 6261 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 6262 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6263 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6264 | "The prefix does not match any imported module of the deviation module.", d_del->dflts[0]); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6265 | goto cleanup; |
| 6266 | } |
| 6267 | if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) { |
| 6268 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6269 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6270 | "The prefix does not match the default case's module.", d_del->dflts[0]); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6271 | goto cleanup; |
| 6272 | } |
| 6273 | } |
| 6274 | /* else { |
| 6275 | * strictly, the default prefix would point to the deviation module, but the value should actually |
| 6276 | * match the default string in the original module (usually unprefixed), so in this case we do not check |
| 6277 | * the module of the default case, just matching its name */ |
| 6278 | if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) { |
| 6279 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6280 | "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".", |
| 6281 | d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6282 | goto cleanup; |
| 6283 | } |
| 6284 | ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT; |
| 6285 | ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL; |
| 6286 | break; |
| 6287 | default: |
| 6288 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6289 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6290 | goto cleanup; |
| 6291 | } |
| 6292 | } |
| 6293 | |
| 6294 | break; |
| 6295 | case LYS_DEV_REPLACE: |
| 6296 | d_rpl = (struct lysp_deviate_rpl*)d; |
| 6297 | |
| 6298 | /* [type-stmt] */ |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6299 | if (d_rpl->type) { |
| 6300 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type"); |
| 6301 | /* type is mandatory, so checking for its presence is not necessary */ |
| 6302 | lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6303 | |
| 6304 | if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
| 6305 | /* the target has default from the previous type - remove it */ |
| 6306 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6307 | /* update the list of incomplete default values if needed */ |
| 6308 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6309 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6310 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6311 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6312 | free(leaf->dflt); |
| 6313 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6314 | leaf->dflt_mod = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6315 | } else { /* LYS_LEAFLIST */ |
| 6316 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6317 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6318 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6319 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6320 | free(llist->dflts[x]); |
| 6321 | } |
| 6322 | LY_ARRAY_FREE(llist->dflts); |
| 6323 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6324 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6325 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6326 | } |
| 6327 | } |
| 6328 | if (!leaf->dflt) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6329 | /* there is no default value, do not set changed_type after type compilation |
| 6330 | * which is used to recompile the default value */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6331 | changed_type = -1; |
| 6332 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6333 | LY_CHECK_GOTO(lys_compile_node_type(ctx, NULL, d_rpl->type, (struct lysc_node_leaf*)devs[u]->target), cleanup); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6334 | changed_type++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6335 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6336 | |
| 6337 | /* [units-stmt] */ |
| 6338 | if (d_rpl->units) { |
| 6339 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units"); |
| 6340 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS), |
| 6341 | units, "replacing", "units", d_rpl->units); |
| 6342 | |
| 6343 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6344 | DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6345 | } |
| 6346 | |
| 6347 | /* [default-stmt] */ |
| 6348 | if (d_rpl->dflt) { |
| 6349 | switch (devs[u]->target->nodetype) { |
| 6350 | case LYS_LEAF: |
| 6351 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6352 | dflt, "replacing", "default", d_rpl->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6353 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6354 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6355 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6356 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6357 | dflt = d_rpl->dflt; |
| 6358 | /* parsing is done at the end after possible replace of the leaf's type */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6359 | break; |
| 6360 | case LYS_CHOICE: |
| 6361 | DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6362 | if (lys_compile_deviation_set_choice_dflt(ctx, d_rpl->dflt, (struct lysc_node_choice*)devs[u]->target)) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6363 | goto cleanup; |
| 6364 | } |
| 6365 | break; |
| 6366 | default: |
| 6367 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6368 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6369 | goto cleanup; |
| 6370 | } |
| 6371 | } |
| 6372 | |
| 6373 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6374 | if (d_rpl->flags & LYS_CONFIG_MASK) { |
| 6375 | if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6376 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6377 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "config"); |
| 6378 | goto cleanup; |
| 6379 | } |
| 6380 | if (!(devs[u]->target->flags & LYS_SET_CONFIG)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6381 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6382 | "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false"); |
| 6383 | goto cleanup; |
| 6384 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6385 | LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6386 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6387 | |
| 6388 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6389 | if (d_rpl->flags & LYS_MAND_MASK) { |
| 6390 | if (!(devs[u]->target->flags & LYS_MAND_MASK)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6391 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6392 | "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
| 6393 | goto cleanup; |
| 6394 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6395 | LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6396 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6397 | |
| 6398 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6399 | if (d_rpl->flags & LYS_SET_MIN) { |
| 6400 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6401 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 6402 | /* change value */ |
| 6403 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min; |
| 6404 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6405 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 6406 | /* change value */ |
| 6407 | ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min; |
| 6408 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6409 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6410 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements"); |
| 6411 | goto cleanup; |
| 6412 | } |
| 6413 | if (d_rpl->min) { |
| 6414 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 6415 | } |
| 6416 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6417 | |
| 6418 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6419 | if (d_rpl->flags & LYS_SET_MAX) { |
| 6420 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6421 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 6422 | /* change value */ |
| 6423 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6424 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6425 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 6426 | /* change value */ |
| 6427 | ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6428 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6429 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6430 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements"); |
| 6431 | goto cleanup; |
| 6432 | } |
| 6433 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6434 | |
| 6435 | break; |
| 6436 | default: |
| 6437 | LOGINT(ctx->ctx); |
| 6438 | goto cleanup; |
| 6439 | } |
| 6440 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6441 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6442 | /* final check when all deviations of a single target node are applied */ |
| 6443 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6444 | /* check min-max compatibility */ |
| 6445 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6446 | min = ((struct lysc_node_leaflist*)devs[u]->target)->min; |
| 6447 | max = ((struct lysc_node_leaflist*)devs[u]->target)->max; |
| 6448 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6449 | min = ((struct lysc_node_list*)devs[u]->target)->min; |
| 6450 | max = ((struct lysc_node_list*)devs[u]->target)->max; |
| 6451 | } else { |
| 6452 | min = max = 0; |
| 6453 | } |
| 6454 | if (min > max) { |
| 6455 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements " |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6456 | "after deviation: min value %u is bigger than max value %u.", min, max); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6457 | goto cleanup; |
| 6458 | } |
| 6459 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6460 | if (dflt) { |
| 6461 | /* parse added/changed default value after possible change of the type */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6462 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6463 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6464 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6465 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 6466 | lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6467 | leaf->dflt->realtype->refcount++; |
| 6468 | if (err) { |
| 6469 | ly_err_print(err); |
| 6470 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6471 | "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 6472 | ly_err_free(err); |
| 6473 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6474 | if (rc == LY_EINCOMPLETE) { |
| 6475 | /* postpone default compilation when the tree is complete */ |
| 6476 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6477 | |
| 6478 | /* but in general result is so far ok */ |
| 6479 | rc = LY_SUCCESS; |
| 6480 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6481 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6482 | } else if (changed_type) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6483 | /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */ |
| 6484 | int dynamic; |
| 6485 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6486 | dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &dynamic); |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6487 | |
| 6488 | /* update the list of incomplete default values if needed */ |
| 6489 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6490 | |
| 6491 | /* remove the previous default */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6492 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6493 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6494 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6495 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6496 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 6497 | lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6498 | leaf->dflt->realtype->refcount++; |
| 6499 | if (err) { |
| 6500 | ly_err_print(err); |
| 6501 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6502 | "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg); |
| 6503 | ly_err_free(err); |
| 6504 | } |
| 6505 | if (dynamic) { |
| 6506 | free((void*)dflt); |
| 6507 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6508 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6509 | if (rc == LY_EINCOMPLETE) { |
| 6510 | /* postpone default compilation when the tree is complete */ |
| 6511 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6512 | |
| 6513 | /* but in general result is so far ok */ |
| 6514 | rc = LY_SUCCESS; |
| 6515 | } |
| 6516 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6517 | } else { /* LYS_LEAFLIST */ |
| 6518 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6519 | dflt = llist->dflts[x]->realtype->plugin->print(llist->dflts[x], LYD_XML, lys_get_prefix, llist->dflts_mods[x], &dynamic); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6520 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6521 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6522 | llist->dflts[x]->realtype = llist->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6523 | rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), |
| 6524 | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 6525 | lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, |
| 6526 | llist->dflts[x], NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6527 | llist->dflts[x]->realtype->refcount++; |
| 6528 | if (err) { |
| 6529 | ly_err_print(err); |
| 6530 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6531 | "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).", |
| 6532 | dflt, err->msg); |
| 6533 | ly_err_free(err); |
| 6534 | } |
| 6535 | if (dynamic) { |
| 6536 | free((void*)dflt); |
| 6537 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6538 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6539 | if (rc == LY_EINCOMPLETE) { |
| 6540 | /* postpone default compilation when the tree is complete */ |
| 6541 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 6542 | |
| 6543 | /* but in general result is so far ok */ |
| 6544 | rc = LY_SUCCESS; |
| 6545 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6546 | LY_CHECK_GOTO(rc, cleanup); |
| 6547 | } |
| 6548 | } |
| 6549 | } |
| 6550 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6551 | /* check mandatory - default compatibility */ |
| 6552 | if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 6553 | && (devs[u]->target->flags & LYS_SET_DFLT) |
| 6554 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6555 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6556 | "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype)); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6557 | goto cleanup; |
| 6558 | } else if ((devs[u]->target->nodetype & LYS_CHOICE) |
| 6559 | && ((struct lysc_node_choice*)devs[u]->target)->dflt |
| 6560 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6561 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation combining default case and mandatory choice."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6562 | goto cleanup; |
| 6563 | } |
| 6564 | if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6565 | /* mandatory node under a default case */ |
| 6566 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6567 | "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".", |
| 6568 | lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6569 | goto cleanup; |
| 6570 | } |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6571 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6572 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6573 | } |
| 6574 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6575 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6576 | ret = LY_SUCCESS; |
| 6577 | |
| 6578 | cleanup: |
| 6579 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
| 6580 | LY_ARRAY_FREE(devs[u]->deviates); |
| 6581 | free(devs[u]); |
| 6582 | } |
| 6583 | free(devs); |
| 6584 | ly_set_erase(&targets, NULL); |
| 6585 | ly_set_erase(&devs_p, NULL); |
| 6586 | |
| 6587 | return ret; |
| 6588 | } |
| 6589 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 6590 | /** |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6591 | * @brief Compile the given YANG submodule into the main module. |
| 6592 | * @param[in] ctx Compile context |
| 6593 | * @param[in] inc Include structure from the main module defining the submodule. |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6594 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 6595 | */ |
| 6596 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6597 | lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc) |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6598 | { |
| 6599 | unsigned int u; |
| 6600 | LY_ERR ret = LY_SUCCESS; |
| 6601 | /* shortcuts */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6602 | struct lysp_submodule *submod = inc->submodule; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6603 | struct lysc_module *mainmod = ctx->mod->compiled; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6604 | struct lysp_node *node_p; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6605 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6606 | if (!mainmod->mod->off_features) { |
| 6607 | /* features are compiled directly into the compiled module structure, |
| 6608 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves. |
| 6609 | * The features compilation is finished in the main module (lys_compile()). */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6610 | ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features); |
| 6611 | LY_CHECK_GOTO(ret, error); |
| 6612 | } |
| 6613 | if (!mainmod->mod->off_extensions) { |
| 6614 | /* extensions are compiled directly into the compiled module structure, compilation is finished in the main module (lys_compile()). */ |
| 6615 | ret = lys_extension_precompile(ctx, NULL, NULL, submod->extensions, &mainmod->extensions); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6616 | LY_CHECK_GOTO(ret, error); |
| 6617 | } |
| 6618 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6619 | lysc_update_path(ctx, NULL, "{identity}"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6620 | COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, u, lys_compile_identity, ret, error); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6621 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6622 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6623 | /* data nodes */ |
| 6624 | LY_LIST_FOR(submod->data, node_p) { |
| 6625 | ret = lys_compile_node(ctx, node_p, NULL, 0); |
| 6626 | LY_CHECK_GOTO(ret, error); |
| 6627 | } |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 6628 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6629 | COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6630 | COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 6631 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6632 | error: |
| 6633 | return ret; |
| 6634 | } |
| 6635 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6636 | static void * |
| 6637 | lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts) |
| 6638 | { |
| 6639 | for (unsigned int u = 0; substmts[u].stmt; ++u) { |
| 6640 | if (substmts[u].stmt == stmt) { |
| 6641 | return substmts[u].storage; |
| 6642 | } |
| 6643 | } |
| 6644 | return NULL; |
| 6645 | } |
| 6646 | |
| 6647 | LY_ERR |
| 6648 | lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts) |
| 6649 | { |
| 6650 | LY_ERR ret = LY_EVALID, r; |
| 6651 | unsigned int u; |
| 6652 | struct lysp_stmt *stmt; |
| 6653 | void *parsed = NULL, **compiled = NULL; |
| 6654 | struct ly_set mandatory_stmts = {0}; |
| 6655 | |
| 6656 | /* check for invalid substatements */ |
| 6657 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
| 6658 | for (u = 0; substmts[u].stmt; ++u) { |
| 6659 | if (substmts[u].stmt == stmt->kw) { |
| 6660 | break; |
| 6661 | } |
| 6662 | } |
| 6663 | if (!substmts[u].stmt) { |
| 6664 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s\" extension instance.", |
| 6665 | stmt->stmt, ext->name); |
| 6666 | goto cleanup; |
| 6667 | } |
| 6668 | if (substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) { |
| 6669 | ly_set_add(&mandatory_stmts, &substmts[u], LY_SET_OPT_USEASLIST); |
| 6670 | } |
| 6671 | } |
| 6672 | |
| 6673 | /* keep order of the processing the same as the order in the defined substmts, |
| 6674 | * the order is important for some of the statements depending on others (e.g. type needs status and units) */ |
| 6675 | for (u = 0; substmts[u].stmt; ++u) { |
| 6676 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
| 6677 | if (substmts[u].stmt != stmt->kw) { |
| 6678 | continue; |
| 6679 | } |
| 6680 | |
| 6681 | if (substmts[u].storage) { |
| 6682 | switch (stmt->kw) { |
| 6683 | case LY_STMT_TYPE: { |
| 6684 | uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts); |
| 6685 | const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts); |
| 6686 | |
| 6687 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6688 | /* single item */ |
| 6689 | if (*(struct lysc_type**)substmts->storage) { |
| 6690 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6691 | goto cleanup; |
| 6692 | } |
| 6693 | compiled = substmts[u].storage; |
| 6694 | } else { |
| 6695 | /* sized array */ |
| 6696 | struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL; |
| 6697 | LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup); |
| 6698 | compiled = (void*)type; |
| 6699 | } |
| 6700 | |
| 6701 | LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed), ret = r, cleanup); |
| 6702 | LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL, |
| 6703 | flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type**)compiled, |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 6704 | units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup); |
| 6705 | lysp_type_free(ctx->ctx, parsed); |
| 6706 | free(parsed); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6707 | break; |
| 6708 | } |
| 6709 | /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc) */ |
| 6710 | default: |
| 6711 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Statement \"%s\" is not supported as an extension (found in \"%s\") substatement.", |
| 6712 | stmt->stmt, ext->name); |
| 6713 | goto cleanup; |
| 6714 | } |
| 6715 | } |
| 6716 | |
| 6717 | if (substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) { |
| 6718 | int i = ly_set_contains(&mandatory_stmts, &substmts[u]); |
| 6719 | if (i != -1) { |
| 6720 | ly_set_rm_index(&mandatory_stmts, i, NULL); |
| 6721 | } |
| 6722 | } |
| 6723 | } |
| 6724 | } |
| 6725 | |
| 6726 | if (mandatory_stmts.count) { |
| 6727 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSTMT, "type", ext->name); |
| 6728 | goto cleanup; |
| 6729 | } |
| 6730 | |
| 6731 | ret = LY_SUCCESS; |
| 6732 | |
| 6733 | cleanup: |
| 6734 | ly_set_erase(&mandatory_stmts, NULL); |
| 6735 | |
| 6736 | return ret; |
| 6737 | } |
| 6738 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6739 | LY_ERR |
| 6740 | lys_compile(struct lys_module *mod, int options) |
| 6741 | { |
| 6742 | struct lysc_ctx ctx = {0}; |
| 6743 | struct lysc_module *mod_c; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6744 | struct lysc_type *type, *typeiter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6745 | struct lysp_module *sp; |
| 6746 | struct lysp_node *node_p; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6747 | struct lysp_augment **augments = NULL; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 6748 | struct lysp_grp *grps; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6749 | struct lys_module *m; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 6750 | unsigned int u, v; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6751 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6752 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6753 | LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 6754 | |
| 6755 | if (!mod->implemented) { |
| 6756 | /* just imported modules are not compiled */ |
| 6757 | return LY_SUCCESS; |
| 6758 | } |
| 6759 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6760 | sp = mod->parsed; |
| 6761 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6762 | ctx.ctx = mod->ctx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6763 | ctx.mod = mod; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 6764 | ctx.mod_def = mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6765 | ctx.options = options; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6766 | ctx.path_len = 1; |
| 6767 | ctx.path[0] = '/'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6768 | |
| 6769 | mod->compiled = mod_c = calloc(1, sizeof *mod_c); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6770 | LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM); |
| 6771 | mod_c->mod = mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6772 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6773 | COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6774 | LY_ARRAY_FOR(sp->includes, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6775 | ret = lys_compile_submodule(&ctx, &sp->includes[u]); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6776 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 6777 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6778 | |
| 6779 | /* features */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6780 | if (mod->off_features) { |
| 6781 | /* there is already precompiled array of features */ |
| 6782 | mod_c->features = mod->off_features; |
| 6783 | mod->off_features = NULL; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6784 | } else { |
| 6785 | /* features are compiled directly into the compiled module structure, |
| 6786 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6787 | ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6788 | LY_CHECK_GOTO(ret, error); |
| 6789 | } |
| 6790 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 6791 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 6792 | * are present. */ |
| 6793 | LY_ARRAY_FOR(sp->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6794 | ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6795 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 6796 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6797 | lysc_update_path(&ctx, NULL, "{submodule}"); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6798 | LY_ARRAY_FOR(sp->includes, v) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6799 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6800 | LY_ARRAY_FOR(sp->includes[v].submodule->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6801 | ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6802 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 6803 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6804 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6805 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6806 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6807 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6808 | /* extensions */ |
| 6809 | /* 2-steps: a) prepare compiled structures and ... */ |
| 6810 | if (mod->off_extensions) { |
| 6811 | /* there is already precompiled array of extension definitions */ |
| 6812 | mod_c->extensions = mod->off_extensions; |
| 6813 | mod->off_extensions = NULL; |
| 6814 | } else { |
| 6815 | /* extension definitions are compiled directly into the compiled module structure */ |
| 6816 | ret = lys_extension_precompile(&ctx, NULL, NULL, sp->extensions, &mod_c->extensions); |
| 6817 | } |
| 6818 | /* ... b) connect the extension definitions with the appropriate extension plugins */ |
| 6819 | lys_compile_extension_plugins(mod_c->extensions); |
| 6820 | |
| 6821 | /* identities */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6822 | lysc_update_path(&ctx, NULL, "{identity}"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6823 | COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, u, lys_compile_identity, ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6824 | if (sp->identities) { |
| 6825 | LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities)); |
| 6826 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6827 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6828 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6829 | /* data nodes */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6830 | LY_LIST_FOR(sp->data, node_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6831 | ret = lys_compile_node(&ctx, node_p, NULL, 0); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6832 | LY_CHECK_GOTO(ret, error); |
| 6833 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6834 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6835 | COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6836 | COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6837 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6838 | /* augments - sort first to cover augments augmenting other augments */ |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 6839 | ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6840 | LY_CHECK_GOTO(ret, error); |
| 6841 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6842 | ret = lys_compile_augment(&ctx, augments[u], NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6843 | LY_CHECK_GOTO(ret, error); |
| 6844 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6845 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6846 | /* deviations TODO cover deviations from submodules */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6847 | ret = lys_compile_deviations(&ctx, sp); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6848 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6849 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6850 | /* extension instances TODO cover extension instances from submodules */ |
| 6851 | COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6852 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6853 | /* validate leafref's paths and when/must xpaths */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6854 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 6855 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 6856 | * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6857 | for (u = 0; u < ctx.unres.count; ++u) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 6858 | if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6859 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 6860 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 6861 | /* validate the path */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6862 | ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), (struct lysc_type_leafref*)type); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6863 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 6864 | } else if (type->basetype == LY_TYPE_UNION) { |
| 6865 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 6866 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 6867 | /* validate the path */ |
| 6868 | ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), |
| 6869 | (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]); |
| 6870 | LY_CHECK_GOTO(ret, error); |
| 6871 | } |
| 6872 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6873 | } |
| 6874 | } |
| 6875 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6876 | for (u = 0; u < ctx.unres.count; ++u) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 6877 | if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6878 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 6879 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 6880 | /* store pointer to the real type */ |
| 6881 | for (typeiter = ((struct lysc_type_leafref*)type)->realtype; |
| 6882 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 6883 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 6884 | ((struct lysc_type_leafref*)type)->realtype = typeiter; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 6885 | } else if (type->basetype == LY_TYPE_UNION) { |
| 6886 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 6887 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 6888 | /* store pointer to the real type */ |
| 6889 | for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype; |
| 6890 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 6891 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 6892 | ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter; |
| 6893 | } |
| 6894 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6895 | } |
| 6896 | } |
| 6897 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6898 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6899 | /* finish incomplete default values compilation */ |
| 6900 | for (u = 0; u < ctx.dflts.count; ++u) { |
| 6901 | struct ly_err_item *err = NULL; |
| 6902 | struct lysc_incomplete_dflt *r = ctx.dflts.objs[u]; |
| 6903 | ret = r->dflt->realtype->plugin->store(ctx.ctx, r->dflt->realtype, r->dflt->canonized, strlen(r->dflt->canonized), |
| 6904 | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix, |
| 6905 | (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err); |
| 6906 | if (err) { |
| 6907 | ly_err_print(err); |
| 6908 | ctx.path[0] = '\0'; |
| 6909 | lysc_path(r->context_node, LY_PATH_LOG, ctx.path, LYSC_CTX_BUFSIZE); |
| 6910 | LOGVAL(ctx.ctx, LY_VLOG_STR, ctx.path, LYVE_SEMANTICS, |
| 6911 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 6912 | ly_err_free(err); |
| 6913 | } |
| 6914 | LY_CHECK_GOTO(ret, error); |
| 6915 | } |
| 6916 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 6917 | /* validate non-instantiated groupings from the parsed schema, |
| 6918 | * without it we would accept even the schemas with invalid grouping specification */ |
| 6919 | ctx.options |= LYSC_OPT_GROUPING; |
| 6920 | LY_ARRAY_FOR(sp->groupings, u) { |
| 6921 | if (!(sp->groupings[u].flags & LYS_USED_GRP)) { |
| 6922 | LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u])) != LY_SUCCESS, error); |
| 6923 | } |
| 6924 | } |
| 6925 | LY_LIST_FOR(sp->data, node_p) { |
| 6926 | grps = (struct lysp_grp*)lysp_node_groupings(node_p); |
| 6927 | LY_ARRAY_FOR(grps, u) { |
| 6928 | if (!(grps[u].flags & LYS_USED_GRP)) { |
| 6929 | LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &grps[u])) != LY_SUCCESS, error); |
| 6930 | } |
| 6931 | } |
| 6932 | } |
| 6933 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6934 | if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) { |
| 6935 | /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile |
| 6936 | leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */ |
| 6937 | } |
| 6938 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 6939 | ly_set_erase(&ctx.dflts, free); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6940 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 6941 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 6942 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6943 | LY_ARRAY_FREE(augments); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6944 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6945 | if (ctx.options & LYSC_OPT_FREE_SP) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6946 | lysp_module_free(mod->parsed); |
| 6947 | ((struct lys_module*)mod)->parsed = NULL; |
| 6948 | } |
| 6949 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6950 | if (!(ctx.options & LYSC_OPT_INTERNAL)) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6951 | /* remove flag of the modules implemented by dependency */ |
| 6952 | for (u = 0; u < ctx.ctx->list.count; ++u) { |
| 6953 | m = ctx.ctx->list.objs[u]; |
| 6954 | if (m->implemented == 2) { |
| 6955 | m->implemented = 1; |
| 6956 | } |
| 6957 | } |
| 6958 | } |
| 6959 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6960 | ((struct lys_module*)mod)->compiled = mod_c; |
| 6961 | return LY_SUCCESS; |
| 6962 | |
| 6963 | error: |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6964 | lys_feature_precompile_revert(&ctx, mod); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 6965 | ly_set_erase(&ctx.dflts, free); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6966 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 6967 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 6968 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6969 | LY_ARRAY_FREE(augments); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6970 | lysc_module_free(mod_c, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6971 | mod->compiled = NULL; |
| 6972 | |
| 6973 | /* revert compilation of modules implemented by dependency */ |
| 6974 | for (u = 0; u < ctx.ctx->list.count; ++u) { |
| 6975 | m = ctx.ctx->list.objs[u]; |
| 6976 | if (m->implemented == 2) { |
| 6977 | /* revert features list to the precompiled state */ |
| 6978 | lys_feature_precompile_revert(&ctx, m); |
| 6979 | /* mark module as imported-only / not-implemented */ |
| 6980 | m->implemented = 0; |
| 6981 | /* free the compiled version of the module */ |
| 6982 | lysc_module_free(m->compiled, NULL); |
| 6983 | m->compiled = NULL; |
| 6984 | } |
| 6985 | } |
| 6986 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6987 | return ret; |
| 6988 | } |