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 | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame^] | 438 | ext->module = ctx->mod_def; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 439 | ext->parent = parent; |
| 440 | ext->parent_type = parent_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 441 | |
| 442 | /* get module where the extension definition should be placed */ |
| 443 | for (u = 0; ext_p->name[u] != ':'; ++u); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 444 | mod = lys_module_find_prefix(ctx->mod_def, ext_p->name, u); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 445 | LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 446 | "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name), |
| 447 | LY_EVALID); |
| 448 | LY_CHECK_ERR_RET(!mod->parsed->extensions, |
| 449 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 450 | "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 451 | ext_p->name, mod->name), |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 452 | LY_EVALID); |
| 453 | name = &ext_p->name[u + 1]; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 454 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 455 | /* find the extension definition there */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 456 | if (mod->off_extensions) { |
| 457 | elist = mod->off_extensions; |
| 458 | } else { |
| 459 | elist = mod->compiled->extensions; |
| 460 | } |
| 461 | LY_ARRAY_FOR(elist, u) { |
| 462 | if (!strcmp(name, elist[u].name)) { |
| 463 | ext->def = &elist[u]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 464 | break; |
| 465 | } |
| 466 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 467 | LY_CHECK_ERR_RET(!ext->def, |
| 468 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 469 | "Extension definition of extension instance \"%s\" not found.", ext_p->name), |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 470 | LY_EVALID); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 471 | |
| 472 | if (ext->def->plugin && ext->def->plugin->compile) { |
| 473 | LY_CHECK_RET(ext->def->plugin->compile(ctx, ext_p, ext),LY_EVALID); |
| 474 | } |
| 475 | |
| 476 | return LY_SUCCESS; |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition. |
| 481 | */ |
| 482 | static LY_ERR |
| 483 | lys_compile_extension(struct lysc_ctx *ctx, struct lysp_ext *ext_p, struct lysc_ext *ext) |
| 484 | { |
| 485 | LY_ERR ret = LY_SUCCESS; |
| 486 | |
| 487 | DUP_STRING(ctx->ctx, ext_p->name, ext->name); |
| 488 | DUP_STRING(ctx->ctx, ext_p->argument, ext->argument); |
| 489 | ext->module = ctx->mod_def; |
| 490 | COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext->exts, ext, LYEXT_PAR_EXT, ret, done); |
| 491 | |
| 492 | done: |
| 493 | return ret; |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * @brief Link the extensions definitions with the available extension plugins. |
| 498 | * |
| 499 | * This is done only in the compiled (implemented) module. Extensions of a non-implemented modules |
| 500 | * are not connected with even available extension plugins. |
| 501 | * |
| 502 | * @param[in] extensions List of extensions to be processed ([sized array](@ref sizedarrays)). |
| 503 | */ |
| 504 | static void |
| 505 | lys_compile_extension_plugins(struct lysc_ext *extensions) |
| 506 | { |
| 507 | unsigned int u; |
| 508 | |
| 509 | LY_ARRAY_FOR(extensions, u) { |
| 510 | extensions[u].plugin = lyext_get_plugin(&extensions[u]); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | LY_ERR |
| 515 | lys_extension_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, |
| 516 | struct lysp_ext *extensions_p, struct lysc_ext **extensions) |
| 517 | { |
| 518 | unsigned int offset = 0, u; |
| 519 | struct lysc_ctx context = {0}; |
| 520 | |
| 521 | assert(ctx_sc || ctx); |
| 522 | |
| 523 | if (!ctx_sc) { |
| 524 | context.ctx = ctx; |
| 525 | context.mod = module; |
| 526 | context.path_len = 1; |
| 527 | context.path[0] = '/'; |
| 528 | ctx_sc = &context; |
| 529 | } |
| 530 | |
| 531 | if (!extensions_p) { |
| 532 | return LY_SUCCESS; |
| 533 | } |
| 534 | if (*extensions) { |
| 535 | offset = LY_ARRAY_SIZE(*extensions); |
| 536 | } |
| 537 | |
| 538 | lysc_update_path(ctx_sc, NULL, "{extension}"); |
| 539 | LY_ARRAY_CREATE_RET(ctx_sc->ctx, *extensions, LY_ARRAY_SIZE(extensions_p), LY_EMEM); |
| 540 | LY_ARRAY_FOR(extensions_p, u) { |
| 541 | lysc_update_path(ctx_sc, NULL, extensions_p[u].name); |
| 542 | LY_ARRAY_INCREMENT(*extensions); |
| 543 | COMPILE_CHECK_UNIQUENESS(ctx_sc, *extensions, name, &(*extensions)[offset + u], "extension", extensions_p[u].name); |
| 544 | LY_CHECK_RET(lys_compile_extension(ctx_sc, &extensions_p[u], &(*extensions)[offset + u])); |
| 545 | lysc_update_path(ctx_sc, NULL, NULL); |
| 546 | } |
| 547 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 548 | |
| 549 | return LY_SUCCESS; |
| 550 | } |
| 551 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 552 | /** |
| 553 | * @brief Compile information from the if-feature statement |
| 554 | * @param[in] ctx Compile context. |
| 555 | * @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] | 556 | * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill. |
| 557 | * @return LY_ERR value. |
| 558 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 559 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 560 | 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] | 561 | { |
| 562 | const char *c = *value; |
| 563 | int r, rc = EXIT_FAILURE; |
| 564 | int i, j, last_not, checkversion = 0; |
| 565 | unsigned int f_size = 0, expr_size = 0, f_exp = 1; |
| 566 | uint8_t op; |
| 567 | struct iff_stack stack = {0, 0, NULL}; |
| 568 | struct lysc_feature *f; |
| 569 | |
| 570 | assert(c); |
| 571 | |
| 572 | /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */ |
| 573 | for (i = j = last_not = 0; c[i]; i++) { |
| 574 | if (c[i] == '(') { |
| 575 | j++; |
| 576 | checkversion = 1; |
| 577 | continue; |
| 578 | } else if (c[i] == ')') { |
| 579 | j--; |
| 580 | continue; |
| 581 | } else if (isspace(c[i])) { |
| 582 | checkversion = 1; |
| 583 | continue; |
| 584 | } |
| 585 | |
| 586 | 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] | 587 | int sp; |
| 588 | for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++); |
| 589 | if (c[i + r + sp] == '\0') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 590 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 591 | "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value); |
| 592 | return LY_EVALID; |
| 593 | } else if (!isspace(c[i + r])) { |
| 594 | /* feature name starting with the not/and/or */ |
| 595 | last_not = 0; |
| 596 | f_size++; |
| 597 | } else if (c[i] == 'n') { /* not operation */ |
| 598 | if (last_not) { |
| 599 | /* double not */ |
| 600 | expr_size = expr_size - 2; |
| 601 | last_not = 0; |
| 602 | } else { |
| 603 | last_not = 1; |
| 604 | } |
| 605 | } else { /* and, or */ |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 606 | if (f_exp != f_size) { |
| 607 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 608 | "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]); |
| 609 | return LY_EVALID; |
| 610 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 611 | f_exp++; |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 612 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 613 | /* not a not operation */ |
| 614 | last_not = 0; |
| 615 | } |
| 616 | i += r; |
| 617 | } else { |
| 618 | f_size++; |
| 619 | last_not = 0; |
| 620 | } |
| 621 | expr_size++; |
| 622 | |
| 623 | while (!isspace(c[i])) { |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 624 | if (!c[i] || c[i] == ')' || c[i] == '(') { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 625 | i--; |
| 626 | break; |
| 627 | } |
| 628 | i++; |
| 629 | } |
| 630 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 631 | if (j) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 632 | /* not matching count of ( and ) */ |
| 633 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 634 | "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value); |
| 635 | return LY_EVALID; |
| 636 | } |
Radek Krejci | 6788abc | 2019-06-14 13:56:49 +0200 | [diff] [blame] | 637 | if (f_exp != f_size) { |
| 638 | /* features do not match the needed arguments for the logical operations */ |
| 639 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 640 | "Invalid value \"%s\" of if-feature - number of features in expression does not match " |
| 641 | "the required number of operands for the operations.", *value); |
| 642 | return LY_EVALID; |
| 643 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 644 | |
| 645 | if (checkversion || expr_size > 1) { |
| 646 | /* check that we have 1.1 module */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 647 | if (ctx->mod_def->version != LYS_VERSION_1_1) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 648 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 649 | "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value); |
| 650 | return LY_EVALID; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | /* allocate the memory */ |
| 655 | LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM); |
| 656 | iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr); |
| 657 | stack.stack = malloc(expr_size * sizeof *stack.stack); |
| 658 | LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error); |
| 659 | |
| 660 | stack.size = expr_size; |
| 661 | f_size--; expr_size--; /* used as indexes from now */ |
| 662 | |
| 663 | for (i--; i >= 0; i--) { |
| 664 | if (c[i] == ')') { |
| 665 | /* push it on stack */ |
| 666 | iff_stack_push(&stack, LYS_IFF_RP); |
| 667 | continue; |
| 668 | } else if (c[i] == '(') { |
| 669 | /* pop from the stack into result all operators until ) */ |
| 670 | while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) { |
| 671 | iff_setop(iff->expr, op, expr_size--); |
| 672 | } |
| 673 | continue; |
| 674 | } else if (isspace(c[i])) { |
| 675 | continue; |
| 676 | } |
| 677 | |
| 678 | /* end of operator or operand -> find beginning and get what is it */ |
| 679 | j = i + 1; |
| 680 | while (i >= 0 && !isspace(c[i]) && c[i] != '(') { |
| 681 | i--; |
| 682 | } |
| 683 | i++; /* go back by one step */ |
| 684 | |
| 685 | if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) { |
| 686 | if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) { |
| 687 | /* double not */ |
| 688 | iff_stack_pop(&stack); |
| 689 | } else { |
| 690 | /* not has the highest priority, so do not pop from the stack |
| 691 | * as in case of AND and OR */ |
| 692 | iff_stack_push(&stack, LYS_IFF_NOT); |
| 693 | } |
| 694 | } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) { |
| 695 | /* as for OR - pop from the stack all operators with the same or higher |
| 696 | * priority and store them to the result, then push the AND to the stack */ |
| 697 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) { |
| 698 | op = iff_stack_pop(&stack); |
| 699 | iff_setop(iff->expr, op, expr_size--); |
| 700 | } |
| 701 | iff_stack_push(&stack, LYS_IFF_AND); |
| 702 | } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) { |
| 703 | while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) { |
| 704 | op = iff_stack_pop(&stack); |
| 705 | iff_setop(iff->expr, op, expr_size--); |
| 706 | } |
| 707 | iff_stack_push(&stack, LYS_IFF_OR); |
| 708 | } else { |
| 709 | /* feature name, length is j - i */ |
| 710 | |
| 711 | /* add it to the expression */ |
| 712 | iff_setop(iff->expr, LYS_IFF_F, expr_size--); |
| 713 | |
| 714 | /* now get the link to the feature definition */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 715 | f = lys_feature_find(ctx->mod_def, &c[i], j - i); |
| 716 | LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 717 | "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]); |
| 718 | rc = LY_EVALID, error) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 719 | iff->features[f_size] = f; |
| 720 | LY_ARRAY_INCREMENT(iff->features); |
| 721 | f_size--; |
| 722 | } |
| 723 | } |
| 724 | while (stack.index) { |
| 725 | op = iff_stack_pop(&stack); |
| 726 | iff_setop(iff->expr, op, expr_size--); |
| 727 | } |
| 728 | |
| 729 | if (++expr_size || ++f_size) { |
| 730 | /* not all expected operators and operands found */ |
| 731 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 732 | "Invalid value \"%s\" of if-feature - processing error.", *value); |
| 733 | rc = LY_EINT; |
| 734 | } else { |
| 735 | rc = LY_SUCCESS; |
| 736 | } |
| 737 | |
| 738 | error: |
| 739 | /* cleanup */ |
| 740 | iff_stack_clean(&stack); |
| 741 | |
| 742 | return rc; |
| 743 | } |
| 744 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 745 | /** |
| 746 | * @brief Compile information from the when statement |
| 747 | * @param[in] ctx Compile context. |
| 748 | * @param[in] when_p The parsed when statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 749 | * @param[out] when Pointer where to store pointer to the created compiled when structure. |
| 750 | * @return LY_ERR value. |
| 751 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 752 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 753 | 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] | 754 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 755 | LY_ERR ret = LY_SUCCESS; |
| 756 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 757 | *when = calloc(1, sizeof **when); |
| 758 | (*when)->refcount = 1; |
| 759 | (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond); |
| 760 | DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc); |
| 761 | DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref); |
| 762 | LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 763 | 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] | 764 | |
| 765 | done: |
| 766 | return ret; |
| 767 | } |
| 768 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 769 | /** |
| 770 | * @brief Compile information from the must statement |
| 771 | * @param[in] ctx Compile context. |
| 772 | * @param[in] must_p The parsed must statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 773 | * @param[in,out] must Prepared (empty) compiled must structure to fill. |
| 774 | * @return LY_ERR value. |
| 775 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 776 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 777 | 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] | 778 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 779 | LY_ERR ret = LY_SUCCESS; |
| 780 | |
| 781 | must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg); |
| 782 | LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done); |
Radek Krejci | 462cf25 | 2019-07-24 09:49:08 +0200 | [diff] [blame] | 783 | must->module = ctx->mod_def; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 784 | DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag); |
| 785 | DUP_STRING(ctx->ctx, must_p->emsg, must->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 786 | DUP_STRING(ctx->ctx, must_p->dsc, must->dsc); |
| 787 | DUP_STRING(ctx->ctx, must_p->ref, must->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 788 | 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] | 789 | |
| 790 | done: |
| 791 | return ret; |
| 792 | } |
| 793 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 794 | /** |
| 795 | * @brief Compile information from the import statement |
| 796 | * @param[in] ctx Compile context. |
| 797 | * @param[in] imp_p The parsed import statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 798 | * @param[in,out] imp Prepared (empty) compiled import structure to fill. |
| 799 | * @return LY_ERR value. |
| 800 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 801 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 802 | 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] | 803 | { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 804 | struct lys_module *mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 805 | LY_ERR ret = LY_SUCCESS; |
| 806 | |
| 807 | DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 808 | 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] | 809 | imp->module = imp_p->module; |
| 810 | |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 811 | /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs. |
| 812 | * 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] | 813 | * 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] | 814 | if (!imp->module->parsed) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 815 | /* try to use filepath if present */ |
| 816 | if (imp->module->filepath) { |
| 817 | mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath, |
| 818 | !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] | 819 | if (mod != imp->module) { |
| 820 | 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] | 821 | imp->module->filepath, imp->module->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 822 | mod = NULL; |
| 823 | } |
| 824 | } |
| 825 | if (!mod) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 826 | 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] | 827 | 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] | 828 | imp->module->name, ctx->mod->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 829 | return LY_ENOTFOUND; |
| 830 | } |
| 831 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | done: |
| 835 | return ret; |
| 836 | } |
| 837 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 838 | /** |
| 839 | * @brief Compile information from the identity statement |
| 840 | * |
| 841 | * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases(). |
| 842 | * |
| 843 | * @param[in] ctx Compile context. |
| 844 | * @param[in] ident_p The parsed identity statement structure. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 845 | * @param[in] idents List of so far compiled identities to check the name uniqueness. |
| 846 | * @param[in,out] ident Prepared (empty) compiled identity structure to fill. |
| 847 | * @return LY_ERR value. |
| 848 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 849 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 850 | 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] | 851 | { |
| 852 | unsigned int u; |
| 853 | LY_ERR ret = LY_SUCCESS; |
| 854 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 855 | lysc_update_path(ctx, NULL, ident_p->name); |
| 856 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 857 | COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 858 | DUP_STRING(ctx->ctx, ident_p->name, ident->name); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 859 | DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc); |
| 860 | DUP_STRING(ctx->ctx, ident_p->ref, ident->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 861 | ident->module = ctx->mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 862 | 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] | 863 | /* 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] | 864 | 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] | 865 | ident->flags = ident_p->flags; |
| 866 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 867 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 868 | done: |
| 869 | return ret; |
| 870 | } |
| 871 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 872 | /** |
| 873 | * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement). |
| 874 | * |
| 875 | * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages. |
| 876 | * |
| 877 | * @param[in] ctx Compile context for logging. |
| 878 | * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed). |
| 879 | * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident) |
| 880 | * @return LY_SUCCESS if everything is ok. |
| 881 | * @return LY_EVALID if the identity is derived from itself. |
| 882 | */ |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 883 | static LY_ERR |
| 884 | lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived) |
| 885 | { |
| 886 | LY_ERR ret = LY_EVALID; |
| 887 | unsigned int u, v; |
| 888 | struct ly_set recursion = {0}; |
| 889 | struct lysc_ident *drv; |
| 890 | |
| 891 | if (!derived) { |
| 892 | return LY_SUCCESS; |
| 893 | } |
| 894 | |
| 895 | for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) { |
| 896 | if (ident == derived[u]) { |
| 897 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 898 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 899 | goto cleanup; |
| 900 | } |
| 901 | ly_set_add(&recursion, derived[u], 0); |
| 902 | } |
| 903 | |
| 904 | for (v = 0; v < recursion.count; ++v) { |
| 905 | drv = recursion.objs[v]; |
| 906 | if (!drv->derived) { |
| 907 | continue; |
| 908 | } |
| 909 | for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) { |
| 910 | if (ident == drv->derived[u]) { |
| 911 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 912 | "Identity \"%s\" is indirectly derived from itself.", ident->name); |
| 913 | goto cleanup; |
| 914 | } |
| 915 | ly_set_add(&recursion, drv->derived[u], 0); |
| 916 | } |
| 917 | } |
| 918 | ret = LY_SUCCESS; |
| 919 | |
| 920 | cleanup: |
| 921 | ly_set_erase(&recursion, NULL); |
| 922 | return ret; |
| 923 | } |
| 924 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 925 | /** |
| 926 | * @brief Find and process the referenced base identities from another identity or identityref |
| 927 | * |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 928 | * 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] | 929 | * the array of pointers to the base identities. So one of the ident or bases parameter must be set |
| 930 | * to distinguish these two use cases. |
| 931 | * |
| 932 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 933 | * @param[in] bases_p Array of names (including prefix if necessary) of base identities. |
| 934 | * @param[in] ident Referencing identity to work with. |
| 935 | * @param[in] bases Array of bases of identityref to fill in. |
| 936 | * @return LY_ERR value. |
| 937 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 938 | static LY_ERR |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 939 | 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] | 940 | { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 941 | unsigned int u, v; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 942 | const char *s, *name; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 943 | struct lys_module *mod; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 944 | struct lysc_ident **idref; |
| 945 | |
| 946 | assert(ident || bases); |
| 947 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 948 | if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 949 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 950 | "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type"); |
| 951 | return LY_EVALID; |
| 952 | } |
| 953 | |
| 954 | for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) { |
| 955 | s = strchr(bases_p[u], ':'); |
| 956 | if (s) { |
| 957 | /* prefixed identity */ |
| 958 | name = &s[1]; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 959 | 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] | 960 | } else { |
| 961 | name = bases_p[u]; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 962 | mod = ctx->mod_def; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 963 | } |
| 964 | if (!mod) { |
| 965 | if (ident) { |
| 966 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 967 | "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 968 | } else { |
| 969 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 970 | "Invalid prefix used for base (%s) of identityref.", bases_p[u]); |
| 971 | } |
| 972 | return LY_EVALID; |
| 973 | } |
| 974 | idref = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 975 | if (mod->compiled && mod->compiled->identities) { |
| 976 | for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) { |
| 977 | if (!strcmp(name, mod->compiled->identities[v].name)) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 978 | if (ident) { |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 979 | if (ident == &mod->compiled->identities[v]) { |
| 980 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 981 | "Identity \"%s\" is derived from itself.", ident->name); |
| 982 | return LY_EVALID; |
| 983 | } |
| 984 | 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] | 985 | /* we have match! store the backlink */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 986 | 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] | 987 | *idref = ident; |
| 988 | } else { |
| 989 | /* we have match! store the found identity */ |
| 990 | LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 991 | *idref = &mod->compiled->identities[v]; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 992 | } |
| 993 | break; |
| 994 | } |
| 995 | } |
| 996 | } |
| 997 | if (!idref || !(*idref)) { |
| 998 | if (ident) { |
| 999 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1000 | "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name); |
| 1001 | } else { |
| 1002 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1003 | "Unable to find base (%s) of identityref.", bases_p[u]); |
| 1004 | } |
| 1005 | return LY_EVALID; |
| 1006 | } |
| 1007 | } |
| 1008 | return LY_SUCCESS; |
| 1009 | } |
| 1010 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1011 | /** |
| 1012 | * @brief For the given array of identities, set the backlinks from all their base identities. |
| 1013 | * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes. |
| 1014 | * @param[in] idents_p Array of identities definitions from the parsed schema structure. |
| 1015 | * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set. |
| 1016 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 1017 | */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1018 | static LY_ERR |
| 1019 | lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents) |
| 1020 | { |
| 1021 | unsigned int i; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1022 | |
| 1023 | for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) { |
| 1024 | if (!idents_p[i].bases) { |
| 1025 | continue; |
| 1026 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1027 | lysc_update_path(ctx, NULL, idents[i].name); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1028 | 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] | 1029 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1030 | } |
| 1031 | return LY_SUCCESS; |
| 1032 | } |
| 1033 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1034 | LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1035 | 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] | 1036 | { |
| 1037 | unsigned int offset = 0, u; |
| 1038 | struct lysc_ctx context = {0}; |
| 1039 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1040 | assert(ctx_sc || ctx); |
| 1041 | |
| 1042 | if (!ctx_sc) { |
| 1043 | context.ctx = ctx; |
| 1044 | context.mod = module; |
| 1045 | context.path_len = 1; |
| 1046 | context.path[0] = '/'; |
| 1047 | ctx_sc = &context; |
| 1048 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1049 | |
| 1050 | if (!features_p) { |
| 1051 | return LY_SUCCESS; |
| 1052 | } |
| 1053 | if (*features) { |
| 1054 | offset = LY_ARRAY_SIZE(*features); |
| 1055 | } |
| 1056 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1057 | lysc_update_path(ctx_sc, NULL, "{feature}"); |
| 1058 | 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] | 1059 | LY_ARRAY_FOR(features_p, u) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1060 | lysc_update_path(ctx_sc, NULL, features_p[u].name); |
| 1061 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1062 | LY_ARRAY_INCREMENT(*features); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1063 | COMPILE_CHECK_UNIQUENESS(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name); |
| 1064 | DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name); |
| 1065 | DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc); |
| 1066 | 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] | 1067 | (*features)[offset + u].flags = features_p[u].flags; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1068 | (*features)[offset + u].module = ctx_sc->mod; |
| 1069 | |
| 1070 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1071 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1072 | lysc_update_path(ctx_sc, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1073 | |
| 1074 | return LY_SUCCESS; |
| 1075 | } |
| 1076 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1077 | /** |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1078 | * @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] | 1079 | * |
| 1080 | * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages. |
| 1081 | * |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1082 | * @param[in] ctx Compile context for logging. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1083 | * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature |
| 1084 | * being currently processed). |
| 1085 | * @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] | 1086 | * @return LY_SUCCESS if everything is ok. |
| 1087 | * @return LY_EVALID if the feature references indirectly itself. |
| 1088 | */ |
| 1089 | static LY_ERR |
| 1090 | lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures) |
| 1091 | { |
| 1092 | LY_ERR ret = LY_EVALID; |
| 1093 | unsigned int u, v; |
| 1094 | struct ly_set recursion = {0}; |
| 1095 | struct lysc_feature *drv; |
| 1096 | |
| 1097 | if (!depfeatures) { |
| 1098 | return LY_SUCCESS; |
| 1099 | } |
| 1100 | |
| 1101 | for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) { |
| 1102 | if (feature == depfeatures[u]) { |
| 1103 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1104 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1105 | goto cleanup; |
| 1106 | } |
| 1107 | ly_set_add(&recursion, depfeatures[u], 0); |
| 1108 | } |
| 1109 | |
| 1110 | for (v = 0; v < recursion.count; ++v) { |
| 1111 | drv = recursion.objs[v]; |
| 1112 | if (!drv->depfeatures) { |
| 1113 | continue; |
| 1114 | } |
| 1115 | for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) { |
| 1116 | if (feature == drv->depfeatures[u]) { |
| 1117 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1118 | "Feature \"%s\" is indirectly referenced from itself.", feature->name); |
| 1119 | goto cleanup; |
| 1120 | } |
| 1121 | ly_set_add(&recursion, drv->depfeatures[u], 0); |
| 1122 | } |
| 1123 | } |
| 1124 | ret = LY_SUCCESS; |
| 1125 | |
| 1126 | cleanup: |
| 1127 | ly_set_erase(&recursion, NULL); |
| 1128 | return ret; |
| 1129 | } |
| 1130 | |
| 1131 | /** |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1132 | * @brief Create pre-compiled features array. |
| 1133 | * |
| 1134 | * See lys_feature_precompile() for more details. |
| 1135 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1136 | * @param[in] ctx Compile context. |
| 1137 | * @param[in] feature_p Parsed feature definition to compile. |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1138 | * @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] | 1139 | * @return LY_ERR value. |
| 1140 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1141 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1142 | 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] | 1143 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1144 | unsigned int u, v, x; |
| 1145 | struct lysc_feature *feature, **df; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1146 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1147 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1148 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1149 | /* find the preprecompiled feature */ |
| 1150 | LY_ARRAY_FOR(features, x) { |
| 1151 | if (strcmp(features[x].name, feature_p->name)) { |
| 1152 | continue; |
| 1153 | } |
| 1154 | feature = &features[x]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1155 | lysc_update_path(ctx, NULL, "{feature}"); |
| 1156 | lysc_update_path(ctx, NULL, feature_p->name); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1157 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1158 | /* finish compilation started in lys_feature_precompile() */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1159 | 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] | 1160 | 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] | 1161 | if (feature->iffeatures) { |
| 1162 | for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) { |
| 1163 | if (feature->iffeatures[u].features) { |
| 1164 | for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) { |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 1165 | /* check for circular dependency - direct reference first,... */ |
| 1166 | if (feature == feature->iffeatures[u].features[v]) { |
| 1167 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 1168 | "Feature \"%s\" is referenced from itself.", feature->name); |
| 1169 | return LY_EVALID; |
| 1170 | } |
| 1171 | /* ... and indirect circular reference */ |
| 1172 | LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures)); |
| 1173 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1174 | /* add itself into the dependants list */ |
| 1175 | LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM); |
| 1176 | *df = feature; |
| 1177 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1178 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1179 | } |
| 1180 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 1181 | lysc_update_path(ctx, NULL, NULL); |
| 1182 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1183 | done: |
| 1184 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1185 | } |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 1186 | |
| 1187 | LOGINT(ctx->ctx); |
| 1188 | return LY_EINT; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1189 | } |
| 1190 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 1191 | /** |
| 1192 | * @brief Revert compiled list of features back to the precompiled state. |
| 1193 | * |
| 1194 | * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status. |
| 1195 | * The features are supposed to be stored again as off_features in ::lys_module structure. |
| 1196 | * |
| 1197 | * @param[in] ctx Compilation context. |
| 1198 | * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema |
| 1199 | * and supposed to hold the off_features list. |
| 1200 | */ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 1201 | static void |
| 1202 | lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod) |
| 1203 | { |
| 1204 | unsigned int u, v; |
| 1205 | |
| 1206 | /* keep the off_features list until the complete lys_module is freed */ |
| 1207 | mod->off_features = mod->compiled->features; |
| 1208 | mod->compiled->features = NULL; |
| 1209 | |
| 1210 | /* in the off_features list, remove all the parts (from finished compiling process) |
| 1211 | * which may points into the data being freed here */ |
| 1212 | LY_ARRAY_FOR(mod->off_features, u) { |
| 1213 | LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) { |
| 1214 | lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]); |
| 1215 | } |
| 1216 | LY_ARRAY_FREE(mod->off_features[u].iffeatures); |
| 1217 | mod->off_features[u].iffeatures = NULL; |
| 1218 | |
| 1219 | LY_ARRAY_FOR(mod->off_features[u].exts, v) { |
| 1220 | lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]); |
| 1221 | } |
| 1222 | LY_ARRAY_FREE(mod->off_features[u].exts); |
| 1223 | mod->off_features[u].exts = NULL; |
| 1224 | } |
| 1225 | } |
| 1226 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1227 | /** |
| 1228 | * @brief Validate and normalize numeric value from a range definition. |
| 1229 | * @param[in] ctx Compile context. |
| 1230 | * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to |
| 1231 | * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into |
| 1232 | * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from |
| 1233 | * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100. |
| 1234 | * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64. |
| 1235 | * @param[in] value String value of the range boundary. |
| 1236 | * @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. |
| 1237 | * @param[out] valcopy NULL-terminated string with the numeric value to parse and store. |
| 1238 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value). |
| 1239 | */ |
Radek Krejci | e88beef | 2019-05-30 15:47:19 +0200 | [diff] [blame] | 1240 | LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1241 | 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] | 1242 | { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1243 | size_t fraction = 0, size; |
| 1244 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1245 | *len = 0; |
| 1246 | |
| 1247 | assert(value); |
| 1248 | /* parse value */ |
| 1249 | if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) { |
| 1250 | return LY_EVALID; |
| 1251 | } |
| 1252 | |
| 1253 | if ((value[*len] == '-') || (value[*len] == '+')) { |
| 1254 | ++(*len); |
| 1255 | } |
| 1256 | |
| 1257 | while (isdigit(value[*len])) { |
| 1258 | ++(*len); |
| 1259 | } |
| 1260 | |
| 1261 | if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1262 | if (basetype == LY_TYPE_DEC64) { |
| 1263 | goto decimal; |
| 1264 | } else { |
| 1265 | *valcopy = strndup(value, *len); |
| 1266 | return LY_SUCCESS; |
| 1267 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1268 | } |
| 1269 | fraction = *len; |
| 1270 | |
| 1271 | ++(*len); |
| 1272 | while (isdigit(value[*len])) { |
| 1273 | ++(*len); |
| 1274 | } |
| 1275 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1276 | if (basetype == LY_TYPE_DEC64) { |
| 1277 | decimal: |
| 1278 | assert(frdigits); |
Radek Krejci | 943177f | 2019-06-14 16:32:43 +0200 | [diff] [blame] | 1279 | if (fraction && (*len - 1 - fraction > frdigits)) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1280 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1281 | "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.", |
| 1282 | *len, value, frdigits); |
| 1283 | return LY_EINVAL; |
| 1284 | } |
| 1285 | if (fraction) { |
| 1286 | size = (*len) + (frdigits - ((*len) - 1 - fraction)); |
| 1287 | } else { |
| 1288 | size = (*len) + frdigits + 1; |
| 1289 | } |
| 1290 | *valcopy = malloc(size * sizeof **valcopy); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1291 | LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM); |
| 1292 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1293 | (*valcopy)[size - 1] = '\0'; |
| 1294 | if (fraction) { |
| 1295 | memcpy(&(*valcopy)[0], &value[0], fraction); |
| 1296 | memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction)); |
| 1297 | memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction)); |
| 1298 | } else { |
| 1299 | memcpy(&(*valcopy)[0], &value[0], *len); |
| 1300 | memset(&(*valcopy)[*len], '0', frdigits); |
| 1301 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1302 | } |
| 1303 | return LY_SUCCESS; |
| 1304 | } |
| 1305 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1306 | /** |
| 1307 | * @brief Check that values in range are in ascendant order. |
| 1308 | * @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] | 1309 | * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous, |
| 1310 | * max can be also equal. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1311 | * @param[in] value Current value to check. |
| 1312 | * @param[in] prev_value The last seen value. |
| 1313 | * @return LY_SUCCESS or LY_EEXIST for invalid order. |
| 1314 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1315 | static LY_ERR |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1316 | 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] | 1317 | { |
| 1318 | if (unsigned_value) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1319 | 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] | 1320 | return LY_EEXIST; |
| 1321 | } |
| 1322 | } else { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1323 | if ((max && prev_value > value) || (!max && prev_value >= value)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1324 | return LY_EEXIST; |
| 1325 | } |
| 1326 | } |
| 1327 | return LY_SUCCESS; |
| 1328 | } |
| 1329 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1330 | /** |
| 1331 | * @brief Set min/max value of the range part. |
| 1332 | * @param[in] ctx Compile context. |
| 1333 | * @param[in] part Range part structure to fill. |
| 1334 | * @param[in] max Flag to distinguish if storing min or max value. |
| 1335 | * @param[in] prev The last seen value to check that all values in range are specified in ascendant order. |
| 1336 | * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules. |
| 1337 | * @param[in] first Flag for the first value of the range to avoid ascendancy order. |
| 1338 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1339 | * @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] | 1340 | * @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] | 1341 | * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set. |
| 1342 | * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number), |
| 1343 | * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the |
| 1344 | * frdigits value), LY_EMEM. |
| 1345 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1346 | static LY_ERR |
| 1347 | 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] | 1348 | uint8_t frdigits, struct lysc_range *base_range, const char **value) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1349 | { |
| 1350 | LY_ERR ret = LY_SUCCESS; |
| 1351 | char *valcopy = NULL; |
| 1352 | size_t len; |
| 1353 | |
| 1354 | if (value) { |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1355 | ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1356 | LY_CHECK_GOTO(ret, finalize); |
| 1357 | } |
| 1358 | if (!valcopy && base_range) { |
| 1359 | if (max) { |
| 1360 | part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64; |
| 1361 | } else { |
| 1362 | part->min_64 = base_range->parts[0].min_64; |
| 1363 | } |
| 1364 | if (!first) { |
| 1365 | ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev); |
| 1366 | } |
| 1367 | goto finalize; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | switch (basetype) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1371 | case LY_TYPE_INT8: /* range */ |
| 1372 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1373 | 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] | 1374 | } else if (max) { |
| 1375 | part->max_64 = INT64_C(127); |
| 1376 | } else { |
| 1377 | part->min_64 = INT64_C(-128); |
| 1378 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1379 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1380 | 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] | 1381 | } |
| 1382 | break; |
| 1383 | case LY_TYPE_INT16: /* range */ |
| 1384 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1385 | 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] | 1386 | } else if (max) { |
| 1387 | part->max_64 = INT64_C(32767); |
| 1388 | } else { |
| 1389 | part->min_64 = INT64_C(-32768); |
| 1390 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1391 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1392 | 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] | 1393 | } |
| 1394 | break; |
| 1395 | case LY_TYPE_INT32: /* range */ |
| 1396 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1397 | 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] | 1398 | } else if (max) { |
| 1399 | part->max_64 = INT64_C(2147483647); |
| 1400 | } else { |
| 1401 | part->min_64 = INT64_C(-2147483648); |
| 1402 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1403 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1404 | 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] | 1405 | } |
| 1406 | break; |
| 1407 | case LY_TYPE_INT64: /* range */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1408 | case LY_TYPE_DEC64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1409 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1410 | 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] | 1411 | max ? &part->max_64 : &part->min_64); |
| 1412 | } else if (max) { |
| 1413 | part->max_64 = INT64_C(9223372036854775807); |
| 1414 | } else { |
| 1415 | part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1); |
| 1416 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1417 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1418 | 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] | 1419 | } |
| 1420 | break; |
| 1421 | case LY_TYPE_UINT8: /* range */ |
| 1422 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1423 | 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] | 1424 | } else if (max) { |
| 1425 | part->max_u64 = UINT64_C(255); |
| 1426 | } else { |
| 1427 | part->min_u64 = UINT64_C(0); |
| 1428 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1429 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1430 | 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] | 1431 | } |
| 1432 | break; |
| 1433 | case LY_TYPE_UINT16: /* range */ |
| 1434 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1435 | 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] | 1436 | } else if (max) { |
| 1437 | part->max_u64 = UINT64_C(65535); |
| 1438 | } else { |
| 1439 | part->min_u64 = UINT64_C(0); |
| 1440 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1441 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1442 | 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] | 1443 | } |
| 1444 | break; |
| 1445 | case LY_TYPE_UINT32: /* range */ |
| 1446 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1447 | 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] | 1448 | } else if (max) { |
| 1449 | part->max_u64 = UINT64_C(4294967295); |
| 1450 | } else { |
| 1451 | part->min_u64 = UINT64_C(0); |
| 1452 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1453 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1454 | 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] | 1455 | } |
| 1456 | break; |
| 1457 | case LY_TYPE_UINT64: /* range */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1458 | case LY_TYPE_STRING: /* length */ |
Radek Krejci | 25cfef7 | 2018-11-23 14:15:52 +0100 | [diff] [blame] | 1459 | case LY_TYPE_BINARY: /* length */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1460 | if (valcopy) { |
Radek Krejci | 249973a | 2019-06-10 10:50:54 +0200 | [diff] [blame] | 1461 | 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] | 1462 | } else if (max) { |
| 1463 | part->max_u64 = UINT64_C(18446744073709551615); |
| 1464 | } else { |
| 1465 | part->min_u64 = UINT64_C(0); |
| 1466 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1467 | if (!ret && !first) { |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1468 | 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] | 1469 | } |
| 1470 | break; |
| 1471 | default: |
| 1472 | LOGINT(ctx->ctx); |
| 1473 | ret = LY_EINT; |
| 1474 | } |
| 1475 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1476 | finalize: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1477 | if (ret == LY_EDENIED) { |
| 1478 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1479 | "Invalid %s restriction - value \"%s\" does not fit the type limitations.", |
| 1480 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1481 | } else if (ret == LY_EVALID) { |
| 1482 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1483 | "Invalid %s restriction - invalid value \"%s\".", |
| 1484 | length_restr ? "length" : "range", valcopy ? valcopy : *value); |
| 1485 | } else if (ret == LY_EEXIST) { |
| 1486 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1487 | "Invalid %s restriction - values are not in ascending order (%s).", |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1488 | length_restr ? "length" : "range", |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1489 | (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min"); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1490 | } else if (!ret && value) { |
| 1491 | *value = *value + len; |
| 1492 | } |
| 1493 | free(valcopy); |
| 1494 | return ret; |
| 1495 | } |
| 1496 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1497 | /** |
| 1498 | * @brief Compile the parsed range restriction. |
| 1499 | * @param[in] ctx Compile context. |
| 1500 | * @param[in] range_p Parsed range structure to compile. |
| 1501 | * @param[in] basetype Base YANG built-in type of the node with the range restriction. |
| 1502 | * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging. |
| 1503 | * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype. |
| 1504 | * @param[in] base_range Range restriction of the type from which the current type is derived. The current |
| 1505 | * range restriction must be more restrictive than the base_range. |
| 1506 | * @param[in,out] range Pointer to the created current range structure. |
| 1507 | * @return LY_ERR value. |
| 1508 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1509 | static LY_ERR |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1510 | 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] | 1511 | struct lysc_range *base_range, struct lysc_range **range) |
| 1512 | { |
| 1513 | LY_ERR ret = LY_EVALID; |
| 1514 | const char *expr; |
| 1515 | struct lysc_range_part *parts = NULL, *part; |
| 1516 | int range_expected = 0, uns; |
| 1517 | unsigned int parts_done = 0, u, v; |
| 1518 | |
| 1519 | assert(range); |
| 1520 | assert(range_p); |
| 1521 | |
| 1522 | expr = range_p->arg; |
| 1523 | while(1) { |
| 1524 | if (isspace(*expr)) { |
| 1525 | ++expr; |
| 1526 | } else if (*expr == '\0') { |
| 1527 | if (range_expected) { |
| 1528 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1529 | "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).", |
| 1530 | length_restr ? "length" : "range", range_p->arg); |
| 1531 | goto cleanup; |
| 1532 | } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) { |
| 1533 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1534 | "Invalid %s restriction - unexpected end of the expression (%s).", |
| 1535 | length_restr ? "length" : "range", range_p->arg); |
| 1536 | goto cleanup; |
| 1537 | } |
| 1538 | parts_done++; |
| 1539 | break; |
| 1540 | } else if (!strncmp(expr, "min", 3)) { |
| 1541 | if (parts) { |
| 1542 | /* min cannot be used elsewhere than in the first part */ |
| 1543 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1544 | "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range", |
| 1545 | expr - range_p->arg, range_p->arg); |
| 1546 | goto cleanup; |
| 1547 | } |
| 1548 | expr += 3; |
| 1549 | |
| 1550 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1551 | 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] | 1552 | part->max_64 = part->min_64; |
| 1553 | } else if (*expr == '|') { |
| 1554 | if (!parts || range_expected) { |
| 1555 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1556 | "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr); |
| 1557 | goto cleanup; |
| 1558 | } |
| 1559 | expr++; |
| 1560 | parts_done++; |
| 1561 | /* process next part of the expression */ |
| 1562 | } else if (!strncmp(expr, "..", 2)) { |
| 1563 | expr += 2; |
| 1564 | while (isspace(*expr)) { |
| 1565 | expr++; |
| 1566 | } |
| 1567 | if (!parts || LY_ARRAY_SIZE(parts) == parts_done) { |
| 1568 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1569 | "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range"); |
| 1570 | goto cleanup; |
| 1571 | } |
| 1572 | /* continue expecting the upper boundary */ |
| 1573 | range_expected = 1; |
| 1574 | } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) { |
| 1575 | /* number */ |
| 1576 | if (range_expected) { |
| 1577 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1578 | 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] | 1579 | range_expected = 0; |
| 1580 | } else { |
| 1581 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1582 | 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] | 1583 | basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1584 | part->max_64 = part->min_64; |
| 1585 | } |
| 1586 | |
| 1587 | /* continue with possible another expression part */ |
| 1588 | } else if (!strncmp(expr, "max", 3)) { |
| 1589 | expr += 3; |
| 1590 | while (isspace(*expr)) { |
| 1591 | expr++; |
| 1592 | } |
| 1593 | if (*expr != '\0') { |
| 1594 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).", |
| 1595 | length_restr ? "length" : "range", expr); |
| 1596 | goto cleanup; |
| 1597 | } |
| 1598 | if (range_expected) { |
| 1599 | part = &parts[LY_ARRAY_SIZE(parts) - 1]; |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1600 | 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] | 1601 | range_expected = 0; |
| 1602 | } else { |
| 1603 | LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup); |
| 1604 | 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] | 1605 | basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1606 | part->min_64 = part->max_64; |
| 1607 | } |
| 1608 | } else { |
| 1609 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).", |
| 1610 | length_restr ? "length" : "range", expr); |
| 1611 | goto cleanup; |
| 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | /* check with the previous range/length restriction */ |
| 1616 | if (base_range) { |
| 1617 | switch (basetype) { |
| 1618 | case LY_TYPE_BINARY: |
| 1619 | case LY_TYPE_UINT8: |
| 1620 | case LY_TYPE_UINT16: |
| 1621 | case LY_TYPE_UINT32: |
| 1622 | case LY_TYPE_UINT64: |
| 1623 | case LY_TYPE_STRING: |
| 1624 | uns = 1; |
| 1625 | break; |
| 1626 | case LY_TYPE_DEC64: |
| 1627 | case LY_TYPE_INT8: |
| 1628 | case LY_TYPE_INT16: |
| 1629 | case LY_TYPE_INT32: |
| 1630 | case LY_TYPE_INT64: |
| 1631 | uns = 0; |
| 1632 | break; |
| 1633 | default: |
| 1634 | LOGINT(ctx->ctx); |
| 1635 | ret = LY_EINT; |
| 1636 | goto cleanup; |
| 1637 | } |
| 1638 | for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) { |
| 1639 | if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) { |
| 1640 | goto baseerror; |
| 1641 | } |
| 1642 | /* current lower bound is not lower than the base */ |
| 1643 | if (base_range->parts[v].min_64 == base_range->parts[v].max_64) { |
| 1644 | /* base has single value */ |
| 1645 | if (base_range->parts[v].min_64 == parts[u].min_64) { |
| 1646 | /* both lower bounds are the same */ |
| 1647 | if (parts[u].min_64 != parts[u].max_64) { |
| 1648 | /* current continues with a range */ |
| 1649 | goto baseerror; |
| 1650 | } else { |
| 1651 | /* equal single values, move both forward */ |
| 1652 | ++v; |
| 1653 | continue; |
| 1654 | } |
| 1655 | } else { |
| 1656 | /* base is single value lower than current range, so the |
| 1657 | * value from base range is removed in the current, |
| 1658 | * move only base and repeat checking */ |
| 1659 | ++v; |
| 1660 | --u; |
| 1661 | continue; |
| 1662 | } |
| 1663 | } else { |
| 1664 | /* base is the range */ |
| 1665 | if (parts[u].min_64 == parts[u].max_64) { |
| 1666 | /* current is a single value */ |
| 1667 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1668 | /* current is behind the base range, so base range is omitted, |
| 1669 | * move the base and keep the current for further check */ |
| 1670 | ++v; |
| 1671 | --u; |
| 1672 | } /* else it is within the base range, so move the current, but keep the base */ |
| 1673 | continue; |
| 1674 | } else { |
| 1675 | /* both are ranges - check the higher bound, the lower was already checked */ |
| 1676 | if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) { |
| 1677 | /* higher bound is higher than the current higher bound */ |
| 1678 | if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) { |
| 1679 | /* but the current lower bound is also higher, so the base range is omitted, |
| 1680 | * continue with the same current, but move the base */ |
| 1681 | --u; |
| 1682 | ++v; |
| 1683 | continue; |
| 1684 | } |
| 1685 | /* current range starts within the base range but end behind it */ |
| 1686 | goto baseerror; |
| 1687 | } else { |
| 1688 | /* current range is smaller than the base, |
| 1689 | * move current, but stay with the base */ |
| 1690 | continue; |
| 1691 | } |
| 1692 | } |
| 1693 | } |
| 1694 | } |
| 1695 | if (u != parts_done) { |
| 1696 | baseerror: |
| 1697 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 1698 | "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.", |
| 1699 | length_restr ? "length" : "range", range_p->arg); |
| 1700 | goto cleanup; |
| 1701 | } |
| 1702 | } |
| 1703 | |
| 1704 | if (!(*range)) { |
| 1705 | *range = calloc(1, sizeof **range); |
| 1706 | LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM); |
| 1707 | } |
| 1708 | |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1709 | /* we rewrite the following values as the types chain is being processed */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1710 | if (range_p->eapptag) { |
| 1711 | lydict_remove(ctx->ctx, (*range)->eapptag); |
| 1712 | (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0); |
| 1713 | } |
| 1714 | if (range_p->emsg) { |
| 1715 | lydict_remove(ctx->ctx, (*range)->emsg); |
| 1716 | (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0); |
| 1717 | } |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1718 | if (range_p->dsc) { |
| 1719 | lydict_remove(ctx->ctx, (*range)->dsc); |
| 1720 | (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0); |
| 1721 | } |
| 1722 | if (range_p->ref) { |
| 1723 | lydict_remove(ctx->ctx, (*range)->ref); |
| 1724 | (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0); |
| 1725 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1726 | /* extensions are taken only from the last range by the caller */ |
| 1727 | |
| 1728 | (*range)->parts = parts; |
| 1729 | parts = NULL; |
| 1730 | ret = LY_SUCCESS; |
| 1731 | cleanup: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1732 | LY_ARRAY_FREE(parts); |
| 1733 | |
| 1734 | return ret; |
| 1735 | } |
| 1736 | |
| 1737 | /** |
| 1738 | * @brief Checks pattern syntax. |
| 1739 | * |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1740 | * @param[in] ctx Compile context. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1741 | * @param[in] pattern Pattern to check. |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1742 | * @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] | 1743 | * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID. |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1744 | */ |
| 1745 | static LY_ERR |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1746 | 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] | 1747 | { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1748 | int idx, idx2, start, end, count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1749 | char *perl_regex, *ptr; |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1750 | int err_code; |
| 1751 | const char *orig_ptr; |
| 1752 | PCRE2_SIZE err_offset; |
| 1753 | pcre2_code *code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1754 | #define URANGE_LEN 19 |
| 1755 | char *ublock2urange[][2] = { |
| 1756 | {"BasicLatin", "[\\x{0000}-\\x{007F}]"}, |
| 1757 | {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"}, |
| 1758 | {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"}, |
| 1759 | {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"}, |
| 1760 | {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"}, |
| 1761 | {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"}, |
| 1762 | {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"}, |
| 1763 | {"Greek", "[\\x{0370}-\\x{03FF}]"}, |
| 1764 | {"Cyrillic", "[\\x{0400}-\\x{04FF}]"}, |
| 1765 | {"Armenian", "[\\x{0530}-\\x{058F}]"}, |
| 1766 | {"Hebrew", "[\\x{0590}-\\x{05FF}]"}, |
| 1767 | {"Arabic", "[\\x{0600}-\\x{06FF}]"}, |
| 1768 | {"Syriac", "[\\x{0700}-\\x{074F}]"}, |
| 1769 | {"Thaana", "[\\x{0780}-\\x{07BF}]"}, |
| 1770 | {"Devanagari", "[\\x{0900}-\\x{097F}]"}, |
| 1771 | {"Bengali", "[\\x{0980}-\\x{09FF}]"}, |
| 1772 | {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"}, |
| 1773 | {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"}, |
| 1774 | {"Oriya", "[\\x{0B00}-\\x{0B7F}]"}, |
| 1775 | {"Tamil", "[\\x{0B80}-\\x{0BFF}]"}, |
| 1776 | {"Telugu", "[\\x{0C00}-\\x{0C7F}]"}, |
| 1777 | {"Kannada", "[\\x{0C80}-\\x{0CFF}]"}, |
| 1778 | {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"}, |
| 1779 | {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"}, |
| 1780 | {"Thai", "[\\x{0E00}-\\x{0E7F}]"}, |
| 1781 | {"Lao", "[\\x{0E80}-\\x{0EFF}]"}, |
| 1782 | {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"}, |
| 1783 | {"Myanmar", "[\\x{1000}-\\x{109F}]"}, |
| 1784 | {"Georgian", "[\\x{10A0}-\\x{10FF}]"}, |
| 1785 | {"HangulJamo", "[\\x{1100}-\\x{11FF}]"}, |
| 1786 | {"Ethiopic", "[\\x{1200}-\\x{137F}]"}, |
| 1787 | {"Cherokee", "[\\x{13A0}-\\x{13FF}]"}, |
| 1788 | {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"}, |
| 1789 | {"Ogham", "[\\x{1680}-\\x{169F}]"}, |
| 1790 | {"Runic", "[\\x{16A0}-\\x{16FF}]"}, |
| 1791 | {"Khmer", "[\\x{1780}-\\x{17FF}]"}, |
| 1792 | {"Mongolian", "[\\x{1800}-\\x{18AF}]"}, |
| 1793 | {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"}, |
| 1794 | {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"}, |
| 1795 | {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"}, |
| 1796 | {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"}, |
| 1797 | {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"}, |
| 1798 | {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"}, |
| 1799 | {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"}, |
| 1800 | {"NumberForms", "[\\x{2150}-\\x{218F}]"}, |
| 1801 | {"Arrows", "[\\x{2190}-\\x{21FF}]"}, |
| 1802 | {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"}, |
| 1803 | {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"}, |
| 1804 | {"ControlPictures", "[\\x{2400}-\\x{243F}]"}, |
| 1805 | {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"}, |
| 1806 | {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"}, |
| 1807 | {"BoxDrawing", "[\\x{2500}-\\x{257F}]"}, |
| 1808 | {"BlockElements", "[\\x{2580}-\\x{259F}]"}, |
| 1809 | {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"}, |
| 1810 | {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"}, |
| 1811 | {"Dingbats", "[\\x{2700}-\\x{27BF}]"}, |
| 1812 | {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"}, |
| 1813 | {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"}, |
| 1814 | {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"}, |
| 1815 | {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"}, |
| 1816 | {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"}, |
| 1817 | {"Hiragana", "[\\x{3040}-\\x{309F}]"}, |
| 1818 | {"Katakana", "[\\x{30A0}-\\x{30FF}]"}, |
| 1819 | {"Bopomofo", "[\\x{3100}-\\x{312F}]"}, |
| 1820 | {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"}, |
| 1821 | {"Kanbun", "[\\x{3190}-\\x{319F}]"}, |
| 1822 | {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"}, |
| 1823 | {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"}, |
| 1824 | {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"}, |
| 1825 | {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"}, |
| 1826 | {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"}, |
| 1827 | {"YiSyllables", "[\\x{A000}-\\x{A48F}]"}, |
| 1828 | {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"}, |
| 1829 | {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"}, |
| 1830 | {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"}, |
| 1831 | {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"}, |
| 1832 | {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"}, |
| 1833 | {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"}, |
| 1834 | {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"}, |
| 1835 | {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"}, |
| 1836 | {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"}, |
| 1837 | {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"}, |
| 1838 | {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"}, |
| 1839 | {NULL, NULL} |
| 1840 | }; |
| 1841 | |
| 1842 | /* adjust the expression to a Perl equivalent |
| 1843 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */ |
| 1844 | |
| 1845 | /* we need to replace all "$" with "\$", count them now */ |
| 1846 | for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$")); |
| 1847 | |
| 1848 | perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char)); |
| 1849 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM); |
| 1850 | perl_regex[0] = '\0'; |
| 1851 | |
| 1852 | ptr = perl_regex; |
| 1853 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1854 | for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) { |
| 1855 | if (orig_ptr[0] == '$') { |
| 1856 | ptr += sprintf(ptr, "\\$"); |
| 1857 | } else if (orig_ptr[0] == '^') { |
| 1858 | ptr += sprintf(ptr, "\\^"); |
| 1859 | } else { |
| 1860 | ptr[0] = orig_ptr[0]; |
| 1861 | ++ptr; |
| 1862 | } |
| 1863 | } |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 1864 | ptr[0] = '\0'; |
| 1865 | ++ptr; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1866 | |
| 1867 | /* substitute Unicode Character Blocks with exact Character Ranges */ |
| 1868 | while ((ptr = strstr(perl_regex, "\\p{Is"))) { |
| 1869 | start = ptr - perl_regex; |
| 1870 | |
| 1871 | ptr = strchr(ptr, '}'); |
| 1872 | if (!ptr) { |
| 1873 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, |
| 1874 | pattern, perl_regex + start + 2, "unterminated character property"); |
| 1875 | free(perl_regex); |
| 1876 | return LY_EVALID; |
| 1877 | } |
| 1878 | end = (ptr - perl_regex) + 1; |
| 1879 | |
| 1880 | /* need more space */ |
| 1881 | if (end - start < URANGE_LEN) { |
| 1882 | perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1); |
| 1883 | LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM); |
| 1884 | } |
| 1885 | |
| 1886 | /* find our range */ |
| 1887 | for (idx = 0; ublock2urange[idx][0]; ++idx) { |
| 1888 | if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) { |
| 1889 | break; |
| 1890 | } |
| 1891 | } |
| 1892 | if (!ublock2urange[idx][0]) { |
| 1893 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, |
| 1894 | pattern, perl_regex + start + 5, "unknown block name"); |
| 1895 | free(perl_regex); |
| 1896 | return LY_EVALID; |
| 1897 | } |
| 1898 | |
| 1899 | /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */ |
| 1900 | for (idx2 = 0, count = 0; idx2 < start; ++idx2) { |
| 1901 | if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 1902 | ++count; |
| 1903 | } |
| 1904 | if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) { |
| 1905 | --count; |
| 1906 | } |
| 1907 | } |
| 1908 | if (count) { |
| 1909 | /* skip brackets */ |
| 1910 | memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1); |
| 1911 | memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2); |
| 1912 | } else { |
| 1913 | memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1); |
| 1914 | memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN); |
| 1915 | } |
| 1916 | } |
| 1917 | |
| 1918 | /* must return 0, already checked during parsing */ |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 1919 | code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, |
| 1920 | 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] | 1921 | &err_code, &err_offset, NULL); |
| 1922 | if (!code_local) { |
| 1923 | PCRE2_UCHAR err_msg[256] = {0}; |
| 1924 | pcre2_get_error_message(err_code, err_msg, 256); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1925 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg); |
| 1926 | free(perl_regex); |
| 1927 | return LY_EVALID; |
| 1928 | } |
| 1929 | free(perl_regex); |
| 1930 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1931 | if (code) { |
| 1932 | *code = code_local; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1933 | } else { |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1934 | free(code_local); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1935 | } |
| 1936 | |
| 1937 | return LY_SUCCESS; |
| 1938 | |
| 1939 | #undef URANGE_LEN |
| 1940 | } |
| 1941 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1942 | /** |
| 1943 | * @brief Compile parsed pattern restriction in conjunction with the patterns from base type. |
| 1944 | * @param[in] ctx Compile context. |
| 1945 | * @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] | 1946 | * @param[in] base_patterns Compiled patterns from the type from which the current type is derived. |
| 1947 | * Patterns from the base type are inherited to have all the patterns that have to match at one place. |
| 1948 | * @param[out] patterns Pointer to the storage for the patterns of the current type. |
| 1949 | * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID. |
| 1950 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1951 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 1952 | 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] | 1953 | struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns) |
| 1954 | { |
| 1955 | struct lysc_pattern **pattern; |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1956 | unsigned int u; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1957 | LY_ERR ret = LY_SUCCESS; |
| 1958 | |
| 1959 | /* first, copy the patterns from the base type */ |
| 1960 | if (base_patterns) { |
| 1961 | *patterns = lysc_patterns_dup(ctx->ctx, base_patterns); |
| 1962 | LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM); |
| 1963 | } |
| 1964 | |
| 1965 | LY_ARRAY_FOR(patterns_p, u) { |
| 1966 | LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM); |
| 1967 | *pattern = calloc(1, sizeof **pattern); |
| 1968 | ++(*pattern)->refcount; |
| 1969 | |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1970 | 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] | 1971 | LY_CHECK_RET(ret); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1972 | |
| 1973 | if (patterns_p[u].arg[0] == 0x15) { |
| 1974 | (*pattern)->inverted = 1; |
| 1975 | } |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1976 | DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1977 | DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag); |
| 1978 | DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 1979 | DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc); |
| 1980 | DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref); |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 1981 | 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] | 1982 | } |
| 1983 | done: |
| 1984 | return ret; |
| 1985 | } |
| 1986 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1987 | /** |
| 1988 | * @brief map of the possible restrictions combination for the specific built-in type. |
| 1989 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1990 | static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = { |
| 1991 | 0 /* LY_TYPE_UNKNOWN */, |
| 1992 | LYS_SET_LENGTH /* LY_TYPE_BINARY */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1993 | LYS_SET_RANGE /* LY_TYPE_UINT8 */, |
| 1994 | LYS_SET_RANGE /* LY_TYPE_UINT16 */, |
| 1995 | LYS_SET_RANGE /* LY_TYPE_UINT32 */, |
| 1996 | LYS_SET_RANGE /* LY_TYPE_UINT64 */, |
| 1997 | LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 1998 | LYS_SET_BIT /* LY_TYPE_BITS */, |
| 1999 | 0 /* LY_TYPE_BOOL */, |
| 2000 | LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */, |
| 2001 | 0 /* LY_TYPE_EMPTY */, |
| 2002 | LYS_SET_ENUM /* LY_TYPE_ENUM */, |
| 2003 | LYS_SET_BASE /* LY_TYPE_IDENT */, |
| 2004 | LYS_SET_REQINST /* LY_TYPE_INST */, |
| 2005 | LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2006 | LYS_SET_TYPE /* LY_TYPE_UNION */, |
| 2007 | LYS_SET_RANGE /* LY_TYPE_INT8 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2008 | LYS_SET_RANGE /* LY_TYPE_INT16 */, |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2009 | LYS_SET_RANGE /* LY_TYPE_INT32 */, |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 2010 | LYS_SET_RANGE /* LY_TYPE_INT64 */ |
| 2011 | }; |
| 2012 | |
| 2013 | /** |
| 2014 | * @brief stringification of the YANG built-in data types |
| 2015 | */ |
| 2016 | const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer", |
| 2017 | "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration", |
| 2018 | "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] | 2019 | }; |
| 2020 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2021 | /** |
| 2022 | * @brief Compile parsed type's enum structures (for enumeration and bits types). |
| 2023 | * @param[in] ctx Compile context. |
| 2024 | * @param[in] enums_p Array of the parsed enum structures to compile. |
| 2025 | * @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] | 2026 | * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible. |
| 2027 | * @param[out] enums Newly created array of the compiled enums information for the current type. |
| 2028 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2029 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2030 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2031 | 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] | 2032 | 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] | 2033 | { |
| 2034 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 4ad42aa | 2019-07-23 16:55:58 +0200 | [diff] [blame] | 2035 | unsigned int u, v, match = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2036 | int32_t value = 0; |
| 2037 | uint32_t position = 0; |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2038 | struct lysc_type_bitenum_item *e, storage; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2039 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2040 | if (base_enums && ctx->mod_def->version < 2) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2041 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.", |
| 2042 | basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits"); |
| 2043 | return LY_EVALID; |
| 2044 | } |
| 2045 | |
| 2046 | LY_ARRAY_FOR(enums_p, u) { |
| 2047 | LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM); |
| 2048 | DUP_STRING(ctx->ctx, enums_p[u].name, e->name); |
Radek Krejci | c8b3100 | 2019-01-08 10:24:45 +0100 | [diff] [blame] | 2049 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc); |
| 2050 | DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref); |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2051 | e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2052 | if (base_enums) { |
| 2053 | /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */ |
| 2054 | LY_ARRAY_FOR(base_enums, v) { |
| 2055 | if (!strcmp(e->name, base_enums[v].name)) { |
| 2056 | break; |
| 2057 | } |
| 2058 | } |
| 2059 | if (v == LY_ARRAY_SIZE(base_enums)) { |
| 2060 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2061 | "Invalid %s - derived type adds new item \"%s\".", |
| 2062 | basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name); |
| 2063 | return LY_EVALID; |
| 2064 | } |
| 2065 | match = v; |
| 2066 | } |
| 2067 | |
| 2068 | if (basetype == LY_TYPE_ENUM) { |
Radek Krejci | 693262f | 2019-04-29 15:23:20 +0200 | [diff] [blame] | 2069 | e->flags |= LYS_ISENUM; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2070 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2071 | e->value = (int32_t)enums_p[u].value; |
| 2072 | if (!u || e->value >= value) { |
| 2073 | value = e->value + 1; |
| 2074 | } |
| 2075 | /* check collision with other values */ |
| 2076 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 2077 | if (e->value == (*enums)[v].value) { |
| 2078 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2079 | "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".", |
| 2080 | e->value, e->name, (*enums)[v].name); |
| 2081 | return LY_EVALID; |
| 2082 | } |
| 2083 | } |
| 2084 | } else if (base_enums) { |
| 2085 | /* inherit the assigned value */ |
| 2086 | e->value = base_enums[match].value; |
| 2087 | if (!u || e->value >= value) { |
| 2088 | value = e->value + 1; |
| 2089 | } |
| 2090 | } else { |
| 2091 | /* assign value automatically */ |
| 2092 | if (u && value == INT32_MIN) { |
| 2093 | /* counter overflow */ |
| 2094 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2095 | "Invalid enumeration - it is not possible to auto-assign enum value for " |
| 2096 | "\"%s\" since the highest value is already 2147483647.", e->name); |
| 2097 | return LY_EVALID; |
| 2098 | } |
| 2099 | e->value = value++; |
| 2100 | } |
| 2101 | } else { /* LY_TYPE_BITS */ |
| 2102 | if (enums_p[u].flags & LYS_SET_VALUE) { |
| 2103 | e->value = (int32_t)enums_p[u].value; |
| 2104 | if (!u || (uint32_t)e->value >= position) { |
| 2105 | position = (uint32_t)e->value + 1; |
| 2106 | } |
| 2107 | /* check collision with other values */ |
| 2108 | for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) { |
| 2109 | if (e->value == (*enums)[v].value) { |
| 2110 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2111 | "Invalid bits - position %u collide in items \"%s\" and \"%s\".", |
| 2112 | (uint32_t)e->value, e->name, (*enums)[v].name); |
| 2113 | return LY_EVALID; |
| 2114 | } |
| 2115 | } |
| 2116 | } else if (base_enums) { |
| 2117 | /* inherit the assigned value */ |
| 2118 | e->value = base_enums[match].value; |
| 2119 | if (!u || (uint32_t)e->value >= position) { |
| 2120 | position = (uint32_t)e->value + 1; |
| 2121 | } |
| 2122 | } else { |
| 2123 | /* assign value automatically */ |
| 2124 | if (u && position == 0) { |
| 2125 | /* counter overflow */ |
| 2126 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2127 | "Invalid bits - it is not possible to auto-assign bit position for " |
| 2128 | "\"%s\" since the highest value is already 4294967295.", e->name); |
| 2129 | return LY_EVALID; |
| 2130 | } |
| 2131 | e->value = position++; |
| 2132 | } |
| 2133 | } |
| 2134 | |
| 2135 | if (base_enums) { |
| 2136 | /* the assigned values must not change from the derived type */ |
| 2137 | if (e->value != base_enums[match].value) { |
| 2138 | if (basetype == LY_TYPE_ENUM) { |
| 2139 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2140 | "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.", |
| 2141 | e->name, base_enums[match].value, e->value); |
| 2142 | } else { |
| 2143 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2144 | "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.", |
| 2145 | e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value); |
| 2146 | } |
| 2147 | return LY_EVALID; |
| 2148 | } |
| 2149 | } |
| 2150 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2151 | 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] | 2152 | 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] | 2153 | |
| 2154 | if (basetype == LY_TYPE_BITS) { |
| 2155 | /* keep bits ordered by position */ |
| 2156 | for (v = u; v && (*enums)[v - 1].value > e->value; --v); |
| 2157 | if (v != u) { |
| 2158 | memcpy(&storage, e, sizeof *e); |
| 2159 | memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums); |
| 2160 | memcpy(&(*enums)[v], &storage, sizeof storage); |
| 2161 | } |
| 2162 | } |
| 2163 | } |
| 2164 | |
| 2165 | done: |
| 2166 | return ret; |
| 2167 | } |
| 2168 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2169 | #define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \ |
| 2170 | for ((NODE) = (NODE)->parent; \ |
| 2171 | (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \ |
| 2172 | (NODE) = (NODE)->parent); \ |
| 2173 | if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \ |
| 2174 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \ |
| 2175 | TERM; \ |
| 2176 | } |
| 2177 | |
| 2178 | /** |
| 2179 | * @brief Validate the predicate(s) from the leafref path. |
| 2180 | * @param[in] ctx Compile context |
| 2181 | * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s). |
| 2182 | * 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] | 2183 | * @param[in] start_node Path context node (where the path is instantiated). |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2184 | * @param[in] context_node Predicate context node (where the predicate is placed). |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2185 | * @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] | 2186 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2187 | */ |
| 2188 | static LY_ERR |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2189 | 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] | 2190 | 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] | 2191 | { |
| 2192 | LY_ERR ret = LY_EVALID; |
| 2193 | const struct lys_module *mod; |
| 2194 | const struct lysc_node *src_node, *dst_node; |
| 2195 | const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix; |
| 2196 | size_t src_len, src_prefix_len, dst_len, dst_prefix_len; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 2197 | unsigned int dest_parent_times, c; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2198 | const char *start, *end, *pke_end; |
| 2199 | struct ly_set keys = {0}; |
| 2200 | int i; |
| 2201 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2202 | assert(path_context); |
| 2203 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2204 | while (**predicate == '[') { |
| 2205 | start = (*predicate)++; |
| 2206 | |
| 2207 | while (isspace(**predicate)) { |
| 2208 | ++(*predicate); |
| 2209 | } |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2210 | 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] | 2211 | while (isspace(**predicate)) { |
| 2212 | ++(*predicate); |
| 2213 | } |
| 2214 | if (**predicate != '=') { |
| 2215 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2216 | "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.", |
| 2217 | *predicate - start + 1, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2218 | goto cleanup; |
| 2219 | } |
| 2220 | ++(*predicate); |
| 2221 | while (isspace(**predicate)) { |
| 2222 | ++(*predicate); |
| 2223 | } |
| 2224 | |
| 2225 | if ((end = pke_end = strchr(*predicate, ']')) == NULL) { |
| 2226 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2227 | "Invalid leafref path predicate \"%s\" - missing predicate termination.", start); |
| 2228 | goto cleanup; |
| 2229 | } |
| 2230 | --pke_end; |
| 2231 | while (isspace(*pke_end)) { |
| 2232 | --pke_end; |
| 2233 | } |
Radek Krejci | 7adf4ff | 2018-12-05 08:55:00 +0100 | [diff] [blame] | 2234 | ++pke_end; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2235 | /* localize path-key-expr */ |
| 2236 | pke_start = path_key_expr = *predicate; |
| 2237 | /* move after the current predicate */ |
| 2238 | *predicate = end + 1; |
| 2239 | |
| 2240 | /* source (must be leaf or leaf-list) */ |
| 2241 | if (src_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2242 | mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2243 | if (!mod) { |
| 2244 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2245 | "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2246 | *predicate - start, start, src_prefix_len, src_prefix, path_context->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2247 | goto cleanup; |
| 2248 | } |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2249 | if (!mod->implemented) { |
| 2250 | /* make the module implemented */ |
| 2251 | ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2); |
| 2252 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2253 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2254 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2255 | } |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2256 | src_node = NULL; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 2257 | if (!(context_node->flags & LYS_KEYLESS)) { |
| 2258 | struct lysc_node *key; |
| 2259 | for (key = context_node->child; key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY); key = key->next) { |
| 2260 | if (!strncmp(src, key->name, src_len) && key->name[src_len] == '\0') { |
| 2261 | src_node = key; |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2262 | break; |
| 2263 | } |
| 2264 | } |
| 2265 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2266 | if (!src_node) { |
| 2267 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2268 | "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.", |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2269 | *predicate - start, start, src_len, src, mod->name); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2270 | goto cleanup; |
| 2271 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2272 | |
| 2273 | /* check that there is only one predicate for the */ |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2274 | c = keys.count; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2275 | i = ly_set_add(&keys, (void*)src_node, 0); |
| 2276 | LY_CHECK_GOTO(i == -1, cleanup); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2277 | if (keys.count == c) { /* node was already present in the set */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2278 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2279 | "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".", |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2280 | *predicate - start, start, src_node->name); |
| 2281 | goto cleanup; |
| 2282 | } |
| 2283 | |
| 2284 | /* destination */ |
| 2285 | dest_parent_times = 0; |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2286 | dst_node = start_node; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2287 | |
| 2288 | /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */ |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2289 | if (strncmp(path_key_expr, "current", 7)) { |
| 2290 | error_current_function_invocation: |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2291 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2292 | "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.", |
| 2293 | *predicate - start, start); |
| 2294 | goto cleanup; |
| 2295 | } |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2296 | for (path_key_expr += 7; isspace(*path_key_expr); ++path_key_expr); |
| 2297 | if (*path_key_expr != '(') { |
| 2298 | goto error_current_function_invocation; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2299 | } |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2300 | for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr); |
| 2301 | if (*path_key_expr != ')') { |
| 2302 | goto error_current_function_invocation; |
| 2303 | } |
| 2304 | for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2305 | |
| 2306 | if (*path_key_expr != '/') { |
| 2307 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2308 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.", |
| 2309 | *predicate - start, start); |
| 2310 | goto cleanup; |
| 2311 | } |
| 2312 | ++path_key_expr; |
| 2313 | while (isspace(*path_key_expr)) { |
| 2314 | ++path_key_expr; |
| 2315 | } |
| 2316 | |
| 2317 | /* rel-path-keyexpr: |
| 2318 | * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */ |
| 2319 | while (!strncmp(path_key_expr, "..", 2)) { |
| 2320 | ++dest_parent_times; |
| 2321 | path_key_expr += 2; |
| 2322 | while (isspace(*path_key_expr)) { |
| 2323 | ++path_key_expr; |
| 2324 | } |
| 2325 | if (*path_key_expr != '/') { |
| 2326 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2327 | "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.", |
| 2328 | *predicate - start, start); |
| 2329 | goto cleanup; |
| 2330 | } |
| 2331 | ++path_key_expr; |
| 2332 | while (isspace(*path_key_expr)) { |
| 2333 | ++path_key_expr; |
| 2334 | } |
| 2335 | |
| 2336 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2337 | * all schema nodes that cannot be instantiated in data tree */ |
| 2338 | MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup, |
| 2339 | "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.", |
| 2340 | *predicate - start, start); |
| 2341 | } |
| 2342 | if (!dest_parent_times) { |
| 2343 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2344 | "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.", |
| 2345 | *predicate - start, start); |
| 2346 | goto cleanup; |
| 2347 | } |
| 2348 | if (path_key_expr == pke_end) { |
| 2349 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2350 | "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.", |
| 2351 | *predicate - start, start); |
| 2352 | goto cleanup; |
| 2353 | } |
| 2354 | |
| 2355 | while(path_key_expr != pke_end) { |
Radek Krejci | 7a3f89f | 2019-07-12 11:17:33 +0200 | [diff] [blame] | 2356 | for (;*path_key_expr == '/' || isspace(*path_key_expr); ++path_key_expr); |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2357 | 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] | 2358 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2359 | "Invalid node identifier in leafref path predicate - character %d (of %.*s).", |
| 2360 | path_key_expr - start + 1, *predicate - start, start); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2361 | goto cleanup; |
| 2362 | } |
| 2363 | |
| 2364 | if (dst_prefix) { |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 2365 | mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2366 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2367 | mod = start_node->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2368 | } |
| 2369 | if (!mod) { |
| 2370 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2371 | "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] | 2372 | *predicate - start, start, dst_len, dst); |
| 2373 | goto cleanup; |
| 2374 | } |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2375 | if (!mod->implemented) { |
| 2376 | /* make the module implemented */ |
| 2377 | ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2); |
| 2378 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2379 | |
| 2380 | dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK); |
| 2381 | if (!dst_node) { |
| 2382 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2383 | "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] | 2384 | *predicate - start, start, path_key_expr - pke_start, pke_start); |
| 2385 | goto cleanup; |
| 2386 | } |
| 2387 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2388 | 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] | 2389 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2390 | "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] | 2391 | *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype)); |
| 2392 | goto cleanup; |
| 2393 | } |
| 2394 | } |
| 2395 | |
| 2396 | ret = LY_SUCCESS; |
| 2397 | cleanup: |
| 2398 | ly_set_erase(&keys, NULL); |
| 2399 | return ret; |
| 2400 | } |
| 2401 | |
| 2402 | /** |
| 2403 | * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function. |
| 2404 | * |
| 2405 | * path-arg = absolute-path / relative-path |
| 2406 | * absolute-path = 1*("/" (node-identifier *path-predicate)) |
| 2407 | * relative-path = 1*(".." "/") descendant-path |
| 2408 | * |
| 2409 | * @param[in,out] path Path to parse. |
| 2410 | * @param[out] prefix Prefix of the token, NULL if there is not any. |
| 2411 | * @param[out] pref_len Length of the prefix, 0 if there is not any. |
| 2412 | * @param[out] name Name of the token. |
| 2413 | * @param[out] nam_len Length of the name. |
| 2414 | * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call, |
| 2415 | * must not be changed between consecutive calls. -1 if the |
| 2416 | * path is absolute. |
| 2417 | * @param[out] has_predicate Flag to mark whether there is a predicate specified. |
| 2418 | * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path. |
| 2419 | */ |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 2420 | LY_ERR |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2421 | lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len, |
| 2422 | int *parent_times, int *has_predicate) |
| 2423 | { |
| 2424 | int par_times = 0; |
| 2425 | |
| 2426 | assert(path && *path); |
| 2427 | assert(parent_times); |
| 2428 | assert(prefix); |
| 2429 | assert(prefix_len); |
| 2430 | assert(name); |
| 2431 | assert(name_len); |
| 2432 | assert(has_predicate); |
| 2433 | |
| 2434 | *prefix = NULL; |
| 2435 | *prefix_len = 0; |
| 2436 | *name = NULL; |
| 2437 | *name_len = 0; |
| 2438 | *has_predicate = 0; |
| 2439 | |
| 2440 | if (!*parent_times) { |
| 2441 | if (!strncmp(*path, "..", 2)) { |
| 2442 | *path += 2; |
| 2443 | ++par_times; |
| 2444 | while (!strncmp(*path, "/..", 3)) { |
| 2445 | *path += 3; |
| 2446 | ++par_times; |
| 2447 | } |
| 2448 | } |
| 2449 | if (par_times) { |
| 2450 | *parent_times = par_times; |
| 2451 | } else { |
| 2452 | *parent_times = -1; |
| 2453 | } |
| 2454 | } |
| 2455 | |
| 2456 | if (**path != '/') { |
| 2457 | return LY_EINVAL; |
| 2458 | } |
| 2459 | /* skip '/' */ |
| 2460 | ++(*path); |
| 2461 | |
| 2462 | /* node-identifier ([prefix:]name) */ |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 2463 | 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] | 2464 | |
| 2465 | if ((**path == '/' && (*path)[1]) || !**path) { |
| 2466 | /* path continues by another token or this is the last token */ |
| 2467 | return LY_SUCCESS; |
| 2468 | } else if ((*path)[0] != '[') { |
| 2469 | /* unexpected character */ |
| 2470 | return LY_EINVAL; |
| 2471 | } else { |
| 2472 | /* predicate starting with [ */ |
| 2473 | *has_predicate = 1; |
| 2474 | return LY_SUCCESS; |
| 2475 | } |
| 2476 | } |
| 2477 | |
| 2478 | /** |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2479 | * @brief Check the features used in if-feature statements applicable to the leafref and its target. |
| 2480 | * |
| 2481 | * The set of features used for target must be a subset of features used for the leafref. |
| 2482 | * This is not a perfect, we should compare the truth tables but it could require too much resources |
| 2483 | * and RFC 7950 does not require it explicitely, so we simplify that. |
| 2484 | * |
| 2485 | * @param[in] refnode The leafref node. |
| 2486 | * @param[in] target Tha target node of the leafref. |
| 2487 | * @return LY_SUCCESS or LY_EVALID; |
| 2488 | */ |
| 2489 | static LY_ERR |
| 2490 | lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target) |
| 2491 | { |
| 2492 | LY_ERR ret = LY_EVALID; |
| 2493 | const struct lysc_node *iter; |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2494 | unsigned int u, v, count; |
| 2495 | struct ly_set features = {0}; |
| 2496 | |
| 2497 | for (iter = refnode; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2498 | if (iter->iffeatures) { |
| 2499 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2500 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2501 | 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] | 2502 | } |
| 2503 | } |
| 2504 | } |
| 2505 | } |
| 2506 | |
| 2507 | /* we should have, in features set, a superset of features applicable to the target node. |
| 2508 | * So when adding features applicable to the target into the features set, we should not be |
| 2509 | * able to actually add any new feature, otherwise it is not a subset of features applicable |
| 2510 | * to the leafref itself. */ |
| 2511 | count = features.count; |
| 2512 | for (iter = target; iter; iter = iter->parent) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2513 | if (iter->iffeatures) { |
| 2514 | LY_ARRAY_FOR(iter->iffeatures, u) { |
| 2515 | LY_ARRAY_FOR(iter->iffeatures[u].features, v) { |
| 2516 | 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] | 2517 | /* new feature was added (or LY_EMEM) */ |
| 2518 | goto cleanup; |
| 2519 | } |
| 2520 | } |
| 2521 | } |
| 2522 | } |
| 2523 | } |
| 2524 | ret = LY_SUCCESS; |
| 2525 | |
| 2526 | cleanup: |
| 2527 | ly_set_erase(&features, NULL); |
| 2528 | return ret; |
| 2529 | } |
| 2530 | |
| 2531 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2532 | * @brief Validate the leafref path. |
| 2533 | * @param[in] ctx Compile context |
| 2534 | * @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] | 2535 | * @param[in] leafref Leafref to validate. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2536 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 2537 | */ |
| 2538 | static LY_ERR |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2539 | 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] | 2540 | { |
| 2541 | const struct lysc_node *node = NULL, *parent = NULL; |
| 2542 | const struct lys_module *mod; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2543 | struct lysc_type *type; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2544 | const char *id, *prefix, *name; |
| 2545 | size_t prefix_len, name_len; |
| 2546 | int parent_times = 0, has_predicate; |
| 2547 | unsigned int iter, u; |
| 2548 | LY_ERR ret = LY_SUCCESS; |
| 2549 | |
| 2550 | assert(ctx); |
| 2551 | assert(startnode); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2552 | assert(leafref); |
| 2553 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 2554 | ctx->path[0] = '\0'; |
| 2555 | lysc_path(startnode, LY_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE); |
| 2556 | ctx->path_len = strlen(ctx->path); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 2557 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2558 | iter = 0; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2559 | id = leafref->path; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2560 | while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) { |
| 2561 | if (!iter) { /* first iteration */ |
| 2562 | /* precess ".." in relative paths */ |
| 2563 | if (parent_times > 0) { |
| 2564 | /* move from the context node */ |
| 2565 | for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) { |
| 2566 | /* path is supposed to be evaluated in data tree, so we have to skip |
| 2567 | * all schema nodes that cannot be instantiated in data tree */ |
| 2568 | 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] | 2569 | "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2570 | } |
| 2571 | } |
| 2572 | } |
| 2573 | |
| 2574 | if (prefix) { |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2575 | mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2576 | } else { |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 2577 | mod = startnode->module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2578 | } |
| 2579 | if (!mod) { |
| 2580 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2581 | "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".", |
| 2582 | id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2583 | return LY_EVALID; |
| 2584 | } |
Radek Krejci | 3d92956 | 2019-02-21 11:25:55 +0100 | [diff] [blame] | 2585 | if (!mod->implemented) { |
| 2586 | /* make the module implemented */ |
| 2587 | ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2); |
| 2588 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2589 | |
| 2590 | node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK); |
| 2591 | if (!node) { |
| 2592 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2593 | "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2594 | return LY_EVALID; |
| 2595 | } |
| 2596 | parent = node; |
| 2597 | |
| 2598 | if (has_predicate) { |
| 2599 | /* we have predicate, so the current result must be list */ |
| 2600 | if (node->nodetype != LYS_LIST) { |
| 2601 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2602 | "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] | 2603 | id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2604 | return LY_EVALID; |
| 2605 | } |
| 2606 | |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2607 | LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context), |
| 2608 | LY_EVALID); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2609 | } |
| 2610 | |
| 2611 | ++iter; |
| 2612 | } |
| 2613 | if (ret) { |
| 2614 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2615 | "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] | 2616 | return LY_EVALID; |
| 2617 | } |
| 2618 | |
| 2619 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 2620 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2621 | "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] | 2622 | leafref->path, lys_nodetype2str(node->nodetype)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2623 | return LY_EVALID; |
| 2624 | } |
| 2625 | |
| 2626 | /* check status */ |
| 2627 | if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) { |
| 2628 | return LY_EVALID; |
| 2629 | } |
| 2630 | |
Radek Krejci | b57cf1e | 2018-11-23 14:07:05 +0100 | [diff] [blame] | 2631 | /* check config */ |
| 2632 | if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) { |
| 2633 | if (node->flags & LYS_CONFIG_R) { |
| 2634 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2635 | "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.", |
| 2636 | leafref->path); |
| 2637 | return LY_EVALID; |
| 2638 | } |
| 2639 | } |
| 2640 | |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2641 | /* store the target's type and check for circular chain of leafrefs */ |
| 2642 | leafref->realtype = ((struct lysc_node_leaf*)node)->type; |
| 2643 | for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) { |
| 2644 | if (type == (struct lysc_type*)leafref) { |
| 2645 | /* circular chain detected */ |
| 2646 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2647 | "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path); |
| 2648 | return LY_EVALID; |
| 2649 | } |
| 2650 | } |
| 2651 | |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 2652 | /* check if leafref and its target are under common if-features */ |
| 2653 | if (lys_compile_leafref_features_validate(startnode, node)) { |
| 2654 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 2655 | "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.", |
| 2656 | leafref->path); |
| 2657 | return LY_EVALID; |
| 2658 | } |
| 2659 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 2660 | ctx->path_len = 1; |
| 2661 | ctx->path[1] = '\0'; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2662 | return LY_SUCCESS; |
| 2663 | } |
| 2664 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2665 | 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] | 2666 | struct lysp_type *type_p, struct lysc_type **type, const char **units); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2667 | /** |
| 2668 | * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list). |
| 2669 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2670 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 2671 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2672 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 2673 | * @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] | 2674 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2675 | * @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] | 2676 | * @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] | 2677 | * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL. |
| 2678 | * @param[in] base The latest base (compiled) type from which the current type is being derived. |
| 2679 | * @param[out] type Newly created type structure with the filled information about the type. |
| 2680 | * @return LY_ERR value. |
| 2681 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 2682 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2683 | 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] | 2684 | 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] | 2685 | struct lysc_type *base, struct lysc_type **type) |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2686 | { |
| 2687 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2688 | unsigned int u, v, additional; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2689 | struct lysc_type_bin *bin; |
| 2690 | struct lysc_type_num *num; |
| 2691 | struct lysc_type_str *str; |
| 2692 | struct lysc_type_bits *bits; |
| 2693 | struct lysc_type_enum *enumeration; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2694 | struct lysc_type_dec *dec; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2695 | struct lysc_type_identityref *idref; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2696 | struct lysc_type_union *un, *un_aux; |
| 2697 | void *p; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2698 | |
| 2699 | switch (basetype) { |
| 2700 | case LY_TYPE_BINARY: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2701 | bin = (struct lysc_type_bin*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2702 | |
| 2703 | /* 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] | 2704 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2705 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2706 | base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2707 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2708 | 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] | 2709 | } |
| 2710 | } |
| 2711 | |
| 2712 | if (tpdfname) { |
| 2713 | type_p->compiled = *type; |
| 2714 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
| 2715 | } |
| 2716 | break; |
| 2717 | case LY_TYPE_BITS: |
| 2718 | /* RFC 7950 9.7 - bits */ |
| 2719 | bits = (struct lysc_type_bits*)(*type); |
| 2720 | if (type_p->bits) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2721 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2722 | base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL, |
| 2723 | (struct lysc_type_bitenum_item**)&bits->bits)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2724 | } |
| 2725 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2726 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2727 | /* 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] | 2728 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2729 | 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] | 2730 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2731 | 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] | 2732 | free(*type); |
| 2733 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2734 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2735 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2736 | } |
| 2737 | |
| 2738 | if (tpdfname) { |
| 2739 | type_p->compiled = *type; |
| 2740 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
| 2741 | } |
| 2742 | break; |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2743 | case LY_TYPE_DEC64: |
| 2744 | dec = (struct lysc_type_dec*)(*type); |
| 2745 | |
| 2746 | /* RFC 7950 9.3.4 - fraction-digits */ |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2747 | if (!base) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2748 | if (!type_p->fraction_digits) { |
| 2749 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2750 | 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] | 2751 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2752 | 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] | 2753 | free(*type); |
| 2754 | *type = NULL; |
| 2755 | } |
| 2756 | return LY_EVALID; |
| 2757 | } |
| 2758 | } else if (type_p->fraction_digits) { |
| 2759 | /* 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] | 2760 | if (tpdfname) { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2761 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2762 | "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] | 2763 | tpdfname); |
| 2764 | } else { |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 2765 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2766 | "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] | 2767 | free(*type); |
| 2768 | *type = NULL; |
| 2769 | } |
| 2770 | return LY_EVALID; |
| 2771 | } |
| 2772 | dec->fraction_digits = type_p->fraction_digits; |
| 2773 | |
| 2774 | /* RFC 7950 9.2.4 - range */ |
| 2775 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2776 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits, |
| 2777 | base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2778 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2779 | 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] | 2780 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2781 | } |
| 2782 | |
| 2783 | if (tpdfname) { |
| 2784 | type_p->compiled = *type; |
| 2785 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 2786 | } |
| 2787 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2788 | case LY_TYPE_STRING: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2789 | str = (struct lysc_type_str*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2790 | |
| 2791 | /* RFC 7950 9.4.4 - length */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2792 | if (type_p->length) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2793 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0, |
| 2794 | base ? ((struct lysc_type_str*)base)->length : NULL, &str->length)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2795 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2796 | 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] | 2797 | } |
| 2798 | } else if (base && ((struct lysc_type_str*)base)->length) { |
| 2799 | str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length); |
| 2800 | } |
| 2801 | |
| 2802 | /* RFC 7950 9.4.5 - pattern */ |
| 2803 | if (type_p->patterns) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2804 | LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2805 | base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2806 | } else if (base && ((struct lysc_type_str*)base)->patterns) { |
| 2807 | str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns); |
| 2808 | } |
| 2809 | |
| 2810 | if (tpdfname) { |
| 2811 | type_p->compiled = *type; |
| 2812 | *type = calloc(1, sizeof(struct lysc_type_str)); |
| 2813 | } |
| 2814 | break; |
| 2815 | case LY_TYPE_ENUM: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2816 | enumeration = (struct lysc_type_enum*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2817 | |
| 2818 | /* RFC 7950 9.6 - enum */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2819 | if (type_p->enums) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2820 | LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype, |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2821 | base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2822 | } |
| 2823 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2824 | if (!base && !type_p->flags) { |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2825 | /* 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] | 2826 | if (tpdfname) { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2827 | 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] | 2828 | } else { |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2829 | 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] | 2830 | free(*type); |
| 2831 | *type = NULL; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2832 | } |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2833 | return LY_EVALID; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2834 | } |
| 2835 | |
| 2836 | if (tpdfname) { |
| 2837 | type_p->compiled = *type; |
| 2838 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
| 2839 | } |
| 2840 | break; |
| 2841 | case LY_TYPE_INT8: |
| 2842 | case LY_TYPE_UINT8: |
| 2843 | case LY_TYPE_INT16: |
| 2844 | case LY_TYPE_UINT16: |
| 2845 | case LY_TYPE_INT32: |
| 2846 | case LY_TYPE_UINT32: |
| 2847 | case LY_TYPE_INT64: |
| 2848 | case LY_TYPE_UINT64: |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2849 | num = (struct lysc_type_num*)(*type); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2850 | |
| 2851 | /* RFC 6020 9.2.4 - range */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2852 | if (type_p->range) { |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2853 | LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0, |
| 2854 | base ? ((struct lysc_type_num*)base)->range : NULL, &num->range)); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 2855 | if (!tpdfname) { |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 2856 | 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] | 2857 | } |
| 2858 | } |
| 2859 | |
| 2860 | if (tpdfname) { |
| 2861 | type_p->compiled = *type; |
| 2862 | *type = calloc(1, sizeof(struct lysc_type_num)); |
| 2863 | } |
| 2864 | break; |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2865 | case LY_TYPE_IDENT: |
| 2866 | idref = (struct lysc_type_identityref*)(*type); |
| 2867 | |
| 2868 | /* RFC 7950 9.10.2 - base */ |
| 2869 | if (type_p->bases) { |
| 2870 | if (base) { |
| 2871 | /* only the directly derived identityrefs can contain base specification */ |
| 2872 | if (tpdfname) { |
| 2873 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2874 | "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] | 2875 | tpdfname); |
| 2876 | } else { |
| 2877 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2878 | "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] | 2879 | free(*type); |
| 2880 | *type = NULL; |
| 2881 | } |
| 2882 | return LY_EVALID; |
| 2883 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2884 | 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] | 2885 | } |
| 2886 | |
| 2887 | if (!base && !type_p->flags) { |
| 2888 | /* type derived from identityref built-in type must contain at least one base */ |
| 2889 | if (tpdfname) { |
| 2890 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname); |
| 2891 | } else { |
| 2892 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", ""); |
| 2893 | free(*type); |
| 2894 | *type = NULL; |
| 2895 | } |
| 2896 | return LY_EVALID; |
| 2897 | } |
| 2898 | |
| 2899 | if (tpdfname) { |
| 2900 | type_p->compiled = *type; |
| 2901 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 2902 | } |
| 2903 | break; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2904 | case LY_TYPE_LEAFREF: |
| 2905 | /* RFC 7950 9.9.3 - require-instance */ |
| 2906 | if (type_p->flags & LYS_SET_REQINST) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2907 | if (context_mod->mod->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2908 | if (tpdfname) { |
| 2909 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2910 | "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname); |
| 2911 | } else { |
| 2912 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 2913 | "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
| 2914 | free(*type); |
| 2915 | *type = NULL; |
| 2916 | } |
| 2917 | return LY_EVALID; |
| 2918 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2919 | ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2920 | } else if (base) { |
| 2921 | /* inherit */ |
| 2922 | ((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] | 2923 | } else { |
| 2924 | /* default is true */ |
| 2925 | ((struct lysc_type_leafref*)(*type))->require_instance = 1; |
| 2926 | } |
| 2927 | if (type_p->path) { |
| 2928 | 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] | 2929 | ((struct lysc_type_leafref*)(*type))->path_context = module; |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2930 | } else if (base) { |
| 2931 | 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] | 2932 | ((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] | 2933 | } else if (tpdfname) { |
| 2934 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname); |
| 2935 | return LY_EVALID; |
| 2936 | } else { |
| 2937 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", ""); |
| 2938 | free(*type); |
| 2939 | *type = NULL; |
| 2940 | return LY_EVALID; |
| 2941 | } |
| 2942 | if (tpdfname) { |
| 2943 | type_p->compiled = *type; |
| 2944 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2945 | } |
| 2946 | break; |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2947 | case LY_TYPE_INST: |
| 2948 | /* RFC 7950 9.9.3 - require-instance */ |
| 2949 | if (type_p->flags & LYS_SET_REQINST) { |
| 2950 | ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance; |
| 2951 | } else { |
| 2952 | /* default is true */ |
| 2953 | ((struct lysc_type_instanceid*)(*type))->require_instance = 1; |
| 2954 | } |
| 2955 | |
| 2956 | if (tpdfname) { |
| 2957 | type_p->compiled = *type; |
| 2958 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 2959 | } |
| 2960 | break; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2961 | case LY_TYPE_UNION: |
| 2962 | un = (struct lysc_type_union*)(*type); |
| 2963 | |
| 2964 | /* RFC 7950 7.4 - type */ |
| 2965 | if (type_p->types) { |
| 2966 | if (base) { |
| 2967 | /* only the directly derived union can contain types specification */ |
| 2968 | if (tpdfname) { |
| 2969 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2970 | "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.", |
| 2971 | tpdfname); |
| 2972 | } else { |
| 2973 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, |
| 2974 | "Invalid type substatement for the type not directly derived from union built-in type."); |
| 2975 | free(*type); |
| 2976 | *type = NULL; |
| 2977 | } |
| 2978 | return LY_EVALID; |
| 2979 | } |
| 2980 | /* compile the type */ |
| 2981 | additional = 0; |
| 2982 | LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID); |
| 2983 | for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 2984 | 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] | 2985 | if (un->types[u + additional]->basetype == LY_TYPE_UNION) { |
| 2986 | /* add space for additional types from the union subtype */ |
| 2987 | un_aux = (struct lysc_type_union *)un->types[u + additional]; |
| 2988 | 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))); |
| 2989 | LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
| 2990 | un->types = (void*)((uint32_t*)(p) + 1); |
| 2991 | |
| 2992 | /* copy subtypes of the subtype union */ |
| 2993 | for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) { |
| 2994 | if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 2995 | /* duplicate the whole structure because of the instance-specific path resolving for realtype */ |
| 2996 | un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref)); |
| 2997 | LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM); |
| 2998 | ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF; |
| 2999 | DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path); |
| 3000 | ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1; |
| 3001 | ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance; |
| 3002 | ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context; |
| 3003 | /* TODO extensions */ |
| 3004 | |
| 3005 | } else { |
| 3006 | un->types[u + additional] = un_aux->types[v]; |
| 3007 | ++un_aux->types[v]->refcount; |
| 3008 | } |
| 3009 | ++additional; |
| 3010 | LY_ARRAY_INCREMENT(un->types); |
| 3011 | } |
| 3012 | /* compensate u increment in main loop */ |
| 3013 | --additional; |
| 3014 | |
| 3015 | /* free the replaced union subtype */ |
| 3016 | lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux); |
| 3017 | } else { |
| 3018 | LY_ARRAY_INCREMENT(un->types); |
| 3019 | } |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3020 | } |
| 3021 | } |
| 3022 | |
| 3023 | if (!base && !type_p->flags) { |
| 3024 | /* type derived from union built-in type must contain at least one type */ |
| 3025 | if (tpdfname) { |
| 3026 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname); |
| 3027 | } else { |
| 3028 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", ""); |
| 3029 | free(*type); |
| 3030 | *type = NULL; |
| 3031 | } |
| 3032 | return LY_EVALID; |
| 3033 | } |
| 3034 | |
| 3035 | if (tpdfname) { |
| 3036 | type_p->compiled = *type; |
| 3037 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3038 | } |
| 3039 | break; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3040 | case LY_TYPE_BOOL: |
| 3041 | case LY_TYPE_EMPTY: |
| 3042 | case LY_TYPE_UNKNOWN: /* just to complete switch */ |
| 3043 | break; |
| 3044 | } |
| 3045 | LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM); |
| 3046 | done: |
| 3047 | return ret; |
| 3048 | } |
| 3049 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3050 | /** |
| 3051 | * @brief Compile information about the leaf/leaf-list's type. |
| 3052 | * @param[in] ctx Compile context. |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3053 | * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types. |
| 3054 | * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 3055 | * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects. |
| 3056 | * @param[in] context_name Name of the context node or referencing typedef for logging. |
| 3057 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3058 | * @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] | 3059 | * @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] | 3060 | * @return LY_ERR value. |
| 3061 | */ |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3062 | static LY_ERR |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3063 | 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] | 3064 | struct lysp_type *type_p, struct lysc_type **type, const char **units) |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3065 | { |
| 3066 | LY_ERR ret = LY_SUCCESS; |
| 3067 | unsigned int u; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3068 | int dummyloops = 0; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3069 | struct type_context { |
| 3070 | const struct lysp_tpdf *tpdf; |
| 3071 | struct lysp_node *node; |
| 3072 | struct lysp_module *mod; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3073 | } *tctx, *tctx_prev = NULL, *tctx_iter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3074 | LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3075 | struct lysc_type *base = NULL, *prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3076 | struct ly_set tpdf_chain = {0}; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3077 | const char *dflt = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3078 | struct lys_module *dflt_mod = NULL; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3079 | |
| 3080 | (*type) = NULL; |
| 3081 | |
| 3082 | tctx = calloc(1, sizeof *tctx); |
| 3083 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3084 | 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] | 3085 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod); |
| 3086 | ret == LY_SUCCESS; |
| 3087 | ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod, |
| 3088 | &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) { |
| 3089 | if (basetype) { |
| 3090 | break; |
| 3091 | } |
| 3092 | |
| 3093 | /* check status */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3094 | ret = lysc_check_status(ctx, context_flags, context_mod, context_name, |
| 3095 | 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] | 3096 | LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup); |
| 3097 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3098 | if (units && !*units) { |
| 3099 | /* inherit units */ |
| 3100 | DUP_STRING(ctx->ctx, tctx->tpdf->units, *units); |
| 3101 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3102 | if (!dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3103 | /* inherit default */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3104 | dflt = tctx->tpdf->dflt; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3105 | dflt_mod = tctx->mod->mod; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3106 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3107 | if (dummyloops && (!units || *units) && dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3108 | basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype; |
| 3109 | break; |
| 3110 | } |
| 3111 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3112 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3113 | /* it is not necessary to continue, the rest of the chain was already compiled, |
| 3114 | * 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] | 3115 | basetype = tctx->tpdf->type.compiled->basetype; |
| 3116 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3117 | if ((units && !*units) || !dflt) { |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3118 | dummyloops = 1; |
| 3119 | goto preparenext; |
| 3120 | } else { |
| 3121 | tctx = NULL; |
| 3122 | break; |
| 3123 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3124 | } |
| 3125 | |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3126 | /* circular typedef reference detection */ |
| 3127 | for (u = 0; u < tpdf_chain.count; u++) { |
| 3128 | /* local part */ |
| 3129 | tctx_iter = (struct type_context*)tpdf_chain.objs[u]; |
| 3130 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 3131 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3132 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 3133 | free(tctx); |
| 3134 | ret = LY_EVALID; |
| 3135 | goto cleanup; |
| 3136 | } |
| 3137 | } |
| 3138 | for (u = 0; u < ctx->tpdf_chain.count; u++) { |
| 3139 | /* global part for unions corner case */ |
| 3140 | tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u]; |
| 3141 | if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) { |
| 3142 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3143 | "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name); |
| 3144 | free(tctx); |
| 3145 | ret = LY_EVALID; |
| 3146 | goto cleanup; |
| 3147 | } |
| 3148 | } |
| 3149 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3150 | /* store information for the following processing */ |
| 3151 | ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3152 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3153 | preparenext: |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3154 | /* prepare next loop */ |
| 3155 | tctx_prev = tctx; |
| 3156 | tctx = calloc(1, sizeof *tctx); |
| 3157 | LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM); |
| 3158 | } |
| 3159 | free(tctx); |
| 3160 | |
| 3161 | /* allocate type according to the basetype */ |
| 3162 | switch (basetype) { |
| 3163 | case LY_TYPE_BINARY: |
| 3164 | *type = calloc(1, sizeof(struct lysc_type_bin)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3165 | break; |
| 3166 | case LY_TYPE_BITS: |
| 3167 | *type = calloc(1, sizeof(struct lysc_type_bits)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3168 | break; |
| 3169 | case LY_TYPE_BOOL: |
| 3170 | case LY_TYPE_EMPTY: |
| 3171 | *type = calloc(1, sizeof(struct lysc_type)); |
| 3172 | break; |
| 3173 | case LY_TYPE_DEC64: |
| 3174 | *type = calloc(1, sizeof(struct lysc_type_dec)); |
| 3175 | break; |
| 3176 | case LY_TYPE_ENUM: |
| 3177 | *type = calloc(1, sizeof(struct lysc_type_enum)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3178 | break; |
| 3179 | case LY_TYPE_IDENT: |
| 3180 | *type = calloc(1, sizeof(struct lysc_type_identityref)); |
| 3181 | break; |
| 3182 | case LY_TYPE_INST: |
| 3183 | *type = calloc(1, sizeof(struct lysc_type_instanceid)); |
| 3184 | break; |
| 3185 | case LY_TYPE_LEAFREF: |
| 3186 | *type = calloc(1, sizeof(struct lysc_type_leafref)); |
| 3187 | break; |
| 3188 | case LY_TYPE_STRING: |
| 3189 | *type = calloc(1, sizeof(struct lysc_type_str)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3190 | break; |
| 3191 | case LY_TYPE_UNION: |
| 3192 | *type = calloc(1, sizeof(struct lysc_type_union)); |
| 3193 | break; |
| 3194 | case LY_TYPE_INT8: |
| 3195 | case LY_TYPE_UINT8: |
| 3196 | case LY_TYPE_INT16: |
| 3197 | case LY_TYPE_UINT16: |
| 3198 | case LY_TYPE_INT32: |
| 3199 | case LY_TYPE_UINT32: |
| 3200 | case LY_TYPE_INT64: |
| 3201 | case LY_TYPE_UINT64: |
| 3202 | *type = calloc(1, sizeof(struct lysc_type_num)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3203 | break; |
| 3204 | case LY_TYPE_UNKNOWN: |
| 3205 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3206 | "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name); |
| 3207 | ret = LY_EVALID; |
| 3208 | goto cleanup; |
| 3209 | } |
| 3210 | LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3211 | if (~type_substmt_map[basetype] & type_p->flags) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3212 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.", |
| 3213 | ly_data_type2str[basetype]); |
| 3214 | free(*type); |
| 3215 | (*type) = NULL; |
| 3216 | ret = LY_EVALID; |
| 3217 | goto cleanup; |
| 3218 | } |
| 3219 | |
| 3220 | /* get restrictions from the referred typedefs */ |
| 3221 | for (u = tpdf_chain.count - 1; u + 1 > 0; --u) { |
| 3222 | tctx = (struct type_context*)tpdf_chain.objs[u]; |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3223 | |
| 3224 | /* remember the typedef context for circular check */ |
| 3225 | ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST); |
| 3226 | |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3227 | if (tctx->tpdf->type.compiled) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3228 | base = tctx->tpdf->type.compiled; |
| 3229 | continue; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3230 | } 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] | 3231 | /* no change, just use the type information from the base */ |
| 3232 | base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled; |
| 3233 | ++base->refcount; |
| 3234 | continue; |
| 3235 | } |
| 3236 | |
| 3237 | ++(*type)->refcount; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3238 | if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) { |
| 3239 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.", |
| 3240 | tctx->tpdf->name, ly_data_type2str[basetype]); |
| 3241 | ret = LY_EVALID; |
| 3242 | goto cleanup; |
| 3243 | } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) { |
| 3244 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3245 | "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).", |
| 3246 | tctx->tpdf->name, tctx->tpdf->dflt); |
| 3247 | ret = LY_EVALID; |
| 3248 | goto cleanup; |
| 3249 | } |
| 3250 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3251 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3252 | /* TODO user type plugins */ |
| 3253 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3254 | prev_type = *type; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3255 | 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] | 3256 | 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] | 3257 | basetype, tctx->tpdf->name, base, type); |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3258 | LY_CHECK_GOTO(ret, cleanup); |
| 3259 | base = prev_type; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3260 | } |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 3261 | /* remove the processed typedef contexts from the stack for circular check */ |
| 3262 | ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3263 | |
Radek Krejci | c5c27e5 | 2018-11-15 14:38:11 +0100 | [diff] [blame] | 3264 | /* process the type definition in leaf */ |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3265 | if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) { |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3266 | /* get restrictions from the node itself */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3267 | (*type)->basetype = basetype; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3268 | /* TODO user type plugins */ |
| 3269 | (*type)->plugin = &ly_builtin_type_plugins[basetype]; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3270 | ++(*type)->refcount; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3271 | 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] | 3272 | LY_CHECK_GOTO(ret, cleanup); |
| 3273 | } else { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3274 | /* no specific restriction in leaf's type definition, copy from the base */ |
| 3275 | free(*type); |
| 3276 | (*type) = base; |
| 3277 | ++(*type)->refcount; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3278 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3279 | if (dflt && !(*type)->dflt) { |
| 3280 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3281 | (*type)->dflt_mod = dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3282 | (*type)->dflt = calloc(1, sizeof *(*type)->dflt); |
| 3283 | (*type)->dflt->realtype = (*type); |
| 3284 | ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt), |
| 3285 | 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] | 3286 | 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] | 3287 | if (err) { |
| 3288 | ly_err_print(err); |
| 3289 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3290 | "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 3291 | ly_err_free(err); |
| 3292 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3293 | if (ret == LY_EINCOMPLETE) { |
| 3294 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3295 | 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] | 3296 | |
| 3297 | /* but in general result is so far ok */ |
| 3298 | ret = LY_SUCCESS; |
| 3299 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3300 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3301 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3302 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 3303 | 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] | 3304 | |
| 3305 | cleanup: |
| 3306 | ly_set_erase(&tpdf_chain, free); |
| 3307 | return ret; |
| 3308 | } |
| 3309 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3310 | /** |
| 3311 | * @brief Compile status information of the given node. |
| 3312 | * |
| 3313 | * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes |
| 3314 | * has the status correctly set during the compilation. |
| 3315 | * |
| 3316 | * @param[in] ctx Compile context |
| 3317 | * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved. |
| 3318 | * If the status was set explicitly on the node, it is already set in the flags value and we just check |
| 3319 | * the compatibility with the parent's status value. |
| 3320 | * @param[in] parent_flags Flags of the parent node to check/inherit the status value. |
| 3321 | * @return LY_ERR value. |
| 3322 | */ |
| 3323 | static LY_ERR |
| 3324 | lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags) |
| 3325 | { |
| 3326 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3327 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3328 | if (!((*node_flags) & LYS_STATUS_MASK)) { |
| 3329 | if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) { |
| 3330 | if ((parent_flags & 0x3) != 0x3) { |
| 3331 | /* do not print the warning when inheriting status from uses - the uses_status value has a special |
| 3332 | * combination of bits (0x3) which marks the uses_status value */ |
| 3333 | LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.", |
| 3334 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3335 | } |
| 3336 | (*node_flags) |= parent_flags & LYS_STATUS_MASK; |
| 3337 | } else { |
| 3338 | (*node_flags) |= LYS_STATUS_CURR; |
| 3339 | } |
| 3340 | } else if (parent_flags & LYS_STATUS_MASK) { |
| 3341 | /* check status compatibility with the parent */ |
| 3342 | if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) { |
| 3343 | if ((*node_flags) & LYS_STATUS_CURR) { |
| 3344 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3345 | "A \"current\" status is in conflict with the parent's \"%s\" status.", |
| 3346 | (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete"); |
| 3347 | } else { /* LYS_STATUS_DEPRC */ |
| 3348 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3349 | "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 3350 | } |
| 3351 | return LY_EVALID; |
| 3352 | } |
| 3353 | } |
| 3354 | return LY_SUCCESS; |
| 3355 | } |
| 3356 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3357 | /** |
| 3358 | * @brief Check uniqness of the node/action/notification name. |
| 3359 | * |
| 3360 | * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema |
| 3361 | * structures, but they share the namespace so we need to check their name collisions. |
| 3362 | * |
| 3363 | * @param[in] ctx Compile context. |
| 3364 | * @param[in] children List (linked list) of data nodes to go through. |
| 3365 | * @param[in] actions List (sized array) of actions or RPCs to go through. |
| 3366 | * @param[in] notifs List (sized array) of Notifications to go through. |
| 3367 | * @param[in] name Name of the item to find in the given lists. |
| 3368 | * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object |
| 3369 | * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity. |
| 3370 | * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise. |
| 3371 | */ |
| 3372 | static LY_ERR |
| 3373 | lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children, |
| 3374 | const struct lysc_action *actions, const struct lysc_notif *notifs, |
| 3375 | const char *name, void *exclude) |
| 3376 | { |
| 3377 | const struct lysc_node *iter; |
| 3378 | unsigned int u; |
| 3379 | |
| 3380 | LY_LIST_FOR(children, iter) { |
| 3381 | if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) { |
| 3382 | goto error; |
| 3383 | } |
| 3384 | } |
| 3385 | LY_ARRAY_FOR(actions, u) { |
| 3386 | if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) { |
| 3387 | goto error; |
| 3388 | } |
| 3389 | } |
| 3390 | LY_ARRAY_FOR(notifs, u) { |
| 3391 | if (¬ifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) { |
| 3392 | goto error; |
| 3393 | } |
| 3394 | } |
| 3395 | return LY_SUCCESS; |
| 3396 | error: |
| 3397 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification"); |
| 3398 | return LY_EEXIST; |
| 3399 | } |
| 3400 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3401 | 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] | 3402 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3403 | /** |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3404 | * @brief Compile parsed RPC/action schema node information. |
| 3405 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3406 | * @param[in] action_p Parsed RPC/action schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3407 | * @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] | 3408 | * @param[in,out] action Prepared (empty) compiled action structure to fill. |
| 3409 | * @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). |
| 3410 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3411 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3412 | */ |
| 3413 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3414 | lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3415 | struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status) |
| 3416 | { |
| 3417 | LY_ERR ret = LY_SUCCESS; |
| 3418 | struct lysp_node *child_p; |
| 3419 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3420 | int opt_prev = ctx->options; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3421 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3422 | lysc_update_path(ctx, parent, action_p->name); |
| 3423 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 3424 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3425 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3426 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3427 | action_p->name, action)) { |
| 3428 | return LY_EVALID; |
| 3429 | } |
| 3430 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3431 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3432 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3433 | "Action \"%s\" is placed inside %s.", action_p->name, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3434 | ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification"); |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 3435 | return LY_EVALID; |
| 3436 | } |
| 3437 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3438 | action->nodetype = LYS_ACTION; |
| 3439 | action->module = ctx->mod; |
| 3440 | action->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3441 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3442 | action->sp = action_p; |
| 3443 | } |
| 3444 | action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3445 | |
| 3446 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3447 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3448 | LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3449 | |
| 3450 | DUP_STRING(ctx->ctx, action_p->name, action->name); |
| 3451 | DUP_STRING(ctx->ctx, action_p->dsc, action->dsc); |
| 3452 | DUP_STRING(ctx->ctx, action_p->ref, action->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3453 | 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] | 3454 | 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] | 3455 | |
| 3456 | /* input */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3457 | lysc_update_path(ctx, (struct lysc_node*)action, "input"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3458 | 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] | 3459 | 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] | 3460 | ctx->options |= LYSC_OPT_RPC_INPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3461 | LY_LIST_FOR(action_p->input.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3462 | 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] | 3463 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3464 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3465 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3466 | |
| 3467 | /* output */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3468 | lysc_update_path(ctx, (struct lysc_node*)action, "output"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3469 | 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] | 3470 | 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] | 3471 | ctx->options |= LYSC_OPT_RPC_OUTPUT; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3472 | LY_LIST_FOR(action_p->output.data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3473 | 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] | 3474 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3475 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3476 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3477 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3478 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3479 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3480 | return ret; |
| 3481 | } |
| 3482 | |
| 3483 | /** |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3484 | * @brief Compile parsed Notification schema node information. |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3485 | * @param[in] ctx Compile context |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3486 | * @param[in] notif_p Parsed Notification schema node. |
Radek Krejci | 43981a3 | 2019-04-12 09:44:11 +0200 | [diff] [blame] | 3487 | * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification |
| 3488 | * @param[in,out] notif Prepared (empty) compiled notification structure to fill. |
| 3489 | * @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] | 3490 | * Zero means no uses, non-zero value with no status bit set mean the default status. |
| 3491 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3492 | */ |
| 3493 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3494 | lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3495 | struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status) |
| 3496 | { |
| 3497 | LY_ERR ret = LY_SUCCESS; |
| 3498 | struct lysp_node *child_p; |
| 3499 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3500 | int opt_prev = ctx->options; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3501 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3502 | lysc_update_path(ctx, parent, notif_p->name); |
| 3503 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3504 | if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data, |
| 3505 | parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs, |
| 3506 | parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs, |
| 3507 | notif_p->name, notif)) { |
| 3508 | return LY_EVALID; |
| 3509 | } |
| 3510 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3511 | if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3512 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3513 | "Notification \"%s\" is placed inside %s.", notif_p->name, |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3514 | ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3515 | return LY_EVALID; |
| 3516 | } |
| 3517 | |
| 3518 | notif->nodetype = LYS_NOTIF; |
| 3519 | notif->module = ctx->mod; |
| 3520 | notif->parent = parent; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3521 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3522 | notif->sp = notif_p; |
| 3523 | } |
| 3524 | notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK; |
| 3525 | |
| 3526 | /* status - it is not inherited by specification, but it does not make sense to have |
| 3527 | * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */ |
| 3528 | LY_CHECK_RET(lys_compile_status(ctx, ¬if->flags, uses_status ? uses_status : (parent ? parent->flags : 0))); |
| 3529 | |
| 3530 | DUP_STRING(ctx->ctx, notif_p->name, notif->name); |
| 3531 | DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc); |
| 3532 | DUP_STRING(ctx->ctx, notif_p->ref, notif->ref); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3533 | 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] | 3534 | 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] | 3535 | 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] | 3536 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3537 | ctx->options |= LYSC_OPT_NOTIFICATION; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3538 | LY_LIST_FOR(notif_p->data, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3539 | 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] | 3540 | } |
| 3541 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3542 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3543 | cleanup: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3544 | ctx->options = opt_prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3545 | return ret; |
| 3546 | } |
| 3547 | |
| 3548 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3549 | * @brief Compile parsed container node information. |
| 3550 | * @param[in] ctx Compile context |
| 3551 | * @param[in] node_p Parsed container node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3552 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3553 | * is enriched with the container-specific information. |
| 3554 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3555 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3556 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3557 | 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] | 3558 | { |
| 3559 | struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p; |
| 3560 | struct lysc_node_container *cont = (struct lysc_node_container*)node; |
| 3561 | struct lysp_node *child_p; |
| 3562 | unsigned int u; |
| 3563 | LY_ERR ret = LY_SUCCESS; |
| 3564 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3565 | if (cont_p->presence) { |
| 3566 | cont->flags |= LYS_PRESENCE; |
| 3567 | } |
| 3568 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3569 | LY_LIST_FOR(cont_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3570 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3571 | } |
| 3572 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3573 | COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done); |
| 3574 | COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done); |
| 3575 | 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] | 3576 | |
| 3577 | done: |
| 3578 | return ret; |
| 3579 | } |
| 3580 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3581 | /* |
| 3582 | * @brief Compile type in leaf/leaf-list node and do all the necessary checks. |
| 3583 | * @param[in] ctx Compile context. |
| 3584 | * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types. |
| 3585 | * @param[in] type_p Parsed type to compile. |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3586 | * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information. |
| 3587 | * @return LY_ERR value. |
| 3588 | */ |
| 3589 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3590 | 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] | 3591 | { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3592 | unsigned int u; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3593 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf; |
| 3594 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3595 | 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] | 3596 | leaf->units ? NULL : &leaf->units)); |
| 3597 | if (leaf->nodetype == LYS_LEAFLIST) { |
| 3598 | if (llist->type->dflt && !llist->dflts && !llist->min) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3599 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM); |
| 3600 | llist->dflts_mods[0] = llist->type->dflt_mod; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3601 | LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3602 | llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]); |
| 3603 | llist->dflts[0]->realtype = llist->type->dflt->realtype; |
| 3604 | llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]); |
| 3605 | llist->dflts[0]->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3606 | LY_ARRAY_INCREMENT(llist->dflts); |
| 3607 | } |
| 3608 | } else { |
| 3609 | if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3610 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3611 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3612 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 3613 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 3614 | leaf->dflt->realtype->refcount++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3615 | } |
| 3616 | } |
| 3617 | if (leaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3618 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 3619 | ly_set_add(&ctx->unres, leaf, 0); |
| 3620 | } else if (leaf->type->basetype == LY_TYPE_UNION) { |
| 3621 | LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) { |
| 3622 | if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) { |
| 3623 | /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */ |
| 3624 | ly_set_add(&ctx->unres, leaf, 0); |
| 3625 | } |
| 3626 | } |
| 3627 | } else if (leaf->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3628 | if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) { |
| 3629 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3630 | "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 3631 | return LY_EVALID; |
| 3632 | } |
| 3633 | } |
| 3634 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3635 | return LY_SUCCESS; |
| 3636 | } |
| 3637 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3638 | /** |
| 3639 | * @brief Compile parsed leaf node information. |
| 3640 | * @param[in] ctx Compile context |
| 3641 | * @param[in] node_p Parsed leaf node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3642 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3643 | * is enriched with the leaf-specific information. |
| 3644 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3645 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3646 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3647 | 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] | 3648 | { |
| 3649 | struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p; |
| 3650 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 3651 | unsigned int u; |
| 3652 | LY_ERR ret = LY_SUCCESS; |
| 3653 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3654 | 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] | 3655 | if (leaf_p->units) { |
| 3656 | leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0); |
| 3657 | leaf->flags |= LYS_SET_UNITS; |
| 3658 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3659 | |
| 3660 | /* the dflt member is just filled to avoid getting the default value from the type */ |
| 3661 | leaf->dflt = (void*)leaf_p->dflt; |
| 3662 | ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf); |
| 3663 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3664 | if (leaf_p->dflt) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3665 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3666 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3667 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 3668 | leaf->dflt->realtype = leaf->type; |
| 3669 | ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt), |
| 3670 | 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] | 3671 | 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] | 3672 | leaf->dflt->realtype->refcount++; |
| 3673 | if (err) { |
| 3674 | ly_err_print(err); |
| 3675 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3676 | "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg); |
| 3677 | ly_err_free(err); |
| 3678 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3679 | if (ret == LY_EINCOMPLETE) { |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3680 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3681 | 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] | 3682 | |
| 3683 | /* but in general result is so far ok */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3684 | ret = LY_SUCCESS; |
| 3685 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3686 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3687 | leaf->flags |= LYS_SET_DFLT; |
| 3688 | } |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3689 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 3690 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 3691 | done: |
| 3692 | return ret; |
| 3693 | } |
| 3694 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3695 | /** |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3696 | * @brief Compile parsed leaf-list node information. |
| 3697 | * @param[in] ctx Compile context |
| 3698 | * @param[in] node_p Parsed leaf-list node. |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3699 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3700 | * is enriched with the leaf-list-specific information. |
| 3701 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3702 | */ |
| 3703 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3704 | 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] | 3705 | { |
| 3706 | struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p; |
| 3707 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3708 | unsigned int u, v; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3709 | LY_ERR ret = LY_SUCCESS; |
| 3710 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3711 | 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] | 3712 | if (llist_p->units) { |
| 3713 | llist->units = lydict_insert(ctx->ctx, llist_p->units, 0); |
| 3714 | llist->flags |= LYS_SET_UNITS; |
| 3715 | } |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3716 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3717 | /* the dflts member is just filled to avoid getting the default value from the type */ |
| 3718 | llist->dflts = (void*)llist_p->dflts; |
| 3719 | 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] | 3720 | if (llist_p->dflts) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3721 | llist->dflts = NULL; /* reset the temporary llist_p->dflts */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3722 | 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] | 3723 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done); |
| 3724 | LY_ARRAY_FOR(llist_p->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3725 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3726 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 3727 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3728 | LY_ARRAY_INCREMENT(llist->dflts); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3729 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 3730 | llist->dflts[u]->realtype = llist->type; |
| 3731 | ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]), |
| 3732 | 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] | 3733 | 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] | 3734 | llist->dflts[u]->realtype->refcount++; |
| 3735 | if (err) { |
| 3736 | ly_err_print(err); |
| 3737 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3738 | "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg); |
| 3739 | ly_err_free(err); |
| 3740 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 3741 | if (ret == LY_EINCOMPLETE) { |
| 3742 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 3743 | 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] | 3744 | |
| 3745 | /* but in general result is so far ok */ |
| 3746 | ret = LY_SUCCESS; |
| 3747 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3748 | LY_CHECK_GOTO(ret, done); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3749 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3750 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3751 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 3752 | if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) { |
| 3753 | /* configuration data values must be unique - so check the default values */ |
| 3754 | LY_ARRAY_FOR(llist->dflts, u) { |
| 3755 | for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) { |
| 3756 | if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) { |
| 3757 | int dynamic = 0; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 3758 | 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] | 3759 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3760 | "Configuration leaf-list has multiple defaults of the same value \"%s\".", val); |
| 3761 | if (dynamic) { |
| 3762 | free((char*)val); |
| 3763 | } |
| 3764 | return LY_EVALID; |
| 3765 | } |
| 3766 | } |
| 3767 | } |
| 3768 | } |
| 3769 | |
| 3770 | /* 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] | 3771 | |
| 3772 | llist->min = llist_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3773 | if (llist->min) { |
| 3774 | llist->flags |= LYS_MAND_TRUE; |
| 3775 | } |
Radek Krejci | b740863 | 2018-11-28 17:12:11 +0100 | [diff] [blame] | 3776 | llist->max = llist_p->max ? llist_p->max : (uint32_t)-1; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3777 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3778 | done: |
| 3779 | return ret; |
| 3780 | } |
| 3781 | |
| 3782 | /** |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3783 | * @brief Compile information about list's uniques. |
| 3784 | * @param[in] ctx Compile context. |
| 3785 | * @param[in] context_module Module where the prefixes are going to be resolved. |
| 3786 | * @param[in] uniques Sized array list of unique statements. |
| 3787 | * @param[in] list Compiled list where the uniques are supposed to be resolved and stored. |
| 3788 | * @return LY_ERR value. |
| 3789 | */ |
| 3790 | static LY_ERR |
| 3791 | lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list) |
| 3792 | { |
| 3793 | LY_ERR ret = LY_SUCCESS; |
| 3794 | struct lysc_node_leaf **key, ***unique; |
| 3795 | const char *keystr, *delim; |
| 3796 | size_t len; |
| 3797 | unsigned int v; |
| 3798 | int config; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3799 | uint16_t flags; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3800 | |
| 3801 | for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) { |
| 3802 | config = -1; |
| 3803 | LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM); |
| 3804 | keystr = uniques[v]; |
| 3805 | while (keystr) { |
| 3806 | delim = strpbrk(keystr, " \t\n"); |
| 3807 | if (delim) { |
| 3808 | len = delim - keystr; |
| 3809 | while (isspace(*delim)) { |
| 3810 | ++delim; |
| 3811 | } |
| 3812 | } else { |
| 3813 | len = strlen(keystr); |
| 3814 | } |
| 3815 | |
| 3816 | /* unique node must be present */ |
| 3817 | LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3818 | ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0, |
| 3819 | (const struct lysc_node**)key, &flags); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3820 | if (ret != LY_SUCCESS) { |
| 3821 | if (ret == LY_EDENIED) { |
| 3822 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3823 | "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] | 3824 | len, keystr, lys_nodetype2str((*key)->nodetype)); |
| 3825 | } |
| 3826 | return LY_EVALID; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3827 | } else if (flags) { |
| 3828 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3829 | "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.", |
| 3830 | len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action"); |
| 3831 | return LY_EVALID; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3832 | } |
| 3833 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 3834 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3835 | /* all referenced leafs must be of the same config type */ |
| 3836 | if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) { |
| 3837 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3838 | "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]); |
| 3839 | return LY_EVALID; |
| 3840 | } else if ((*key)->flags & LYS_CONFIG_W) { |
| 3841 | config = 1; |
| 3842 | } else { /* LYS_CONFIG_R */ |
| 3843 | config = 0; |
| 3844 | } |
| 3845 | |
| 3846 | /* check status */ |
| 3847 | LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, |
| 3848 | (*key)->flags, (*key)->module, (*key)->name)); |
| 3849 | |
| 3850 | /* mark leaf as unique */ |
| 3851 | (*key)->flags |= LYS_UNIQUE; |
| 3852 | |
| 3853 | /* next unique value in line */ |
| 3854 | keystr = delim; |
| 3855 | } |
| 3856 | /* next unique definition */ |
| 3857 | } |
| 3858 | |
| 3859 | return LY_SUCCESS; |
| 3860 | } |
| 3861 | |
| 3862 | /** |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3863 | * @brief Compile parsed list node information. |
| 3864 | * @param[in] ctx Compile context |
| 3865 | * @param[in] node_p Parsed list node. |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3866 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 3867 | * is enriched with the list-specific information. |
| 3868 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 3869 | */ |
| 3870 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3871 | 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] | 3872 | { |
| 3873 | struct lysp_node_list *list_p = (struct lysp_node_list*)node_p; |
| 3874 | struct lysc_node_list *list = (struct lysc_node_list*)node; |
| 3875 | struct lysp_node *child_p; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3876 | struct lysc_node_leaf *key, *prev_key = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3877 | size_t len; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3878 | unsigned int u; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3879 | const char *keystr, *delim; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3880 | LY_ERR ret = LY_SUCCESS; |
| 3881 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3882 | list->min = list_p->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 3883 | if (list->min) { |
| 3884 | list->flags |= LYS_MAND_TRUE; |
| 3885 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3886 | list->max = list_p->max ? list_p->max : (uint32_t)-1; |
| 3887 | |
| 3888 | LY_LIST_FOR(list_p->child, child_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3889 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3890 | } |
| 3891 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 3892 | 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] | 3893 | |
| 3894 | /* keys */ |
| 3895 | if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) { |
| 3896 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data."); |
| 3897 | return LY_EVALID; |
| 3898 | } |
| 3899 | |
| 3900 | /* find all the keys (must be direct children) */ |
| 3901 | keystr = list_p->key; |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3902 | if (!keystr) { |
| 3903 | /* keyless list */ |
| 3904 | list->flags |= LYS_KEYLESS; |
| 3905 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3906 | while (keystr) { |
| 3907 | delim = strpbrk(keystr, " \t\n"); |
| 3908 | if (delim) { |
| 3909 | len = delim - keystr; |
| 3910 | while (isspace(*delim)) { |
| 3911 | ++delim; |
| 3912 | } |
| 3913 | } else { |
| 3914 | len = strlen(keystr); |
| 3915 | } |
| 3916 | |
| 3917 | /* key node must be present */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3918 | key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK); |
| 3919 | if (!(key)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3920 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 3921 | "The list's key \"%.*s\" not found.", len, keystr); |
| 3922 | return LY_EVALID; |
| 3923 | } |
| 3924 | /* keys must be unique */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3925 | if (key->flags & LYS_KEY) { |
| 3926 | /* the node was already marked as a key */ |
| 3927 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 3928 | "Duplicated key identifier \"%.*s\".", len, keystr); |
| 3929 | return LY_EVALID; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3930 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3931 | |
| 3932 | lysc_update_path(ctx, (struct lysc_node*)list, key->name); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3933 | /* key must have the same config flag as the list itself */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3934 | if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3935 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf."); |
| 3936 | return LY_EVALID; |
| 3937 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 3938 | if (ctx->mod_def->version < LYS_VERSION_1_1) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3939 | /* YANG 1.0 denies key to be of empty type */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3940 | if (key->type->basetype == LY_TYPE_EMPTY) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3941 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3942 | "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] | 3943 | return LY_EVALID; |
| 3944 | } |
| 3945 | } else { |
| 3946 | /* when and if-feature are illegal on list keys */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3947 | if (key->when) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3948 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3949 | "List's key must not have any \"when\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3950 | return LY_EVALID; |
| 3951 | } |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3952 | if (key->iffeatures) { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3953 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 3954 | "List's key must not have any \"if-feature\" statement."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3955 | return LY_EVALID; |
| 3956 | } |
| 3957 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3958 | |
| 3959 | /* check status */ |
| 3960 | 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] | 3961 | key->flags, key->module, key->name)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 3962 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3963 | /* ignore default values of the key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3964 | if (key->dflt) { |
| 3965 | key->dflt->realtype->plugin->free(ctx->ctx, key->dflt); |
| 3966 | lysc_type_free(ctx->ctx, key->dflt->realtype); |
| 3967 | free(key->dflt); |
| 3968 | key->dflt = NULL; |
| 3969 | key->dflt_mod = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3970 | } |
| 3971 | /* mark leaf as key */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 3972 | key->flags |= LYS_KEY; |
| 3973 | |
| 3974 | /* move it to the correct position */ |
| 3975 | if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) { |
| 3976 | /* fix links in closest previous siblings of the key */ |
| 3977 | if (key->next) { |
| 3978 | key->next->prev = key->prev; |
| 3979 | } else { |
| 3980 | /* last child */ |
| 3981 | list->child->prev = key->prev; |
| 3982 | } |
| 3983 | if (key->prev->next) { |
| 3984 | key->prev->next = key->next; |
| 3985 | } |
| 3986 | /* fix links in the key */ |
| 3987 | if (prev_key) { |
| 3988 | key->prev = (struct lysc_node*)prev_key; |
| 3989 | key->next = prev_key->next; |
| 3990 | } else { |
| 3991 | key->prev = list->child->prev; |
| 3992 | key->next = list->child; |
| 3993 | } |
| 3994 | /* fix links in closes future siblings of the key */ |
| 3995 | if (prev_key) { |
| 3996 | if (prev_key->next) { |
| 3997 | prev_key->next->prev = (struct lysc_node*)key; |
| 3998 | } else { |
| 3999 | list->child->prev = (struct lysc_node*)key; |
| 4000 | } |
| 4001 | prev_key->next = (struct lysc_node*)key; |
| 4002 | } else { |
| 4003 | list->child->prev = (struct lysc_node*)key; |
| 4004 | } |
| 4005 | /* fix links in parent */ |
| 4006 | if (!key->prev->next) { |
| 4007 | list->child = (struct lysc_node*)key; |
| 4008 | } |
| 4009 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4010 | |
| 4011 | /* next key value */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 4012 | prev_key = key; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4013 | keystr = delim; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4014 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4015 | } |
| 4016 | |
| 4017 | /* uniques */ |
| 4018 | if (list_p->uniques) { |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4019 | 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] | 4020 | } |
| 4021 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4022 | COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done); |
| 4023 | 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] | 4024 | |
| 4025 | done: |
| 4026 | return ret; |
| 4027 | } |
| 4028 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4029 | /** |
| 4030 | * @brief Do some checks and set the default choice's case. |
| 4031 | * |
| 4032 | * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it. |
| 4033 | * |
| 4034 | * @param[in] ctx Compile context. |
| 4035 | * @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, |
| 4036 | * not the reference to the imported module. |
| 4037 | * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice. |
| 4038 | * @return LY_ERR value. |
| 4039 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4040 | static LY_ERR |
| 4041 | lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch) |
| 4042 | { |
| 4043 | struct lysc_node *iter, *node = (struct lysc_node*)ch; |
| 4044 | const char *prefix = NULL, *name; |
| 4045 | size_t prefix_len = 0; |
| 4046 | |
| 4047 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 4048 | name = strchr(dflt, ':'); |
| 4049 | if (name) { |
| 4050 | prefix = dflt; |
| 4051 | prefix_len = name - prefix; |
| 4052 | ++name; |
| 4053 | } else { |
| 4054 | name = dflt; |
| 4055 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4056 | 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] | 4057 | /* prefixed default case make sense only for the prefix of the schema itself */ |
| 4058 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4059 | "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").", |
| 4060 | prefix_len, prefix); |
| 4061 | return LY_EVALID; |
| 4062 | } |
| 4063 | ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
| 4064 | if (!ch->dflt) { |
| 4065 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4066 | "Default case \"%s\" not found.", dflt); |
| 4067 | return LY_EVALID; |
| 4068 | } |
| 4069 | /* no mandatory nodes directly under the default case */ |
| 4070 | LY_LIST_FOR(ch->dflt->child, iter) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4071 | if (iter->parent != (struct lysc_node*)ch->dflt) { |
| 4072 | break; |
| 4073 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4074 | if (iter->flags & LYS_MAND_TRUE) { |
| 4075 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4076 | "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt); |
| 4077 | return LY_EVALID; |
| 4078 | } |
| 4079 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4080 | ch->dflt->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4081 | return LY_SUCCESS; |
| 4082 | } |
| 4083 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4084 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4085 | 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] | 4086 | { |
| 4087 | struct lys_module *mod; |
| 4088 | const char *prefix = NULL, *name; |
| 4089 | size_t prefix_len = 0; |
| 4090 | struct lysc_node_case *cs; |
| 4091 | struct lysc_node *node; |
| 4092 | |
| 4093 | /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */ |
| 4094 | name = strchr(dflt, ':'); |
| 4095 | if (name) { |
| 4096 | prefix = dflt; |
| 4097 | prefix_len = name - prefix; |
| 4098 | ++name; |
| 4099 | } else { |
| 4100 | name = dflt; |
| 4101 | } |
| 4102 | /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */ |
| 4103 | if (prefix) { |
| 4104 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 4105 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4106 | "Invalid deviation adding \"default\" property \"%s\" of choice. " |
| 4107 | "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] | 4108 | return LY_EVALID; |
| 4109 | } |
| 4110 | } else { |
| 4111 | mod = ctx->mod; |
| 4112 | } |
| 4113 | /* get the default case */ |
| 4114 | cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE); |
| 4115 | if (!cs) { |
| 4116 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4117 | "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] | 4118 | return LY_EVALID; |
| 4119 | } |
| 4120 | |
| 4121 | /* check that there is no mandatory node */ |
| 4122 | LY_LIST_FOR(cs->child, node) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4123 | if (node->parent != (struct lysc_node*)cs) { |
| 4124 | break; |
| 4125 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4126 | if (node->flags & LYS_MAND_TRUE) { |
| 4127 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4128 | "Invalid deviation adding \"default\" property \"%s\" of choice - " |
| 4129 | "mandatory node \"%s\" under the default case.", dflt, node->name); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 4130 | return LY_EVALID; |
| 4131 | } |
| 4132 | } |
| 4133 | |
| 4134 | /* set the default case in choice */ |
| 4135 | ch->dflt = cs; |
| 4136 | cs->flags |= LYS_SET_DFLT; |
| 4137 | |
| 4138 | return LY_SUCCESS; |
| 4139 | } |
| 4140 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 4141 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4142 | * @brief Compile parsed choice node information. |
| 4143 | * @param[in] ctx Compile context |
| 4144 | * @param[in] node_p Parsed choice node. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4145 | * @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] | 4146 | * is enriched with the choice-specific information. |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4147 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4148 | */ |
| 4149 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4150 | 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] | 4151 | { |
| 4152 | struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p; |
| 4153 | struct lysc_node_choice *ch = (struct lysc_node_choice*)node; |
| 4154 | struct lysp_node *child_p, *case_child_p; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4155 | LY_ERR ret = LY_SUCCESS; |
| 4156 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4157 | LY_LIST_FOR(ch_p->child, child_p) { |
| 4158 | if (child_p->nodetype == LYS_CASE) { |
| 4159 | 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] | 4160 | LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4161 | } |
| 4162 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4163 | LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4164 | } |
| 4165 | } |
| 4166 | |
| 4167 | /* default branch */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 4168 | if (ch_p->dflt) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4169 | 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] | 4170 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4171 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4172 | return ret; |
| 4173 | } |
| 4174 | |
| 4175 | /** |
| 4176 | * @brief Compile parsed anydata or anyxml node information. |
| 4177 | * @param[in] ctx Compile context |
| 4178 | * @param[in] node_p Parsed anydata or anyxml node. |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4179 | * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information |
| 4180 | * is enriched with the any-specific information. |
| 4181 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4182 | */ |
| 4183 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4184 | 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] | 4185 | { |
| 4186 | struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p; |
| 4187 | struct lysc_node_anydata *any = (struct lysc_node_anydata*)node; |
| 4188 | unsigned int u; |
| 4189 | LY_ERR ret = LY_SUCCESS; |
| 4190 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4191 | 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] | 4192 | |
| 4193 | if (any->flags & LYS_CONFIG_W) { |
| 4194 | 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] | 4195 | ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 4196 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4197 | done: |
| 4198 | return ret; |
| 4199 | } |
| 4200 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4201 | /** |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4202 | * @brief Connect the node into the siblings list and check its name uniqueness. |
| 4203 | * |
| 4204 | * @param[in] ctx Compile context |
| 4205 | * @param[in] parent Parent node holding the children list, in case of node from a choice's case, |
| 4206 | * the choice itself is expected instead of a specific case node. |
| 4207 | * @param[in] node Schema node to connect into the list. |
| 4208 | * @return LY_ERR value - LY_SUCCESS or LY_EEXIST. |
| 4209 | */ |
| 4210 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4211 | 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] | 4212 | { |
| 4213 | struct lysc_node **children; |
| 4214 | |
| 4215 | if (node->nodetype == LYS_CASE) { |
| 4216 | children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases; |
| 4217 | } else { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4218 | children = lysc_node_children_p(parent, ctx->options); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4219 | } |
| 4220 | if (children) { |
| 4221 | if (!(*children)) { |
| 4222 | /* first child */ |
| 4223 | *children = node; |
| 4224 | } else if (*children != node) { |
| 4225 | /* by the condition in previous branch we cover the choice/case children |
| 4226 | * - the children list is shared by the choice and the the first case, in addition |
| 4227 | * the first child of each case must be referenced from the case node. So the node is |
| 4228 | * actually always already inserted in case it is the first children - so here such |
| 4229 | * a situation actually corresponds to the first branch */ |
| 4230 | /* insert at the end of the parent's children list */ |
| 4231 | (*children)->prev->next = node; |
| 4232 | node->prev = (*children)->prev; |
| 4233 | (*children)->prev = node; |
| 4234 | |
| 4235 | /* check the name uniqueness */ |
| 4236 | if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent), |
| 4237 | lysc_node_notifs(parent), node->name, node)) { |
| 4238 | return LY_EEXIST; |
| 4239 | } |
| 4240 | } |
| 4241 | } |
| 4242 | return LY_SUCCESS; |
| 4243 | } |
| 4244 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4245 | /** |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4246 | * @brief Get the XPath context node for the given schema node. |
| 4247 | * @param[in] start The schema node where the XPath expression appears. |
| 4248 | * @return The context node to evaluate XPath expression in given schema node. |
| 4249 | * @return NULL in case the context node is the root node. |
| 4250 | */ |
| 4251 | static struct lysc_node * |
| 4252 | lysc_xpath_context(struct lysc_node *start) |
| 4253 | { |
| 4254 | for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF)); |
| 4255 | start = start->parent); |
| 4256 | return start; |
| 4257 | } |
| 4258 | |
| 4259 | /** |
| 4260 | * @brief Prepare the case structure in choice node for the new data node. |
| 4261 | * |
| 4262 | * 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 |
| 4263 | * created in the choice when the first child was processed. |
| 4264 | * |
| 4265 | * @param[in] ctx Compile context. |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4266 | * @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, |
| 4267 | * it is the LYS_CHOICE node or LYS_AUGMENT node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4268 | * @param[in] ch The compiled choice structure where the new case structures are created (if needed). |
| 4269 | * @param[in] child The new data node being part of a case (no matter if explicit or implicit). |
| 4270 | * @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, |
| 4271 | * 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] | 4272 | */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4273 | static struct lysc_node_case* |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4274 | 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] | 4275 | { |
| 4276 | struct lysc_node *iter; |
Radek Krejci | 98b1a66 | 2019-04-23 10:25:50 +0200 | [diff] [blame] | 4277 | struct lysc_node_case *cs = NULL; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4278 | struct lysc_when **when; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4279 | unsigned int u; |
| 4280 | LY_ERR ret; |
| 4281 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4282 | #define UNIQUE_CHECK(NAME, MOD) \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4283 | LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \ |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4284 | if (iter->module == MOD && !strcmp(iter->name, NAME)) { \ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4285 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \ |
| 4286 | return NULL; \ |
| 4287 | } \ |
| 4288 | } |
| 4289 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4290 | if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) { |
| 4291 | UNIQUE_CHECK(child->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4292 | |
| 4293 | /* we have to add an implicit case node into the parent choice */ |
| 4294 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4295 | DUP_STRING(ctx->ctx, child->name, cs->name); |
| 4296 | cs->flags = ch->flags & LYS_STATUS_MASK; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4297 | } else if (node_p->nodetype == LYS_CASE) { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4298 | if (ch->cases && (node_p == ch->cases->prev->sp)) { |
| 4299 | /* the case is already present since the child is not its first children */ |
| 4300 | return (struct lysc_node_case*)ch->cases->prev; |
| 4301 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 4302 | UNIQUE_CHECK(node_p->name, ctx->mod); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4303 | |
| 4304 | /* explicit parent case is not present (this is its first child) */ |
| 4305 | cs = calloc(1, sizeof(struct lysc_node_case)); |
| 4306 | DUP_STRING(ctx->ctx, node_p->name, cs->name); |
| 4307 | cs->flags = LYS_STATUS_MASK & node_p->flags; |
| 4308 | cs->sp = node_p; |
| 4309 | |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4310 | /* 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] | 4311 | LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4312 | |
| 4313 | if (node_p->when) { |
| 4314 | LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4315 | ret = lys_compile_when(ctx, node_p->when, when); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4316 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4317 | (*when)->context = lysc_xpath_context(ch->parent); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4318 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4319 | 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] | 4320 | } else { |
| 4321 | LOGINT(ctx->ctx); |
| 4322 | goto error; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4323 | } |
| 4324 | cs->module = ctx->mod; |
| 4325 | cs->prev = (struct lysc_node*)cs; |
| 4326 | cs->nodetype = LYS_CASE; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4327 | 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] | 4328 | cs->parent = (struct lysc_node*)ch; |
| 4329 | cs->child = child; |
| 4330 | |
| 4331 | return cs; |
| 4332 | error: |
Radek Krejci | 1b1e925 | 2019-04-24 08:45:50 +0200 | [diff] [blame] | 4333 | if (cs) { |
| 4334 | lysc_node_free(ctx->ctx, (struct lysc_node*)cs); |
| 4335 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4336 | return NULL; |
| 4337 | |
| 4338 | #undef UNIQUE_CHECK |
| 4339 | } |
| 4340 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4341 | /** |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4342 | * @brief Apply refined or deviated config to the target node. |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4343 | * |
| 4344 | * @param[in] ctx Compile context. |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4345 | * @param[in] node Target node where the config is supposed to be changed. |
| 4346 | * @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] | 4347 | * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is |
| 4348 | * 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] | 4349 | * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes. |
| 4350 | * @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] | 4351 | * @return LY_ERR value. |
| 4352 | */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4353 | static LY_ERR |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4354 | 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] | 4355 | int inheriting, int refine_flag) |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4356 | { |
| 4357 | struct lysc_node *child; |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4358 | uint16_t config = config_flag & LYS_CONFIG_MASK; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4359 | |
| 4360 | if (config == (node->flags & LYS_CONFIG_MASK)) { |
| 4361 | /* nothing to do */ |
| 4362 | return LY_SUCCESS; |
| 4363 | } |
| 4364 | |
| 4365 | if (!inheriting) { |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4366 | /* explicit change */ |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4367 | if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) { |
| 4368 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4369 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4370 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4371 | return LY_EVALID; |
| 4372 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4373 | node->flags |= LYS_SET_CONFIG; |
| 4374 | } else { |
| 4375 | if (node->flags & LYS_SET_CONFIG) { |
| 4376 | if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) { |
| 4377 | /* setting config flags, but have node with explicit config true */ |
| 4378 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4379 | "Invalid %s of config - configuration node cannot be child of any state data node.", |
| 4380 | refine_flag ? "refine" : "deviation"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 4381 | return LY_EVALID; |
| 4382 | } |
| 4383 | /* do not change config on nodes where the config is explicitely set, this does not apply to |
| 4384 | * nodes, which are being changed explicitly (targets of refine or deviation) */ |
| 4385 | return LY_SUCCESS; |
| 4386 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4387 | } |
| 4388 | node->flags &= ~LYS_CONFIG_MASK; |
| 4389 | node->flags |= config; |
| 4390 | |
| 4391 | /* inherit the change into the children */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4392 | LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4393 | 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] | 4394 | } |
| 4395 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4396 | return LY_SUCCESS; |
| 4397 | } |
| 4398 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4399 | /** |
| 4400 | * @brief Set LYS_MAND_TRUE flag for the non-presence container parents. |
| 4401 | * |
| 4402 | * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate |
| 4403 | * the flag to such parents from a mandatory children. |
| 4404 | * |
| 4405 | * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory. |
| 4406 | * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag |
| 4407 | * (mandatory children was removed). |
| 4408 | */ |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4409 | void |
| 4410 | lys_compile_mandatory_parents(struct lysc_node *parent, int add) |
| 4411 | { |
| 4412 | struct lysc_node *iter; |
| 4413 | |
| 4414 | if (add) { /* set flag */ |
| 4415 | for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE); |
| 4416 | parent = parent->parent) { |
| 4417 | parent->flags |= LYS_MAND_TRUE; |
| 4418 | } |
| 4419 | } else { /* unset flag */ |
| 4420 | 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] | 4421 | 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] | 4422 | if (iter->flags & LYS_MAND_TRUE) { |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 4423 | /* there is another mandatory node */ |
| 4424 | return; |
| 4425 | } |
| 4426 | } |
| 4427 | /* unset mandatory flag - there is no mandatory children in the non-presence container */ |
| 4428 | parent->flags &= ~LYS_MAND_TRUE; |
| 4429 | } |
| 4430 | } |
| 4431 | } |
| 4432 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 4433 | /** |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4434 | * @brief Internal sorting process for the lys_compile_augment_sort(). |
| 4435 | * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result. |
| 4436 | * @param[in,out] result Sized array to store the sorted list of augments. The array is expected |
| 4437 | * to be allocated to hold the complete list, its size is just incremented by adding another item. |
| 4438 | */ |
| 4439 | static void |
| 4440 | lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result) |
| 4441 | { |
| 4442 | unsigned int v; |
| 4443 | size_t len; |
| 4444 | |
| 4445 | len = strlen(aug_p->nodeid); |
| 4446 | LY_ARRAY_FOR(result, v) { |
| 4447 | if (strlen(result[v]->nodeid) <= len) { |
| 4448 | continue; |
| 4449 | } |
| 4450 | if (v < LY_ARRAY_SIZE(result)) { |
| 4451 | /* move the rest of array */ |
| 4452 | memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result); |
| 4453 | break; |
| 4454 | } |
| 4455 | } |
| 4456 | result[v] = aug_p; |
| 4457 | LY_ARRAY_INCREMENT(result); |
| 4458 | } |
| 4459 | |
| 4460 | /** |
| 4461 | * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment). |
| 4462 | * |
| 4463 | * The sorting is based only on the length of the augment's path since it guarantee the correct order |
| 4464 | * (it doesn't matter the /a/x is done before /a/b/c from the example above). |
| 4465 | * |
| 4466 | * @param[in] ctx Compile context. |
| 4467 | * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken). |
| 4468 | * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's) |
| 4469 | * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules. |
| 4470 | * @param[out] augments Resulting sorted sized array of pointers to the augments. |
| 4471 | * @return LY_ERR value. |
| 4472 | */ |
| 4473 | LY_ERR |
| 4474 | lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments) |
| 4475 | { |
| 4476 | struct lysp_augment **result = NULL; |
| 4477 | unsigned int u, v; |
| 4478 | size_t count = 0; |
| 4479 | |
| 4480 | assert(augments); |
| 4481 | |
| 4482 | /* get count of the augments in module and all its submodules */ |
| 4483 | if (aug_p) { |
| 4484 | count += LY_ARRAY_SIZE(aug_p); |
| 4485 | } |
| 4486 | LY_ARRAY_FOR(inc_p, u) { |
| 4487 | if (inc_p[u].submodule->augments) { |
| 4488 | count += LY_ARRAY_SIZE(inc_p[u].submodule->augments); |
| 4489 | } |
| 4490 | } |
| 4491 | |
| 4492 | if (!count) { |
| 4493 | *augments = NULL; |
| 4494 | return LY_SUCCESS; |
| 4495 | } |
| 4496 | LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM); |
| 4497 | |
| 4498 | /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them |
| 4499 | * together, so there can be even /z/y betwwen them. */ |
| 4500 | LY_ARRAY_FOR(aug_p, u) { |
| 4501 | lys_compile_augment_sort_(&aug_p[u], result); |
| 4502 | } |
| 4503 | LY_ARRAY_FOR(inc_p, u) { |
| 4504 | LY_ARRAY_FOR(inc_p[u].submodule->augments, v) { |
| 4505 | lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result); |
| 4506 | } |
| 4507 | } |
| 4508 | |
| 4509 | *augments = result; |
| 4510 | return LY_SUCCESS; |
| 4511 | } |
| 4512 | |
| 4513 | /** |
| 4514 | * @brief Compile the parsed augment connecting it into its target. |
| 4515 | * |
| 4516 | * It is expected that all the data referenced in path are present - augments are ordered so that augment B |
| 4517 | * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path |
| 4518 | * are already implemented and compiled. |
| 4519 | * |
| 4520 | * @param[in] ctx Compile context. |
| 4521 | * @param[in] aug_p Parsed augment to compile. |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4522 | * @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 |
| 4523 | * children in case of the augmenting uses data. |
| 4524 | * @return LY_SUCCESS on success. |
| 4525 | * @return LY_EVALID on failure. |
| 4526 | */ |
| 4527 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4528 | 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] | 4529 | { |
| 4530 | LY_ERR ret = LY_SUCCESS; |
| 4531 | struct lysp_node *node_p, *case_node_p; |
| 4532 | struct lysc_node *target; /* target target of the augment */ |
| 4533 | struct lysc_node *node; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4534 | struct lysc_when **when, *when_shared; |
| 4535 | int allow_mandatory = 0; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4536 | uint16_t flags = 0; |
| 4537 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4538 | int opt_prev = ctx->options; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4539 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4540 | lysc_update_path(ctx, NULL, "{augment}"); |
| 4541 | lysc_update_path(ctx, NULL, aug_p->nodeid); |
| 4542 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4543 | 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] | 4544 | LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF, |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4545 | 1, (const struct lysc_node**)&target, &flags); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4546 | if (ret != LY_SUCCESS) { |
| 4547 | if (ret == LY_EDENIED) { |
| 4548 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4549 | "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.", |
| 4550 | parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype)); |
| 4551 | } |
| 4552 | return LY_EVALID; |
| 4553 | } |
| 4554 | |
| 4555 | /* check for mandatory nodes |
| 4556 | * - new cases augmenting some choice can have mandatory nodes |
| 4557 | * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement |
| 4558 | */ |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4559 | if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) { |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4560 | allow_mandatory = 1; |
| 4561 | } |
| 4562 | |
| 4563 | when_shared = NULL; |
| 4564 | LY_LIST_FOR(aug_p->child, node_p) { |
| 4565 | /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */ |
| 4566 | if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE) |
| 4567 | && !((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] | 4568 | && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES) |
| 4569 | && !(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] | 4570 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4571 | "Invalid augment of %s node which is not allowed to contain %s node \"%s\".", |
| 4572 | lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4573 | return LY_EVALID; |
| 4574 | } |
| 4575 | |
| 4576 | /* compile the children */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4577 | ctx->options |= flags; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4578 | if (node_p->nodetype != LYS_CASE) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4579 | LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4580 | } else { |
| 4581 | 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] | 4582 | LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4583 | } |
| 4584 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4585 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4586 | |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4587 | /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children, |
| 4588 | * 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] | 4589 | if (target->nodetype == LYS_CASE) { |
Radek Krejci | fe13da4 | 2019-02-15 14:51:01 +0100 | [diff] [blame] | 4590 | /* 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] | 4591 | 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] | 4592 | } else if (target->nodetype == LYS_CHOICE) { |
| 4593 | /* to pass when statement, we need the last case no matter if it is explicit or implicit case */ |
| 4594 | node = ((struct lysc_node_choice*)target)->cases->prev; |
| 4595 | } else { |
| 4596 | /* the compiled node is the last child of the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4597 | node = lysc_node_children(target, flags)->prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4598 | } |
| 4599 | |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 4600 | 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] | 4601 | node->flags &= ~LYS_MAND_TRUE; |
| 4602 | lys_compile_mandatory_parents(target, 0); |
| 4603 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4604 | "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] | 4605 | return LY_EVALID; |
| 4606 | } |
| 4607 | |
| 4608 | /* pass augment's when to all the children */ |
| 4609 | if (aug_p->when) { |
| 4610 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
| 4611 | if (!when_shared) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4612 | ret = lys_compile_when(ctx, aug_p->when, when); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4613 | LY_CHECK_GOTO(ret, error); |
| 4614 | (*when)->context = lysc_xpath_context(target); |
| 4615 | when_shared = *when; |
| 4616 | } else { |
| 4617 | ++when_shared->refcount; |
| 4618 | (*when) = when_shared; |
| 4619 | } |
| 4620 | } |
| 4621 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4622 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4623 | ctx->options |= flags; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4624 | switch (target->nodetype) { |
| 4625 | case LYS_CONTAINER: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4626 | 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] | 4627 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4628 | 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] | 4629 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4630 | break; |
| 4631 | case LYS_LIST: |
Radek Krejci | 05b774b | 2019-02-25 13:26:18 +0100 | [diff] [blame] | 4632 | 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] | 4633 | u, lys_compile_action, 0, ret, error); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4634 | 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] | 4635 | u, lys_compile_notif, 0, ret, error); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4636 | break; |
| 4637 | default: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4638 | ctx->options = opt_prev; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4639 | if (aug_p->actions) { |
| 4640 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4641 | "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".", |
| 4642 | lys_nodetype2str(target->nodetype), aug_p->actions[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4643 | return LY_EVALID; |
| 4644 | } |
| 4645 | if (aug_p->notifs) { |
| 4646 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4647 | "Invalid augment of %s node which is not allowed to contain Notification node \"%s\".", |
| 4648 | lys_nodetype2str(target->nodetype), aug_p->notifs[0].name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4649 | return LY_EVALID; |
| 4650 | } |
| 4651 | } |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4652 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4653 | lysc_update_path(ctx, NULL, NULL); |
| 4654 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4655 | error: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4656 | ctx->options = opt_prev; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4657 | return ret; |
| 4658 | } |
| 4659 | |
| 4660 | /** |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4661 | * @brief Apply refined or deviated mandatory flag to the target node. |
| 4662 | * |
| 4663 | * @param[in] ctx Compile context. |
| 4664 | * @param[in] node Target node where the mandatory property is supposed to be changed. |
| 4665 | * @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] | 4666 | * @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] | 4667 | * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations, |
| 4668 | * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure, |
| 4669 | * 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] | 4670 | * @return LY_ERR value. |
| 4671 | */ |
| 4672 | static LY_ERR |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4673 | 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] | 4674 | { |
| 4675 | if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) { |
| 4676 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4677 | "Invalid %s of mandatory - %s cannot hold mandatory statement.", |
| 4678 | refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype)); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4679 | return LY_EVALID; |
| 4680 | } |
| 4681 | |
| 4682 | if (mandatory_flag & LYS_MAND_TRUE) { |
| 4683 | /* check if node has default value */ |
| 4684 | if (node->nodetype & LYS_LEAF) { |
| 4685 | if (node->flags & LYS_SET_DFLT) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4686 | if (refine_flag) { |
| 4687 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4688 | "Invalid refine of mandatory - leaf already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4689 | return LY_EVALID; |
| 4690 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4691 | } else if (((struct lysc_node_leaf*)node)->dflt) { |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4692 | /* remove the default value taken from the leaf's type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4693 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4694 | |
| 4695 | /* update the list of incomplete default values if needed */ |
| 4696 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 4697 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4698 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4699 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4700 | free(leaf->dflt); |
| 4701 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4702 | leaf->dflt_mod = NULL; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4703 | } |
| 4704 | } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) { |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4705 | if (refine_flag) { |
| 4706 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4707 | "Invalid refine of mandatory - choice already has \"default\" statement."); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4708 | return LY_EVALID; |
| 4709 | } |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4710 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 4711 | if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4712 | 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] | 4713 | return LY_EVALID; |
| 4714 | } |
| 4715 | |
| 4716 | node->flags &= ~LYS_MAND_FALSE; |
| 4717 | node->flags |= LYS_MAND_TRUE; |
| 4718 | lys_compile_mandatory_parents(node->parent, 1); |
| 4719 | } else { |
| 4720 | /* make mandatory false */ |
| 4721 | node->flags &= ~LYS_MAND_TRUE; |
| 4722 | node->flags |= LYS_MAND_FALSE; |
| 4723 | lys_compile_mandatory_parents(node->parent, 0); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4724 | 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] | 4725 | /* get the type's default value if any */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4726 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4727 | leaf->dflt_mod = leaf->type->dflt_mod; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4728 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4729 | leaf->dflt->realtype = leaf->type->dflt->realtype; |
| 4730 | leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt); |
| 4731 | leaf->dflt->realtype->refcount++; |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 4732 | } |
| 4733 | } |
| 4734 | return LY_SUCCESS; |
| 4735 | } |
| 4736 | |
| 4737 | /** |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4738 | * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent. |
| 4739 | * If present, also apply uses's modificators. |
| 4740 | * |
| 4741 | * @param[in] ctx Compile context |
| 4742 | * @param[in] uses_p Parsed uses schema node. |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4743 | * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is |
| 4744 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 4745 | * the compile context. |
| 4746 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 4747 | */ |
| 4748 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4749 | 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] | 4750 | { |
| 4751 | struct lysp_node *node_p; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4752 | struct lysc_node *node, *child; |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4753 | /* 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] | 4754 | struct lysc_node_container context_node_fake = |
| 4755 | {.nodetype = LYS_CONTAINER, |
| 4756 | .module = ctx->mod, |
| 4757 | .flags = parent ? parent->flags : 0, |
| 4758 | .child = NULL, .next = NULL, |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4759 | .prev = (struct lysc_node*)&context_node_fake, |
| 4760 | .actions = NULL, .notifs = NULL}; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4761 | struct lysp_grp *grp = NULL; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4762 | unsigned int u, v, grp_stack_count; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4763 | int found; |
| 4764 | const char *id, *name, *prefix; |
| 4765 | size_t prefix_len, name_len; |
| 4766 | struct lys_module *mod, *mod_old; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4767 | struct lysp_refine *rfn; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4768 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4769 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 4770 | uint16_t flags; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4771 | struct ly_set refined = {0}; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4772 | struct lysc_when **when, *when_shared; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4773 | struct lysp_augment **augments = NULL; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4774 | unsigned int actions_index, notifs_index; |
| 4775 | struct lysc_notif **notifs = NULL; |
| 4776 | struct lysc_action **actions = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4777 | |
| 4778 | /* search for the grouping definition */ |
| 4779 | found = 0; |
| 4780 | id = uses_p->name; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 4781 | 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] | 4782 | if (prefix) { |
| 4783 | mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len); |
| 4784 | if (!mod) { |
| 4785 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4786 | "Invalid prefix used for grouping reference.", uses_p->name); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4787 | return LY_EVALID; |
| 4788 | } |
| 4789 | } else { |
| 4790 | mod = ctx->mod_def; |
| 4791 | } |
| 4792 | if (mod == ctx->mod_def) { |
| 4793 | 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] | 4794 | grp = (struct lysp_grp*)lysp_node_groupings(node_p); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4795 | LY_ARRAY_FOR(grp, u) { |
| 4796 | if (!strcmp(grp[u].name, name)) { |
| 4797 | grp = &grp[u]; |
| 4798 | found = 1; |
| 4799 | break; |
| 4800 | } |
| 4801 | } |
| 4802 | } |
| 4803 | } |
| 4804 | if (!found) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4805 | /* search in top-level groupings of the main module ... */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4806 | grp = mod->parsed->groupings; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4807 | if (grp) { |
| 4808 | for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) { |
| 4809 | if (!strcmp(grp[u].name, name)) { |
| 4810 | grp = &grp[u]; |
| 4811 | found = 1; |
| 4812 | } |
| 4813 | } |
| 4814 | } |
| 4815 | if (!found && mod->parsed->includes) { |
| 4816 | /* ... and all the submodules */ |
| 4817 | for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) { |
| 4818 | grp = mod->parsed->includes[u].submodule->groupings; |
| 4819 | if (grp) { |
| 4820 | for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) { |
| 4821 | if (!strcmp(grp[v].name, name)) { |
| 4822 | grp = &grp[v]; |
| 4823 | found = 1; |
| 4824 | } |
| 4825 | } |
| 4826 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4827 | } |
| 4828 | } |
| 4829 | } |
| 4830 | if (!found) { |
| 4831 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4832 | "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name); |
| 4833 | return LY_EVALID; |
| 4834 | } |
| 4835 | |
| 4836 | /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */ |
| 4837 | grp_stack_count = ctx->groupings.count; |
| 4838 | ly_set_add(&ctx->groupings, (void*)grp, 0); |
| 4839 | if (grp_stack_count == ctx->groupings.count) { |
| 4840 | /* the target grouping is already in the stack, so we are already inside it -> circular dependency */ |
| 4841 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 4842 | "Grouping \"%s\" references itself through a uses statement.", grp->name); |
| 4843 | return LY_EVALID; |
| 4844 | } |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 4845 | if (!(ctx->options & LYSC_OPT_GROUPING)) { |
| 4846 | /* remember that the grouping is instantiated to avoid its standalone validation */ |
| 4847 | grp->flags |= LYS_USED_GRP; |
| 4848 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4849 | |
| 4850 | /* switch context's mod_def */ |
| 4851 | mod_old = ctx->mod_def; |
| 4852 | ctx->mod_def = mod; |
| 4853 | |
| 4854 | /* check status */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4855 | 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] | 4856 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4857 | /* compile data nodes */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 4858 | LY_LIST_FOR(grp->data, node_p) { |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 4859 | /* 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] | 4860 | 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] | 4861 | |
| 4862 | /* some preparation for applying refines */ |
| 4863 | if (grp->data == node_p) { |
| 4864 | /* remember the first child */ |
Radek Krejci | 684faf2 | 2019-05-27 14:31:16 +0200 | [diff] [blame] | 4865 | if (parent) { |
| 4866 | child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK); |
| 4867 | } else if (ctx->mod->compiled->data) { |
| 4868 | child = ctx->mod->compiled->data; |
| 4869 | } else { |
| 4870 | child = NULL; |
| 4871 | } |
| 4872 | context_node_fake.child = child ? child->prev : NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4873 | } |
| 4874 | } |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4875 | when_shared = NULL; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4876 | LY_LIST_FOR(context_node_fake.child, child) { |
| 4877 | child->parent = (struct lysc_node*)&context_node_fake; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4878 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4879 | /* 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] | 4880 | if (uses_p->when) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4881 | LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4882 | if (!when_shared) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4883 | LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, when), cleanup); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 4884 | (*when)->context = lysc_xpath_context(parent); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 4885 | when_shared = *when; |
| 4886 | } else { |
| 4887 | ++when_shared->refcount; |
| 4888 | (*when) = when_shared; |
| 4889 | } |
| 4890 | } |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4891 | } |
| 4892 | if (context_node_fake.child) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4893 | /* child is the last data node added by grouping */ |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4894 | child = context_node_fake.child->prev; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4895 | /* 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] | 4896 | 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] | 4897 | } |
| 4898 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4899 | /* compile actions */ |
| 4900 | actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs; |
| 4901 | if (actions) { |
| 4902 | actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4903 | 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] | 4904 | if (*actions && (uses_p->augments || uses_p->refines)) { |
| 4905 | /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */ |
| 4906 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup); |
| 4907 | LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index; |
| 4908 | memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 4909 | } |
| 4910 | } |
| 4911 | |
| 4912 | /* compile notifications */ |
| 4913 | notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs; |
| 4914 | if (notifs) { |
| 4915 | notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4916 | 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] | 4917 | if (*notifs && (uses_p->augments || uses_p->refines)) { |
| 4918 | /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */ |
| 4919 | LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup); |
| 4920 | LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index; |
| 4921 | memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 4922 | } |
| 4923 | } |
| 4924 | |
| 4925 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 4926 | /* sort and apply augments */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4927 | 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] | 4928 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 4929 | 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] | 4930 | } |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 4931 | |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4932 | /* reload previous context's mod_def */ |
| 4933 | ctx->mod_def = mod_old; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4934 | lysc_update_path(ctx, NULL, "{refine}"); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 4935 | |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4936 | /* apply refine */ |
| 4937 | LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4938 | lysc_update_path(ctx, NULL, rfn->nodeid); |
| 4939 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 4940 | 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] | 4941 | 0, 0, (const struct lysc_node**)&node, &flags), |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4942 | cleanup); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 4943 | ly_set_add(&refined, node, LY_SET_OPT_USEASLIST); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4944 | |
| 4945 | /* default value */ |
| 4946 | if (rfn->dflts) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4947 | if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) { |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4948 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4949 | "Invalid refine of default - %s cannot hold %d default values.", |
| 4950 | lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4951 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4952 | } |
| 4953 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) { |
| 4954 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 4955 | "Invalid refine of default - %s cannot hold default value(s).", |
| 4956 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 4957 | goto cleanup; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4958 | } |
| 4959 | if (node->nodetype == LYS_LEAF) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4960 | struct ly_err_item *err = NULL; |
| 4961 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node; |
| 4962 | if (leaf->dflt) { |
| 4963 | /* remove the previous default value */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4964 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4965 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 4966 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 4967 | } else { |
| 4968 | /* prepare a new one */ |
| 4969 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
| 4970 | leaf->dflt->realtype = leaf->type; |
| 4971 | } |
| 4972 | /* parse the new one */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 4973 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4974 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]), |
| 4975 | 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] | 4976 | 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] | 4977 | leaf->dflt->realtype->refcount++; |
| 4978 | if (err) { |
| 4979 | ly_err_print(err); |
| 4980 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4981 | "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg); |
| 4982 | ly_err_free(err); |
| 4983 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 4984 | if (rc == LY_EINCOMPLETE) { |
| 4985 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 4986 | 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] | 4987 | |
| 4988 | /* but in general result is so far ok */ |
| 4989 | rc = LY_SUCCESS; |
| 4990 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4991 | LY_CHECK_GOTO(rc, cleanup); |
| 4992 | leaf->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 4993 | } else if (node->nodetype == LYS_LEAFLIST) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 4994 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node; |
| 4995 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 4996 | if (ctx->mod->version < 2) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 4997 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 4998 | "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] | 4999 | goto cleanup; |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5000 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5001 | |
| 5002 | /* remove previous set of default values */ |
| 5003 | LY_ARRAY_FOR(llist->dflts, u) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5004 | lysc_incomplete_dflts_remove(ctx, llist->dflts[u]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5005 | llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]); |
| 5006 | lysc_type_free(ctx->ctx, llist->dflts[u]->realtype); |
| 5007 | free(llist->dflts[u]); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5008 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5009 | LY_ARRAY_FREE(llist->dflts); |
| 5010 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5011 | LY_ARRAY_FREE(llist->dflts_mods); |
| 5012 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5013 | |
| 5014 | /* create the new set of the default values */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5015 | 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] | 5016 | 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] | 5017 | LY_ARRAY_FOR(rfn->dflts, u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5018 | struct ly_err_item *err = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5019 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 5020 | llist->dflts_mods[u] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5021 | LY_ARRAY_INCREMENT(llist->dflts); |
| 5022 | llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]); |
| 5023 | llist->dflts[u]->realtype = llist->type; |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 5024 | 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] | 5025 | 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] | 5026 | 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] | 5027 | llist->dflts[u]->realtype->refcount++; |
| 5028 | if (err) { |
| 5029 | ly_err_print(err); |
| 5030 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5031 | "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg); |
| 5032 | ly_err_free(err); |
| 5033 | } |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 5034 | if (rc == LY_EINCOMPLETE) { |
| 5035 | /* postpone default compilation when the tree is complete */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5036 | 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] | 5037 | |
| 5038 | /* but in general result is so far ok */ |
| 5039 | rc = LY_SUCCESS; |
| 5040 | } |
| 5041 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5042 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5043 | llist->flags |= LYS_SET_DFLT; |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5044 | } else if (node->nodetype == LYS_CHOICE) { |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5045 | if (((struct lysc_node_choice*)node)->dflt) { |
| 5046 | /* unset LYS_SET_DFLT from the current default case */ |
| 5047 | ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT; |
| 5048 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5049 | 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] | 5050 | } |
| 5051 | } |
| 5052 | |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5053 | /* description */ |
| 5054 | if (rfn->dsc) { |
| 5055 | FREE_STRING(ctx->ctx, node->dsc); |
| 5056 | node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0); |
| 5057 | } |
| 5058 | |
| 5059 | /* reference */ |
| 5060 | if (rfn->ref) { |
| 5061 | FREE_STRING(ctx->ctx, node->ref); |
| 5062 | node->ref = lydict_insert(ctx->ctx, rfn->ref, 0); |
| 5063 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5064 | |
| 5065 | /* config */ |
| 5066 | if (rfn->flags & LYS_CONFIG_MASK) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5067 | if (!flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5068 | 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] | 5069 | } else { |
| 5070 | LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).", |
| 5071 | flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path); |
| 5072 | } |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5073 | } |
| 5074 | |
| 5075 | /* mandatory */ |
| 5076 | if (rfn->flags & LYS_MAND_MASK) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5077 | 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] | 5078 | } |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 5079 | |
| 5080 | /* presence */ |
| 5081 | if (rfn->presence) { |
| 5082 | if (node->nodetype != LYS_CONTAINER) { |
| 5083 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5084 | "Invalid refine of presence statement - %s cannot hold the presence statement.", |
| 5085 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5086 | goto cleanup; |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 5087 | } |
| 5088 | node->flags |= LYS_PRESENCE; |
| 5089 | } |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5090 | |
| 5091 | /* must */ |
| 5092 | if (rfn->musts) { |
| 5093 | switch (node->nodetype) { |
| 5094 | case LYS_LEAF: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5095 | 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] | 5096 | break; |
| 5097 | case LYS_LEAFLIST: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5098 | 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] | 5099 | break; |
| 5100 | case LYS_LIST: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5101 | 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] | 5102 | break; |
| 5103 | case LYS_CONTAINER: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5104 | 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] | 5105 | break; |
| 5106 | case LYS_ANYXML: |
| 5107 | case LYS_ANYDATA: |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5108 | 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] | 5109 | break; |
| 5110 | default: |
| 5111 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5112 | "Invalid refine of must statement - %s cannot hold any must statement.", |
| 5113 | lys_nodetype2str(node->nodetype)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5114 | goto cleanup; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 5115 | } |
| 5116 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5117 | |
| 5118 | /* min/max-elements */ |
| 5119 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5120 | switch (node->nodetype) { |
| 5121 | case LYS_LEAFLIST: |
| 5122 | if (rfn->flags & LYS_SET_MAX) { |
| 5123 | ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5124 | } |
| 5125 | if (rfn->flags & LYS_SET_MIN) { |
| 5126 | ((struct lysc_node_leaflist*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5127 | if (rfn->min) { |
| 5128 | node->flags |= LYS_MAND_TRUE; |
| 5129 | lys_compile_mandatory_parents(node->parent, 1); |
| 5130 | } else { |
| 5131 | node->flags &= ~LYS_MAND_TRUE; |
| 5132 | lys_compile_mandatory_parents(node->parent, 0); |
| 5133 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5134 | } |
| 5135 | break; |
| 5136 | case LYS_LIST: |
| 5137 | if (rfn->flags & LYS_SET_MAX) { |
| 5138 | ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1; |
| 5139 | } |
| 5140 | if (rfn->flags & LYS_SET_MIN) { |
| 5141 | ((struct lysc_node_list*)node)->min = rfn->min; |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5142 | if (rfn->min) { |
| 5143 | node->flags |= LYS_MAND_TRUE; |
| 5144 | lys_compile_mandatory_parents(node->parent, 1); |
| 5145 | } else { |
| 5146 | node->flags &= ~LYS_MAND_TRUE; |
| 5147 | lys_compile_mandatory_parents(node->parent, 0); |
| 5148 | } |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5149 | } |
| 5150 | break; |
| 5151 | default: |
| 5152 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5153 | "Invalid refine of %s statement - %s cannot hold this statement.", |
| 5154 | (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] | 5155 | goto cleanup; |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 5156 | } |
| 5157 | } |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 5158 | |
| 5159 | /* if-feature */ |
| 5160 | if (rfn->iffeatures) { |
| 5161 | /* 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] | 5162 | 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] | 5163 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5164 | |
| 5165 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 5166 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5167 | |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5168 | /* do some additional checks of the changed nodes when all the refines are applied */ |
| 5169 | for (u = 0; u < refined.count; ++u) { |
| 5170 | node = (struct lysc_node*)refined.objs[u]; |
| 5171 | rfn = &uses_p->refines[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5172 | lysc_update_path(ctx, NULL, rfn->nodeid); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5173 | |
| 5174 | /* check possible conflict with default value (default added, mandatory left true) */ |
| 5175 | if ((node->flags & LYS_MAND_TRUE) && |
| 5176 | (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) || |
| 5177 | ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) { |
| 5178 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5179 | "Invalid refine of default - the node is mandatory."); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5180 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5181 | } |
| 5182 | |
| 5183 | if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) { |
| 5184 | if (node->nodetype == LYS_LIST) { |
| 5185 | min = ((struct lysc_node_list*)node)->min; |
| 5186 | max = ((struct lysc_node_list*)node)->max; |
| 5187 | } else { |
| 5188 | min = ((struct lysc_node_leaflist*)node)->min; |
| 5189 | max = ((struct lysc_node_leaflist*)node)->max; |
| 5190 | } |
| 5191 | if (min > max) { |
| 5192 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5193 | "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".", |
| 5194 | (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements"); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5195 | goto cleanup; |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5196 | } |
| 5197 | } |
| 5198 | } |
| 5199 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5200 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5201 | ret = LY_SUCCESS; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5202 | |
| 5203 | cleanup: |
| 5204 | /* fix connection of the children nodes from fake context node back into the parent */ |
| 5205 | if (context_node_fake.child) { |
| 5206 | context_node_fake.child->prev = child; |
| 5207 | } |
| 5208 | LY_LIST_FOR(context_node_fake.child, child) { |
| 5209 | child->parent = parent; |
| 5210 | } |
| 5211 | |
| 5212 | if (uses_p->augments || uses_p->refines) { |
| 5213 | /* 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] | 5214 | if (context_node_fake.actions) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5215 | memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions); |
| 5216 | LY_ARRAY_FREE(context_node_fake.actions); |
| 5217 | } |
Radek Krejci | 65e20e2 | 2019-04-12 09:44:37 +0200 | [diff] [blame] | 5218 | if (context_node_fake.notifs) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5219 | memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs); |
| 5220 | LY_ARRAY_FREE(context_node_fake.notifs); |
| 5221 | } |
| 5222 | } |
| 5223 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5224 | /* reload previous context's mod_def */ |
| 5225 | ctx->mod_def = mod_old; |
| 5226 | /* remove the grouping from the stack for circular groupings dependency check */ |
| 5227 | ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL); |
| 5228 | assert(ctx->groupings.count == grp_stack_count); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 5229 | ly_set_erase(&refined, NULL); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 5230 | LY_ARRAY_FREE(augments); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5231 | |
| 5232 | return ret; |
| 5233 | } |
| 5234 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5235 | static int |
| 5236 | lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path) |
| 5237 | { |
| 5238 | struct lysp_node *iter; |
| 5239 | int len = 0; |
| 5240 | |
| 5241 | *path = NULL; |
| 5242 | for (iter = node; iter && len >= 0; iter = iter->parent) { |
| 5243 | char *s = *path; |
| 5244 | char *id; |
| 5245 | |
| 5246 | switch (iter->nodetype) { |
| 5247 | case LYS_USES: |
| 5248 | asprintf(&id, "{uses='%s'}", iter->name); |
| 5249 | break; |
| 5250 | case LYS_GROUPING: |
| 5251 | asprintf(&id, "{grouping='%s'}", iter->name); |
| 5252 | break; |
| 5253 | case LYS_AUGMENT: |
| 5254 | asprintf(&id, "{augment='%s'}", iter->name); |
| 5255 | break; |
| 5256 | default: |
| 5257 | id = strdup(iter->name); |
| 5258 | break; |
| 5259 | } |
| 5260 | |
| 5261 | if (!iter->parent) { |
| 5262 | /* print prefix */ |
| 5263 | len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : ""); |
| 5264 | } else { |
| 5265 | /* prefix is the same as in parent */ |
| 5266 | len = asprintf(path, "/%s%s", id, s ? s : ""); |
| 5267 | } |
| 5268 | free(s); |
| 5269 | free(id); |
| 5270 | } |
| 5271 | |
| 5272 | if (len < 0) { |
| 5273 | free(*path); |
| 5274 | *path = NULL; |
| 5275 | } else if (len == 0) { |
| 5276 | *path = strdup("/"); |
| 5277 | len = 1; |
| 5278 | } |
| 5279 | return len; |
| 5280 | } |
| 5281 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5282 | /** |
| 5283 | * @brief Validate groupings that were defined but not directly used in the schema itself. |
| 5284 | * |
| 5285 | * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately), |
| 5286 | * but to have the complete result of the schema validity, even such groupings are supposed to be checked. |
| 5287 | */ |
| 5288 | static LY_ERR |
| 5289 | lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp) |
| 5290 | { |
| 5291 | LY_ERR ret; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5292 | char *path; |
| 5293 | int len; |
| 5294 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5295 | struct lysp_node_uses fake_uses = { |
| 5296 | .parent = node_p, |
| 5297 | .nodetype = LYS_USES, |
| 5298 | .flags = 0, .next = NULL, |
| 5299 | .name = grp->name, |
| 5300 | .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL, |
| 5301 | .refines = NULL, .augments = NULL |
| 5302 | }; |
| 5303 | struct lysc_node_container fake_container = { |
| 5304 | .nodetype = LYS_CONTAINER, |
| 5305 | .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0, |
| 5306 | .module = ctx->mod, |
| 5307 | .sp = NULL, .parent = NULL, .next = NULL, |
| 5308 | .prev = (struct lysc_node*)&fake_container, |
| 5309 | .name = "fake", |
| 5310 | .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL, |
| 5311 | .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL |
| 5312 | }; |
| 5313 | |
| 5314 | if (grp->parent) { |
| 5315 | LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name); |
| 5316 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5317 | |
| 5318 | len = lys_compile_grouping_pathlog(ctx, grp->parent, &path); |
| 5319 | if (len < 0) { |
| 5320 | LOGMEM(ctx->ctx); |
| 5321 | return LY_EMEM; |
| 5322 | } |
| 5323 | strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1); |
| 5324 | ctx->path_len = (uint16_t)len; |
| 5325 | free(path); |
| 5326 | |
| 5327 | lysc_update_path(ctx, NULL, "{grouping}"); |
| 5328 | lysc_update_path(ctx, NULL, grp->name); |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5329 | ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container); |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5330 | lysc_update_path(ctx, NULL, NULL); |
| 5331 | lysc_update_path(ctx, NULL, NULL); |
| 5332 | |
| 5333 | ctx->path_len = 1; |
| 5334 | ctx->path[1] = '\0'; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 5335 | |
| 5336 | /* cleanup */ |
| 5337 | lysc_node_container_free(ctx->ctx, &fake_container); |
| 5338 | |
| 5339 | return ret; |
| 5340 | } |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5341 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5342 | /** |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5343 | * @brief Compile parsed schema node information. |
| 5344 | * @param[in] ctx Compile context |
| 5345 | * @param[in] node_p Parsed schema node. |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5346 | * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is |
| 5347 | * NULL for top-level nodes, in such a case the module where the node will be connected is taken from |
| 5348 | * the compile context. |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5349 | * @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). |
| 5350 | * 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] | 5351 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 5352 | */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5353 | static LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5354 | 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] | 5355 | { |
| 5356 | LY_ERR ret = LY_EVALID; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5357 | struct lysc_node *node; |
| 5358 | struct lysc_node_case *cs; |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5359 | struct lysc_when **when; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5360 | unsigned int u; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5361 | 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] | 5362 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5363 | if (node_p->nodetype != LYS_USES) { |
| 5364 | lysc_update_path(ctx, parent, node_p->name); |
| 5365 | } else { |
| 5366 | lysc_update_path(ctx, NULL, "{uses}"); |
| 5367 | lysc_update_path(ctx, NULL, node_p->name); |
| 5368 | } |
| 5369 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5370 | switch (node_p->nodetype) { |
| 5371 | case LYS_CONTAINER: |
| 5372 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container)); |
| 5373 | node_compile_spec = lys_compile_node_container; |
| 5374 | break; |
| 5375 | case LYS_LEAF: |
| 5376 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf)); |
| 5377 | node_compile_spec = lys_compile_node_leaf; |
| 5378 | break; |
| 5379 | case LYS_LIST: |
| 5380 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5381 | node_compile_spec = lys_compile_node_list; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5382 | break; |
| 5383 | case LYS_LEAFLIST: |
| 5384 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5385 | node_compile_spec = lys_compile_node_leaflist; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5386 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5387 | case LYS_CHOICE: |
| 5388 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5389 | node_compile_spec = lys_compile_node_choice; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5390 | break; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5391 | case LYS_ANYXML: |
| 5392 | case LYS_ANYDATA: |
| 5393 | node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 5394 | node_compile_spec = lys_compile_node_any; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5395 | break; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 5396 | case LYS_USES: |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5397 | ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent); |
| 5398 | lysc_update_path(ctx, NULL, NULL); |
| 5399 | lysc_update_path(ctx, NULL, NULL); |
| 5400 | return ret; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5401 | default: |
| 5402 | LOGINT(ctx->ctx); |
| 5403 | return LY_EINT; |
| 5404 | } |
| 5405 | LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM); |
| 5406 | node->nodetype = node_p->nodetype; |
| 5407 | node->module = ctx->mod; |
| 5408 | node->prev = node; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 5409 | node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5410 | |
| 5411 | /* config */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5412 | if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5413 | /* ignore config statements inside RPC/action data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5414 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5415 | node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R; |
| 5416 | } else if (ctx->options & LYSC_OPT_NOTIFICATION) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5417 | /* ignore config statements inside Notification data */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5418 | node->flags &= ~LYS_CONFIG_MASK; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5419 | node->flags |= LYS_CONFIG_R; |
| 5420 | } else if (!(node->flags & LYS_CONFIG_MASK)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5421 | /* config not explicitely set, inherit it from parent */ |
| 5422 | if (parent) { |
| 5423 | node->flags |= parent->flags & LYS_CONFIG_MASK; |
| 5424 | } else { |
| 5425 | /* default is config true */ |
| 5426 | node->flags |= LYS_CONFIG_W; |
| 5427 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5428 | } else { |
| 5429 | /* config set explicitely */ |
| 5430 | node->flags |= LYS_SET_CONFIG; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5431 | } |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 5432 | if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) { |
| 5433 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5434 | "Configuration node cannot be child of any state data node."); |
| 5435 | goto error; |
| 5436 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5437 | |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5438 | /* *list ordering */ |
| 5439 | if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 5440 | if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5441 | 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] | 5442 | (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" : |
| 5443 | (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 5444 | node->flags &= ~LYS_ORDBY_MASK; |
| 5445 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5446 | } else if (!(node->flags & LYS_ORDBY_MASK)) { |
| 5447 | /* default ordering is system */ |
| 5448 | node->flags |= LYS_ORDBY_SYSTEM; |
| 5449 | } |
| 5450 | } |
| 5451 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5452 | /* status - it is not inherited by specification, but it does not make sense to have |
| 5453 | * 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] | 5454 | if (!parent || parent->nodetype != LYS_CHOICE) { |
| 5455 | /* in case of choice/case's children, postpone the check to the moment we know if |
| 5456 | * 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] | 5457 | 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] | 5458 | } |
| 5459 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5460 | if (!(ctx->options & LYSC_OPT_FREE_SP)) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5461 | node->sp = node_p; |
| 5462 | } |
| 5463 | DUP_STRING(ctx->ctx, node_p->name, node->name); |
Radek Krejci | 12fb914 | 2019-01-08 09:45:30 +0100 | [diff] [blame] | 5464 | DUP_STRING(ctx->ctx, node_p->dsc, node->dsc); |
| 5465 | DUP_STRING(ctx->ctx, node_p->ref, node->ref); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5466 | if (node_p->when) { |
| 5467 | LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5468 | ret = lys_compile_when(ctx, node_p->when, when); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5469 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 5470 | (*when)->context = lysc_xpath_context(node); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 5471 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5472 | 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] | 5473 | |
| 5474 | /* nodetype-specific part */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5475 | LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5476 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 5477 | COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error); |
| 5478 | |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 5479 | /* inherit LYS_MAND_TRUE in parent containers */ |
| 5480 | if (node->flags & LYS_MAND_TRUE) { |
| 5481 | lys_compile_mandatory_parents(parent, 1); |
| 5482 | } |
| 5483 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5484 | lysc_update_path(ctx, NULL, NULL); |
| 5485 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5486 | /* insert into parent's children */ |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 5487 | if (parent) { |
| 5488 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5489 | if (node_p->parent->nodetype == LYS_CASE) { |
| 5490 | lysc_update_path(ctx, parent, node_p->parent->name); |
| 5491 | } else { |
| 5492 | lysc_update_path(ctx, parent, node->name); |
| 5493 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5494 | 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] | 5495 | LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 5496 | if (uses_status) { |
| 5497 | |
| 5498 | } |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5499 | /* 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] | 5500 | * it directly gets the same status flags as the choice; |
| 5501 | * 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] | 5502 | LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5503 | node->parent = (struct lysc_node*)cs; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5504 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5505 | } else { /* other than choice */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5506 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 5507 | node->parent = parent; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5508 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5509 | 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] | 5510 | |
| 5511 | if (parent->nodetype == LYS_CHOICE) { |
| 5512 | lysc_update_path(ctx, NULL, NULL); |
| 5513 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5514 | } else { |
| 5515 | /* top-level element */ |
| 5516 | if (!ctx->mod->compiled->data) { |
| 5517 | ctx->mod->compiled->data = node; |
| 5518 | } else { |
| 5519 | /* insert at the end of the module's top-level nodes list */ |
| 5520 | ctx->mod->compiled->data->prev->next = node; |
| 5521 | node->prev = ctx->mod->compiled->data->prev; |
| 5522 | ctx->mod->compiled->data->prev = node; |
| 5523 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5524 | lysc_update_path(ctx, parent, node->name); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 5525 | if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs, |
| 5526 | ctx->mod->compiled->notifs, node->name, node)) { |
| 5527 | return LY_EVALID; |
| 5528 | } |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5529 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5530 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 5531 | |
| 5532 | return LY_SUCCESS; |
| 5533 | |
| 5534 | error: |
| 5535 | lysc_node_free(ctx->ctx, node); |
| 5536 | return ret; |
| 5537 | } |
| 5538 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5539 | static void |
| 5540 | lysc_disconnect(struct lysc_node *node) |
| 5541 | { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5542 | struct lysc_node *parent, *child, *prev = NULL, *next; |
| 5543 | struct lysc_node_case *cs = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5544 | int remove_cs = 0; |
| 5545 | |
| 5546 | parent = node->parent; |
| 5547 | |
| 5548 | /* parent's first child */ |
| 5549 | if (!parent) { |
| 5550 | return; |
| 5551 | } |
| 5552 | if (parent->nodetype == LYS_CHOICE) { |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5553 | cs = (struct lysc_node_case*)node; |
| 5554 | } else if (parent->nodetype == LYS_CASE) { |
| 5555 | /* disconnecting some node in a case */ |
| 5556 | cs = (struct lysc_node_case*)parent; |
| 5557 | parent = cs->parent; |
| 5558 | for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) { |
| 5559 | if (child == node) { |
| 5560 | if (cs->child == child) { |
| 5561 | if (!child->next || child->next->parent != (struct lysc_node*)cs) { |
| 5562 | /* case with a single child -> remove also the case */ |
| 5563 | child->parent = NULL; |
| 5564 | remove_cs = 1; |
| 5565 | } else { |
| 5566 | cs->child = child->next; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5567 | } |
| 5568 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5569 | break; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5570 | } |
| 5571 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5572 | if (!remove_cs) { |
| 5573 | cs = NULL; |
| 5574 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5575 | } else if (lysc_node_children(parent, node->flags) == node) { |
| 5576 | *lysc_node_children_p(parent, node->flags) = node->next; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5577 | } |
| 5578 | |
| 5579 | if (cs) { |
| 5580 | if (remove_cs) { |
| 5581 | /* cs has only one child which is being also removed */ |
| 5582 | lysc_disconnect((struct lysc_node*)cs); |
| 5583 | lysc_node_free(cs->module->ctx, (struct lysc_node*)cs); |
| 5584 | } else { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5585 | if (((struct lysc_node_choice*)parent)->dflt == cs) { |
| 5586 | /* default case removed */ |
| 5587 | ((struct lysc_node_choice*)parent)->dflt = NULL; |
| 5588 | } |
| 5589 | if (((struct lysc_node_choice*)parent)->cases == cs) { |
| 5590 | /* first case removed */ |
| 5591 | ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next; |
| 5592 | } |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5593 | if (cs->child) { |
| 5594 | /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */ |
| 5595 | if (cs->child->prev->parent != (struct lysc_node*)cs) { |
| 5596 | prev = cs->child->prev; |
| 5597 | } /* else all the children are under a single case */ |
| 5598 | LY_LIST_FOR_SAFE(cs->child, next, child) { |
| 5599 | if (child->parent != (struct lysc_node*)cs) { |
| 5600 | break; |
| 5601 | } |
| 5602 | lysc_node_free(node->module->ctx, child); |
| 5603 | } |
| 5604 | if (prev) { |
| 5605 | if (prev->next) { |
| 5606 | prev->next = child; |
| 5607 | } |
| 5608 | if (child) { |
| 5609 | child->prev = prev; |
| 5610 | } else { |
| 5611 | /* link from the first child under the cases */ |
| 5612 | ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev; |
| 5613 | } |
| 5614 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5615 | } |
| 5616 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5617 | } |
| 5618 | |
| 5619 | /* siblings */ |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5620 | if (node->prev->next) { |
| 5621 | node->prev->next = node->next; |
| 5622 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5623 | if (node->next) { |
| 5624 | node->next->prev = node->prev; |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5625 | } else if (node->nodetype != LYS_CASE) { |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5626 | child = (struct lysc_node*)lysc_node_children(parent, node->flags); |
Radek Krejci | 0a048dd | 2019-02-18 16:01:43 +0100 | [diff] [blame] | 5627 | if (child) { |
| 5628 | child->prev = node->prev; |
| 5629 | } |
| 5630 | } else if (((struct lysc_node_choice*)parent)->cases) { |
| 5631 | ((struct lysc_node_choice*)parent)->cases->prev = node->prev; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5632 | } |
| 5633 | } |
| 5634 | |
| 5635 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 5636 | lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p) |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5637 | { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5638 | LY_ERR ret = LY_EVALID, rc; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5639 | struct ly_set devs_p = {0}; |
| 5640 | struct ly_set targets = {0}; |
| 5641 | struct lysc_node *target; /* target target of the deviation */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5642 | struct lysc_node_list *list; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5643 | struct lysc_action *rpcs; |
| 5644 | struct lysc_notif *notifs; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5645 | struct lysp_deviation *dev; |
| 5646 | struct lysp_deviate *d, **dp_new; |
| 5647 | struct lysp_deviate_add *d_add; |
| 5648 | struct lysp_deviate_del *d_del; |
| 5649 | struct lysp_deviate_rpl *d_rpl; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5650 | unsigned int u, v, x, y, z; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5651 | struct lysc_deviation { |
| 5652 | const char *nodeid; |
| 5653 | struct lysc_node *target; /* target node of the deviation */ |
| 5654 | 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] | 5655 | uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5656 | uint8_t not_supported; /* flag if deviates contains not-supported deviate */ |
| 5657 | } **devs = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5658 | int i, changed_type; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5659 | size_t prefix_len, name_len; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5660 | const char *prefix, *name, *nodeid, *dflt; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5661 | struct lys_module *mod; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5662 | uint32_t min, max; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5663 | uint16_t flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5664 | |
| 5665 | /* get all deviations from the module and all its submodules ... */ |
| 5666 | LY_ARRAY_FOR(mod_p->deviations, u) { |
| 5667 | ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST); |
| 5668 | } |
| 5669 | LY_ARRAY_FOR(mod_p->includes, v) { |
| 5670 | LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) { |
| 5671 | ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST); |
| 5672 | } |
| 5673 | } |
| 5674 | if (!devs_p.count) { |
| 5675 | /* nothing to do */ |
| 5676 | return LY_SUCCESS; |
| 5677 | } |
| 5678 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5679 | lysc_update_path(ctx, NULL, "{deviation}"); |
| 5680 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5681 | /* ... and group them by the target node */ |
| 5682 | devs = calloc(devs_p.count, sizeof *devs); |
| 5683 | for (u = 0; u < devs_p.count; ++u) { |
| 5684 | dev = devs_p.objs[u]; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5685 | lysc_update_path(ctx, NULL, dev->nodeid); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5686 | |
| 5687 | /* resolve the target */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5688 | LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1, |
| 5689 | (const struct lysc_node**)&target, &flags), cleanup); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5690 | if (target->nodetype == LYS_ACTION) { |
| 5691 | /* move the target pointer to input/output to make them different from the action and |
| 5692 | * between them. Before the devs[] item is being processed, the target pointer must be fixed |
| 5693 | * back to the RPC/action node due to a better compatibility and decision code in this function. |
| 5694 | * The LYSC_OPT_INTERNAL is used as a flag to this change. */ |
| 5695 | if (flags & LYSC_OPT_RPC_INPUT) { |
| 5696 | target = (struct lysc_node*)&((struct lysc_action*)target)->input; |
| 5697 | flags |= LYSC_OPT_INTERNAL; |
| 5698 | } else if (flags & LYSC_OPT_RPC_OUTPUT) { |
| 5699 | target = (struct lysc_node*)&((struct lysc_action*)target)->output; |
| 5700 | flags |= LYSC_OPT_INTERNAL; |
| 5701 | } |
| 5702 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5703 | /* insert into the set of targets with duplicity detection */ |
| 5704 | i = ly_set_add(&targets, target, 0); |
| 5705 | if (!devs[i]) { |
| 5706 | /* new record */ |
| 5707 | devs[i] = calloc(1, sizeof **devs); |
| 5708 | devs[i]->target = target; |
| 5709 | devs[i]->nodeid = dev->nodeid; |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5710 | devs[i]->flags = flags; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5711 | } |
| 5712 | /* add deviates into the deviation's list of deviates */ |
| 5713 | for (d = dev->deviates; d; d = d->next) { |
| 5714 | LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup); |
| 5715 | *dp_new = d; |
| 5716 | if (d->mod == LYS_DEV_NOT_SUPPORTED) { |
| 5717 | devs[i]->not_supported = 1; |
| 5718 | } |
| 5719 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5720 | |
| 5721 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5722 | } |
| 5723 | |
| 5724 | /* MACROS for deviates checking */ |
| 5725 | #define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \ |
| 5726 | if (!(devs[u]->target->nodetype & (NODETYPES))) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5727 | 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] | 5728 | goto cleanup; \ |
| 5729 | } |
| 5730 | |
| 5731 | #define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \ |
| 5732 | if (LY_ARRAY_SIZE(ARRAY) > MAX) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5733 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%u) %s properties.", \ |
| 5734 | lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5735 | goto cleanup; \ |
| 5736 | } |
| 5737 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5738 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5739 | #define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5740 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
| 5741 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
| 5742 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \ |
| 5743 | PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \ |
| 5744 | goto cleanup; \ |
| 5745 | } |
| 5746 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5747 | #define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 5748 | if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5749 | int dynamic_ = 0; const char *val_; \ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5750 | val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \ |
| 5751 | lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5752 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5753 | "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \ |
| 5754 | if (dynamic_) {free((void*)val_);} \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5755 | goto cleanup; \ |
| 5756 | } |
| 5757 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5758 | #define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5759 | if (((TYPE)devs[u]->target)->MEMBER COND) { \ |
| 5760 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5761 | "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \ |
| 5762 | PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5763 | goto cleanup; \ |
| 5764 | } |
| 5765 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5766 | #define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \ |
| 5767 | if (!((TYPE)devs[u]->target)->MEMBER || COND) { \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5768 | 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] | 5769 | goto cleanup; \ |
| 5770 | } |
| 5771 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5772 | #define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \ |
| 5773 | if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \ |
| 5774 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5775 | "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] | 5776 | goto cleanup; \ |
| 5777 | } |
| 5778 | |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5779 | #define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \ |
| 5780 | DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \ |
| 5781 | LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \ |
| 5782 | LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \ |
| 5783 | 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] | 5784 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5785 | if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5786 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5787 | "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \ |
| 5788 | PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5789 | goto cleanup; \ |
| 5790 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5791 | LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5792 | DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \ |
| 5793 | memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \ |
| 5794 | &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \ |
| 5795 | (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] | 5796 | } \ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5797 | if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \ |
| 5798 | LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \ |
| 5799 | ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5800 | } |
| 5801 | |
| 5802 | /* apply deviations */ |
| 5803 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5804 | struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target; |
| 5805 | struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target; |
| 5806 | struct ly_err_item *err = NULL; |
| 5807 | |
| 5808 | dflt = NULL; |
| 5809 | changed_type = 0; |
| 5810 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5811 | lysc_update_path(ctx, NULL, devs[u]->nodeid); |
| 5812 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5813 | if (devs[u]->flags & LYSC_OPT_INTERNAL) { |
| 5814 | /* fix the target pointer in case of RPC's/action's input/output */ |
| 5815 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5816 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input)); |
| 5817 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5818 | devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output)); |
| 5819 | } |
| 5820 | } |
| 5821 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5822 | /* not-supported */ |
| 5823 | if (devs[u]->not_supported) { |
| 5824 | if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) { |
| 5825 | LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.", |
| 5826 | LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid); |
| 5827 | } |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5828 | |
| 5829 | #define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \ |
| 5830 | if (devs[u]->target->parent) { \ |
| 5831 | ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \ |
| 5832 | } else { \ |
| 5833 | ARRAY = devs[u]->target->module->compiled->ARRAY; \ |
| 5834 | } \ |
| 5835 | LY_ARRAY_FOR(ARRAY, x) { \ |
| 5836 | if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \ |
| 5837 | } \ |
| 5838 | if (x < LY_ARRAY_SIZE(ARRAY)) { \ |
| 5839 | FREEFUNC(ctx->ctx, &ARRAY[x]); \ |
| 5840 | memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \ |
| 5841 | LY_ARRAY_DECREMENT(ARRAY); \ |
| 5842 | } |
| 5843 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5844 | if (devs[u]->target->nodetype == LYS_ACTION) { |
| 5845 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5846 | /* remove RPC's/action's input */ |
| 5847 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input); |
| 5848 | 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] | 5849 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free); |
| 5850 | ((struct lysc_action*)devs[u]->target)->input_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5851 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5852 | /* remove RPC's/action's output */ |
| 5853 | lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output); |
| 5854 | 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] | 5855 | FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free); |
| 5856 | ((struct lysc_action*)devs[u]->target)->output_exts = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5857 | } else { |
| 5858 | /* remove RPC/action */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5859 | REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5860 | } |
| 5861 | } else if (devs[u]->target->nodetype == LYS_NOTIF) { |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5862 | /* remove Notification */ |
| 5863 | REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 5864 | } else { |
| 5865 | /* remove the target node */ |
| 5866 | lysc_disconnect(devs[u]->target); |
| 5867 | lysc_node_free(ctx->ctx, devs[u]->target); |
| 5868 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5869 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5870 | /* mark the context for later re-compilation of objects that could reference the curently removed node */ |
| 5871 | ctx->ctx->flags |= LY_CTX_CHANGED_TREE; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5872 | continue; |
| 5873 | } |
| 5874 | |
| 5875 | /* list of deviates (not-supported is not present in the list) */ |
| 5876 | LY_ARRAY_FOR(devs[u]->deviates, v) { |
| 5877 | d = devs[u]->deviates[v]; |
| 5878 | |
| 5879 | switch (d->mod) { |
| 5880 | case LYS_DEV_ADD: |
| 5881 | d_add = (struct lysp_deviate_add*)d; |
| 5882 | /* [units-stmt] */ |
| 5883 | if (d_add->units) { |
| 5884 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units"); |
| 5885 | DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units); |
| 5886 | |
| 5887 | FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5888 | DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 5889 | } |
| 5890 | |
| 5891 | /* *must-stmt */ |
| 5892 | if (d_add->musts) { |
| 5893 | switch (devs[u]->target->nodetype) { |
| 5894 | case LYS_CONTAINER: |
| 5895 | case LYS_LIST: |
| 5896 | 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] | 5897 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5898 | break; |
| 5899 | case LYS_LEAF: |
| 5900 | case LYS_LEAFLIST: |
| 5901 | case LYS_ANYDATA: |
| 5902 | 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] | 5903 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5904 | break; |
| 5905 | case LYS_NOTIF: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 5906 | 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] | 5907 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5908 | break; |
| 5909 | case LYS_ACTION: |
| 5910 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 5911 | 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] | 5912 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5913 | break; |
| 5914 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 5915 | 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] | 5916 | x, lys_compile_must, ret, cleanup); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 5917 | break; |
| 5918 | } |
| 5919 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5920 | default: |
| 5921 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 5922 | lys_nodetype2str(devs[u]->target->nodetype), "add", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5923 | goto cleanup; |
| 5924 | } |
| 5925 | } |
| 5926 | |
| 5927 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 5928 | if (d_add->uniques) { |
| 5929 | DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique"); |
| 5930 | LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup); |
| 5931 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5932 | |
| 5933 | /* *default-stmt */ |
| 5934 | if (d_add->dflts) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5935 | switch (devs[u]->target->nodetype) { |
| 5936 | case LYS_LEAF: |
| 5937 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5938 | 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] | 5939 | if (leaf->dflt) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5940 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5941 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5942 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 5943 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 5944 | } else { |
| 5945 | /* prepare new default value storage */ |
| 5946 | leaf->dflt = calloc(1, sizeof *leaf->dflt); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5947 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5948 | dflt = d_add->dflts[0]; |
| 5949 | /* parsing is done at the end after possible replace of the leaf's type */ |
| 5950 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5951 | /* mark the new default values as leaf's own */ |
| 5952 | devs[u]->target->flags |= LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5953 | break; |
| 5954 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5955 | if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5956 | /* first, remove the default value taken from the type */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5957 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5958 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5959 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 5960 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 5961 | free(llist->dflts[x]); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5962 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5963 | LY_ARRAY_FREE(llist->dflts); |
| 5964 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5965 | LY_ARRAY_FREE(llist->dflts_mods); |
| 5966 | llist->dflts_mods = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5967 | } |
| 5968 | /* add new default value(s) */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5969 | 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] | 5970 | LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup); |
| 5971 | for (x = y = LY_ARRAY_SIZE(llist->dflts); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5972 | x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 5973 | LY_ARRAY_INCREMENT(llist->dflts_mods); |
| 5974 | llist->dflts_mods[x] = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5975 | LY_ARRAY_INCREMENT(llist->dflts); |
| 5976 | llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]); |
| 5977 | llist->dflts[x]->realtype = llist->type; |
| 5978 | 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] | 5979 | 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] | 5980 | (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] | 5981 | llist->dflts[x]->realtype->refcount++; |
| 5982 | if (err) { |
| 5983 | ly_err_print(err); |
| 5984 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 5985 | "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).", |
| 5986 | d_add->dflts[x - y], err->msg); |
| 5987 | ly_err_free(err); |
| 5988 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 5989 | if (rc == LY_EINCOMPLETE) { |
| 5990 | /* postpone default compilation when the tree is complete */ |
| 5991 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 5992 | |
| 5993 | /* but in general result is so far ok */ |
| 5994 | rc = LY_SUCCESS; |
| 5995 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 5996 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5997 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 5998 | /* mark the new default values as leaf-list's own */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 5999 | devs[u]->target->flags |= LYS_SET_DFLT; |
| 6000 | break; |
| 6001 | case LYS_CHOICE: |
| 6002 | DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default"); |
| 6003 | DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name); |
| 6004 | /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation |
| 6005 | * 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] | 6006 | 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] | 6007 | goto cleanup; |
| 6008 | } |
| 6009 | break; |
| 6010 | default: |
| 6011 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6012 | lys_nodetype2str(devs[u]->target->nodetype), "add", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6013 | goto cleanup; |
| 6014 | } |
| 6015 | } |
| 6016 | |
| 6017 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6018 | if (d_add->flags & LYS_CONFIG_MASK) { |
| 6019 | 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] | 6020 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6021 | lys_nodetype2str(devs[u]->target->nodetype), "add", "config"); |
| 6022 | goto cleanup; |
| 6023 | } |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6024 | if (devs[u]->flags) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6025 | LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.", |
| 6026 | devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6027 | } |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6028 | if (devs[u]->target->flags & LYS_SET_CONFIG) { |
| 6029 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6030 | "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").", |
| 6031 | devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false"); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6032 | goto cleanup; |
| 6033 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6034 | 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] | 6035 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6036 | |
| 6037 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6038 | if (d_add->flags & LYS_MAND_MASK) { |
| 6039 | if (devs[u]->target->flags & LYS_MAND_MASK) { |
| 6040 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6041 | "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").", |
| 6042 | devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false"); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6043 | goto cleanup; |
| 6044 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6045 | 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] | 6046 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6047 | |
| 6048 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6049 | if (d_add->flags & LYS_SET_MIN) { |
| 6050 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6051 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 6052 | /* change value */ |
| 6053 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min; |
| 6054 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6055 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 6056 | /* change value */ |
| 6057 | ((struct lysc_node_list*)devs[u]->target)->min = d_add->min; |
| 6058 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6059 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6060 | lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements"); |
| 6061 | goto cleanup; |
| 6062 | } |
| 6063 | if (d_add->min) { |
| 6064 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 6065 | } |
| 6066 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6067 | |
| 6068 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6069 | if (d_add->flags & LYS_SET_MAX) { |
| 6070 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6071 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 6072 | /* change value */ |
| 6073 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 6074 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6075 | DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 6076 | /* change value */ |
| 6077 | ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1; |
| 6078 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6079 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6080 | lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements"); |
| 6081 | goto cleanup; |
| 6082 | } |
| 6083 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6084 | |
| 6085 | break; |
| 6086 | case LYS_DEV_DELETE: |
| 6087 | d_del = (struct lysp_deviate_del*)d; |
| 6088 | |
| 6089 | /* [units-stmt] */ |
| 6090 | if (d_del->units) { |
| 6091 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units"); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6092 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units); |
| 6093 | if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) { |
| 6094 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6095 | "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6096 | d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6097 | goto cleanup; |
| 6098 | } |
| 6099 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6100 | ((struct lysc_node_leaf*)devs[u]->target)->units = NULL; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6101 | } |
| 6102 | |
| 6103 | /* *must-stmt */ |
| 6104 | if (d_del->musts) { |
| 6105 | switch (devs[u]->target->nodetype) { |
| 6106 | case LYS_CONTAINER: |
| 6107 | case LYS_LIST: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6108 | 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] | 6109 | break; |
| 6110 | case LYS_LEAF: |
| 6111 | case LYS_LEAFLIST: |
| 6112 | case LYS_ANYDATA: |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 6113 | 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] | 6114 | break; |
| 6115 | case LYS_NOTIF: |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 6116 | 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] | 6117 | break; |
| 6118 | case LYS_ACTION: |
| 6119 | if (devs[u]->flags & LYSC_OPT_RPC_INPUT) { |
| 6120 | DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 6121 | break; |
| 6122 | } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) { |
| 6123 | DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must"); |
| 6124 | break; |
| 6125 | } |
| 6126 | /* fall through */ |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6127 | default: |
| 6128 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6129 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "must"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6130 | goto cleanup; |
| 6131 | } |
| 6132 | } |
| 6133 | |
| 6134 | /* *unique-stmt */ |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6135 | if (d_del->uniques) { |
| 6136 | DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique"); |
| 6137 | list = (struct lysc_node_list*)devs[u]->target; /* shortcut */ |
| 6138 | LY_ARRAY_FOR(d_del->uniques, x) { |
| 6139 | LY_ARRAY_FOR(list->uniques, z) { |
| 6140 | for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) { |
| 6141 | nodeid = strpbrk(name, " \t\n"); |
| 6142 | if (nodeid) { |
| 6143 | if (strncmp(name, list->uniques[z][y]->name, nodeid - name) |
| 6144 | || list->uniques[z][y]->name[nodeid - name] != '\0') { |
| 6145 | break; |
| 6146 | } |
| 6147 | while (isspace(*nodeid)) { |
| 6148 | ++nodeid; |
| 6149 | } |
| 6150 | } else { |
| 6151 | if (strcmp(name, list->uniques[z][y]->name)) { |
| 6152 | break; |
| 6153 | } |
| 6154 | } |
| 6155 | } |
| 6156 | if (!name) { |
| 6157 | /* complete match - remove the unique */ |
| 6158 | LY_ARRAY_DECREMENT(list->uniques); |
| 6159 | LY_ARRAY_FREE(list->uniques[z]); |
| 6160 | memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques)); |
| 6161 | --z; |
| 6162 | break; |
| 6163 | } |
| 6164 | } |
| 6165 | if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) { |
| 6166 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6167 | "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.", |
| 6168 | d_del->uniques[x]); |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 6169 | goto cleanup; |
| 6170 | } |
| 6171 | } |
| 6172 | if (!LY_ARRAY_SIZE(list->uniques)) { |
| 6173 | LY_ARRAY_FREE(list->uniques); |
| 6174 | list->uniques = NULL; |
| 6175 | } |
| 6176 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6177 | |
| 6178 | /* *default-stmt */ |
| 6179 | if (d_del->dflts) { |
| 6180 | switch (devs[u]->target->nodetype) { |
| 6181 | case LYS_LEAF: |
| 6182 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6183 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6184 | dflt, "deleting", "default", d_del->dflts[0]); |
| 6185 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6186 | /* check that the values matches */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6187 | 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] | 6188 | if (strcmp(dflt, d_del->dflts[0])) { |
| 6189 | if (i) { |
| 6190 | free((char*)dflt); |
| 6191 | } |
| 6192 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
| 6193 | "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".", |
| 6194 | d_del->dflts[0], dflt); |
| 6195 | goto cleanup; |
| 6196 | } |
| 6197 | if (i) { |
| 6198 | free((char*)dflt); |
| 6199 | } |
| 6200 | dflt = NULL; |
| 6201 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6202 | /* update the list of incomplete default values if needed */ |
| 6203 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6204 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6205 | /* remove the default specification */ |
| 6206 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6207 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6208 | free(leaf->dflt); |
| 6209 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6210 | leaf->dflt_mod = NULL; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6211 | devs[u]->target->flags &= ~LYS_SET_DFLT; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6212 | break; |
| 6213 | case LYS_LEAFLIST: |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6214 | DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]); |
| 6215 | LY_ARRAY_FOR(d_del->dflts, x) { |
| 6216 | LY_ARRAY_FOR(llist->dflts, y) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6217 | 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] | 6218 | if (!strcmp(dflt, d_del->dflts[x])) { |
| 6219 | if (i) { |
| 6220 | free((char*)dflt); |
| 6221 | } |
| 6222 | dflt = NULL; |
| 6223 | break; |
| 6224 | } |
| 6225 | if (i) { |
| 6226 | free((char*)dflt); |
| 6227 | } |
| 6228 | dflt = NULL; |
| 6229 | } |
| 6230 | if (y == LY_ARRAY_SIZE(llist->dflts)) { |
| 6231 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" " |
| 6232 | "which does not match any of the target's property values.", d_del->dflts[x]); |
| 6233 | goto cleanup; |
| 6234 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6235 | |
| 6236 | /* update the list of incomplete default values if needed */ |
| 6237 | lysc_incomplete_dflts_remove(ctx, llist->dflts[y]); |
| 6238 | |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6239 | LY_ARRAY_DECREMENT(llist->dflts_mods); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6240 | LY_ARRAY_DECREMENT(llist->dflts); |
| 6241 | llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]); |
| 6242 | lysc_type_free(ctx->ctx, llist->dflts[y]->realtype); |
| 6243 | free(llist->dflts[y]); |
| 6244 | 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] | 6245 | 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] | 6246 | } |
| 6247 | if (!LY_ARRAY_SIZE(llist->dflts)) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6248 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6249 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6250 | LY_ARRAY_FREE(llist->dflts); |
| 6251 | llist->dflts = NULL; |
| 6252 | llist->flags &= ~LYS_SET_DFLT; |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6253 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6254 | break; |
| 6255 | case LYS_CHOICE: |
| 6256 | DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default"); |
| 6257 | DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]); |
| 6258 | nodeid = d_del->dflts[0]; |
Radek Krejci | b4a4a27 | 2019-06-10 12:44:52 +0200 | [diff] [blame] | 6259 | 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] | 6260 | if (prefix) { |
| 6261 | /* use module prefixes from the deviation module to match the module of the default case */ |
| 6262 | if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) { |
| 6263 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6264 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6265 | "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] | 6266 | goto cleanup; |
| 6267 | } |
| 6268 | if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) { |
| 6269 | LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6270 | "Invalid deviation deleting \"default\" property \"%s\" of choice. " |
| 6271 | "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] | 6272 | goto cleanup; |
| 6273 | } |
| 6274 | } |
| 6275 | /* else { |
| 6276 | * strictly, the default prefix would point to the deviation module, but the value should actually |
| 6277 | * match the default string in the original module (usually unprefixed), so in this case we do not check |
| 6278 | * the module of the default case, just matching its name */ |
| 6279 | if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) { |
| 6280 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6281 | "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".", |
| 6282 | 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] | 6283 | goto cleanup; |
| 6284 | } |
| 6285 | ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT; |
| 6286 | ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL; |
| 6287 | break; |
| 6288 | default: |
| 6289 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6290 | lys_nodetype2str(devs[u]->target->nodetype), "delete", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6291 | goto cleanup; |
| 6292 | } |
| 6293 | } |
| 6294 | |
| 6295 | break; |
| 6296 | case LYS_DEV_REPLACE: |
| 6297 | d_rpl = (struct lysp_deviate_rpl*)d; |
| 6298 | |
| 6299 | /* [type-stmt] */ |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6300 | if (d_rpl->type) { |
| 6301 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type"); |
| 6302 | /* type is mandatory, so checking for its presence is not necessary */ |
| 6303 | 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] | 6304 | |
| 6305 | if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) { |
| 6306 | /* the target has default from the previous type - remove it */ |
| 6307 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6308 | /* update the list of incomplete default values if needed */ |
| 6309 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6310 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6311 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6312 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6313 | free(leaf->dflt); |
| 6314 | leaf->dflt = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6315 | leaf->dflt_mod = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6316 | } else { /* LYS_LEAFLIST */ |
| 6317 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6318 | lysc_incomplete_dflts_remove(ctx, llist->dflts[x]); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6319 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6320 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6321 | free(llist->dflts[x]); |
| 6322 | } |
| 6323 | LY_ARRAY_FREE(llist->dflts); |
| 6324 | llist->dflts = NULL; |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6325 | LY_ARRAY_FREE(llist->dflts_mods); |
| 6326 | llist->dflts_mods = NULL; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6327 | } |
| 6328 | } |
| 6329 | if (!leaf->dflt) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6330 | /* there is no default value, do not set changed_type after type compilation |
| 6331 | * which is used to recompile the default value */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6332 | changed_type = -1; |
| 6333 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6334 | 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] | 6335 | changed_type++; |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6336 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6337 | |
| 6338 | /* [units-stmt] */ |
| 6339 | if (d_rpl->units) { |
| 6340 | DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units"); |
| 6341 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS), |
| 6342 | units, "replacing", "units", d_rpl->units); |
| 6343 | |
| 6344 | lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6345 | DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units); |
| 6346 | } |
| 6347 | |
| 6348 | /* [default-stmt] */ |
| 6349 | if (d_rpl->dflt) { |
| 6350 | switch (devs[u]->target->nodetype) { |
| 6351 | case LYS_LEAF: |
| 6352 | DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT), |
| 6353 | dflt, "replacing", "default", d_rpl->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6354 | /* first, remove the default value taken from the type */ |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6355 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6356 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6357 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6358 | dflt = d_rpl->dflt; |
| 6359 | /* 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] | 6360 | break; |
| 6361 | case LYS_CHOICE: |
| 6362 | 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] | 6363 | 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] | 6364 | goto cleanup; |
| 6365 | } |
| 6366 | break; |
| 6367 | default: |
| 6368 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6369 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "default"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6370 | goto cleanup; |
| 6371 | } |
| 6372 | } |
| 6373 | |
| 6374 | /* [config-stmt] */ |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6375 | if (d_rpl->flags & LYS_CONFIG_MASK) { |
| 6376 | 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] | 6377 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 6378 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "config"); |
| 6379 | goto cleanup; |
| 6380 | } |
| 6381 | if (!(devs[u]->target->flags & LYS_SET_CONFIG)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6382 | 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] | 6383 | "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false"); |
| 6384 | goto cleanup; |
| 6385 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6386 | 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] | 6387 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6388 | |
| 6389 | /* [mandatory-stmt] */ |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 6390 | if (d_rpl->flags & LYS_MAND_MASK) { |
| 6391 | if (!(devs[u]->target->flags & LYS_MAND_MASK)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6392 | 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] | 6393 | "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false"); |
| 6394 | goto cleanup; |
| 6395 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6396 | 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] | 6397 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6398 | |
| 6399 | /* [min-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6400 | if (d_rpl->flags & LYS_SET_MIN) { |
| 6401 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6402 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements"); |
| 6403 | /* change value */ |
| 6404 | ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min; |
| 6405 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6406 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements"); |
| 6407 | /* change value */ |
| 6408 | ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min; |
| 6409 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6410 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6411 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements"); |
| 6412 | goto cleanup; |
| 6413 | } |
| 6414 | if (d_rpl->min) { |
| 6415 | devs[u]->target->flags |= LYS_MAND_TRUE; |
| 6416 | } |
| 6417 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6418 | |
| 6419 | /* [max-elements-stmt] */ |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6420 | if (d_rpl->flags & LYS_SET_MAX) { |
| 6421 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6422 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements"); |
| 6423 | /* change value */ |
| 6424 | ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6425 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6426 | DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements"); |
| 6427 | /* change value */ |
| 6428 | ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1; |
| 6429 | } else { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6430 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6431 | lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements"); |
| 6432 | goto cleanup; |
| 6433 | } |
| 6434 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6435 | |
| 6436 | break; |
| 6437 | default: |
| 6438 | LOGINT(ctx->ctx); |
| 6439 | goto cleanup; |
| 6440 | } |
| 6441 | } |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6442 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6443 | /* final check when all deviations of a single target node are applied */ |
| 6444 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6445 | /* check min-max compatibility */ |
| 6446 | if (devs[u]->target->nodetype == LYS_LEAFLIST) { |
| 6447 | min = ((struct lysc_node_leaflist*)devs[u]->target)->min; |
| 6448 | max = ((struct lysc_node_leaflist*)devs[u]->target)->max; |
| 6449 | } else if (devs[u]->target->nodetype == LYS_LIST) { |
| 6450 | min = ((struct lysc_node_list*)devs[u]->target)->min; |
| 6451 | max = ((struct lysc_node_list*)devs[u]->target)->max; |
| 6452 | } else { |
| 6453 | min = max = 0; |
| 6454 | } |
| 6455 | if (min > max) { |
| 6456 | 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] | 6457 | "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] | 6458 | goto cleanup; |
| 6459 | } |
| 6460 | |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6461 | if (dflt) { |
| 6462 | /* parse added/changed default value after possible change of the type */ |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6463 | leaf->dflt_mod = ctx->mod_def; |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6464 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6465 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6466 | 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] | 6467 | 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] | 6468 | leaf->dflt->realtype->refcount++; |
| 6469 | if (err) { |
| 6470 | ly_err_print(err); |
| 6471 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6472 | "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg); |
| 6473 | ly_err_free(err); |
| 6474 | } |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6475 | if (rc == LY_EINCOMPLETE) { |
| 6476 | /* postpone default compilation when the tree is complete */ |
| 6477 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6478 | |
| 6479 | /* but in general result is so far ok */ |
| 6480 | rc = LY_SUCCESS; |
| 6481 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6482 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6483 | } else if (changed_type) { |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6484 | /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */ |
| 6485 | int dynamic; |
| 6486 | if (devs[u]->target->nodetype == LYS_LEAF) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6487 | 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] | 6488 | |
| 6489 | /* update the list of incomplete default values if needed */ |
| 6490 | lysc_incomplete_dflts_remove(ctx, leaf->dflt); |
| 6491 | |
| 6492 | /* remove the previous default */ |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6493 | leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt); |
| 6494 | lysc_type_free(ctx->ctx, leaf->dflt->realtype); |
| 6495 | leaf->dflt->realtype = leaf->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6496 | rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt), |
| 6497 | 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] | 6498 | 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] | 6499 | leaf->dflt->realtype->refcount++; |
| 6500 | if (err) { |
| 6501 | ly_err_print(err); |
| 6502 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6503 | "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg); |
| 6504 | ly_err_free(err); |
| 6505 | } |
| 6506 | if (dynamic) { |
| 6507 | free((void*)dflt); |
| 6508 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6509 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6510 | if (rc == LY_EINCOMPLETE) { |
| 6511 | /* postpone default compilation when the tree is complete */ |
| 6512 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup); |
| 6513 | |
| 6514 | /* but in general result is so far ok */ |
| 6515 | rc = LY_SUCCESS; |
| 6516 | } |
| 6517 | LY_CHECK_GOTO(rc, cleanup); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6518 | } else { /* LYS_LEAFLIST */ |
| 6519 | LY_ARRAY_FOR(llist->dflts, x) { |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6520 | 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] | 6521 | llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]); |
| 6522 | lysc_type_free(ctx->ctx, llist->dflts[x]->realtype); |
| 6523 | llist->dflts[x]->realtype = llist->type; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6524 | rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt), |
| 6525 | 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] | 6526 | lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, |
| 6527 | llist->dflts[x], NULL, &err); |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6528 | llist->dflts[x]->realtype->refcount++; |
| 6529 | if (err) { |
| 6530 | ly_err_print(err); |
| 6531 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
| 6532 | "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).", |
| 6533 | dflt, err->msg); |
| 6534 | ly_err_free(err); |
| 6535 | } |
| 6536 | if (dynamic) { |
| 6537 | free((void*)dflt); |
| 6538 | } |
Radek Krejci | d0ef1af | 2019-07-23 12:22:05 +0200 | [diff] [blame] | 6539 | dflt = NULL; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6540 | if (rc == LY_EINCOMPLETE) { |
| 6541 | /* postpone default compilation when the tree is complete */ |
| 6542 | LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup); |
| 6543 | |
| 6544 | /* but in general result is so far ok */ |
| 6545 | rc = LY_SUCCESS; |
| 6546 | } |
Radek Krejci | a191122 | 2019-07-22 17:24:50 +0200 | [diff] [blame] | 6547 | LY_CHECK_GOTO(rc, cleanup); |
| 6548 | } |
| 6549 | } |
| 6550 | } |
| 6551 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 6552 | /* check mandatory - default compatibility */ |
| 6553 | if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 6554 | && (devs[u]->target->flags & LYS_SET_DFLT) |
| 6555 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6556 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6557 | "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] | 6558 | goto cleanup; |
| 6559 | } else if ((devs[u]->target->nodetype & LYS_CHOICE) |
| 6560 | && ((struct lysc_node_choice*)devs[u]->target)->dflt |
| 6561 | && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6562 | 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] | 6563 | goto cleanup; |
| 6564 | } |
| 6565 | if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) { |
| 6566 | /* mandatory node under a default case */ |
| 6567 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6568 | "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".", |
| 6569 | 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] | 6570 | goto cleanup; |
| 6571 | } |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 6572 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6573 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6574 | } |
| 6575 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6576 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6577 | ret = LY_SUCCESS; |
| 6578 | |
| 6579 | cleanup: |
| 6580 | for (u = 0; u < devs_p.count && devs[u]; ++u) { |
| 6581 | LY_ARRAY_FREE(devs[u]->deviates); |
| 6582 | free(devs[u]); |
| 6583 | } |
| 6584 | free(devs); |
| 6585 | ly_set_erase(&targets, NULL); |
| 6586 | ly_set_erase(&devs_p, NULL); |
| 6587 | |
| 6588 | return ret; |
| 6589 | } |
| 6590 | |
Radek Krejci | b56c750 | 2019-02-13 14:19:54 +0100 | [diff] [blame] | 6591 | /** |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6592 | * @brief Compile the given YANG submodule into the main module. |
| 6593 | * @param[in] ctx Compile context |
| 6594 | * @param[in] inc Include structure from the main module defining the submodule. |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6595 | * @return LY_ERR value - LY_SUCCESS or LY_EVALID. |
| 6596 | */ |
| 6597 | LY_ERR |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6598 | lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc) |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6599 | { |
| 6600 | unsigned int u; |
| 6601 | LY_ERR ret = LY_SUCCESS; |
| 6602 | /* shortcuts */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6603 | struct lysp_submodule *submod = inc->submodule; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6604 | struct lysc_module *mainmod = ctx->mod->compiled; |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6605 | struct lysp_node *node_p; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6606 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6607 | if (!mainmod->mod->off_features) { |
| 6608 | /* features are compiled directly into the compiled module structure, |
| 6609 | * but it must be done in two steps to allow forward references (via if-feature) between the features themselves. |
| 6610 | * The features compilation is finished in the main module (lys_compile()). */ |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6611 | ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features); |
| 6612 | LY_CHECK_GOTO(ret, error); |
| 6613 | } |
| 6614 | if (!mainmod->mod->off_extensions) { |
| 6615 | /* extensions are compiled directly into the compiled module structure, compilation is finished in the main module (lys_compile()). */ |
| 6616 | ret = lys_extension_precompile(ctx, NULL, NULL, submod->extensions, &mainmod->extensions); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6617 | LY_CHECK_GOTO(ret, error); |
| 6618 | } |
| 6619 | |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6620 | lysc_update_path(ctx, NULL, "{identity}"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6621 | 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] | 6622 | lysc_update_path(ctx, NULL, NULL); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6623 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6624 | /* data nodes */ |
| 6625 | LY_LIST_FOR(submod->data, node_p) { |
| 6626 | ret = lys_compile_node(ctx, node_p, NULL, 0); |
| 6627 | LY_CHECK_GOTO(ret, error); |
| 6628 | } |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 6629 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6630 | COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6631 | 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] | 6632 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6633 | error: |
| 6634 | return ret; |
| 6635 | } |
| 6636 | |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6637 | static void * |
| 6638 | lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts) |
| 6639 | { |
| 6640 | for (unsigned int u = 0; substmts[u].stmt; ++u) { |
| 6641 | if (substmts[u].stmt == stmt) { |
| 6642 | return substmts[u].storage; |
| 6643 | } |
| 6644 | } |
| 6645 | return NULL; |
| 6646 | } |
| 6647 | |
| 6648 | LY_ERR |
| 6649 | lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts) |
| 6650 | { |
| 6651 | LY_ERR ret = LY_EVALID, r; |
| 6652 | unsigned int u; |
| 6653 | struct lysp_stmt *stmt; |
| 6654 | void *parsed = NULL, **compiled = NULL; |
| 6655 | struct ly_set mandatory_stmts = {0}; |
| 6656 | |
| 6657 | /* check for invalid substatements */ |
| 6658 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
| 6659 | for (u = 0; substmts[u].stmt; ++u) { |
| 6660 | if (substmts[u].stmt == stmt->kw) { |
| 6661 | break; |
| 6662 | } |
| 6663 | } |
| 6664 | if (!substmts[u].stmt) { |
| 6665 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s\" extension instance.", |
| 6666 | stmt->stmt, ext->name); |
| 6667 | goto cleanup; |
| 6668 | } |
| 6669 | if (substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) { |
| 6670 | ly_set_add(&mandatory_stmts, &substmts[u], LY_SET_OPT_USEASLIST); |
| 6671 | } |
| 6672 | } |
| 6673 | |
| 6674 | /* keep order of the processing the same as the order in the defined substmts, |
| 6675 | * the order is important for some of the statements depending on others (e.g. type needs status and units) */ |
| 6676 | for (u = 0; substmts[u].stmt; ++u) { |
| 6677 | for (stmt = ext->child; stmt; stmt = stmt->next) { |
| 6678 | if (substmts[u].stmt != stmt->kw) { |
| 6679 | continue; |
| 6680 | } |
| 6681 | |
| 6682 | if (substmts[u].storage) { |
| 6683 | switch (stmt->kw) { |
| 6684 | case LY_STMT_TYPE: { |
| 6685 | uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts); |
| 6686 | const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts); |
| 6687 | |
| 6688 | if (substmts[u].cardinality < LY_STMT_CARD_SOME) { |
| 6689 | /* single item */ |
| 6690 | if (*(struct lysc_type**)substmts->storage) { |
| 6691 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt); |
| 6692 | goto cleanup; |
| 6693 | } |
| 6694 | compiled = substmts[u].storage; |
| 6695 | } else { |
| 6696 | /* sized array */ |
| 6697 | struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL; |
| 6698 | LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup); |
| 6699 | compiled = (void*)type; |
| 6700 | } |
| 6701 | |
| 6702 | LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed), ret = r, cleanup); |
| 6703 | LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL, |
| 6704 | 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] | 6705 | units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup); |
| 6706 | lysp_type_free(ctx->ctx, parsed); |
| 6707 | free(parsed); |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 6708 | break; |
| 6709 | } |
| 6710 | /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc) */ |
| 6711 | default: |
| 6712 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Statement \"%s\" is not supported as an extension (found in \"%s\") substatement.", |
| 6713 | stmt->stmt, ext->name); |
| 6714 | goto cleanup; |
| 6715 | } |
| 6716 | } |
| 6717 | |
| 6718 | if (substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) { |
| 6719 | int i = ly_set_contains(&mandatory_stmts, &substmts[u]); |
| 6720 | if (i != -1) { |
| 6721 | ly_set_rm_index(&mandatory_stmts, i, NULL); |
| 6722 | } |
| 6723 | } |
| 6724 | } |
| 6725 | } |
| 6726 | |
| 6727 | if (mandatory_stmts.count) { |
| 6728 | LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSTMT, "type", ext->name); |
| 6729 | goto cleanup; |
| 6730 | } |
| 6731 | |
| 6732 | ret = LY_SUCCESS; |
| 6733 | |
| 6734 | cleanup: |
| 6735 | ly_set_erase(&mandatory_stmts, NULL); |
| 6736 | |
| 6737 | return ret; |
| 6738 | } |
| 6739 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6740 | LY_ERR |
| 6741 | lys_compile(struct lys_module *mod, int options) |
| 6742 | { |
| 6743 | struct lysc_ctx ctx = {0}; |
| 6744 | struct lysc_module *mod_c; |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6745 | struct lysc_type *type, *typeiter; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6746 | struct lysp_module *sp; |
| 6747 | struct lysp_node *node_p; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6748 | struct lysp_augment **augments = NULL; |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 6749 | struct lysp_grp *grps; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6750 | struct lys_module *m; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 6751 | unsigned int u, v; |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6752 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6753 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6754 | LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 6755 | |
| 6756 | if (!mod->implemented) { |
| 6757 | /* just imported modules are not compiled */ |
| 6758 | return LY_SUCCESS; |
| 6759 | } |
| 6760 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6761 | sp = mod->parsed; |
| 6762 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6763 | ctx.ctx = mod->ctx; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6764 | ctx.mod = mod; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 6765 | ctx.mod_def = mod; |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6766 | ctx.options = options; |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6767 | ctx.path_len = 1; |
| 6768 | ctx.path[0] = '/'; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6769 | |
| 6770 | mod->compiled = mod_c = calloc(1, sizeof *mod_c); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 6771 | LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM); |
| 6772 | mod_c->mod = mod; |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6773 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6774 | 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] | 6775 | LY_ARRAY_FOR(sp->includes, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6776 | ret = lys_compile_submodule(&ctx, &sp->includes[u]); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 6777 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 6778 | } |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6779 | |
| 6780 | /* features */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6781 | if (mod->off_features) { |
| 6782 | /* there is already precompiled array of features */ |
| 6783 | mod_c->features = mod->off_features; |
| 6784 | mod->off_features = NULL; |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6785 | } else { |
| 6786 | /* features are compiled directly into the compiled module structure, |
| 6787 | * 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] | 6788 | ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6789 | LY_CHECK_GOTO(ret, error); |
| 6790 | } |
| 6791 | /* finish feature compilation, not only for the main module, but also for the submodules. |
| 6792 | * Due to possible forward references, it must be done when all the features (including submodules) |
| 6793 | * are present. */ |
| 6794 | LY_ARRAY_FOR(sp->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6795 | ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6796 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 6797 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6798 | lysc_update_path(&ctx, NULL, "{submodule}"); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6799 | LY_ARRAY_FOR(sp->includes, v) { |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6800 | lysc_update_path(&ctx, NULL, sp->includes[v].name); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6801 | LY_ARRAY_FOR(sp->includes[v].submodule->features, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6802 | 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] | 6803 | LY_CHECK_GOTO(ret != LY_SUCCESS, error); |
| 6804 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6805 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6806 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6807 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 6808 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6809 | /* extensions */ |
| 6810 | /* 2-steps: a) prepare compiled structures and ... */ |
| 6811 | if (mod->off_extensions) { |
| 6812 | /* there is already precompiled array of extension definitions */ |
| 6813 | mod_c->extensions = mod->off_extensions; |
| 6814 | mod->off_extensions = NULL; |
| 6815 | } else { |
| 6816 | /* extension definitions are compiled directly into the compiled module structure */ |
| 6817 | ret = lys_extension_precompile(&ctx, NULL, NULL, sp->extensions, &mod_c->extensions); |
| 6818 | } |
| 6819 | /* ... b) connect the extension definitions with the appropriate extension plugins */ |
| 6820 | lys_compile_extension_plugins(mod_c->extensions); |
| 6821 | |
| 6822 | /* identities */ |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6823 | lysc_update_path(&ctx, NULL, "{identity}"); |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6824 | 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] | 6825 | if (sp->identities) { |
| 6826 | LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities)); |
| 6827 | } |
Radek Krejci | 327de16 | 2019-06-14 12:52:07 +0200 | [diff] [blame] | 6828 | lysc_update_path(&ctx, NULL, NULL); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6829 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6830 | /* data nodes */ |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6831 | LY_LIST_FOR(sp->data, node_p) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6832 | ret = lys_compile_node(&ctx, node_p, NULL, 0); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6833 | LY_CHECK_GOTO(ret, error); |
| 6834 | } |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6835 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6836 | COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error); |
| 6837 | 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] | 6838 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6839 | /* augments - sort first to cover augments augmenting other augments */ |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 6840 | ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6841 | LY_CHECK_GOTO(ret, error); |
| 6842 | LY_ARRAY_FOR(augments, u) { |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6843 | ret = lys_compile_augment(&ctx, augments[u], NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6844 | LY_CHECK_GOTO(ret, error); |
| 6845 | } |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6846 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6847 | /* deviations TODO cover deviations from submodules */ |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6848 | ret = lys_compile_deviations(&ctx, sp); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 6849 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6850 | |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 6851 | /* extension instances TODO cover extension instances from submodules */ |
| 6852 | 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] | 6853 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6854 | /* validate leafref's paths and when/must xpaths */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6855 | /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which |
| 6856 | * can be also leafref, in case it is already resolved, go through the chain and check that it does not |
| 6857 | * 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] | 6858 | for (u = 0; u < ctx.unres.count; ++u) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 6859 | 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] | 6860 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 6861 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 6862 | /* validate the path */ |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6863 | 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] | 6864 | LY_CHECK_GOTO(ret, error); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 6865 | } else if (type->basetype == LY_TYPE_UNION) { |
| 6866 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 6867 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 6868 | /* validate the path */ |
| 6869 | ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), |
| 6870 | (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]); |
| 6871 | LY_CHECK_GOTO(ret, error); |
| 6872 | } |
| 6873 | } |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6874 | } |
| 6875 | } |
| 6876 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6877 | for (u = 0; u < ctx.unres.count; ++u) { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 6878 | 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] | 6879 | type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type; |
| 6880 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 6881 | /* store pointer to the real type */ |
| 6882 | for (typeiter = ((struct lysc_type_leafref*)type)->realtype; |
| 6883 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 6884 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 6885 | ((struct lysc_type_leafref*)type)->realtype = typeiter; |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 6886 | } else if (type->basetype == LY_TYPE_UNION) { |
| 6887 | LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) { |
| 6888 | if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) { |
| 6889 | /* store pointer to the real type */ |
| 6890 | for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype; |
| 6891 | typeiter->basetype == LY_TYPE_LEAFREF; |
| 6892 | typeiter = ((struct lysc_type_leafref*)typeiter)->realtype); |
| 6893 | ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter; |
| 6894 | } |
| 6895 | } |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 6896 | } |
| 6897 | } |
| 6898 | } |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6899 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6900 | /* finish incomplete default values compilation */ |
| 6901 | for (u = 0; u < ctx.dflts.count; ++u) { |
| 6902 | struct ly_err_item *err = NULL; |
| 6903 | struct lysc_incomplete_dflt *r = ctx.dflts.objs[u]; |
| 6904 | ret = r->dflt->realtype->plugin->store(ctx.ctx, r->dflt->realtype, r->dflt->canonized, strlen(r->dflt->canonized), |
| 6905 | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix, |
| 6906 | (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err); |
| 6907 | if (err) { |
| 6908 | ly_err_print(err); |
| 6909 | ctx.path[0] = '\0'; |
| 6910 | lysc_path(r->context_node, LY_PATH_LOG, ctx.path, LYSC_CTX_BUFSIZE); |
| 6911 | LOGVAL(ctx.ctx, LY_VLOG_STR, ctx.path, LYVE_SEMANTICS, |
| 6912 | "Invalid default - value does not fit the type (%s).", err->msg); |
| 6913 | ly_err_free(err); |
| 6914 | } |
| 6915 | LY_CHECK_GOTO(ret, error); |
| 6916 | } |
| 6917 | |
Radek Krejci | f2de0ed | 2019-05-02 14:13:18 +0200 | [diff] [blame] | 6918 | /* validate non-instantiated groupings from the parsed schema, |
| 6919 | * without it we would accept even the schemas with invalid grouping specification */ |
| 6920 | ctx.options |= LYSC_OPT_GROUPING; |
| 6921 | LY_ARRAY_FOR(sp->groupings, u) { |
| 6922 | if (!(sp->groupings[u].flags & LYS_USED_GRP)) { |
| 6923 | LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u])) != LY_SUCCESS, error); |
| 6924 | } |
| 6925 | } |
| 6926 | LY_LIST_FOR(sp->data, node_p) { |
| 6927 | grps = (struct lysp_grp*)lysp_node_groupings(node_p); |
| 6928 | LY_ARRAY_FOR(grps, u) { |
| 6929 | if (!(grps[u].flags & LYS_USED_GRP)) { |
| 6930 | LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &grps[u])) != LY_SUCCESS, error); |
| 6931 | } |
| 6932 | } |
| 6933 | } |
| 6934 | |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 6935 | if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) { |
| 6936 | /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile |
| 6937 | leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */ |
| 6938 | } |
| 6939 | |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 6940 | ly_set_erase(&ctx.dflts, free); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6941 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 6942 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 6943 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6944 | LY_ARRAY_FREE(augments); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6945 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6946 | if (ctx.options & LYSC_OPT_FREE_SP) { |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6947 | lysp_module_free(mod->parsed); |
| 6948 | ((struct lys_module*)mod)->parsed = NULL; |
| 6949 | } |
| 6950 | |
Radek Krejci | ec4da80 | 2019-05-02 13:02:41 +0200 | [diff] [blame] | 6951 | if (!(ctx.options & LYSC_OPT_INTERNAL)) { |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6952 | /* remove flag of the modules implemented by dependency */ |
| 6953 | for (u = 0; u < ctx.ctx->list.count; ++u) { |
| 6954 | m = ctx.ctx->list.objs[u]; |
| 6955 | if (m->implemented == 2) { |
| 6956 | m->implemented = 1; |
| 6957 | } |
| 6958 | } |
| 6959 | } |
| 6960 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6961 | ((struct lys_module*)mod)->compiled = mod_c; |
| 6962 | return LY_SUCCESS; |
| 6963 | |
| 6964 | error: |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6965 | lys_feature_precompile_revert(&ctx, mod); |
Radek Krejci | 1c0c344 | 2019-07-23 16:08:47 +0200 | [diff] [blame] | 6966 | ly_set_erase(&ctx.dflts, free); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 6967 | ly_set_erase(&ctx.unres, NULL); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 6968 | ly_set_erase(&ctx.groupings, NULL); |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 6969 | ly_set_erase(&ctx.tpdf_chain, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6970 | LY_ARRAY_FREE(augments); |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6971 | lysc_module_free(mod_c, NULL); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 6972 | mod->compiled = NULL; |
| 6973 | |
| 6974 | /* revert compilation of modules implemented by dependency */ |
| 6975 | for (u = 0; u < ctx.ctx->list.count; ++u) { |
| 6976 | m = ctx.ctx->list.objs[u]; |
| 6977 | if (m->implemented == 2) { |
| 6978 | /* revert features list to the precompiled state */ |
| 6979 | lys_feature_precompile_revert(&ctx, m); |
| 6980 | /* mark module as imported-only / not-implemented */ |
| 6981 | m->implemented = 0; |
| 6982 | /* free the compiled version of the module */ |
| 6983 | lysc_module_free(m->compiled, NULL); |
| 6984 | m->compiled = NULL; |
| 6985 | } |
| 6986 | } |
| 6987 | |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 6988 | return ret; |
| 6989 | } |