Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file context.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Context implementations |
| 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 | */ |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 14 | #define _GNU_SOURCE /* asprintf */ |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 15 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 16 | #include "context.h" |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 17 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 18 | #include <assert.h> |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 19 | #include <errno.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 20 | #include <pthread.h> |
| 21 | #include <stddef.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 22 | #include <stdio.h> |
Radek Krejci | 7ada9a0 | 2018-09-07 15:34:05 +0200 | [diff] [blame] | 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 25 | #include <sys/stat.h> |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 28 | #include "common.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 29 | #include "hash_table.h" |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 30 | #include "plugins_types.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 31 | #include "set.h" |
| 32 | #include "tree.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 33 | #include "tree_data.h" |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 34 | #include "tree_schema.h" |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 35 | #include "tree_schema_internal.h" |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 36 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 37 | #define LY_INTERNAL_MODS_COUNT 6 |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 38 | |
Radek Krejci | f3f4784 | 2018-11-15 11:22:15 +0100 | [diff] [blame] | 39 | #include "../models/ietf-yang-metadata@2016-08-05.h" |
| 40 | #include "../models/yang@2017-02-20.h" |
| 41 | #include "../models/ietf-inet-types@2013-07-15.h" |
| 42 | #include "../models/ietf-yang-types@2013-07-15.h" |
Michal Vasko | cf296c8 | 2020-05-27 11:16:45 +0200 | [diff] [blame] | 43 | #include "../models/ietf-datastores@2018-02-14.h" |
| 44 | #include "../models/ietf-yang-library@2019-01-04.h" |
| 45 | #define IETF_YANG_LIB_REV "2019-01-04" |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 46 | |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 47 | static struct internal_modules_s { |
| 48 | const char *name; |
| 49 | const char *revision; |
| 50 | const char *data; |
| 51 | uint8_t implemented; |
| 52 | LYS_INFORMAT format; |
| 53 | } internal_modules[LY_INTERNAL_MODS_COUNT] = { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 54 | {"ietf-yang-metadata", "2016-08-05", (const char*)ietf_yang_metadata_2016_08_05_yang, 1, LYS_IN_YANG}, |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 55 | {"yang", "2017-02-20", (const char*)yang_2017_02_20_yang, 1, LYS_IN_YANG}, |
| 56 | {"ietf-inet-types", "2013-07-15", (const char*)ietf_inet_types_2013_07_15_yang, 0, LYS_IN_YANG}, |
| 57 | {"ietf-yang-types", "2013-07-15", (const char*)ietf_yang_types_2013_07_15_yang, 0, LYS_IN_YANG}, |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 58 | /* ietf-datastores and ietf-yang-library must be right here at the end of the list! */ |
Michal Vasko | cf296c8 | 2020-05-27 11:16:45 +0200 | [diff] [blame] | 59 | {"ietf-datastores", "2018-02-14", (const char*)ietf_datastores_2018_02_14_yang, 1, LYS_IN_YANG}, |
| 60 | {"ietf-yang-library", IETF_YANG_LIB_REV, (const char*)ietf_yang_library_2019_01_04_yang, 1, LYS_IN_YANG} |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | API LY_ERR |
| 64 | ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir) |
| 65 | { |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 66 | struct stat st; |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 67 | char *new_dir = NULL; |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 68 | unsigned int u; |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 69 | |
| 70 | LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL); |
| 71 | |
| 72 | if (search_dir) { |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 73 | new_dir = realpath(search_dir, NULL); |
Radek Krejci | a77e0e1 | 2018-09-20 12:39:15 +0200 | [diff] [blame] | 74 | LY_CHECK_ERR_RET(!new_dir, |
Radek Krejci | 3a077b9 | 2019-04-09 17:10:35 +0200 | [diff] [blame] | 75 | LOGERR(ctx, LY_ESYS, "Unable to use search directory \"%s\" (%s).", search_dir, strerror(errno)), |
| 76 | LY_EINVAL); |
| 77 | if (strcmp(search_dir, new_dir)) { |
| 78 | LOGVRB("Canonicalizing search directory string from \"%s\" to \"%s\".", search_dir, new_dir); |
| 79 | } |
| 80 | LY_CHECK_ERR_RET(access(new_dir, R_OK | X_OK), |
| 81 | LOGERR(ctx, LY_ESYS, "Unable to fully access search directory \"%s\" (%s).", new_dir, strerror(errno)); free(new_dir), |
| 82 | LY_EINVAL); |
| 83 | LY_CHECK_ERR_RET(stat(new_dir, &st), |
| 84 | LOGERR(ctx, LY_ESYS, "stat() failed for \"%s\" (%s).", new_dir, strerror(errno)); free(new_dir), |
Radek Krejci | a77e0e1 | 2018-09-20 12:39:15 +0200 | [diff] [blame] | 85 | LY_ESYS); |
Radek Krejci | 3a077b9 | 2019-04-09 17:10:35 +0200 | [diff] [blame] | 86 | LY_CHECK_ERR_RET(!S_ISDIR(st.st_mode), |
| 87 | LOGERR(ctx, LY_ESYS, "Given search directory \"%s\" is not a directory.", new_dir); free(new_dir), |
| 88 | LY_EINVAL); |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 89 | /* avoid path duplication */ |
| 90 | for (u = 0; u < ctx->search_paths.count; ++u) { |
Radek Krejci | 14946ab | 2018-09-20 13:42:06 +0200 | [diff] [blame] | 91 | if (!strcmp(new_dir, ctx->search_paths.objs[u])) { |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 92 | free(new_dir); |
Radek Krejci | 14946ab | 2018-09-20 13:42:06 +0200 | [diff] [blame] | 93 | return LY_EEXIST; |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | if (ly_set_add(&ctx->search_paths, new_dir, LY_SET_OPT_USEASLIST) == -1) { |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 97 | free(new_dir); |
| 98 | return LY_EMEM; |
| 99 | } |
| 100 | |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 101 | /* new searchdir - possibly more latest revision available */ |
| 102 | ly_ctx_reset_latests(ctx); |
| 103 | |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 104 | return LY_SUCCESS; |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 105 | } else { |
| 106 | /* consider that no change is not actually an error */ |
| 107 | return LY_SUCCESS; |
| 108 | } |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | API const char * const * |
| 112 | ly_ctx_get_searchdirs(const struct ly_ctx *ctx) |
| 113 | { |
Radek Krejci | 0502643 | 2018-09-20 12:13:43 +0200 | [diff] [blame] | 114 | void **new; |
| 115 | |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 116 | LY_CHECK_ARG_RET(ctx, ctx, NULL); |
Radek Krejci | 0502643 | 2018-09-20 12:13:43 +0200 | [diff] [blame] | 117 | |
| 118 | if (ctx->search_paths.count == ctx->search_paths.size) { |
| 119 | /* not enough space for terminating NULL byte */ |
| 120 | new = realloc(((struct ly_ctx *)ctx)->search_paths.objs, (ctx->search_paths.size + 8) * sizeof *ctx->search_paths.objs); |
| 121 | LY_CHECK_ERR_RET(!new, LOGMEM(NULL), NULL); |
| 122 | ((struct ly_ctx *)ctx)->search_paths.size += 8; |
| 123 | ((struct ly_ctx *)ctx)->search_paths.objs = new; |
| 124 | } |
| 125 | /* set terminating NULL byte to the strings list */ |
| 126 | ctx->search_paths.objs[ctx->search_paths.count] = NULL; |
| 127 | |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 128 | return (const char * const *)ctx->search_paths.objs; |
| 129 | } |
| 130 | |
| 131 | API LY_ERR |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 132 | ly_ctx_unset_searchdirs(struct ly_ctx *ctx, const char *value) |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 133 | { |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 134 | unsigned int index; |
| 135 | |
Radek Krejci | cd64907 | 2018-09-20 12:14:45 +0200 | [diff] [blame] | 136 | LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL); |
Radek Krejci | cd64907 | 2018-09-20 12:14:45 +0200 | [diff] [blame] | 137 | |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 138 | if (!ctx->search_paths.count) { |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 139 | return LY_SUCCESS; |
| 140 | } |
| 141 | |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 142 | if (value) { |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 143 | /* remove specific search directory */ |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 144 | for (index = 0; index < ctx->search_paths.count; ++index) { |
| 145 | if (!strcmp(value, ctx->search_paths.objs[index])) { |
| 146 | break; |
| 147 | } |
| 148 | } |
| 149 | if (index == ctx->search_paths.count) { |
| 150 | LOGARG(ctx, value); |
| 151 | return LY_EINVAL; |
| 152 | } else { |
| 153 | return ly_set_rm_index(&ctx->search_paths, index, free); |
| 154 | } |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 155 | } else { |
| 156 | /* remove them all */ |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 157 | ly_set_erase(&ctx->search_paths, free); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 158 | memset(&ctx->search_paths, 0, sizeof ctx->search_paths); |
| 159 | } |
| 160 | |
| 161 | return LY_SUCCESS; |
| 162 | } |
| 163 | |
| 164 | API LY_ERR |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 165 | ly_ctx_unset_searchdir(struct ly_ctx *ctx, unsigned int index) |
| 166 | { |
| 167 | LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL); |
| 168 | |
| 169 | if (!ctx->search_paths.count) { |
| 170 | return LY_SUCCESS; |
| 171 | } |
| 172 | |
| 173 | if (index >= ctx->search_paths.count) { |
| 174 | LOGARG(ctx, value); |
| 175 | return LY_EINVAL; |
| 176 | } else { |
| 177 | return ly_set_rm_index(&ctx->search_paths, index, free); |
| 178 | } |
| 179 | |
| 180 | return LY_SUCCESS; |
| 181 | } |
| 182 | |
| 183 | API const struct lys_module * |
| 184 | ly_ctx_load_module(struct ly_ctx *ctx, const char *name, const char *revision) |
| 185 | { |
| 186 | struct lys_module *result = NULL; |
| 187 | |
| 188 | LY_CHECK_ARG_RET(ctx, ctx, name, NULL); |
| 189 | |
| 190 | LY_CHECK_RET(lysp_load_module(ctx, name, revision, 1, 0, &result), NULL); |
| 191 | return result; |
| 192 | } |
| 193 | |
| 194 | API LY_ERR |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 195 | ly_ctx_new(const char *search_dir, int options, struct ly_ctx **new_ctx) |
| 196 | { |
| 197 | struct ly_ctx *ctx = NULL; |
| 198 | struct lys_module *module; |
| 199 | char *search_dir_list; |
| 200 | char *sep, *dir; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 201 | int i; |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 202 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 203 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 204 | LY_CHECK_ARG_RET(NULL, new_ctx, LY_EINVAL); |
| 205 | |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 206 | ctx = calloc(1, sizeof *ctx); |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 207 | LY_CHECK_ERR_RET(!ctx, LOGMEM(NULL), LY_EMEM); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 208 | |
| 209 | /* dictionary */ |
| 210 | lydict_init(&ctx->dict); |
| 211 | |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 212 | #if 0 /* TODO when plugins implemented */ |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 213 | /* plugins */ |
| 214 | ly_load_plugins(); |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 215 | #endif |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 216 | |
| 217 | /* initialize thread-specific key */ |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 218 | while ((pthread_key_create(&ctx->errlist_key, ly_err_free)) == EAGAIN); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 219 | |
| 220 | /* models list */ |
| 221 | ctx->flags = options; |
| 222 | if (search_dir) { |
| 223 | search_dir_list = strdup(search_dir); |
| 224 | LY_CHECK_ERR_GOTO(!search_dir_list, LOGMEM(NULL); rc = LY_EMEM, error); |
| 225 | |
| 226 | for (dir = search_dir_list; (sep = strchr(dir, ':')) != NULL && rc == LY_SUCCESS; dir = sep + 1) { |
| 227 | *sep = 0; |
| 228 | rc = ly_ctx_set_searchdir(ctx, dir); |
Radek Krejci | 14946ab | 2018-09-20 13:42:06 +0200 | [diff] [blame] | 229 | if (rc == LY_EEXIST) { |
| 230 | /* ignore duplication */ |
| 231 | rc = LY_SUCCESS; |
| 232 | } |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 233 | } |
| 234 | if (*dir && rc == LY_SUCCESS) { |
| 235 | rc = ly_ctx_set_searchdir(ctx, dir); |
Radek Krejci | 14946ab | 2018-09-20 13:42:06 +0200 | [diff] [blame] | 236 | if (rc == LY_EEXIST) { |
| 237 | /* ignore duplication */ |
| 238 | rc = LY_SUCCESS; |
| 239 | } |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 240 | } |
| 241 | free(search_dir_list); |
| 242 | |
| 243 | /* If ly_ctx_set_searchdir() failed, the error is already logged. Just exit */ |
| 244 | if (rc != LY_SUCCESS) { |
| 245 | goto error; |
| 246 | } |
| 247 | } |
| 248 | ctx->module_set_id = 1; |
| 249 | |
| 250 | /* load internal modules */ |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 251 | for (i = 0; i < ((options & LY_CTX_NOYANGLIBRARY) ? (LY_INTERNAL_MODS_COUNT - 2) : LY_INTERNAL_MODS_COUNT); i++) { |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 252 | module = (struct lys_module *)lys_parse_mem_module(ctx, internal_modules[i].data, internal_modules[i].format, |
| 253 | internal_modules[i].implemented, NULL, NULL); |
Radek Krejci | 0e75e6f | 2018-11-08 09:40:02 +0100 | [diff] [blame] | 254 | LY_CHECK_ERR_GOTO(!module, rc = ly_errcode(ctx), error); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 255 | LY_CHECK_GOTO((rc = lys_compile(&module, 0)), error); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | *new_ctx = ctx; |
| 259 | return rc; |
| 260 | |
| 261 | error: |
| 262 | ly_ctx_destroy(ctx, NULL); |
| 263 | return rc; |
| 264 | } |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 265 | API int |
Radek Krejci | 3fbe89a | 2018-09-20 13:54:46 +0200 | [diff] [blame] | 266 | ly_ctx_get_options(const struct ly_ctx *ctx) |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 267 | { |
Radek Krejci | 3fbe89a | 2018-09-20 13:54:46 +0200 | [diff] [blame] | 268 | LY_CHECK_ARG_RET(ctx, ctx, 0); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 269 | return ctx->flags; |
| 270 | } |
| 271 | |
Radek Krejci | ca828d9 | 2018-09-20 14:19:43 +0200 | [diff] [blame] | 272 | API LY_ERR |
Radek Krejci | 3fa46b6 | 2019-09-11 10:47:30 +0200 | [diff] [blame] | 273 | ly_ctx_set_options(struct ly_ctx *ctx, int option) |
Radek Krejci | ca828d9 | 2018-09-20 14:19:43 +0200 | [diff] [blame] | 274 | { |
| 275 | LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL); |
| 276 | LY_CHECK_ERR_RET(option & LY_CTX_NOYANGLIBRARY, LOGARG(ctx, option), LY_EINVAL); |
| 277 | |
| 278 | /* set the option(s) */ |
| 279 | ctx->flags |= option; |
| 280 | |
| 281 | return LY_SUCCESS; |
| 282 | } |
| 283 | |
| 284 | API LY_ERR |
Radek Krejci | 3fa46b6 | 2019-09-11 10:47:30 +0200 | [diff] [blame] | 285 | ly_ctx_unset_options(struct ly_ctx *ctx, int option) |
Radek Krejci | ca828d9 | 2018-09-20 14:19:43 +0200 | [diff] [blame] | 286 | { |
| 287 | LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL); |
| 288 | LY_CHECK_ERR_RET(option & LY_CTX_NOYANGLIBRARY, LOGARG(ctx, option), LY_EINVAL); |
| 289 | |
| 290 | /* unset the option(s) */ |
| 291 | ctx->flags &= ~option; |
| 292 | |
| 293 | return LY_SUCCESS; |
| 294 | } |
| 295 | |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 296 | API uint16_t |
| 297 | ly_ctx_get_module_set_id(const struct ly_ctx *ctx) |
| 298 | { |
Radek Krejci | 3fbe89a | 2018-09-20 13:54:46 +0200 | [diff] [blame] | 299 | LY_CHECK_ARG_RET(ctx, ctx, 0); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 300 | return ctx->module_set_id; |
| 301 | } |
| 302 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 303 | API void |
| 304 | ly_ctx_set_module_imp_clb(struct ly_ctx *ctx, ly_module_imp_clb clb, void *user_data) |
| 305 | { |
| 306 | LY_CHECK_ARG_RET(ctx, ctx,); |
| 307 | |
| 308 | ctx->imp_clb = clb; |
| 309 | ctx->imp_clb_data = user_data; |
| 310 | } |
| 311 | |
| 312 | API ly_module_imp_clb |
| 313 | ly_ctx_get_module_imp_clb(const struct ly_ctx *ctx, void **user_data) |
| 314 | { |
| 315 | LY_CHECK_ARG_RET(ctx, ctx, NULL); |
| 316 | |
| 317 | if (user_data) { |
| 318 | *user_data = ctx->imp_clb_data; |
| 319 | } |
| 320 | return ctx->imp_clb; |
| 321 | } |
| 322 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 323 | API const struct lys_module * |
| 324 | ly_ctx_get_module_iter(const struct ly_ctx *ctx, unsigned int *index) |
| 325 | { |
| 326 | LY_CHECK_ARG_RET(ctx, ctx, index, NULL); |
| 327 | |
Radek Krejci | 2390fb5 | 2019-04-30 13:27:43 +0200 | [diff] [blame] | 328 | if (*index < (unsigned)ctx->list.count) { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 329 | return ctx->list.objs[(*index)++]; |
Radek Krejci | 2390fb5 | 2019-04-30 13:27:43 +0200 | [diff] [blame] | 330 | } else { |
| 331 | return NULL; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 332 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 333 | } |
| 334 | |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 335 | /** |
| 336 | * @brief Iterate over the modules in the given context. Returned modules must match the given key at the offset of |
| 337 | * lysp_module and lysc_module structures (they are supposed to be placed at the same offset in both structures). |
| 338 | * |
| 339 | * @param[in] ctx Context where to iterate. |
| 340 | * @param[in] key Key value to search for. |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 341 | * @param[in] key_offset Key's offset in struct lys_module to get value from the context's modules to match with the key. |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 342 | * @param[in,out] Iterator to pass between the function calls. On the first call, the variable is supposed to be |
| 343 | * initiated to 0. After each call returning a module, the value is greater by 1 than the index of the returned |
| 344 | * module in the context. |
| 345 | * @return Module matching the given key, NULL if no such module found. |
| 346 | */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 347 | static struct lys_module * |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 348 | ly_ctx_get_module_by_iter(const struct ly_ctx *ctx, const char *key, size_t key_offset, unsigned int *index) |
| 349 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 350 | struct lys_module *mod; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 351 | const char *value; |
| 352 | |
| 353 | for (; *index < ctx->list.count; ++(*index)) { |
| 354 | mod = ctx->list.objs[*index]; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 355 | value = *(const char**)(((int8_t*)(mod)) + key_offset); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 356 | if (!strcmp(key, value)) { |
| 357 | /* increment index for the next run */ |
| 358 | ++(*index); |
| 359 | return mod; |
| 360 | } |
| 361 | } |
| 362 | /* done */ |
| 363 | return NULL; |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * @brief Unifying function for ly_ctx_get_module() and ly_ctx_get_module_ns() |
| 368 | * @param[in] ctx Context where to search. |
| 369 | * @param[in] key Name or Namespace as a search key. |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 370 | * @param[in] key_offset Key's offset in struct lys_module to get value from the context's modules to match with the key. |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 371 | * @param[in] revision Revision date to match. If NULL, the matching module must have no revision. To search for the latest |
| 372 | * revision module, use ly_ctx_get_module_latest_by(). |
| 373 | * @return Matching module if any. |
| 374 | */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 375 | static struct lys_module * |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 376 | ly_ctx_get_module_by(const struct ly_ctx *ctx, const char *key, size_t key_offset, const char *revision) |
| 377 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 378 | struct lys_module *mod; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 379 | unsigned int index = 0; |
| 380 | |
| 381 | while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) { |
| 382 | if (!revision) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 383 | if (!mod->revision) { |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 384 | /* found requested module without revision */ |
| 385 | return mod; |
| 386 | } |
| 387 | } else { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 388 | if (mod->revision && !strcmp(mod->revision, revision)) { |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 389 | /* found requested module of the specific revision */ |
| 390 | return mod; |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | return NULL; |
| 396 | } |
| 397 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 398 | API struct lys_module * |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 399 | ly_ctx_get_module_ns(const struct ly_ctx *ctx, const char *ns, const char *revision) |
| 400 | { |
| 401 | LY_CHECK_ARG_RET(ctx, ctx, ns, NULL); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 402 | return ly_ctx_get_module_by(ctx, ns, offsetof(struct lys_module, ns), revision); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 403 | } |
| 404 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 405 | API struct lys_module * |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 406 | ly_ctx_get_module(const struct ly_ctx *ctx, const char *name, const char *revision) |
| 407 | { |
| 408 | LY_CHECK_ARG_RET(ctx, ctx, name, NULL); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 409 | return ly_ctx_get_module_by(ctx, name, offsetof(struct lys_module, name), revision); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | /** |
| 413 | * @brief Unifying function for ly_ctx_get_module_latest() and ly_ctx_get_module_latest_ns() |
| 414 | * @param[in] ctx Context where to search. |
| 415 | * @param[in] key Name or Namespace as a search key. |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 416 | * @param[in] key_offset Key's offset in struct lys_module to get value from the context's modules to match with the key. |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 417 | * @return Matching module if any. |
| 418 | */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 419 | static struct lys_module * |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 420 | ly_ctx_get_module_latest_by(const struct ly_ctx *ctx, const char *key, size_t key_offset) |
| 421 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 422 | struct lys_module *mod; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 423 | unsigned int index = 0; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 424 | |
| 425 | while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 426 | if (mod->latest_revision) { |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 427 | return mod; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 431 | return NULL; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 432 | } |
| 433 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 434 | API struct lys_module * |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 435 | ly_ctx_get_module_latest(const struct ly_ctx *ctx, const char *name) |
| 436 | { |
| 437 | LY_CHECK_ARG_RET(ctx, ctx, name, NULL); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 438 | return ly_ctx_get_module_latest_by(ctx, name, offsetof(struct lys_module, name)); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 439 | } |
| 440 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 441 | API struct lys_module * |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 442 | ly_ctx_get_module_latest_ns(const struct ly_ctx *ctx, const char *ns) |
| 443 | { |
| 444 | LY_CHECK_ARG_RET(ctx, ctx, ns, NULL); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 445 | return ly_ctx_get_module_latest_by(ctx, ns, offsetof(struct lys_module, ns)); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /** |
| 449 | * @brief Unifying function for ly_ctx_get_module_implemented() and ly_ctx_get_module_implemented_ns() |
| 450 | * @param[in] ctx Context where to search. |
| 451 | * @param[in] key Name or Namespace as a search key. |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 452 | * @param[in] key_offset Key's offset in struct lys_module to get value from the context's modules to match with the key. |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 453 | * @return Matching module if any. |
| 454 | */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 455 | static struct lys_module * |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 456 | ly_ctx_get_module_implemented_by(const struct ly_ctx *ctx, const char *key, size_t key_offset) |
| 457 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 458 | struct lys_module *mod; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 459 | unsigned int index = 0; |
| 460 | |
| 461 | while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 462 | if (mod->implemented) { |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 463 | return mod; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | return NULL; |
| 468 | } |
| 469 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 470 | API struct lys_module * |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 471 | ly_ctx_get_module_implemented(const struct ly_ctx *ctx, const char *name) |
| 472 | { |
| 473 | LY_CHECK_ARG_RET(ctx, ctx, name, NULL); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 474 | return ly_ctx_get_module_implemented_by(ctx, name, offsetof(struct lys_module, name)); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 475 | } |
| 476 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 477 | API struct lys_module * |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 478 | ly_ctx_get_module_implemented_ns(const struct ly_ctx *ctx, const char *ns) |
| 479 | { |
| 480 | LY_CHECK_ARG_RET(ctx, ctx, ns, NULL); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 481 | return ly_ctx_get_module_implemented_by(ctx, ns, offsetof(struct lys_module, ns)); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 482 | } |
| 483 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 484 | struct lysp_submodule * |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 485 | ly_ctx_get_submodule(const struct ly_ctx *ctx, const char *module, const char *submodule, const char *revision) |
| 486 | { |
| 487 | const struct lys_module *mod; |
| 488 | struct lysp_include *inc; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 489 | uint32_t v; |
| 490 | LY_ARRAY_SIZE_TYPE u; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 491 | |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 492 | assert(submodule); |
| 493 | |
| 494 | for (v = 0; v < ctx->list.count; ++v) { |
| 495 | mod = ctx->list.objs[v]; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 496 | if (!mod->parsed) { |
| 497 | continue; |
| 498 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 499 | if (module && strcmp(module, mod->name)) { |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 500 | continue; |
| 501 | } |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 502 | |
| 503 | LY_ARRAY_FOR(mod->parsed->includes, u) { |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 504 | if (mod->parsed->includes[u].submodule && !strcmp(submodule, mod->parsed->includes[u].submodule->name)) { |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 505 | inc = &mod->parsed->includes[u]; |
| 506 | if (!revision) { |
| 507 | if (inc->submodule->latest_revision) { |
| 508 | return inc->submodule; |
| 509 | } |
| 510 | } else if (inc->submodule->revs && !strcmp(revision, inc->submodule->revs[0].date)) { |
| 511 | return inc->submodule; |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | return NULL; |
| 518 | } |
| 519 | |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 520 | API void |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 521 | ly_ctx_reset_latests(struct ly_ctx *ctx) |
| 522 | { |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 523 | struct lys_module *mod; |
| 524 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 525 | for (uint32_t v = 0; v < ctx->list.count; ++v) { |
| 526 | mod = ctx->list.objs[v]; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 527 | if (mod->latest_revision == 2) { |
| 528 | mod->latest_revision = 1; |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 529 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 530 | if (mod->parsed && mod->parsed->includes) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 531 | for (LY_ARRAY_SIZE_TYPE u = 0; u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) { |
| 532 | if (mod->parsed->includes[u].submodule->latest_revision == 2) { |
| 533 | mod->parsed->includes[u].submodule->latest_revision = 1; |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | } |
| 539 | |
Michal Vasko | 57c10cd | 2020-05-27 15:57:11 +0200 | [diff] [blame] | 540 | static LY_ERR |
| 541 | ylib_feature(struct lyd_node *parent, const struct lys_module *cur_mod) |
| 542 | { |
| 543 | LY_ARRAY_SIZE_TYPE i; |
| 544 | struct lyd_node *node; |
| 545 | |
| 546 | if (!cur_mod->implemented) { |
| 547 | /* no features can be enabled */ |
| 548 | return LY_SUCCESS; |
| 549 | } |
| 550 | |
| 551 | LY_ARRAY_FOR(cur_mod->compiled->features, i) { |
| 552 | if (!(cur_mod->compiled->features[i].flags & LYS_FENABLED)) { |
| 553 | continue; |
| 554 | } |
| 555 | |
| 556 | node = lyd_new_term(parent, NULL, "feature", cur_mod->compiled->features[i].name); |
| 557 | LY_CHECK_RET(!node, LY_EOTHER); |
| 558 | } |
| 559 | |
| 560 | return LY_SUCCESS; |
| 561 | } |
| 562 | |
| 563 | static LY_ERR |
| 564 | ylib_deviation(struct lyd_node *parent, const struct lys_module *cur_mod, int bis) |
| 565 | { |
| 566 | LY_ARRAY_SIZE_TYPE i; |
| 567 | struct lyd_node *node; |
| 568 | struct lys_module *mod; |
| 569 | |
| 570 | if (!cur_mod->implemented) { |
| 571 | /* no deviations of the module for certain */ |
| 572 | return LY_SUCCESS; |
| 573 | } |
| 574 | |
| 575 | LY_ARRAY_FOR(cur_mod->compiled->deviated_by, i) { |
| 576 | mod = cur_mod->compiled->deviated_by[i]; |
| 577 | |
| 578 | if (bis) { |
| 579 | node = lyd_new_term(parent, NULL, "deviation", mod->name); |
| 580 | LY_CHECK_RET(!node, LY_EOTHER); |
| 581 | } else { |
| 582 | node = lyd_new_list(parent, NULL, "deviation", mod->name, (mod->parsed->revs ? mod->parsed->revs[0].date : "")); |
| 583 | LY_CHECK_RET(!node, LY_EOTHER); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | return LY_SUCCESS; |
| 588 | } |
| 589 | |
| 590 | static LY_ERR |
| 591 | ylib_submodules(struct lyd_node *parent, const struct lys_module *cur_mod, int bis) |
| 592 | { |
| 593 | LY_ARRAY_SIZE_TYPE i; |
| 594 | struct lyd_node *node, *cont; |
| 595 | struct lysp_submodule *submod; |
| 596 | int ret; |
| 597 | char *str; |
| 598 | |
| 599 | LY_ARRAY_FOR(cur_mod->parsed->includes, i) { |
| 600 | submod = cur_mod->parsed->includes[i].submodule; |
| 601 | |
| 602 | if (bis) { |
| 603 | cont = lyd_new_list(parent, NULL, "submodule", submod->name); |
| 604 | LY_CHECK_RET(!cont, LY_EOTHER); |
| 605 | |
| 606 | if (submod->revs) { |
| 607 | node = lyd_new_term(cont, NULL, "revision", submod->revs[0].date); |
| 608 | LY_CHECK_RET(!node, LY_EOTHER); |
| 609 | } |
| 610 | } else { |
| 611 | cont = lyd_new_list(parent, NULL, "submodule", submod->name, (submod->revs ? submod->revs[0].date : "")); |
| 612 | LY_CHECK_RET(!cont, LY_EOTHER); |
| 613 | } |
| 614 | |
| 615 | if (submod->filepath) { |
| 616 | ret = asprintf(&str, "file://%s", submod->filepath); |
| 617 | LY_CHECK_ERR_RET(ret == -1, LOGMEM(cur_mod->ctx), LY_EMEM); |
| 618 | |
| 619 | node = lyd_new_term(cont, NULL, bis ? "location" : "schema", str); |
| 620 | LY_CHECK_RET(!node, LY_EOTHER); |
| 621 | free(str); |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | return LY_SUCCESS; |
| 626 | } |
| 627 | |
| 628 | API uint16_t |
| 629 | ly_ctx_get_yanglib_id(const struct ly_ctx *ctx) |
| 630 | { |
| 631 | return ctx->module_set_id; |
| 632 | } |
| 633 | |
| 634 | API struct lyd_node * |
| 635 | ly_ctx_get_yanglib_data(const struct ly_ctx *ctx) |
| 636 | { |
| 637 | uint32_t i; |
| 638 | int bis = 0, ret; |
| 639 | char id[8], *str; |
| 640 | const struct lys_module *mod; |
| 641 | struct lyd_node *root = NULL, *root_bis = NULL, *cont, *set_bis, *node; |
| 642 | |
| 643 | LY_CHECK_ARG_RET(ctx, ctx, NULL); |
| 644 | |
| 645 | mod = ly_ctx_get_module_implemented(ctx, "ietf-yang-library"); |
| 646 | LY_CHECK_ERR_RET(!mod, LOGERR(ctx, LY_EINVAL, "Module \"ietf-yang-library\" is not implemented."), NULL); |
| 647 | |
| 648 | if (mod->parsed->revs && !strcmp(mod->parsed->revs[0].date, "2016-06-21")) { |
| 649 | bis = 0; |
| 650 | } else if (mod->parsed->revs && !strcmp(mod->parsed->revs[0].date, IETF_YANG_LIB_REV)) { |
| 651 | bis = 1; |
| 652 | } else { |
| 653 | LOGERR(ctx, LY_EINVAL, "Incompatible ietf-yang-library version in context."); |
| 654 | return NULL; |
| 655 | } |
| 656 | |
| 657 | root = lyd_new_inner(NULL, mod, "modules-state"); |
| 658 | LY_CHECK_GOTO(!root, error); |
| 659 | |
| 660 | if (bis) { |
| 661 | root_bis = lyd_new_inner(NULL, mod, "yang-library"); |
| 662 | LY_CHECK_GOTO(!root_bis, error); |
| 663 | |
| 664 | set_bis = lyd_new_list(root_bis, NULL, "module-set", "complete"); |
| 665 | LY_CHECK_GOTO(!set_bis, error); |
| 666 | } |
| 667 | |
| 668 | for (i = 0; i < ctx->list.count; ++i) { |
| 669 | mod = ctx->list.objs[i]; |
| 670 | |
| 671 | /* |
| 672 | * deprecated legacy |
| 673 | */ |
| 674 | cont = lyd_new_list(root, NULL, "module", mod->name, (mod->parsed->revs ? mod->parsed->revs[0].date : "")); |
| 675 | LY_CHECK_GOTO(!cont, error); |
| 676 | |
| 677 | /* schema */ |
| 678 | if (mod->filepath) { |
| 679 | ret = asprintf(&str, "file://%s", mod->filepath); |
| 680 | LY_CHECK_ERR_GOTO(ret == -1, LOGMEM(ctx), error); |
| 681 | |
| 682 | node = lyd_new_term(cont, NULL, "schema", str); |
| 683 | free(str); |
| 684 | LY_CHECK_GOTO(!node, error); |
| 685 | } |
| 686 | |
| 687 | /* namespace */ |
| 688 | node = lyd_new_term(cont, NULL, "namespace", mod->ns); |
| 689 | LY_CHECK_GOTO(!node, error); |
| 690 | |
| 691 | /* feature leaf-list */ |
| 692 | LY_CHECK_GOTO(ylib_feature(cont, mod), error); |
| 693 | |
| 694 | /* deviation list */ |
| 695 | LY_CHECK_GOTO(ylib_deviation(cont, mod, 0), error); |
| 696 | |
| 697 | /* conformance-type */ |
| 698 | node = lyd_new_term(cont, NULL, "conformance-type", (mod->implemented ? "implement" : "import")); |
| 699 | LY_CHECK_GOTO(!node, error); |
| 700 | |
| 701 | /* submodule list */ |
| 702 | LY_CHECK_GOTO(ylib_submodules(cont, mod, 0), error); |
| 703 | |
| 704 | /* |
| 705 | * current revision |
| 706 | */ |
| 707 | if (bis) { |
| 708 | /* name and revision */ |
| 709 | if (mod->implemented) { |
| 710 | cont = lyd_new_list(set_bis, NULL, "module", mod->name); |
| 711 | LY_CHECK_GOTO(!cont, error); |
| 712 | |
| 713 | if (mod->parsed->revs) { |
| 714 | node = lyd_new_term(cont, NULL, "revision", mod->parsed->revs[0].date); |
| 715 | LY_CHECK_GOTO(!node, error); |
| 716 | } |
| 717 | } else { |
| 718 | cont = lyd_new_list(set_bis, NULL, "import-only-module", mod->name, |
| 719 | (mod->parsed->revs ? mod->parsed->revs[0].date : "")); |
| 720 | LY_CHECK_GOTO(!cont, error); |
| 721 | } |
| 722 | |
| 723 | /* namespace */ |
| 724 | node = lyd_new_term(cont, NULL, "namespace", mod->ns); |
| 725 | LY_CHECK_GOTO(!node, error); |
| 726 | |
| 727 | /* location */ |
| 728 | if (mod->filepath) { |
| 729 | ret = asprintf(&str, "file://%s", mod->filepath); |
| 730 | LY_CHECK_ERR_GOTO(ret == -1, LOGMEM(ctx), error); |
| 731 | |
| 732 | node = lyd_new_term(cont, NULL, "schema", str); |
| 733 | free(str); |
| 734 | LY_CHECK_GOTO(!node, error); |
| 735 | } |
| 736 | |
| 737 | /* submodule list */ |
| 738 | LY_CHECK_GOTO(ylib_submodules(cont, mod, 1), error); |
| 739 | |
| 740 | /* feature list */ |
| 741 | LY_CHECK_GOTO(ylib_feature(cont, mod), error); |
| 742 | |
| 743 | /* deviation */ |
| 744 | LY_CHECK_GOTO(ylib_deviation(cont, mod, 1), error); |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | /* IDs */ |
| 749 | sprintf(id, "%u", ctx->module_set_id); |
| 750 | node = lyd_new_term(root, NULL, "module-set-id", id); |
| 751 | LY_CHECK_GOTO(!node, error); |
| 752 | |
| 753 | if (bis) { |
| 754 | /* create one complete schema */ |
| 755 | cont = lyd_new_list(root_bis, NULL, "schema", "complete"); |
| 756 | LY_CHECK_GOTO(!cont, error); |
| 757 | |
| 758 | node = lyd_new_term(cont, NULL, "module-set", "complete"); |
| 759 | LY_CHECK_GOTO(!node, error); |
| 760 | |
| 761 | /* content-id */ |
| 762 | node = lyd_new_term(root_bis, NULL, "content-id", id); |
| 763 | LY_CHECK_GOTO(!node, error); |
| 764 | } |
| 765 | |
| 766 | if (root_bis) { |
| 767 | if (lyd_insert_sibling(root_bis, root)) { |
| 768 | goto error; |
| 769 | } |
| 770 | root = root_bis; |
| 771 | root_bis = 0; |
| 772 | } |
| 773 | |
Michal Vasko | 57c10cd | 2020-05-27 15:57:11 +0200 | [diff] [blame] | 774 | if (lyd_validate(&root, NULL, LYD_VALOPT_DATA_ONLY)) { |
| 775 | goto error; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 776 | } |
Michal Vasko | 57c10cd | 2020-05-27 15:57:11 +0200 | [diff] [blame] | 777 | |
| 778 | return root; |
| 779 | |
| 780 | error: |
| 781 | lyd_free_all(root); |
| 782 | lyd_free_all(root_bis); |
| 783 | return NULL; |
| 784 | } |
| 785 | |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 786 | API void |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 787 | ly_ctx_destroy(struct ly_ctx *ctx, void (*private_destructor)(const struct lysc_node *node, void *priv)) |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 788 | { |
Radek Krejci | 418823a | 2018-09-20 16:42:13 +0200 | [diff] [blame] | 789 | if (!ctx) { |
| 790 | return; |
| 791 | } |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 792 | |
| 793 | /* models list */ |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 794 | for (; ctx->list.count; ctx->list.count--) { |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 795 | /* remove the module */ |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 796 | lys_module_free(ctx->list.objs[ctx->list.count - 1], private_destructor); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 797 | } |
| 798 | free(ctx->list.objs); |
| 799 | |
| 800 | /* search paths list */ |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 801 | ly_set_erase(&ctx->search_paths, free); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 802 | |
| 803 | /* clean the error list */ |
| 804 | ly_err_clean(ctx, 0); |
| 805 | pthread_key_delete(ctx->errlist_key); |
| 806 | |
| 807 | /* dictionary */ |
| 808 | lydict_clean(&ctx->dict); |
| 809 | |
| 810 | #if 0 /* TODO when plugins implemented */ |
| 811 | /* plugins - will be removed only if this is the last context */ |
| 812 | ly_clean_plugins(); |
| 813 | #endif |
| 814 | |
| 815 | free(ctx); |
| 816 | } |