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; |
| 46 | uint64_t line; /* line number */ |
| 47 | uint64_t indent; /* current position on the line for YANG indentation */ |
| 48 | }; |
| 49 | |
| 50 | /** |
| 51 | * @brief Check the currently present prefixes in the module for collision with the new one. |
| 52 | * |
| 53 | * @param[in] ctx yang parser context. |
| 54 | * @param[in] module Schema tree to check. |
| 55 | * @param[in] value Newly added prefix value (including its location to distinguish collision with itself). |
| 56 | * @return LY_EEXIST when prefix is already used in the module, LY_SUCCESS otherwise |
| 57 | */ |
| 58 | LY_ERR lysp_check_prefix(struct ly_parser_ctx *ctx, struct lysp_module *module, const char **value); |
| 59 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 60 | /** |
| 61 | * @brief Check date string (4DIGIT "-" 2DIGIT "-" 2DIGIT) |
| 62 | * |
| 63 | * @param[in] ctx Context to store log message. |
| 64 | * @param[in] date Date string to check (non-necessarily terminated by \0) |
| 65 | * @param[in] date_len Length of the date string, 10 expected. |
| 66 | * @param[in] stmt Statement name for error message. |
| 67 | * @return LY_ERR value. |
| 68 | */ |
| 69 | LY_ERR lysp_check_date(struct ly_ctx *ctx, const char *date, int date_len, const char *stmt); |
| 70 | |
| 71 | /** |
| 72 | * @brief Just move the newest revision into the first position, does not sort the rest |
| 73 | * @param[in] revs Sized-array of the revisions in a printable schema tree. |
| 74 | */ |
| 75 | void lysp_sort_revisions(struct lysp_revision *revs); |
| 76 | |
| 77 | /** |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 78 | * @brief Parse included submodule into the simply parsed YANG module. |
| 79 | * |
| 80 | * @param[in] ctx yang parser context. |
| 81 | * @param[in] mod Module including a submodule. |
| 82 | * @param[in] name Name of the submodule to include. |
| 83 | * @param[in,out] inc Include structure holding all available information about the include statement, the parsed |
| 84 | * submodule is stored into this structure. |
| 85 | * @return LY_ERR value. |
| 86 | */ |
| 87 | LY_ERR lysp_parse_include(struct ly_parser_ctx *ctx, struct lysp_module *mod, const char *name, struct lysp_include *inc); |
| 88 | |
| 89 | /** |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 90 | * @brief Find the module referenced by prefix in the provided mod. |
| 91 | * |
| 92 | * @param[in] mod Schema module where the prefix was used. |
| 93 | * @param[in] prefix Prefix used to reference a module. |
| 94 | * @param[in] len Length of the prefix since it is not necessary NULL-terminated. |
| 95 | * @return Pointer to the module or NULL if the module is not found. |
| 96 | */ |
| 97 | struct lysc_module *lysc_module_find_prefix(struct lysc_module *mod, const char *prefix, size_t len); |
| 98 | |
| 99 | /** |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 100 | * @brief Parse YANG module and submodule from a string. |
| 101 | * |
| 102 | * In contrast to public lys_parse_mem(), also submodules can be parsed here. However, |
| 103 | * while the modules are added into the context, submodules not. The latest_revision |
| 104 | * flag is updated in both cases. |
| 105 | * |
| 106 | * @param[in] ctx libyang context where to process the data model. |
| 107 | * @param[in] data The string containing the dumped data model in the specified |
| 108 | * format. |
| 109 | * @param[in] format Format of the input data (YANG or YIN). |
| 110 | * @param[in] revision If a specific revision is required, it is checked before the parsed |
| 111 | * schema is accepted. |
| 112 | * @param[in] implement Flag if the schema is supposed to be marked as implemented. |
| 113 | * @return Pointer to the data model structure or NULL on error. |
| 114 | */ |
| 115 | struct lys_module *lys_parse_mem_(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, const char *revision, int implement); |
| 116 | |
| 117 | /** |
| 118 | * @brief Parse YANG module and submodule from a file descriptor. |
| 119 | * |
| 120 | * In contrast to public lys_parse_mem(), also submodules can be parsed here. However, |
| 121 | * while the modules are added into the context, submodules not. The latest_revision |
| 122 | * flag is updated in both cases. |
| 123 | * |
| 124 | * \note Current implementation supports only reading data from standard (disk) file, not from sockets, pipes, etc. |
| 125 | * |
| 126 | * @param[in] ctx libyang context where to process the data model. |
| 127 | * @param[in] fd File descriptor of a regular file (e.g. sockets are not supported) containing the schema |
| 128 | * in the specified format. |
| 129 | * @param[in] format Format of the input data (YANG or YIN). |
| 130 | * @param[in] revision If a specific revision is required, it is checked before the parsed |
| 131 | * schema is accepted. |
| 132 | * @param[in] implement Flag if the schema is supposed to be marked as implemented. |
| 133 | * @return Pointer to the data model structure or NULL on error. |
| 134 | */ |
| 135 | struct lys_module *lys_parse_fd_(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, const char *revision, int implement); |
| 136 | |
| 137 | /** |
| 138 | * @brief Parse YANG module and submodule from a file descriptor. |
| 139 | * |
| 140 | * In contrast to public lys_parse_mem(), also submodules can be parsed here. However, |
| 141 | * while the modules are added into the context, submodules not. The latest_revision |
| 142 | * flag is updated in both cases. |
| 143 | * |
| 144 | * \note Current implementation supports only reading data from standard (disk) file, not from sockets, pipes, etc. |
| 145 | * |
| 146 | * @brief REad a schema into the specified context from a file. |
| 147 | * |
| 148 | * @param[in] ctx libyang context where to process the data model. |
| 149 | * @param[in] path Path to the file with the model in the specified format. |
| 150 | * @param[in] format Format of the input data (YANG or YIN). |
| 151 | * @param[in] revision If a specific revision is required, it is checked before the parsed |
| 152 | * schema is accepted. |
| 153 | * @param[in] implement Flag if the schema is supposed to be marked as implemented. |
| 154 | * @return Pointer to the data model structure or NULL on error. |
| 155 | */ |
| 156 | struct lys_module *lys_parse_path_(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, const char *revision, int implement); |
| 157 | |
| 158 | /** |
| 159 | * @brief Load the (sub)module into the context. |
| 160 | * |
| 161 | * This function does not check the presence of the (sub)module in context, it should be done before calling this function. |
| 162 | * |
| 163 | * module_name and submodule_name are alternatives - only one of the |
| 164 | * |
| 165 | * @param[in] ctx libyang context where to work. |
| 166 | * @param[in] name Name of the (sub)module to load. |
| 167 | * @param[in] revision Optional revision of the (sub)module to load, if NULL the newest revision is being loaded. |
| 168 | * @param[in] implement Flag if the (sub)module is supposed to be marked as implemented. |
| 169 | * @param[out] result Parsed YANG schema tree of the requested module. If it is a module, it is already in the context! |
| 170 | * @return LY_ERR value, in case of LY_SUCCESS, the \arg result is always provided. |
| 171 | */ |
| 172 | LY_ERR lys_module_localfile(struct ly_ctx *ctx, const char *name, const char *revision, int implement, struct lys_module **result); |
| 173 | /** |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 174 | * @brief Free the schema structure. It just frees, it does not remove the schema from its context. |
| 175 | * @param[in,out] module Schema module structure to free. |
| 176 | * @param[in] private_destructor Function to remove private data from the compiled schema tree. |
| 177 | */ |
| 178 | void lys_module_free(struct lys_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv)); |
| 179 | |
| 180 | /** |
| 181 | * @brief |
| 182 | */ |
| 183 | LY_ERR yang_parse(struct ly_ctx *ctx, const char *data, struct lysp_module **mod_p); |
| 184 | |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 185 | #endif /* LY_TREE_SCHEMA_INTERNAL_H_ */ |