Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file tree_schema_internal.h |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief internal functions for YANG schema trees. |
| 5 | * |
| 6 | * Copyright (c) 2015 - 2018 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
| 15 | #ifndef LY_TREE_SCHEMA_INTERNAL_H_ |
| 16 | #define LY_TREE_SCHEMA_INTERNAL_H_ |
| 17 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 18 | #define LOGVAL_YANG(CTX, ...) LOGVAL((CTX)->ctx, LY_VLOG_LINE, &(CTX)->line, __VA_ARGS__) |
| 19 | |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 20 | /** |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 21 | * @brief List of YANG statement groups - the (sub)module's substatements |
| 22 | */ |
| 23 | enum yang_module_stmt { |
| 24 | Y_MOD_MODULE_HEADER, |
| 25 | Y_MOD_LINKAGE, |
| 26 | Y_MOD_META, |
| 27 | Y_MOD_REVISION, |
| 28 | Y_MOD_BODY |
| 29 | }; |
| 30 | |
| 31 | /** |
| 32 | * @brief Types of arguments of YANG statements |
| 33 | */ |
| 34 | enum yang_arg { |
| 35 | Y_IDENTIF_ARG, /**< YANG "identifier-arg-str" rule */ |
| 36 | Y_PREF_IDENTIF_ARG, /**< YANG "identifier-ref-arg-str" rule */ |
| 37 | Y_STR_ARG, /**< YANG "string" rule */ |
| 38 | Y_MAYBE_STR_ARG /**< optional YANG "string" rule */ |
| 39 | }; |
| 40 | |
| 41 | /** |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 42 | * @brief internal context for schema parsers |
| 43 | */ |
| 44 | struct ly_parser_ctx { |
| 45 | struct ly_ctx *ctx; |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 46 | struct lysp_module *mod; |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 47 | uint64_t line; /* line number */ |
| 48 | uint64_t indent; /* current position on the line for YANG indentation */ |
| 49 | }; |
| 50 | |
| 51 | /** |
| 52 | * @brief Check the currently present prefixes in the module for collision with the new one. |
| 53 | * |
| 54 | * @param[in] ctx yang parser context. |
| 55 | * @param[in] module Schema tree to check. |
| 56 | * @param[in] value Newly added prefix value (including its location to distinguish collision with itself). |
| 57 | * @return LY_EEXIST when prefix is already used in the module, LY_SUCCESS otherwise |
| 58 | */ |
| 59 | LY_ERR lysp_check_prefix(struct ly_parser_ctx *ctx, struct lysp_module *module, const char **value); |
| 60 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 61 | /** |
| 62 | * @brief Check date string (4DIGIT "-" 2DIGIT "-" 2DIGIT) |
| 63 | * |
| 64 | * @param[in] ctx Context to store log message. |
| 65 | * @param[in] date Date string to check (non-necessarily terminated by \0) |
| 66 | * @param[in] date_len Length of the date string, 10 expected. |
| 67 | * @param[in] stmt Statement name for error message. |
| 68 | * @return LY_ERR value. |
| 69 | */ |
| 70 | LY_ERR lysp_check_date(struct ly_ctx *ctx, const char *date, int date_len, const char *stmt); |
| 71 | |
| 72 | /** |
| 73 | * @brief Just move the newest revision into the first position, does not sort the rest |
| 74 | * @param[in] revs Sized-array of the revisions in a printable schema tree. |
| 75 | */ |
| 76 | void lysp_sort_revisions(struct lysp_revision *revs); |
| 77 | |
| 78 | /** |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 79 | * @brief Find and parse module of the given name. |
| 80 | * |
| 81 | * @param[in] ctx libyang context. |
| 82 | * @param[in] name Name of the module to load. |
| 83 | * @param[in] revison Optional revision of the module to load. If NULL, the newest revision is loaded. |
Radek Krejci | 6d6e4e4 | 2018-10-29 13:28:19 +0100 | [diff] [blame] | 84 | * @param[in] implement Flag if the loaded module is supposed to be marked as implemented. |
| 85 | * @param[in] require_parsed Flag to require parsed module structure in case the module is already in the context, |
| 86 | * but only the compiled structure is available. |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 87 | * @param[out] mod Parsed module structure. |
| 88 | * @return LY_ERR value. |
| 89 | */ |
Radek Krejci | 6d6e4e4 | 2018-10-29 13:28:19 +0100 | [diff] [blame] | 90 | LY_ERR lysp_load_module(struct ly_ctx *ctx, const char *name, const char *revision, int implement, int require_parsed, struct lys_module **mod); |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 91 | |
| 92 | /** |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 93 | * @brief Parse included submodule into the simply parsed YANG module. |
| 94 | * |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 95 | * @param[in] ctx libyang context |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 96 | * @param[in] mod Module including a submodule. |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 97 | * @param[in,out] inc Include structure holding all available information about the include statement, the parsed |
| 98 | * submodule is stored into this structure. |
| 99 | * @return LY_ERR value. |
| 100 | */ |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 101 | LY_ERR lysp_load_submodule(struct ly_ctx *ctx, struct lysp_module *mod, struct lysp_include *inc); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 102 | |
| 103 | /** |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 104 | * @brief Find the module referenced by prefix in the provided parsed mod. |
| 105 | * |
| 106 | * @param[in] mod Schema module where the prefix was used. |
| 107 | * @param[in] prefix Prefix used to reference a module. |
| 108 | * @param[in] len Length of the prefix since it is not necessary NULL-terminated. |
| 109 | * @return Pointer to the module or NULL if the module is not found. |
| 110 | */ |
| 111 | struct lys_module *lysp_module_find_prefix(struct lysp_module *mod, const char *prefix, size_t len); |
| 112 | |
| 113 | /** |
| 114 | * @brief Find the module referenced by prefix in the provided compiled mod. |
| 115 | * |
| 116 | * @param[in] mod Schema module where the prefix was used. |
| 117 | * @param[in] prefix Prefix used to reference a module. |
| 118 | * @param[in] len Length of the prefix since it is not necessary NULL-terminated. |
| 119 | * @return Pointer to the module or NULL if the module is not found. |
| 120 | */ |
| 121 | struct lys_module *lysc_module_find_prefix(struct lysc_module *mod, const char *prefix, size_t len); |
| 122 | |
| 123 | /** |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 124 | * @brief Find the module referenced by prefix in the provided mod. |
| 125 | * |
| 126 | * @param[in] mod Schema module where the prefix was used. |
| 127 | * @param[in] prefix Prefix used to reference a module. |
| 128 | * @param[in] len Length of the prefix since it is not necessary NULL-terminated. |
| 129 | * @return Pointer to the module or NULL if the module is not found. |
| 130 | */ |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 131 | struct lys_module *lys_module_find_prefix(struct lys_module *mod, const char *prefix, size_t len); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 132 | |
| 133 | /** |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 134 | * @brief Parse YANG module and submodule from a string. |
| 135 | * |
| 136 | * In contrast to public lys_parse_mem(), also submodules can be parsed here. However, |
| 137 | * while the modules are added into the context, submodules not. The latest_revision |
| 138 | * flag is updated in both cases. |
| 139 | * |
| 140 | * @param[in] ctx libyang context where to process the data model. |
| 141 | * @param[in] data The string containing the dumped data model in the specified |
| 142 | * format. |
| 143 | * @param[in] format Format of the input data (YANG or YIN). |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 144 | * @param[in] implement Flag if the schema is supposed to be marked as implemented. |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame^] | 145 | * @param[in] custom_check Callback to check the parsed schema before it is accepted. |
| 146 | * @param[in] check_data Caller's data to pass to the custom_check callback. |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 147 | * @return Pointer to the data model structure or NULL on error. |
| 148 | */ |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame^] | 149 | struct lys_module *lys_parse_mem_(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, int implement, |
| 150 | LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, void *data), void *check_data); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 151 | |
| 152 | /** |
| 153 | * @brief Parse YANG module and submodule from a file descriptor. |
| 154 | * |
| 155 | * In contrast to public lys_parse_mem(), also submodules can be parsed here. However, |
| 156 | * while the modules are added into the context, submodules not. The latest_revision |
| 157 | * flag is updated in both cases. |
| 158 | * |
| 159 | * \note Current implementation supports only reading data from standard (disk) file, not from sockets, pipes, etc. |
| 160 | * |
| 161 | * @param[in] ctx libyang context where to process the data model. |
| 162 | * @param[in] fd File descriptor of a regular file (e.g. sockets are not supported) containing the schema |
| 163 | * in the specified format. |
| 164 | * @param[in] format Format of the input data (YANG or YIN). |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 165 | * @param[in] implement Flag if the schema is supposed to be marked as implemented. |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame^] | 166 | * @param[in] custom_check Callback to check the parsed schema before it is accepted. |
| 167 | * @param[in] check_data Caller's data to pass to the custom_check callback. |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 168 | * @return Pointer to the data model structure or NULL on error. |
| 169 | */ |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame^] | 170 | struct lys_module *lys_parse_fd_(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, int implement, |
| 171 | LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, void *data), void *check_data); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 172 | |
| 173 | /** |
| 174 | * @brief Parse YANG module and submodule from a file descriptor. |
| 175 | * |
| 176 | * In contrast to public lys_parse_mem(), also submodules can be parsed here. However, |
| 177 | * while the modules are added into the context, submodules not. The latest_revision |
| 178 | * flag is updated in both cases. |
| 179 | * |
| 180 | * \note Current implementation supports only reading data from standard (disk) file, not from sockets, pipes, etc. |
| 181 | * |
| 182 | * @brief REad a schema into the specified context from a file. |
| 183 | * |
| 184 | * @param[in] ctx libyang context where to process the data model. |
| 185 | * @param[in] path Path to the file with the model in the specified format. |
| 186 | * @param[in] format Format of the input data (YANG or YIN). |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 187 | * @param[in] implement Flag if the schema is supposed to be marked as implemented. |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame^] | 188 | * @param[in] custom_check Callback to check the parsed schema before it is accepted. |
| 189 | * @param[in] check_data Caller's data to pass to the custom_check callback. |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 190 | * @return Pointer to the data model structure or NULL on error. |
| 191 | */ |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame^] | 192 | struct lys_module *lys_parse_path_(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, int implement, |
| 193 | LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, void *data), void *check_data); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 194 | |
| 195 | /** |
| 196 | * @brief Load the (sub)module into the context. |
| 197 | * |
| 198 | * This function does not check the presence of the (sub)module in context, it should be done before calling this function. |
| 199 | * |
| 200 | * module_name and submodule_name are alternatives - only one of the |
| 201 | * |
| 202 | * @param[in] ctx libyang context where to work. |
| 203 | * @param[in] name Name of the (sub)module to load. |
| 204 | * @param[in] revision Optional revision of the (sub)module to load, if NULL the newest revision is being loaded. |
| 205 | * @param[in] implement Flag if the (sub)module is supposed to be marked as implemented. |
| 206 | * @param[out] result Parsed YANG schema tree of the requested module. If it is a module, it is already in the context! |
| 207 | * @return LY_ERR value, in case of LY_SUCCESS, the \arg result is always provided. |
| 208 | */ |
| 209 | LY_ERR lys_module_localfile(struct ly_ctx *ctx, const char *name, const char *revision, int implement, struct lys_module **result); |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 210 | |
| 211 | /** |
| 212 | * @brief Make the module implemented. |
| 213 | * Does not check for collision in context, it must be done before calling the function, this is a simple switch. |
| 214 | * @param[in] mod Module to make implemented. |
| 215 | */ |
| 216 | void lys_module_implement(struct lys_module *mod); |
| 217 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 218 | /** |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 219 | * @brief Free the schema structure. It just frees, it does not remove the schema from its context. |
| 220 | * @param[in,out] module Schema module structure to free. |
| 221 | * @param[in] private_destructor Function to remove private data from the compiled schema tree. |
| 222 | */ |
| 223 | void lys_module_free(struct lys_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv)); |
| 224 | |
| 225 | /** |
| 226 | * @brief |
| 227 | */ |
| 228 | LY_ERR yang_parse(struct ly_ctx *ctx, const char *data, struct lysp_module **mod_p); |
| 229 | |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 230 | #endif /* LY_TREE_SCHEMA_INTERNAL_H_ */ |