blob: c835567d3d0db0da533df1ac9bc6baaa59c49b69 [file] [log] [blame]
Radek Krejcid3ca0632019-04-16 16:54:54 +02001/**
2 * @file printer_internal.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Internal structures and functions 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_INTERNAL_H_
16#define LY_PRINTER_INTERNAL_H_
17
Michal Vaskoafac7822020-10-20 14:22:26 +020018#include "out.h"
aPiecek03cb4872022-10-24 10:31:51 +020019#include "plugins_exts.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020020#include "printer_data.h"
Michal Vasko69730152020-10-09 16:30:07 +020021#include "printer_schema.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020022
Michal Vasko7c8439f2020-08-05 13:25:19 +020023struct lysp_module;
24struct lysp_submodule;
25
Radek Krejcie7b95092019-05-15 11:03:07 +020026/**
Radek Krejcif8d7f9a2021-03-10 14:32:36 +010027 * @brief Generic YANG schema printer context
28 *
29 * Note that the YANG extensions API provides getter to the members for the extension plugins.
30 */
31struct lyspr_ctx {
32 struct ly_out *out; /**< output specification */
33 uint16_t level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
Radek Krejciaa14a0c2020-09-04 11:16:47 +020034 uint16_t flags; /**< internal flags for use by printer */
Radek Krejcif8d7f9a2021-03-10 14:32:36 +010035 uint32_t options; /**< Schema output options (see @ref schemaprinterflags). */
36 const struct lys_module *module; /**< schema to print */
37};
38
39/**
aPiecek03cb4872022-10-24 10:31:51 +020040 * @brief YANG schema provided from plugin extension for printer_tree.
41 *
42 * The YANG extensions API provides setting functions.
43 */
44struct lyspr_tree_schema {
45 ly_bool compiled; /**< Flag if it is a compiled schema. */
46
47 union {
48 struct lysc_node *ctree; /**< Compiled schema. */
49 struct lysp_node *ptree; /**< Parsed schema. */
50 };
51 union {
52 lyplg_ext_sprinter_ctree_override_clb cn_overr; /**< Override clb function for compiled node. */
53 lyplg_ext_sprinter_ptree_override_clb pn_overr; /**< Override clb function for parsed node. */
54 };
55};
56
57/**
58 * @brief Context used between plugin extension and printer_tree.
59 *
60 * The YANG extensions API provides setting functions.
61 */
62struct lyspr_tree_ctx {
63 struct lyspr_tree_schema *schemas; /**< Parsed or compiled schemas ([sized array](@ref sizedarrays)) */
64 void *plugin_priv; /**< Private data from plugin which printer_tree does not use. */
65
66 void (*free_plugin_priv)(void *plugin_priv); /**< Release function for lyspr_tree_ctx.plugin_priv. */
67};
68
69/**
Michal Vasko7c8439f2020-08-05 13:25:19 +020070 * @brief YANG printer of the parsed module. Full YANG printer.
Radek Krejcie7b95092019-05-15 11:03:07 +020071 *
72 * @param[in] out Output specification.
Michal Vasko7c8439f2020-08-05 13:25:19 +020073 * @param[in] modp Parsed module to print.
Radek Krejci5536d282020-08-04 23:27:44 +020074 * @param[in] options Schema output options (see @ref schemaprinterflags).
Radek Krejci8678fa42020-08-18 16:07:28 +020075 * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed.
Radek Krejcid3ca0632019-04-16 16:54:54 +020076 */
Michal Vasko7997d5a2021-02-22 10:55:56 +010077LY_ERR yang_print_parsed_module(struct ly_out *out, const struct lysp_module *modp, uint32_t options);
Radek Krejci5536d282020-08-04 23:27:44 +020078
79/**
80 * @brief Helper macros for data printers
81 */
Michal Vasko61ad1ff2022-02-10 15:48:39 +010082#define DO_FORMAT (!(pctx->options & LY_PRINT_SHRINK))
83#define LEVEL pctx->level /**< current level */
Radek Krejci5536d282020-08-04 23:27:44 +020084#define INDENT (DO_FORMAT ? (LEVEL)*2 : 0),"" /**< indentation parameters for printer functions */
85#define LEVEL_INC LEVEL++ /**< increase indentation level */
86#define LEVEL_DEC LEVEL-- /**< decrease indentation level */
Michal Vasko7c8439f2020-08-05 13:25:19 +020087
Radek Krejcif13b87b2020-12-01 22:02:17 +010088#define XML_NS_INDENT 8
89
Michal Vasko7c8439f2020-08-05 13:25:19 +020090/**
91 * @brief YANG printer of the parsed submodule. Full YANG printer.
92 *
93 * @param[in] out Output specification.
Michal Vasko7c8439f2020-08-05 13:25:19 +020094 * @param[in] submodp Parsed submodule to print.
Radek Krejci5536d282020-08-04 23:27:44 +020095 * @param[in] options Schema output options (see @ref schemaprinterflags).
Radek Krejci8678fa42020-08-18 16:07:28 +020096 * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed.
Michal Vasko7c8439f2020-08-05 13:25:19 +020097 */
Michal Vasko7997d5a2021-02-22 10:55:56 +010098LY_ERR yang_print_parsed_submodule(struct ly_out *out, const struct lysp_submodule *submodp, uint32_t options);
Radek Krejcid3ca0632019-04-16 16:54:54 +020099
100/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200101 * @brief YANG printer of the compiled schemas.
102 *
103 * This printer provides information about modules how they are understood by libyang.
104 * Despite the format is inspired by YANG, it is not fully compatible and should not be
105 * used as a standard YANG format.
106 *
107 * @param[in] out Output specification.
108 * @param[in] module Schema to be printed (the compiled member is used).
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800109 * @param[in] options Schema output options (see @ref schemaprinterflags).
Radek Krejci8678fa42020-08-18 16:07:28 +0200110 * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed.
Radek Krejcid3ca0632019-04-16 16:54:54 +0200111 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200112LY_ERR yang_print_compiled(struct ly_out *out, const struct lys_module *module, uint32_t options);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800113
114/**
115 * @brief YANG printer of the compiled schema node
116 *
117 * This printer provides information about modules how they are understood by libyang.
118 * Despite the format is inspired by YANG, it is not fully compatible and should not be
119 * used as a standard YANG format.
120 *
121 * @param[in] out Output specification.
122 * @param[in] node Schema node to be printed including all its substatements.
123 * @param[in] options Schema output options (see @ref schemaprinterflags).
Radek Krejci8678fa42020-08-18 16:07:28 +0200124 * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed.
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800125 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200126LY_ERR yang_print_compiled_node(struct ly_out *out, const struct lysc_node *node, uint32_t options);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200127
Radek Krejcie7b95092019-05-15 11:03:07 +0200128/**
Michal Vasko7c8439f2020-08-05 13:25:19 +0200129 * @brief YIN printer of the parsed module. Full YIN printer.
FredGand944bdc2019-11-05 21:57:07 +0800130 *
131 * @param[in] out Output specification.
Michal Vasko7c8439f2020-08-05 13:25:19 +0200132 * @param[in] modp Parsed module to print.
Radek Krejci5536d282020-08-04 23:27:44 +0200133 * @param[in] options Schema output options (see @ref schemaprinterflags).
Radek Krejci8678fa42020-08-18 16:07:28 +0200134 * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed.
FredGand944bdc2019-11-05 21:57:07 +0800135 */
Michal Vasko7997d5a2021-02-22 10:55:56 +0100136LY_ERR yin_print_parsed_module(struct ly_out *out, const struct lysp_module *modp, uint32_t options);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200137
138/**
139 * @brief YIN printer of the parsed submodule. Full YIN printer.
140 *
141 * @param[in] out Output specification.
Michal Vasko7c8439f2020-08-05 13:25:19 +0200142 * @param[in] submodp Parsed submodule to print.
Radek Krejci5536d282020-08-04 23:27:44 +0200143 * @param[in] options Schema output options (see @ref schemaprinterflags).
Radek Krejci8678fa42020-08-18 16:07:28 +0200144 * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed.
Michal Vasko7c8439f2020-08-05 13:25:19 +0200145 */
Michal Vasko7997d5a2021-02-22 10:55:56 +0100146LY_ERR yin_print_parsed_submodule(struct ly_out *out, const struct lysp_submodule *submodp, uint32_t options);
FredGand944bdc2019-11-05 21:57:07 +0800147
148/**
aPiecek3f247652021-04-19 13:40:25 +0200149 * @brief Full YANG Tree Diagram printer.
150 *
151 * The module should be compiled and the @ref contextoptions must be set to LY_CTX_SET_PRIV_PARSED.
152 * If not, the printer will use parsed (unresolved) YANG schema tree, which means,
153 * for example, that `grouping` sections will be on the output.
aPiecek61d062b2020-11-02 11:05:09 +0100154 *
155 * @param[in] out Output specification.
156 * @param[in] module Main module.
157 * @param[in] options Schema output options (see @ref schemaprinterflags).
158 * @param[in] line_length Maximum characters to be printed on a line, 0 for unlimited. Only for ::LYS_OUT_TREE printer.
159 * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed.
160 */
aPiecek3f247652021-04-19 13:40:25 +0200161LY_ERR tree_print_module(struct ly_out *out, const struct lys_module *module, uint32_t options,
aPiecek61d062b2020-11-02 11:05:09 +0100162 size_t line_length);
163
164/**
aPiecek9f792e52021-04-21 08:33:56 +0200165 * @brief YANG Tree Diagram printer of the parsed submodule. Full Tree printer.
166 *
167 * @param[in] out Output specification.
168 * @param[in] submodp Parsed submodule to print.
169 * @param[in] options Schema output options (see @ref schemaprinterflags).
170 * @param[in] line_length Maximum characters to be printed on a line, 0 for unlimited. Only for ::LYS_OUT_TREE printer.
171 * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed.
172 */
173LY_ERR tree_print_parsed_submodule(struct ly_out *out, const struct lysp_submodule *submodp, uint32_t options,
174 size_t line_length);
175
176/**
177 * @brief YANG Tree Diagram printer of the compiled schema node.
aPiecek153b00f2021-04-20 13:52:57 +0200178 *
179 * @param[in] out Output specification.
180 * @param[in] node Schema node to be printed including all its substatements.
181 * @param[in] options Schema output options (see @ref schemaprinterflags).
182 * @param[in] line_length Maximum characters to be printed on a line, 0 for unlimited. Only for ::LYS_OUT_TREE printer.
183 * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed.
184 */
185LY_ERR tree_print_compiled_node(struct ly_out *out, const struct lysc_node *node, uint32_t options,
186 size_t line_length);
187
188/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200189 * @brief XML printer of YANG data.
Radek Krejcie7b95092019-05-15 11:03:07 +0200190 *
191 * @param[in] out Output specification.
192 * @param[in] root The root element of the (sub)tree to print.
193 * @param[in] options [Data printer flags](@ref dataprinterflags).
Radek Krejci8678fa42020-08-18 16:07:28 +0200194 * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed.
Radek Krejcie7b95092019-05-15 11:03:07 +0200195 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200196LY_ERR xml_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options);
Radek Krejcie7b95092019-05-15 11:03:07 +0200197
198/**
Radek Krejci5536d282020-08-04 23:27:44 +0200199 * @brief JSON printer of YANG data.
200 *
201 * @param[in] out Output specification.
202 * @param[in] root The root element of the (sub)tree to print.
203 * @param[in] options [Data printer flags](@ref dataprinterflags).
Radek Krejci8678fa42020-08-18 16:07:28 +0200204 * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed.
Radek Krejci5536d282020-08-04 23:27:44 +0200205 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200206LY_ERR json_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options);
Radek Krejci5536d282020-08-04 23:27:44 +0200207
208/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200209 * @brief LYB printer of YANG data.
210 *
211 * @param[in] out Output structure.
212 * @param[in] root The root element of the (sub)tree to print.
213 * @param[in] options [Data printer flags](@ref dataprinterflags).
Radek Krejci8678fa42020-08-18 16:07:28 +0200214 * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed.
Michal Vasko60ea6352020-06-29 13:39:39 +0200215 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200216LY_ERR lyb_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options);
Michal Vasko60ea6352020-06-29 13:39:39 +0200217
Radek Krejcid3ca0632019-04-16 16:54:54 +0200218#endif /* LY_PRINTER_INTERNAL_H_ */