blob: 9ca5e7ae41abb9e44024f557edac36ae85b15583 [file] [log] [blame]
Radek Krejcid3ca0632019-04-16 16:54:54 +02001/**
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 Krejcica376bd2020-06-11 16:04:06 +020018#include <stdio.h>
Radek Krejcid3ca0632019-04-16 16:54:54 +020019#include <unistd.h>
20
Radek Krejcica376bd2020-06-11 16:04:06 +020021#include "log.h"
Michal Vaskoce2b8742020-08-24 13:20:25 +020022#include "printer.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020023
24#ifdef __cplusplus
25extern "C" {
26#endif
Radek Krejcie7b95092019-05-15 11:03:07 +020027
Radek Krejci535ea9f2020-05-29 16:01:05 +020028struct ly_out;
Radek Krejcica376bd2020-06-11 16:04:06 +020029struct lys_module;
30struct lysc_node;
Michal Vasko7c8439f2020-08-05 13:25:19 +020031struct lysp_submodule;
Radek Krejci535ea9f2020-05-29 16:01:05 +020032
Radek Krejci8678fa42020-08-18 16:07:28 +020033
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 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 */
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 Krejcid8c0f5e2019-11-17 12:18:34 +0800103
Radek Krejci2ff0d572020-05-21 15:27:28 +0200104/** @} schemaprinterflags */
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800105
Radek Krejcid3ca0632019-04-16 16:54:54 +0200106/**
Radek Krejci8678fa42020-08-18 16:07:28 +0200107 * @brief Schema output formats accepted by libyang [printer functions](@ref howtoSchemaPrinters).
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200108 */
109typedef 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 Krejci8678fa42020-08-18 16:07:28 +0200114 LYS_OUT_TREE /**< Tree schema output format */
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200115} LYS_OUTFORMAT;
116
117/**
Radek Krejcia5bba312020-01-09 15:41:20 +0100118 * @brief Schema module printer.
Radek Krejcid3ca0632019-04-16 16:54:54 +0200119 *
Radek Krejci241f6b52020-05-21 18:13:49 +0200120 * @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 +0200121 * @param[in] module Main module with the parsed schema to print.
Radek Krejcia5bba312020-01-09 15:41:20 +0100122 * @param[in] format Output format.
Radek Krejcid3ca0632019-04-16 16:54:54 +0200123 * @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 Vasko63f3d842020-07-08 10:10:14 +0200125 * @return LY_ERR value.
Radek Krejcid3ca0632019-04-16 16:54:54 +0200126 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200127LY_ERR lys_print_module(struct ly_out *out, const struct lys_module *module, LYS_OUTFORMAT format, size_t line_length, uint32_t options);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200128
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 Krejci8678fa42020-08-18 16:07:28 +0200135 * @param[in] format Output format (LYS_OUT_YANG_COMPILED is not supported).
Michal Vasko7c8439f2020-08-05 13:25:19 +0200136 * @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 */
140LY_ERR lys_print_submodule(struct ly_out *out, const struct lys_module *module, const struct lysp_submodule *submodule,
Radek Krejci1deb5be2020-08-26 16:43:36 +0200141 LYS_OUTFORMAT format, size_t line_length, uint32_t options);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200142
143/**
Radek Krejci26da5222020-06-09 17:43:17 +0200144 * @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 Krejci8678fa42020-08-18 16:07:28 +0200147 * This is just a wrapper around ::lys_print_module() for simple use cases.
Radek Krejci26da5222020-06-09 17:43:17 +0200148 * 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 Krejci26da5222020-06-09 17:43:17 +0200153 * @param[in] options Schema output options (see @ref schemaprinterflags).
154 * @return LY_ERR value.
155 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200156LY_ERR lys_print_mem(char **strp, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options);
Radek Krejci26da5222020-06-09 17:43:17 +0200157
158/**
159 * @brief Print schema tree in the specified format into a file descriptor.
160 *
Radek Krejci8678fa42020-08-18 16:07:28 +0200161 * This is just a wrapper around ::lys_print_module() for simple use cases.
Radek Krejci26da5222020-06-09 17:43:17 +0200162 * 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 Krejci26da5222020-06-09 17:43:17 +0200167 * @param[in] options Schema output options (see @ref schemaprinterflags).
168 * @return LY_ERR value.
169 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200170LY_ERR lys_print_fd(int fd, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options);
Radek Krejci26da5222020-06-09 17:43:17 +0200171
172/**
173 * @brief Print schema tree in the specified format into a file stream.
174 *
Radek Krejci8678fa42020-08-18 16:07:28 +0200175 * This is just a wrapper around ::lys_print_module() for simple use cases.
Radek Krejci26da5222020-06-09 17:43:17 +0200176 * 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 Krejci26da5222020-06-09 17:43:17 +0200181 * @param[in] options Schema output options (see @ref schemaprinterflags).
182 * @return LY_ERR value.
183 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200184LY_ERR lys_print_file(FILE *f, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options);
Radek Krejci26da5222020-06-09 17:43:17 +0200185
186/**
187 * @brief Print schema tree in the specified format into a file.
188 *
Radek Krejci8678fa42020-08-18 16:07:28 +0200189 * This is just a wrapper around ::lys_print_module() for simple use cases.
Radek Krejci26da5222020-06-09 17:43:17 +0200190 * 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 Krejci26da5222020-06-09 17:43:17 +0200195 * @param[in] options Schema output options (see @ref schemaprinterflags).
196 * @return LY_ERR value.
197 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200198LY_ERR lys_print_path(const char *path, const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options);
Radek Krejci26da5222020-06-09 17:43:17 +0200199
200/**
201 * @brief Print schema tree in the specified format using a provided callback.
202 *
Radek Krejci8678fa42020-08-18 16:07:28 +0200203 * This is just a wrapper around ::lys_print_module() for simple use cases.
Radek Krejci26da5222020-06-09 17:43:17 +0200204 * 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 Vaskoce2b8742020-08-24 13:20:25 +0200208 * @param[in] user_data Optional caller-specific argument to be passed to the \p writeclb callback.
Radek Krejci26da5222020-06-09 17:43:17 +0200209 * @param[in] format Schema output format.
Radek Krejci26da5222020-06-09 17:43:17 +0200210 * @param[in] options Schema output options (see @ref schemaprinterflags).
211 * @return LY_ERR value.
212 */
Michal Vaskoce2b8742020-08-24 13:20:25 +0200213LY_ERR lys_print_clb(ly_write_clb writeclb, void *user_data,
Radek Krejci1deb5be2020-08-26 16:43:36 +0200214 const struct lys_module *module, LYS_OUTFORMAT format, uint32_t options);
Radek Krejci26da5222020-06-09 17:43:17 +0200215
216/**
Radek Krejcia5bba312020-01-09 15:41:20 +0100217 * @brief Schema node printer.
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800218 *
Radek Krejci241f6b52020-05-21 18:13:49 +0200219 * @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 +0200220 * @param[in] node Schema node to print.
Radek Krejcia5bba312020-01-09 15:41:20 +0100221 * @param[in] format Output format.
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800222 * @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 Vasko63f3d842020-07-08 10:10:14 +0200224 * @return LY_ERR value.
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800225 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200226LY_ERR lys_print_node(struct ly_out *out, const struct lysc_node *node, LYS_OUTFORMAT format, size_t line_length, uint32_t options);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800227
Radek Krejci2ff0d572020-05-21 15:27:28 +0200228/** @} schematree */
229
Radek Krejcica376bd2020-06-11 16:04:06 +0200230#ifdef __cplusplus
231}
232#endif
233
Radek Krejcid3ca0632019-04-16 16:54:54 +0200234#endif /* LY_PRINTER_SCHEMA_H_ */