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