Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1 | /** |
| 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 Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 18 | #include "out.h" |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 19 | #include "plugins_exts.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 20 | #include "printer_data.h" |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 21 | #include "printer_schema.h" |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 22 | |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 23 | struct lysp_module; |
| 24 | struct lysp_submodule; |
| 25 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 26 | /** |
Radek Krejci | f8d7f9a | 2021-03-10 14:32:36 +0100 | [diff] [blame] | 27 | * @brief Generic YANG schema printer context |
| 28 | * |
| 29 | * Note that the YANG extensions API provides getter to the members for the extension plugins. |
| 30 | */ |
| 31 | struct lyspr_ctx { |
| 32 | struct ly_out *out; /**< output specification */ |
| 33 | uint16_t level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */ |
Radek Krejci | aa14a0c | 2020-09-04 11:16:47 +0200 | [diff] [blame] | 34 | uint16_t flags; /**< internal flags for use by printer */ |
Radek Krejci | f8d7f9a | 2021-03-10 14:32:36 +0100 | [diff] [blame] | 35 | uint32_t options; /**< Schema output options (see @ref schemaprinterflags). */ |
| 36 | const struct lys_module *module; /**< schema to print */ |
| 37 | }; |
| 38 | |
| 39 | /** |
aPiecek | 03cb487 | 2022-10-24 10:31:51 +0200 | [diff] [blame] | 40 | * @brief YANG schema provided from plugin extension for printer_tree. |
| 41 | * |
| 42 | * The YANG extensions API provides setting functions. |
| 43 | */ |
| 44 | struct 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 | */ |
| 62 | struct 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 Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 70 | * @brief YANG printer of the parsed module. Full YANG printer. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 71 | * |
| 72 | * @param[in] out Output specification. |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 73 | * @param[in] modp Parsed module to print. |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 74 | * @param[in] options Schema output options (see @ref schemaprinterflags). |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 75 | * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed. |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 76 | */ |
Michal Vasko | 7997d5a | 2021-02-22 10:55:56 +0100 | [diff] [blame] | 77 | LY_ERR yang_print_parsed_module(struct ly_out *out, const struct lysp_module *modp, uint32_t options); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * @brief Helper macros for data printers |
| 81 | */ |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 82 | #define DO_FORMAT (!(pctx->options & LY_PRINT_SHRINK)) |
| 83 | #define LEVEL pctx->level /**< current level */ |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 84 | #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 Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 87 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 88 | #define XML_NS_INDENT 8 |
| 89 | |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 90 | /** |
| 91 | * @brief YANG printer of the parsed submodule. Full YANG printer. |
| 92 | * |
| 93 | * @param[in] out Output specification. |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 94 | * @param[in] submodp Parsed submodule to print. |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 95 | * @param[in] options Schema output options (see @ref schemaprinterflags). |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 96 | * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed. |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 97 | */ |
Michal Vasko | 7997d5a | 2021-02-22 10:55:56 +0100 | [diff] [blame] | 98 | LY_ERR yang_print_parsed_submodule(struct ly_out *out, const struct lysp_submodule *submodp, uint32_t options); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 99 | |
| 100 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 101 | * @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 Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 109 | * @param[in] options Schema output options (see @ref schemaprinterflags). |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 110 | * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed. |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 111 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 112 | LY_ERR yang_print_compiled(struct ly_out *out, const struct lys_module *module, uint32_t options); |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 113 | |
| 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 Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 124 | * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed. |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 125 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 126 | LY_ERR yang_print_compiled_node(struct ly_out *out, const struct lysc_node *node, uint32_t options); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 127 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 128 | /** |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 129 | * @brief YIN printer of the parsed module. Full YIN printer. |
FredGan | d944bdc | 2019-11-05 21:57:07 +0800 | [diff] [blame] | 130 | * |
| 131 | * @param[in] out Output specification. |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 132 | * @param[in] modp Parsed module to print. |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 133 | * @param[in] options Schema output options (see @ref schemaprinterflags). |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 134 | * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed. |
FredGan | d944bdc | 2019-11-05 21:57:07 +0800 | [diff] [blame] | 135 | */ |
Michal Vasko | 7997d5a | 2021-02-22 10:55:56 +0100 | [diff] [blame] | 136 | LY_ERR yin_print_parsed_module(struct ly_out *out, const struct lysp_module *modp, uint32_t options); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 137 | |
| 138 | /** |
| 139 | * @brief YIN printer of the parsed submodule. Full YIN printer. |
| 140 | * |
| 141 | * @param[in] out Output specification. |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 142 | * @param[in] submodp Parsed submodule to print. |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 143 | * @param[in] options Schema output options (see @ref schemaprinterflags). |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 144 | * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed. |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 145 | */ |
Michal Vasko | 7997d5a | 2021-02-22 10:55:56 +0100 | [diff] [blame] | 146 | LY_ERR yin_print_parsed_submodule(struct ly_out *out, const struct lysp_submodule *submodp, uint32_t options); |
FredGan | d944bdc | 2019-11-05 21:57:07 +0800 | [diff] [blame] | 147 | |
| 148 | /** |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 149 | * @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. |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 154 | * |
| 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 | */ |
aPiecek | 3f24765 | 2021-04-19 13:40:25 +0200 | [diff] [blame] | 161 | LY_ERR tree_print_module(struct ly_out *out, const struct lys_module *module, uint32_t options, |
aPiecek | 61d062b | 2020-11-02 11:05:09 +0100 | [diff] [blame] | 162 | size_t line_length); |
| 163 | |
| 164 | /** |
aPiecek | 9f792e5 | 2021-04-21 08:33:56 +0200 | [diff] [blame] | 165 | * @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 | */ |
| 173 | LY_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. |
aPiecek | 153b00f | 2021-04-20 13:52:57 +0200 | [diff] [blame] | 178 | * |
| 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 | */ |
| 185 | LY_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 Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 189 | * @brief XML printer of YANG data. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 190 | * |
| 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 Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 194 | * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 195 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 196 | LY_ERR xml_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 197 | |
| 198 | /** |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 199 | * @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 Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 204 | * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed. |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 205 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 206 | LY_ERR json_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 207 | |
| 208 | /** |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 209 | * @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 Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 214 | * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed. |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 215 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 216 | LY_ERR lyb_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 217 | |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 218 | #endif /* LY_PRINTER_INTERNAL_H_ */ |