blob: 471d38e4b3541745162596c9011ba8bf8a9ce818 [file] [log] [blame]
Radek Krejcid3ca0632019-04-16 16:54:54 +02001/**
2 * @file printer_schema.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko09abf882022-12-14 12:32:48 +01004 * @author Michal Vasko <mvasko@cesnet.cz>
Radek Krejcid3ca0632019-04-16 16:54:54 +02005 * @brief Schema printers for libyang
6 *
Michal Vasko09abf882022-12-14 12:32:48 +01007 * Copyright (c) 2015-2022 CESNET, z.s.p.o.
Radek Krejcid3ca0632019-04-16 16:54:54 +02008 *
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 Krejci47fab892020-11-05 17:02:41 +010019#include <stdint.h>
Radek Krejcica376bd2020-06-11 16:04:06 +020020#include <stdio.h>
Radek Krejcid3ca0632019-04-16 16:54:54 +020021
Radek Krejcica376bd2020-06-11 16:04:06 +020022#include "log.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020023#include "out.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020024
25#ifdef __cplusplus
26extern "C" {
27#endif
Radek Krejcie7b95092019-05-15 11:03:07 +020028
Radek Krejci535ea9f2020-05-29 16:01:05 +020029struct ly_out;
Radek Krejcica376bd2020-06-11 16:04:06 +020030struct lys_module;
31struct lysc_node;
Michal Vasko7c8439f2020-08-05 13:25:19 +020032struct lysp_submodule;
Radek Krejci535ea9f2020-05-29 16:01:05 +020033
Radek Krejci8678fa42020-08-18 16:07:28 +020034/**
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 Vaskoafac7822020-10-20 14:22:26 +020066 * The full functionality of the schema printers is available via functions using [output handler](@ref howtoOutput). Besides
Radek Krejci8678fa42020-08-18 16:07:28 +020067 * 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 Krejci2ff0d572020-05-21 15:27:28 +020084/**
85 * @addtogroup schematree
86 * @{
87 */
Radek Krejcid8c0f5e2019-11-17 12:18:34 +080088
89/**
90 * @defgroup schemaprinterflags Schema output options
Radek Krejci8678fa42020-08-18 16:07:28 +020091 *
92 * Options to change default behavior of the schema printers.
93 *
Radek Krejcid8c0f5e2019-11-17 12:18:34 +080094 * @{
95 */
Radek Krejci52f65552020-09-01 17:03:35 +020096#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 Krejcid8c0f5e2019-11-17 12:18:34 +080099
Radek Krejci2ff0d572020-05-21 15:27:28 +0200100/** @} schemaprinterflags */
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800101
Radek Krejcid3ca0632019-04-16 16:54:54 +0200102/**
Radek Krejci8678fa42020-08-18 16:07:28 +0200103 * @brief Schema output formats accepted by libyang [printer functions](@ref howtoSchemaPrinters).
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200104 */
105typedef 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 Krejci8678fa42020-08-18 16:07:28 +0200110 LYS_OUT_TREE /**< Tree schema output format */
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200111} LYS_OUTFORMAT;
112
113/**
Radek Krejcia5bba312020-01-09 15:41:20 +0100114 * @brief Schema module printer.
Radek Krejcid3ca0632019-04-16 16:54:54 +0200115 *
Radek Krejci241f6b52020-05-21 18:13:49 +0200116 * @param[in] out Printer handler for a specific output. Use ly_out_*() functions to create and free the handler.
Michal Vasko7c8439f2020-08-05 13:25:19 +0200117 * @param[in] module Main module with the parsed schema to print.
Radek Krejcia5bba312020-01-09 15:41:20 +0100118 * @param[in] format Output format.
Radek Krejcid3ca0632019-04-16 16:54:54 +0200119 * @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 Vasko63f3d842020-07-08 10:10:14 +0200121 * @return LY_ERR value.
Radek Krejcid3ca0632019-04-16 16:54:54 +0200122 */
Michal Vasko09abf882022-12-14 12:32:48 +0100123LIBYANG_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 Vasko7c8439f2020-08-05 13:25:19 +0200125
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 Vasko7c8439f2020-08-05 13:25:19 +0200130 * @param[in] submodule Parsed submodule to print.
Radek Krejci8678fa42020-08-18 16:07:28 +0200131 * @param[in] format Output format (LYS_OUT_YANG_COMPILED is not supported).
Michal Vasko7c8439f2020-08-05 13:25:19 +0200132 * @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átc53a7ec2021-12-09 16:01:19 +0100136LIBYANG_API_DECL LY_ERR lys_print_submodule(struct ly_out *out, const struct lysp_submodule *submodule, LYS_OUTFORMAT format,
Michal Vasko7997d5a2021-02-22 10:55:56 +0100137 size_t line_length, uint32_t options);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200138
139/**
Radek Krejci26da5222020-06-09 17:43:17 +0200140 * @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 Krejci8678fa42020-08-18 16:07:28 +0200143 * This is just a wrapper around ::lys_print_module() for simple use cases.
Radek Krejci26da5222020-06-09 17:43:17 +0200144 * 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 Krejci26da5222020-06-09 17:43:17 +0200149 * @param[in] options Schema output options (see @ref schemaprinterflags).
150 * @return LY_ERR value.
151 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100152LIBYANG_API_DECL LY_ERR lys_print_mem(char **strp, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options);
Radek Krejci26da5222020-06-09 17:43:17 +0200153
154/**
155 * @brief Print schema tree in the specified format into a file descriptor.
156 *
Radek Krejci8678fa42020-08-18 16:07:28 +0200157 * This is just a wrapper around ::lys_print_module() for simple use cases.
Radek Krejci26da5222020-06-09 17:43:17 +0200158 * 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 Krejci26da5222020-06-09 17:43:17 +0200163 * @param[in] options Schema output options (see @ref schemaprinterflags).
164 * @return LY_ERR value.
165 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100166LIBYANG_API_DECL LY_ERR lys_print_fd(int fd, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options);
Radek Krejci26da5222020-06-09 17:43:17 +0200167
168/**
169 * @brief Print schema tree in the specified format into a file stream.
170 *
Radek Krejci8678fa42020-08-18 16:07:28 +0200171 * This is just a wrapper around ::lys_print_module() for simple use cases.
Radek Krejci26da5222020-06-09 17:43:17 +0200172 * 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 Krejci26da5222020-06-09 17:43:17 +0200177 * @param[in] options Schema output options (see @ref schemaprinterflags).
178 * @return LY_ERR value.
179 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100180LIBYANG_API_DECL LY_ERR lys_print_file(FILE *f, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options);
Radek Krejci26da5222020-06-09 17:43:17 +0200181
182/**
183 * @brief Print schema tree in the specified format into a file.
184 *
Radek Krejci8678fa42020-08-18 16:07:28 +0200185 * This is just a wrapper around ::lys_print_module() for simple use cases.
Radek Krejci26da5222020-06-09 17:43:17 +0200186 * 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 Krejci26da5222020-06-09 17:43:17 +0200191 * @param[in] options Schema output options (see @ref schemaprinterflags).
192 * @return LY_ERR value.
193 */
Michal Vasko09abf882022-12-14 12:32:48 +0100194LIBYANG_API_DECL LY_ERR lys_print_path(const char *path, const struct lys_module *module, LYS_OUTFORMAT format,
195 uint32_t options);
Radek Krejci26da5222020-06-09 17:43:17 +0200196
197/**
198 * @brief Print schema tree in the specified format using a provided callback.
199 *
Radek Krejci8678fa42020-08-18 16:07:28 +0200200 * This is just a wrapper around ::lys_print_module() for simple use cases.
Radek Krejci26da5222020-06-09 17:43:17 +0200201 * 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 Vaskoce2b8742020-08-24 13:20:25 +0200205 * @param[in] user_data Optional caller-specific argument to be passed to the \p writeclb callback.
Radek Krejci26da5222020-06-09 17:43:17 +0200206 * @param[in] format Schema output format.
Radek Krejci26da5222020-06-09 17:43:17 +0200207 * @param[in] options Schema output options (see @ref schemaprinterflags).
208 * @return LY_ERR value.
209 */
Michal Vasko09abf882022-12-14 12:32:48 +0100210LIBYANG_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 Krejci26da5222020-06-09 17:43:17 +0200212
213/**
Radek Krejcia5bba312020-01-09 15:41:20 +0100214 * @brief Schema node printer.
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800215 *
Radek Krejci241f6b52020-05-21 18:13:49 +0200216 * @param[in] out Printer handler for a specific output. Use ly_out_*() functions to create and free the handler.
Radek Krejci8678fa42020-08-18 16:07:28 +0200217 * @param[in] node Schema node to print.
Radek Krejcia5bba312020-01-09 15:41:20 +0100218 * @param[in] format Output format.
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800219 * @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 Vasko63f3d842020-07-08 10:10:14 +0200221 * @return LY_ERR value.
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800222 */
Michal Vasko09abf882022-12-14 12:32:48 +0100223LIBYANG_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 Krejcid8c0f5e2019-11-17 12:18:34 +0800225
Radek Krejci2ff0d572020-05-21 15:27:28 +0200226/** @} schematree */
227
Radek Krejcica376bd2020-06-11 16:04:06 +0200228#ifdef __cplusplus
229}
230#endif
231
Radek Krejcid3ca0632019-04-16 16:54:54 +0200232#endif /* LY_PRINTER_SCHEMA_H_ */