Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file printer_schema.h |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Schema printers for libyang |
| 5 | * |
| 6 | * Copyright (c) 2015-2019 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_PRINTER_SCHEMA_H_ |
| 16 | #define LY_PRINTER_SCHEMA_H_ |
| 17 | |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 18 | #include <stdint.h> |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 19 | #include <stdio.h> |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 20 | |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 21 | #include "log.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 22 | #include "out.h" |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 23 | |
| 24 | #ifdef __cplusplus |
| 25 | extern "C" { |
| 26 | #endif |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 27 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 28 | struct ly_out; |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 29 | struct lys_module; |
| 30 | struct lysc_node; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 31 | struct lysp_submodule; |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 32 | |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 33 | /** |
| 34 | * @page howtoSchemaPrinters Module Printers |
| 35 | * |
| 36 | * Schema printers allows to serialize internal representations of a schema module in a specific format. libyang |
| 37 | * supports the following schema formats for printing: |
| 38 | * |
| 39 | * - YANG |
| 40 | * |
| 41 | * Basic YANG schemas format described in [RFC 6020](http://tools.ietf.org/html/rfc6020) and |
| 42 | * [RFC 7951](http://tools.ietf.org/html/rfc7951) (so both YANG 1.0 and YANG 1.1 versions are supported). |
| 43 | * |
| 44 | * - YANG compiled |
| 45 | * |
| 46 | * Syntactically, this format is based on standard YANG format. In contrast to standard YANG format, YANG compiled format |
| 47 | * represents the module how it is used by libyang - with all uses expanded, augments and deviations applied, etc. |
| 48 | * (more details about the compiled modules can be found on @ref howtoContext page). |
| 49 | * |
| 50 | * - YIN |
| 51 | * |
| 52 | * Alternative XML-based format to YANG - YANG Independent Notation. The details can be found in |
| 53 | * [RFC 6020](http://tools.ietf.org/html/rfc6020#section-11) and |
| 54 | * [RFC 7951](http://tools.ietf.org/html/rfc7951#section-13). |
| 55 | * |
| 56 | * - Tree Diagram |
| 57 | * |
| 58 | * Simple tree diagram providing overview of the module. The details can be found in |
| 59 | * [RFC 8340](https://tools.ietf.org/html/rfc8340). |
| 60 | * |
| 61 | * For simpler transition from libyang 1.x (and for some simple use cases), there are functions (::lys_print_clb(), |
| 62 | * ::lys_print_fd(), ::lys_print_file() and ::lys_print_mem()) to print the complete module into the specified output. But note, |
| 63 | * that these functions are limited to print only the complete module. |
| 64 | * |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 65 | * The full functionality of the schema printers is available via functions using [output handler](@ref howtoOutput). Besides |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 66 | * the ::lys_print_module() function to print the complete module, there are functions to print a submodule |
| 67 | * (::lys_print_submodule()) or a subtree (::lys_print_node()). Note that these functions might not support all the output |
| 68 | * formats mentioned above. |
| 69 | * |
| 70 | * Functions List |
| 71 | * -------------- |
| 72 | * - ::lys_print_module() |
| 73 | * - ::lys_print_submodule() |
| 74 | * - ::lys_print_node() |
| 75 | * |
| 76 | * - ::lys_print_clb() |
| 77 | * - ::lys_print_fd() |
| 78 | * - ::lys_print_file() |
| 79 | * - ::lys_print_mem() |
| 80 | * - ::lys_print_path() |
| 81 | */ |
| 82 | |
Radek Krejci | 2ff0d57 | 2020-05-21 15:27:28 +0200 | [diff] [blame] | 83 | /** |
| 84 | * @addtogroup schematree |
| 85 | * @{ |
| 86 | */ |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * @defgroup schemaprinterflags Schema output options |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 90 | * |
| 91 | * Options to change default behavior of the schema printers. |
| 92 | * |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 93 | * @{ |
| 94 | */ |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 95 | #define LYS_PRINT_SHRINK LY_PRINT_SHRINK /**< Flag for output without indentation and formatting new lines. */ |
| 96 | #define LYS_PRINT_NO_SUBSTMT 0x10 /**< Print only top-level/referede node information, |
| 97 | do not print information from the substatements */ |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 98 | // #define LYS_PRINT_TREE_RFC 0x01 /**< Conform to the RFC8340 tree output (only for tree format) */ |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 99 | // #define LYS_PRINT_TREE_GROUPING 0x02 /**< Print groupings separately (only for tree format) */ |
| 100 | // #define LYS_PRINT_TREE_USES 0x04 /**< Print only uses instead the resolved grouping nodes (only for tree format) */ |
| 101 | // #define LYS_PRINT_TREE_NO_LEAFREF 0x08 /**< Do not print the target of leafrefs (only for tree format) */ |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 102 | |
Radek Krejci | 2ff0d57 | 2020-05-21 15:27:28 +0200 | [diff] [blame] | 103 | /** @} schemaprinterflags */ |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 104 | |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 105 | /** |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 106 | * @brief Schema output formats accepted by libyang [printer functions](@ref howtoSchemaPrinters). |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 107 | */ |
| 108 | typedef enum { |
| 109 | LYS_OUT_UNKNOWN = 0, /**< unknown format, used as return value in case of error */ |
| 110 | LYS_OUT_YANG = 1, /**< YANG schema output format */ |
| 111 | LYS_OUT_YANG_COMPILED = 2, /**< YANG schema output format of the compiled schema tree */ |
| 112 | LYS_OUT_YIN = 3, /**< YIN schema output format */ |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 113 | LYS_OUT_TREE /**< Tree schema output format */ |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 114 | } LYS_OUTFORMAT; |
| 115 | |
| 116 | /** |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 117 | * @brief Schema module printer. |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 118 | * |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 119 | * @param[in] out Printer handler for a specific output. Use ly_out_*() functions to create and free the handler. |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 120 | * @param[in] module Main module with the parsed schema to print. |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 121 | * @param[in] format Output format. |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 122 | * @param[in] line_length Maximum characters to be printed on a line, 0 for unlimited. Only for #LYS_OUT_TREE printer. |
| 123 | * @param[in] options Schema output options (see @ref schemaprinterflags). |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 124 | * @return LY_ERR value. |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 125 | */ |
Michal Vasko | 7997d5a | 2021-02-22 10:55:56 +0100 | [diff] [blame] | 126 | LY_ERR lys_print_module(struct ly_out *out, const struct lys_module *module, LYS_OUTFORMAT format, size_t line_length, |
| 127 | uint32_t options); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 128 | |
| 129 | /** |
| 130 | * @brief Schema submodule printer. |
| 131 | * |
| 132 | * @param[in] out Printer handler for a specific output. Use ly_out_*() functions to create and free the handler. |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 133 | * @param[in] submodule Parsed submodule to print. |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 134 | * @param[in] format Output format (LYS_OUT_YANG_COMPILED is not supported). |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 135 | * @param[in] line_length Maximum characters to be printed on a line, 0 for unlimited. Only for #LYS_OUT_TREE printer. |
| 136 | * @param[in] options Schema output options (see @ref schemaprinterflags). |
| 137 | * @return LY_ERR value. |
| 138 | */ |
Michal Vasko | 7997d5a | 2021-02-22 10:55:56 +0100 | [diff] [blame] | 139 | LY_ERR lys_print_submodule(struct ly_out *out, const struct lysp_submodule *submodule, LYS_OUTFORMAT format, |
| 140 | size_t line_length, uint32_t options); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 141 | |
| 142 | /** |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 143 | * @brief Print schema tree in the specified format into a memory block. |
| 144 | * It is up to caller to free the returned string by free(). |
| 145 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 146 | * This is just a wrapper around ::lys_print_module() for simple use cases. |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 147 | * In case of a complex use cases, use lys_print with ly_out output handler. |
| 148 | * |
| 149 | * @param[out] strp Pointer to store the resulting dump. |
| 150 | * @param[in] module Schema tree to print. |
| 151 | * @param[in] format Schema output format. |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 152 | * @param[in] options Schema output options (see @ref schemaprinterflags). |
| 153 | * @return LY_ERR value. |
| 154 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 155 | LY_ERR lys_print_mem(char **strp, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 156 | |
| 157 | /** |
| 158 | * @brief Print schema tree in the specified format into a file descriptor. |
| 159 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 160 | * This is just a wrapper around ::lys_print_module() for simple use cases. |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 161 | * In case of a complex use cases, use lys_print with ly_out output handler. |
| 162 | * |
| 163 | * @param[in] fd File descriptor where to print the data. |
| 164 | * @param[in] module Schema tree to print. |
| 165 | * @param[in] format Schema output format. |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 166 | * @param[in] options Schema output options (see @ref schemaprinterflags). |
| 167 | * @return LY_ERR value. |
| 168 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 169 | LY_ERR lys_print_fd(int fd, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 170 | |
| 171 | /** |
| 172 | * @brief Print schema tree in the specified format into a file stream. |
| 173 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 174 | * This is just a wrapper around ::lys_print_module() for simple use cases. |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 175 | * In case of a complex use cases, use lys_print with ly_out output handler. |
| 176 | * |
| 177 | * @param[in] module Schema tree to print. |
| 178 | * @param[in] f File stream where to print the schema. |
| 179 | * @param[in] format Schema output format. |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 180 | * @param[in] options Schema output options (see @ref schemaprinterflags). |
| 181 | * @return LY_ERR value. |
| 182 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 183 | LY_ERR lys_print_file(FILE *f, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 184 | |
| 185 | /** |
| 186 | * @brief Print schema tree in the specified format into a file. |
| 187 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 188 | * This is just a wrapper around ::lys_print_module() for simple use cases. |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 189 | * In case of a complex use cases, use lys_print with ly_out output handler. |
| 190 | * |
| 191 | * @param[in] path File where to print the schema. |
| 192 | * @param[in] module Schema tree to print. |
| 193 | * @param[in] format Schema output format. |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 194 | * @param[in] options Schema output options (see @ref schemaprinterflags). |
| 195 | * @return LY_ERR value. |
| 196 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 197 | LY_ERR lys_print_path(const char *path, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 198 | |
| 199 | /** |
| 200 | * @brief Print schema tree in the specified format using a provided callback. |
| 201 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 202 | * This is just a wrapper around ::lys_print_module() for simple use cases. |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 203 | * In case of a complex use cases, use lys_print with ly_out output handler. |
| 204 | * |
| 205 | * @param[in] module Schema tree to print. |
| 206 | * @param[in] writeclb Callback function to write the data (see write(1)). |
Michal Vasko | ce2b874 | 2020-08-24 13:20:25 +0200 | [diff] [blame] | 207 | * @param[in] user_data Optional caller-specific argument to be passed to the \p writeclb callback. |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 208 | * @param[in] format Schema output format. |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 209 | * @param[in] options Schema output options (see @ref schemaprinterflags). |
| 210 | * @return LY_ERR value. |
| 211 | */ |
Michal Vasko | ce2b874 | 2020-08-24 13:20:25 +0200 | [diff] [blame] | 212 | LY_ERR lys_print_clb(ly_write_clb writeclb, void *user_data, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 213 | const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 214 | |
| 215 | /** |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 216 | * @brief Schema node printer. |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 217 | * |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 218 | * @param[in] out Printer handler for a specific output. Use ly_out_*() functions to create and free the handler. |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 219 | * @param[in] node Schema node to print. |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 220 | * @param[in] format Output format. |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 221 | * @param[in] line_length Maximum characters to be printed on a line, 0 for unlimited. Only for #LYS_OUT_TREE printer. |
| 222 | * @param[in] options Schema output options (see @ref schemaprinterflags). |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 223 | * @return LY_ERR value. |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 224 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 225 | LY_ERR lys_print_node(struct ly_out *out, const struct lysc_node *node, LYS_OUTFORMAT format, size_t line_length, uint32_t options); |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 226 | |
Radek Krejci | 2ff0d57 | 2020-05-21 15:27:28 +0200 | [diff] [blame] | 227 | /** @} schematree */ |
| 228 | |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 229 | #ifdef __cplusplus |
| 230 | } |
| 231 | #endif |
| 232 | |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 233 | #endif /* LY_PRINTER_SCHEMA_H_ */ |