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 | */ |
| 14 | |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 15 | #include "common.h" |
| 16 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 17 | #include <assert.h> |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 18 | #include <errno.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 19 | #include <pthread.h> |
| 20 | #include <stddef.h> |
Radek Krejci | 7ada9a0 | 2018-09-07 15:34:05 +0200 | [diff] [blame] | 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 23 | #include <sys/stat.h> |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 24 | #include <unistd.h> |
| 25 | |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 26 | #include "context.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 27 | #include "hash_table.h" |
| 28 | #include "set.h" |
| 29 | #include "tree.h" |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 30 | #include "tree_schema_internal.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 31 | #include "plugins_types.h" |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 32 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 33 | #define LY_INTERNAL_MODS_COUNT 6 |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 34 | |
Radek Krejci | f3f4784 | 2018-11-15 11:22:15 +0100 | [diff] [blame] | 35 | #include "../models/ietf-yang-metadata@2016-08-05.h" |
| 36 | #include "../models/yang@2017-02-20.h" |
| 37 | #include "../models/ietf-inet-types@2013-07-15.h" |
| 38 | #include "../models/ietf-yang-types@2013-07-15.h" |
Michal Vasko | cf296c8 | 2020-05-27 11:16:45 +0200 | [diff] [blame] | 39 | #include "../models/ietf-datastores@2018-02-14.h" |
| 40 | #include "../models/ietf-yang-library@2019-01-04.h" |
| 41 | #define IETF_YANG_LIB_REV "2019-01-04" |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 42 | |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 43 | static struct internal_modules_s { |
| 44 | const char *name; |
| 45 | const char *revision; |
| 46 | const char *data; |
| 47 | uint8_t implemented; |
| 48 | LYS_INFORMAT format; |
| 49 | } internal_modules[LY_INTERNAL_MODS_COUNT] = { |
Radek Krejci | 335332a | 2019-09-05 13:03:35 +0200 | [diff] [blame] | 50 | {"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] | 51 | {"yang", "2017-02-20", (const char*)yang_2017_02_20_yang, 1, LYS_IN_YANG}, |
| 52 | {"ietf-inet-types", "2013-07-15", (const char*)ietf_inet_types_2013_07_15_yang, 0, LYS_IN_YANG}, |
| 53 | {"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] | 54 | /* 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] | 55 | {"ietf-datastores", "2018-02-14", (const char*)ietf_datastores_2018_02_14_yang, 1, LYS_IN_YANG}, |
| 56 | {"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] | 57 | }; |
| 58 | |
| 59 | API LY_ERR |
| 60 | ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir) |
| 61 | { |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 62 | struct stat st; |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 63 | char *new_dir = NULL; |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 64 | unsigned int u; |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 65 | |
| 66 | LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL); |
| 67 | |
| 68 | if (search_dir) { |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 69 | new_dir = realpath(search_dir, NULL); |
Radek Krejci | a77e0e1 | 2018-09-20 12:39:15 +0200 | [diff] [blame] | 70 | LY_CHECK_ERR_RET(!new_dir, |
Radek Krejci | 3a077b9 | 2019-04-09 17:10:35 +0200 | [diff] [blame] | 71 | LOGERR(ctx, LY_ESYS, "Unable to use search directory \"%s\" (%s).", search_dir, strerror(errno)), |
| 72 | LY_EINVAL); |
| 73 | if (strcmp(search_dir, new_dir)) { |
| 74 | LOGVRB("Canonicalizing search directory string from \"%s\" to \"%s\".", search_dir, new_dir); |
| 75 | } |
| 76 | LY_CHECK_ERR_RET(access(new_dir, R_OK | X_OK), |
| 77 | LOGERR(ctx, LY_ESYS, "Unable to fully access search directory \"%s\" (%s).", new_dir, strerror(errno)); free(new_dir), |
| 78 | LY_EINVAL); |
| 79 | LY_CHECK_ERR_RET(stat(new_dir, &st), |
| 80 | 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] | 81 | LY_ESYS); |
Radek Krejci | 3a077b9 | 2019-04-09 17:10:35 +0200 | [diff] [blame] | 82 | LY_CHECK_ERR_RET(!S_ISDIR(st.st_mode), |
| 83 | LOGERR(ctx, LY_ESYS, "Given search directory \"%s\" is not a directory.", new_dir); free(new_dir), |
| 84 | LY_EINVAL); |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 85 | /* avoid path duplication */ |
| 86 | for (u = 0; u < ctx->search_paths.count; ++u) { |
Radek Krejci | 14946ab | 2018-09-20 13:42:06 +0200 | [diff] [blame] | 87 | if (!strcmp(new_dir, ctx->search_paths.objs[u])) { |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 88 | free(new_dir); |
Radek Krejci | 14946ab | 2018-09-20 13:42:06 +0200 | [diff] [blame] | 89 | return LY_EEXIST; |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | 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] | 93 | free(new_dir); |
| 94 | return LY_EMEM; |
| 95 | } |
| 96 | |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 97 | /* new searchdir - possibly more latest revision available */ |
| 98 | ly_ctx_reset_latests(ctx); |
| 99 | |
Radek Krejci | 0e21240 | 2018-09-20 11:29:38 +0200 | [diff] [blame] | 100 | return LY_SUCCESS; |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 101 | } else { |
| 102 | /* consider that no change is not actually an error */ |
| 103 | return LY_SUCCESS; |
| 104 | } |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | API const char * const * |
| 108 | ly_ctx_get_searchdirs(const struct ly_ctx *ctx) |
| 109 | { |
Radek Krejci | 0502643 | 2018-09-20 12:13:43 +0200 | [diff] [blame] | 110 | void **new; |
| 111 | |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 112 | LY_CHECK_ARG_RET(ctx, ctx, NULL); |
Radek Krejci | 0502643 | 2018-09-20 12:13:43 +0200 | [diff] [blame] | 113 | |
| 114 | if (ctx->search_paths.count == ctx->search_paths.size) { |
| 115 | /* not enough space for terminating NULL byte */ |
| 116 | new = realloc(((struct ly_ctx *)ctx)->search_paths.objs, (ctx->search_paths.size + 8) * sizeof *ctx->search_paths.objs); |
| 117 | LY_CHECK_ERR_RET(!new, LOGMEM(NULL), NULL); |
| 118 | ((struct ly_ctx *)ctx)->search_paths.size += 8; |
| 119 | ((struct ly_ctx *)ctx)->search_paths.objs = new; |
| 120 | } |
| 121 | /* set terminating NULL byte to the strings list */ |
| 122 | ctx->search_paths.objs[ctx->search_paths.count] = NULL; |
| 123 | |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 124 | return (const char * const *)ctx->search_paths.objs; |
| 125 | } |
| 126 | |
| 127 | API LY_ERR |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 128 | ly_ctx_unset_searchdirs(struct ly_ctx *ctx, const char *value) |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 129 | { |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 130 | unsigned int index; |
| 131 | |
Radek Krejci | cd64907 | 2018-09-20 12:14:45 +0200 | [diff] [blame] | 132 | LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL); |
Radek Krejci | cd64907 | 2018-09-20 12:14:45 +0200 | [diff] [blame] | 133 | |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 134 | if (!ctx->search_paths.count) { |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 135 | return LY_SUCCESS; |
| 136 | } |
| 137 | |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 138 | if (value) { |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 139 | /* remove specific search directory */ |
Radek Krejci | 0759b79 | 2018-09-20 13:53:15 +0200 | [diff] [blame] | 140 | for (index = 0; index < ctx->search_paths.count; ++index) { |
| 141 | if (!strcmp(value, ctx->search_paths.objs[index])) { |
| 142 | break; |
| 143 | } |
| 144 | } |
| 145 | if (index == ctx->search_paths.count) { |
| 146 | LOGARG(ctx, value); |
| 147 | return LY_EINVAL; |
| 148 | } else { |
| 149 | return ly_set_rm_index(&ctx->search_paths, index, free); |
| 150 | } |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 151 | } else { |
| 152 | /* remove them all */ |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 153 | ly_set_erase(&ctx->search_paths, free); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 154 | memset(&ctx->search_paths, 0, sizeof ctx->search_paths); |
| 155 | } |
| 156 | |
| 157 | return LY_SUCCESS; |
| 158 | } |
| 159 | |
| 160 | API LY_ERR |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 161 | ly_ctx_unset_searchdir(struct ly_ctx *ctx, unsigned int index) |
| 162 | { |
| 163 | LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL); |
| 164 | |
| 165 | if (!ctx->search_paths.count) { |
| 166 | return LY_SUCCESS; |
| 167 | } |
| 168 | |
| 169 | if (index >= ctx->search_paths.count) { |
| 170 | LOGARG(ctx, value); |
| 171 | return LY_EINVAL; |
| 172 | } else { |
| 173 | return ly_set_rm_index(&ctx->search_paths, index, free); |
| 174 | } |
| 175 | |
| 176 | return LY_SUCCESS; |
| 177 | } |
| 178 | |
| 179 | API const struct lys_module * |
| 180 | ly_ctx_load_module(struct ly_ctx *ctx, const char *name, const char *revision) |
| 181 | { |
| 182 | struct lys_module *result = NULL; |
| 183 | |
| 184 | LY_CHECK_ARG_RET(ctx, ctx, name, NULL); |
| 185 | |
| 186 | LY_CHECK_RET(lysp_load_module(ctx, name, revision, 1, 0, &result), NULL); |
| 187 | return result; |
| 188 | } |
| 189 | |
| 190 | API LY_ERR |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 191 | ly_ctx_new(const char *search_dir, int options, struct ly_ctx **new_ctx) |
| 192 | { |
| 193 | struct ly_ctx *ctx = NULL; |
| 194 | struct lys_module *module; |
| 195 | char *search_dir_list; |
| 196 | char *sep, *dir; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 197 | int i; |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 198 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 199 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 200 | LY_CHECK_ARG_RET(NULL, new_ctx, LY_EINVAL); |
| 201 | |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 202 | ctx = calloc(1, sizeof *ctx); |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 203 | LY_CHECK_ERR_RET(!ctx, LOGMEM(NULL), LY_EMEM); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 204 | |
| 205 | /* dictionary */ |
| 206 | lydict_init(&ctx->dict); |
| 207 | |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 208 | #if 0 /* TODO when plugins implemented */ |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 209 | /* plugins */ |
| 210 | ly_load_plugins(); |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 211 | #endif |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 212 | |
| 213 | /* initialize thread-specific key */ |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 214 | while ((pthread_key_create(&ctx->errlist_key, ly_err_free)) == EAGAIN); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 215 | |
| 216 | /* models list */ |
| 217 | ctx->flags = options; |
| 218 | if (search_dir) { |
| 219 | search_dir_list = strdup(search_dir); |
| 220 | LY_CHECK_ERR_GOTO(!search_dir_list, LOGMEM(NULL); rc = LY_EMEM, error); |
| 221 | |
| 222 | for (dir = search_dir_list; (sep = strchr(dir, ':')) != NULL && rc == LY_SUCCESS; dir = sep + 1) { |
| 223 | *sep = 0; |
| 224 | rc = ly_ctx_set_searchdir(ctx, dir); |
Radek Krejci | 14946ab | 2018-09-20 13:42:06 +0200 | [diff] [blame] | 225 | if (rc == LY_EEXIST) { |
| 226 | /* ignore duplication */ |
| 227 | rc = LY_SUCCESS; |
| 228 | } |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 229 | } |
| 230 | if (*dir && rc == LY_SUCCESS) { |
| 231 | rc = ly_ctx_set_searchdir(ctx, dir); |
Radek Krejci | 14946ab | 2018-09-20 13:42:06 +0200 | [diff] [blame] | 232 | if (rc == LY_EEXIST) { |
| 233 | /* ignore duplication */ |
| 234 | rc = LY_SUCCESS; |
| 235 | } |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 236 | } |
| 237 | free(search_dir_list); |
| 238 | |
| 239 | /* If ly_ctx_set_searchdir() failed, the error is already logged. Just exit */ |
| 240 | if (rc != LY_SUCCESS) { |
| 241 | goto error; |
| 242 | } |
| 243 | } |
| 244 | ctx->module_set_id = 1; |
| 245 | |
| 246 | /* load internal modules */ |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 247 | 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] | 248 | module = (struct lys_module *)lys_parse_mem_module(ctx, internal_modules[i].data, internal_modules[i].format, |
| 249 | internal_modules[i].implemented, NULL, NULL); |
Radek Krejci | 0e75e6f | 2018-11-08 09:40:02 +0100 | [diff] [blame] | 250 | LY_CHECK_ERR_GOTO(!module, rc = ly_errcode(ctx), error); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 251 | LY_CHECK_GOTO((rc = lys_compile(module, 0)), error); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | *new_ctx = ctx; |
| 255 | return rc; |
| 256 | |
| 257 | error: |
| 258 | ly_ctx_destroy(ctx, NULL); |
| 259 | return rc; |
| 260 | } |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 261 | API int |
Radek Krejci | 3fbe89a | 2018-09-20 13:54:46 +0200 | [diff] [blame] | 262 | ly_ctx_get_options(const struct ly_ctx *ctx) |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 263 | { |
Radek Krejci | 3fbe89a | 2018-09-20 13:54:46 +0200 | [diff] [blame] | 264 | LY_CHECK_ARG_RET(ctx, ctx, 0); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 265 | return ctx->flags; |
| 266 | } |
| 267 | |
Radek Krejci | ca828d9 | 2018-09-20 14:19:43 +0200 | [diff] [blame] | 268 | API LY_ERR |
Radek Krejci | 3fa46b6 | 2019-09-11 10:47:30 +0200 | [diff] [blame] | 269 | ly_ctx_set_options(struct ly_ctx *ctx, int option) |
Radek Krejci | ca828d9 | 2018-09-20 14:19:43 +0200 | [diff] [blame] | 270 | { |
| 271 | LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL); |
| 272 | LY_CHECK_ERR_RET(option & LY_CTX_NOYANGLIBRARY, LOGARG(ctx, option), LY_EINVAL); |
| 273 | |
| 274 | /* set the option(s) */ |
| 275 | ctx->flags |= option; |
| 276 | |
| 277 | return LY_SUCCESS; |
| 278 | } |
| 279 | |
| 280 | API LY_ERR |
Radek Krejci | 3fa46b6 | 2019-09-11 10:47:30 +0200 | [diff] [blame] | 281 | ly_ctx_unset_options(struct ly_ctx *ctx, int option) |
Radek Krejci | ca828d9 | 2018-09-20 14:19:43 +0200 | [diff] [blame] | 282 | { |
| 283 | LY_CHECK_ARG_RET(ctx, ctx, LY_EINVAL); |
| 284 | LY_CHECK_ERR_RET(option & LY_CTX_NOYANGLIBRARY, LOGARG(ctx, option), LY_EINVAL); |
| 285 | |
| 286 | /* unset the option(s) */ |
| 287 | ctx->flags &= ~option; |
| 288 | |
| 289 | return LY_SUCCESS; |
| 290 | } |
| 291 | |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 292 | API uint16_t |
| 293 | ly_ctx_get_module_set_id(const struct ly_ctx *ctx) |
| 294 | { |
Radek Krejci | 3fbe89a | 2018-09-20 13:54:46 +0200 | [diff] [blame] | 295 | LY_CHECK_ARG_RET(ctx, ctx, 0); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 296 | return ctx->module_set_id; |
| 297 | } |
| 298 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 299 | API void |
| 300 | ly_ctx_set_module_imp_clb(struct ly_ctx *ctx, ly_module_imp_clb clb, void *user_data) |
| 301 | { |
| 302 | LY_CHECK_ARG_RET(ctx, ctx,); |
| 303 | |
| 304 | ctx->imp_clb = clb; |
| 305 | ctx->imp_clb_data = user_data; |
| 306 | } |
| 307 | |
| 308 | API ly_module_imp_clb |
| 309 | ly_ctx_get_module_imp_clb(const struct ly_ctx *ctx, void **user_data) |
| 310 | { |
| 311 | LY_CHECK_ARG_RET(ctx, ctx, NULL); |
| 312 | |
| 313 | if (user_data) { |
| 314 | *user_data = ctx->imp_clb_data; |
| 315 | } |
| 316 | return ctx->imp_clb; |
| 317 | } |
| 318 | |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 319 | API const struct lys_module * |
| 320 | ly_ctx_get_module_iter(const struct ly_ctx *ctx, unsigned int *index) |
| 321 | { |
| 322 | LY_CHECK_ARG_RET(ctx, ctx, index, NULL); |
| 323 | |
Radek Krejci | 2390fb5 | 2019-04-30 13:27:43 +0200 | [diff] [blame] | 324 | if (*index < (unsigned)ctx->list.count) { |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 325 | return ctx->list.objs[(*index)++]; |
Radek Krejci | 2390fb5 | 2019-04-30 13:27:43 +0200 | [diff] [blame] | 326 | } else { |
| 327 | return NULL; |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 328 | } |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 329 | } |
| 330 | |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 331 | /** |
| 332 | * @brief Iterate over the modules in the given context. Returned modules must match the given key at the offset of |
| 333 | * lysp_module and lysc_module structures (they are supposed to be placed at the same offset in both structures). |
| 334 | * |
| 335 | * @param[in] ctx Context where to iterate. |
| 336 | * @param[in] key Key value to search for. |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 337 | * @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] | 338 | * @param[in,out] Iterator to pass between the function calls. On the first call, the variable is supposed to be |
| 339 | * initiated to 0. After each call returning a module, the value is greater by 1 than the index of the returned |
| 340 | * module in the context. |
| 341 | * @return Module matching the given key, NULL if no such module found. |
| 342 | */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 343 | static struct lys_module * |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 344 | ly_ctx_get_module_by_iter(const struct ly_ctx *ctx, const char *key, size_t key_offset, unsigned int *index) |
| 345 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 346 | struct lys_module *mod; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 347 | const char *value; |
| 348 | |
| 349 | for (; *index < ctx->list.count; ++(*index)) { |
| 350 | mod = ctx->list.objs[*index]; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 351 | value = *(const char**)(((int8_t*)(mod)) + key_offset); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 352 | if (!strcmp(key, value)) { |
| 353 | /* increment index for the next run */ |
| 354 | ++(*index); |
| 355 | return mod; |
| 356 | } |
| 357 | } |
| 358 | /* done */ |
| 359 | return NULL; |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * @brief Unifying function for ly_ctx_get_module() and ly_ctx_get_module_ns() |
| 364 | * @param[in] ctx Context where to search. |
| 365 | * @param[in] key Name or Namespace as a search key. |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 366 | * @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] | 367 | * @param[in] revision Revision date to match. If NULL, the matching module must have no revision. To search for the latest |
| 368 | * revision module, use ly_ctx_get_module_latest_by(). |
| 369 | * @return Matching module if any. |
| 370 | */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 371 | static struct lys_module * |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 372 | ly_ctx_get_module_by(const struct ly_ctx *ctx, const char *key, size_t key_offset, const char *revision) |
| 373 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 374 | struct lys_module *mod; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 375 | unsigned int index = 0; |
| 376 | |
| 377 | while ((mod = ly_ctx_get_module_by_iter(ctx, key, key_offset, &index))) { |
| 378 | if (!revision) { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 379 | if (!mod->revision) { |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 380 | /* found requested module without revision */ |
| 381 | return mod; |
| 382 | } |
| 383 | } else { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 384 | if (mod->revision && !strcmp(mod->revision, revision)) { |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 385 | /* found requested module of the specific revision */ |
| 386 | return mod; |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | return NULL; |
| 392 | } |
| 393 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 394 | API struct lys_module * |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 395 | ly_ctx_get_module_ns(const struct ly_ctx *ctx, const char *ns, const char *revision) |
| 396 | { |
| 397 | LY_CHECK_ARG_RET(ctx, ctx, ns, NULL); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 398 | 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] | 399 | } |
| 400 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 401 | API struct lys_module * |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 402 | ly_ctx_get_module(const struct ly_ctx *ctx, const char *name, const char *revision) |
| 403 | { |
| 404 | LY_CHECK_ARG_RET(ctx, ctx, name, NULL); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 405 | 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] | 406 | } |
| 407 | |
| 408 | /** |
| 409 | * @brief Unifying function for ly_ctx_get_module_latest() and ly_ctx_get_module_latest_ns() |
| 410 | * @param[in] ctx Context where to search. |
| 411 | * @param[in] key Name or Namespace as a search key. |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 412 | * @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] | 413 | * @return Matching module if any. |
| 414 | */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 415 | static struct lys_module * |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 416 | ly_ctx_get_module_latest_by(const struct ly_ctx *ctx, const char *key, size_t key_offset) |
| 417 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 418 | struct lys_module *mod; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 419 | unsigned int index = 0; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 420 | |
| 421 | 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] | 422 | if (mod->latest_revision) { |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 423 | return mod; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | |
Radek Krejci | 9f5e6fb | 2018-10-25 09:26:12 +0200 | [diff] [blame] | 427 | return NULL; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 428 | } |
| 429 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 430 | API struct lys_module * |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 431 | ly_ctx_get_module_latest(const struct ly_ctx *ctx, const char *name) |
| 432 | { |
| 433 | LY_CHECK_ARG_RET(ctx, ctx, name, NULL); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 434 | 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] | 435 | } |
| 436 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 437 | API struct lys_module * |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 438 | ly_ctx_get_module_latest_ns(const struct ly_ctx *ctx, const char *ns) |
| 439 | { |
| 440 | LY_CHECK_ARG_RET(ctx, ctx, ns, NULL); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 441 | 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] | 442 | } |
| 443 | |
| 444 | /** |
| 445 | * @brief Unifying function for ly_ctx_get_module_implemented() and ly_ctx_get_module_implemented_ns() |
| 446 | * @param[in] ctx Context where to search. |
| 447 | * @param[in] key Name or Namespace as a search key. |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 448 | * @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] | 449 | * @return Matching module if any. |
| 450 | */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 451 | static struct lys_module * |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 452 | ly_ctx_get_module_implemented_by(const struct ly_ctx *ctx, const char *key, size_t key_offset) |
| 453 | { |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 454 | struct lys_module *mod; |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 455 | unsigned int index = 0; |
| 456 | |
| 457 | 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] | 458 | if (mod->implemented) { |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 459 | return mod; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | return NULL; |
| 464 | } |
| 465 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 466 | API struct lys_module * |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 467 | ly_ctx_get_module_implemented(const struct ly_ctx *ctx, const char *name) |
| 468 | { |
| 469 | LY_CHECK_ARG_RET(ctx, ctx, name, NULL); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 470 | 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] | 471 | } |
| 472 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 473 | API struct lys_module * |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 474 | ly_ctx_get_module_implemented_ns(const struct ly_ctx *ctx, const char *ns) |
| 475 | { |
| 476 | LY_CHECK_ARG_RET(ctx, ctx, ns, NULL); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 477 | 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] | 478 | } |
| 479 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 480 | struct lysp_submodule * |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 481 | ly_ctx_get_submodule(const struct ly_ctx *ctx, const char *module, const char *submodule, const char *revision) |
| 482 | { |
| 483 | const struct lys_module *mod; |
| 484 | struct lysp_include *inc; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 485 | uint32_t v; |
| 486 | LY_ARRAY_SIZE_TYPE u; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 487 | |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 488 | assert(submodule); |
| 489 | |
| 490 | for (v = 0; v < ctx->list.count; ++v) { |
| 491 | mod = ctx->list.objs[v]; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 492 | if (!mod->parsed) { |
| 493 | continue; |
| 494 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 495 | if (module && strcmp(module, mod->name)) { |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 496 | continue; |
| 497 | } |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 498 | |
| 499 | LY_ARRAY_FOR(mod->parsed->includes, u) { |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 500 | 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] | 501 | inc = &mod->parsed->includes[u]; |
| 502 | if (!revision) { |
| 503 | if (inc->submodule->latest_revision) { |
| 504 | return inc->submodule; |
| 505 | } |
| 506 | } else if (inc->submodule->revs && !strcmp(revision, inc->submodule->revs[0].date)) { |
| 507 | return inc->submodule; |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | return NULL; |
| 514 | } |
| 515 | |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 516 | API void |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 517 | ly_ctx_reset_latests(struct ly_ctx *ctx) |
| 518 | { |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 519 | struct lys_module *mod; |
| 520 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 521 | for (uint32_t v = 0; v < ctx->list.count; ++v) { |
| 522 | mod = ctx->list.objs[v]; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 523 | if (mod->latest_revision == 2) { |
| 524 | mod->latest_revision = 1; |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 525 | } |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 526 | if (mod->parsed && mod->parsed->includes) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 527 | for (LY_ARRAY_SIZE_TYPE u = 0; u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) { |
| 528 | if (mod->parsed->includes[u].submodule->latest_revision == 2) { |
| 529 | mod->parsed->includes[u].submodule->latest_revision = 1; |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | API void |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 537 | 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] | 538 | { |
Radek Krejci | 418823a | 2018-09-20 16:42:13 +0200 | [diff] [blame] | 539 | if (!ctx) { |
| 540 | return; |
| 541 | } |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 542 | |
| 543 | /* models list */ |
Michal Vasko | b34480a | 2018-09-17 10:34:45 +0200 | [diff] [blame] | 544 | for (; ctx->list.count; ctx->list.count--) { |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 545 | /* remove the module */ |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 546 | lys_module_free(ctx->list.objs[ctx->list.count - 1], private_destructor); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 547 | } |
| 548 | free(ctx->list.objs); |
| 549 | |
| 550 | /* search paths list */ |
Radek Krejci | a40f21b | 2018-09-18 10:42:08 +0200 | [diff] [blame] | 551 | ly_set_erase(&ctx->search_paths, free); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 552 | |
| 553 | /* clean the error list */ |
| 554 | ly_err_clean(ctx, 0); |
| 555 | pthread_key_delete(ctx->errlist_key); |
| 556 | |
| 557 | /* dictionary */ |
| 558 | lydict_clean(&ctx->dict); |
| 559 | |
| 560 | #if 0 /* TODO when plugins implemented */ |
| 561 | /* plugins - will be removed only if this is the last context */ |
| 562 | ly_clean_plugins(); |
| 563 | #endif |
| 564 | |
| 565 | free(ctx); |
| 566 | } |