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 | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 18 | #include <stdio.h> |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 19 | #include <unistd.h> |
| 20 | |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 21 | #include "log.h" |
Michal Vasko | ce2b874 | 2020-08-24 13:20:25 +0200 | [diff] [blame] | 22 | #include "printer.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 | /** |
| 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 | * |
| 66 | * The full functionality of the schema printers is available via functions using [output handler](@ref howtoPrinters). Besides |
| 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 */ |
| 99 | // #define LYS_PRINT_TREE_RFC 0x01 /**< Conform to the RFC TODO tree output (only for tree format) */ |
| 100 | // #define LYS_PRINT_TREE_GROUPING 0x02 /**< Print groupings separately (only for tree format) */ |
| 101 | // #define LYS_PRINT_TREE_USES 0x04 /**< Print only uses instead the resolved grouping nodes (only for tree format) */ |
| 102 | // #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] | 103 | |
Radek Krejci | 2ff0d57 | 2020-05-21 15:27:28 +0200 | [diff] [blame] | 104 | /** @} schemaprinterflags */ |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 105 | |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 106 | /** |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame^] | 107 | * @brief Schema output formats accepted by libyang [printer functions](@ref howtoSchemaPrinters). |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 108 | */ |
| 109 | typedef enum { |
| 110 | LYS_OUT_UNKNOWN = 0, /**< unknown format, used as return value in case of error */ |
| 111 | LYS_OUT_YANG = 1, /**< YANG schema output format */ |
| 112 | LYS_OUT_YANG_COMPILED = 2, /**< YANG schema output format of the compiled schema tree */ |
| 113 | LYS_OUT_YIN = 3, /**< YIN schema output format */ |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame^] | 114 | LYS_OUT_TREE /**< Tree schema output format */ |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 115 | } LYS_OUTFORMAT; |
| 116 | |
| 117 | /** |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 118 | * @brief Schema module printer. |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 119 | * |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 120 | * @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] | 121 | * @param[in] module Main module with the parsed schema to print. |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 122 | * @param[in] format Output format. |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 123 | * @param[in] line_length Maximum characters to be printed on a line, 0 for unlimited. Only for #LYS_OUT_TREE printer. |
| 124 | * @param[in] options Schema output options (see @ref schemaprinterflags). |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 125 | * @return LY_ERR value. |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 126 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 127 | LY_ERR lys_print_module(struct ly_out *out, const struct lys_module *module, LYS_OUTFORMAT format, size_t line_length, 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. |
| 133 | * @param[in] module Main module of the submodule to print. |
| 134 | * @param[in] submodule Parsed submodule to print. |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame^] | 135 | * @param[in] format Output format (LYS_OUT_YANG_COMPILED is not supported). |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 136 | * @param[in] line_length Maximum characters to be printed on a line, 0 for unlimited. Only for #LYS_OUT_TREE printer. |
| 137 | * @param[in] options Schema output options (see @ref schemaprinterflags). |
| 138 | * @return LY_ERR value. |
| 139 | */ |
| 140 | LY_ERR lys_print_submodule(struct ly_out *out, const struct lys_module *module, const struct lysp_submodule *submodule, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 141 | LYS_OUTFORMAT format, size_t line_length, uint32_t options); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 142 | |
| 143 | /** |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 144 | * @brief Print schema tree in the specified format into a memory block. |
| 145 | * It is up to caller to free the returned string by free(). |
| 146 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame^] | 147 | * 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] | 148 | * In case of a complex use cases, use lys_print with ly_out output handler. |
| 149 | * |
| 150 | * @param[out] strp Pointer to store the resulting dump. |
| 151 | * @param[in] module Schema tree to print. |
| 152 | * @param[in] format Schema output format. |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 153 | * @param[in] options Schema output options (see @ref schemaprinterflags). |
| 154 | * @return LY_ERR value. |
| 155 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 156 | 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] | 157 | |
| 158 | /** |
| 159 | * @brief Print schema tree in the specified format into a file descriptor. |
| 160 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame^] | 161 | * 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] | 162 | * In case of a complex use cases, use lys_print with ly_out output handler. |
| 163 | * |
| 164 | * @param[in] fd File descriptor where to print the data. |
| 165 | * @param[in] module Schema tree to print. |
| 166 | * @param[in] format Schema output format. |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 167 | * @param[in] options Schema output options (see @ref schemaprinterflags). |
| 168 | * @return LY_ERR value. |
| 169 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 170 | 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] | 171 | |
| 172 | /** |
| 173 | * @brief Print schema tree in the specified format into a file stream. |
| 174 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame^] | 175 | * 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] | 176 | * In case of a complex use cases, use lys_print with ly_out output handler. |
| 177 | * |
| 178 | * @param[in] module Schema tree to print. |
| 179 | * @param[in] f File stream where to print the schema. |
| 180 | * @param[in] format Schema output format. |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 181 | * @param[in] options Schema output options (see @ref schemaprinterflags). |
| 182 | * @return LY_ERR value. |
| 183 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 184 | 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] | 185 | |
| 186 | /** |
| 187 | * @brief Print schema tree in the specified format into a file. |
| 188 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame^] | 189 | * 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] | 190 | * In case of a complex use cases, use lys_print with ly_out output handler. |
| 191 | * |
| 192 | * @param[in] path File where to print the schema. |
| 193 | * @param[in] module Schema tree to print. |
| 194 | * @param[in] format Schema output format. |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 195 | * @param[in] options Schema output options (see @ref schemaprinterflags). |
| 196 | * @return LY_ERR value. |
| 197 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 198 | 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] | 199 | |
| 200 | /** |
| 201 | * @brief Print schema tree in the specified format using a provided callback. |
| 202 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame^] | 203 | * 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] | 204 | * In case of a complex use cases, use lys_print with ly_out output handler. |
| 205 | * |
| 206 | * @param[in] module Schema tree to print. |
| 207 | * @param[in] writeclb Callback function to write the data (see write(1)). |
Michal Vasko | ce2b874 | 2020-08-24 13:20:25 +0200 | [diff] [blame] | 208 | * @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] | 209 | * @param[in] format Schema output format. |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 210 | * @param[in] options Schema output options (see @ref schemaprinterflags). |
| 211 | * @return LY_ERR value. |
| 212 | */ |
Michal Vasko | ce2b874 | 2020-08-24 13:20:25 +0200 | [diff] [blame] | 213 | LY_ERR lys_print_clb(ly_write_clb writeclb, void *user_data, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 214 | const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 215 | |
| 216 | /** |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 217 | * @brief Schema node printer. |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 218 | * |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 219 | * @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^] | 220 | * @param[in] node Schema node to print. |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 221 | * @param[in] format Output format. |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 222 | * @param[in] line_length Maximum characters to be printed on a line, 0 for unlimited. Only for #LYS_OUT_TREE printer. |
| 223 | * @param[in] options Schema output options (see @ref schemaprinterflags). |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 224 | * @return LY_ERR value. |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 225 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 226 | 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] | 227 | |
Radek Krejci | 2ff0d57 | 2020-05-21 15:27:28 +0200 | [diff] [blame] | 228 | /** @} schematree */ |
| 229 | |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 230 | #ifdef __cplusplus |
| 231 | } |
| 232 | #endif |
| 233 | |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 234 | #endif /* LY_PRINTER_SCHEMA_H_ */ |