summaryrefslogtreecommitdiffstats
path: root/src/printer_internal.h
blob: c835567d3d0db0da533df1ac9bc6baaa59c49b69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
 * @file printer_internal.h
 * @author Radek Krejci <rkrejci@cesnet.cz>
 * @brief Internal structures and functions for libyang
 *
 * Copyright (c) 2015-2019 CESNET, z.s.p.o.
 *
 * This source code is licensed under BSD 3-Clause License (the "License").
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://opensource.org/licenses/BSD-3-Clause
 */

#ifndef LY_PRINTER_INTERNAL_H_
#define LY_PRINTER_INTERNAL_H_

#include "out.h"
#include "plugins_exts.h"
#include "printer_data.h"
#include "printer_schema.h"

struct lysp_module;
struct lysp_submodule;

/**
 * @brief Generic YANG schema printer context
 *
 * Note that the YANG extensions API provides getter to the members for the extension plugins.
 */
struct lyspr_ctx {
    struct ly_out *out;              /**< output specification */
    uint16_t level;                  /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
    uint16_t flags;                  /**< internal flags for use by printer */
    uint32_t options;                /**< Schema output options (see @ref schemaprinterflags). */
    const struct lys_module *module; /**< schema to print */
};

/**
 * @brief YANG schema provided from plugin extension for printer_tree.
 *
 * The YANG extensions API provides setting functions.
 */
struct lyspr_tree_schema {
    ly_bool compiled;                                           /**< Flag if it is a compiled schema. */

    union {
        struct lysc_node *ctree;                                /**< Compiled schema. */
        struct lysp_node *ptree;                                /**< Parsed schema. */
    };
    union {
        lyplg_ext_sprinter_ctree_override_clb cn_overr;    /**< Override clb function for compiled node. */
        lyplg_ext_sprinter_ptree_override_clb pn_overr;    /**< Override clb function for parsed node. */
    };
};

/**
 * @brief Context used between plugin extension and printer_tree.
 *
 * The YANG extensions API provides setting functions.
 */
struct lyspr_tree_ctx {
    struct lyspr_tree_schema *schemas;              /**< Parsed or compiled schemas ([sized array](@ref sizedarrays)) */
    void *plugin_priv;                              /**< Private data from plugin which printer_tree does not use. */

    void (*free_plugin_priv)(void *plugin_priv);    /**< Release function for lyspr_tree_ctx.plugin_priv. */
};

/**
 * @brief YANG printer of the parsed module. Full YANG printer.
 *
 * @param[in] out Output specification.
 * @param[in] modp Parsed module to print.
 * @param[in] options Schema output options (see @ref schemaprinterflags).
 * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed.
 */
LY_ERR yang_print_parsed_module(struct ly_out *out, const struct lysp_module *modp, uint32_t options);

/**
 * @brief Helper macros for data printers
 */
#define DO_FORMAT (!(pctx->options & LY_PRINT_SHRINK))
#define LEVEL pctx->level                     /**< current level */
#define INDENT (DO_FORMAT ? (LEVEL)*2 : 0),"" /**< indentation parameters for printer functions */
#define LEVEL_INC LEVEL++                     /**< increase indentation level */
#define LEVEL_DEC LEVEL--                     /**< decrease indentation level */

#define XML_NS_INDENT 8

/**
 * @brief YANG printer of the parsed submodule. Full YANG printer.
 *
 * @param[in] out Output specification.
 * @param[in] submodp Parsed submodule to print.
 * @param[in] options Schema output options (see @ref schemaprinterflags).
 * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed.
 */
LY_ERR yang_print_parsed_submodule(struct ly_out *out, const struct lysp_submodule *submodp, uint32_t options);

/**
 * @brief YANG printer of the compiled schemas.
 *
 * This printer provides information about modules how they are understood by libyang.
 * Despite the format is inspired by YANG, it is not fully compatible and should not be
 * used as a standard YANG format.
 *
 * @param[in] out Output specification.
 * @param[in] module Schema to be printed (the compiled member is used).
 * @param[in] options Schema output options (see @ref schemaprinterflags).
 * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed.
 */
LY_ERR yang_print_compiled(struct ly_out *out, const struct lys_module *module, uint32_t options);

/**
 * @brief YANG printer of the compiled schema node
 *
 * This printer provides information about modules how they are understood by libyang.
 * Despite the format is inspired by YANG, it is not fully compatible and should not be
 * used as a standard YANG format.
 *
 * @param[in] out Output specification.
 * @param[in] node Schema node to be printed including all its substatements.
 * @param[in] options Schema output options (see @ref schemaprinterflags).
 * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed.
 */
LY_ERR yang_print_compiled_node(struct ly_out *out, const struct lysc_node *node, uint32_t options);

/**
 * @brief YIN printer of the parsed module. Full YIN printer.
 *
 * @param[in] out Output specification.
 * @param[in] modp Parsed module to print.
 * @param[in] options Schema output options (see @ref schemaprinterflags).
 * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed.
 */
LY_ERR yin_print_parsed_module(struct ly_out *out, const struct lysp_module *modp, uint32_t options);

/**
 * @brief YIN printer of the parsed submodule. Full YIN printer.
 *
 * @param[in] out Output specification.
 * @param[in] submodp Parsed submodule to print.
 * @param[in] options Schema output options (see @ref schemaprinterflags).
 * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed.
 */
LY_ERR yin_print_parsed_submodule(struct ly_out *out, const struct lysp_submodule *submodp, uint32_t options);

/**
 * @brief Full YANG Tree Diagram printer.
 *
 * The module should be compiled and the @ref contextoptions must be set to LY_CTX_SET_PRIV_PARSED.
 * If not, the printer will use parsed (unresolved) YANG schema tree, which means,
 * for example, that `grouping` sections will be on the output.
 *
 * @param[in] out Output specification.
 * @param[in] module Main module.
 * @param[in] options Schema output options (see @ref schemaprinterflags).
 * @param[in] line_length Maximum characters to be printed on a line, 0 for unlimited. Only for ::LYS_OUT_TREE printer.
 * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed.
 */
LY_ERR tree_print_module(struct ly_out *out, const struct lys_module *module, uint32_t options,
        size_t line_length);

/**
 * @brief YANG Tree Diagram printer of the parsed submodule. Full Tree printer.
 *
 * @param[in] out Output specification.
 * @param[in] submodp Parsed submodule to print.
 * @param[in] options Schema output options (see @ref schemaprinterflags).
 * @param[in] line_length Maximum characters to be printed on a line, 0 for unlimited. Only for ::LYS_OUT_TREE printer.
 * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed.
 */
LY_ERR tree_print_parsed_submodule(struct ly_out *out, const struct lysp_submodule *submodp, uint32_t options,
        size_t line_length);

/**
 * @brief YANG Tree Diagram printer of the compiled schema node.
 *
 * @param[in] out Output specification.
 * @param[in] node Schema node to be printed including all its substatements.
 * @param[in] options Schema output options (see @ref schemaprinterflags).
 * @param[in] line_length Maximum characters to be printed on a line, 0 for unlimited. Only for ::LYS_OUT_TREE printer.
 * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed.
 */
LY_ERR tree_print_compiled_node(struct ly_out *out, const struct lysc_node *node, uint32_t options,
        size_t line_length);

/**
 * @brief XML printer of YANG data.
 *
 * @param[in] out Output specification.
 * @param[in] root The root element of the (sub)tree to print.
 * @param[in] options [Data printer flags](@ref dataprinterflags).
 * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed.
 */
LY_ERR xml_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options);

/**
 * @brief JSON printer of YANG data.
 *
 * @param[in] out Output specification.
 * @param[in] root The root element of the (sub)tree to print.
 * @param[in] options [Data printer flags](@ref dataprinterflags).
 * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed.
 */
LY_ERR json_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options);

/**
 * @brief LYB printer of YANG data.
 *
 * @param[in] out Output structure.
 * @param[in] root The root element of the (sub)tree to print.
 * @param[in] options [Data printer flags](@ref dataprinterflags).
 * @return LY_ERR value, number of the printed bytes is updated in ::ly_out.printed.
 */
LY_ERR lyb_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options);

#endif /* LY_PRINTER_INTERNAL_H_ */