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