blob: cdc9d071eafd7bf96951ffa01c433a56f496ab61 [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
Radek Krejcia5bba312020-01-09 15:41:20 +010018#include "printer.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include "printer_data.h"
Michal Vasko69730152020-10-09 16:30:07 +020020#include "printer_schema.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020021
Michal Vasko7c8439f2020-08-05 13:25:19 +020022struct lysp_module;
23struct lysp_submodule;
24
Radek Krejcie7b95092019-05-15 11:03:07 +020025/**
Radek Krejcie7b95092019-05-15 11:03:07 +020026 * @brief Printer output structure specifying where the data are printed.
27 */
Radek Krejci241f6b52020-05-21 18:13:49 +020028struct ly_out {
29 LY_OUT_TYPE type; /**< type of the output to select the output method */
Radek Krejcid3ca0632019-04-16 16:54:54 +020030 union {
Radek Krejci241f6b52020-05-21 18:13:49 +020031 int fd; /**< file descriptor for LY_OUT_FD type */
32 FILE *f; /**< file structure for LY_OUT_FILE, LY_OUT_FDSTREAM and LY_OUT_FILEPATH types */
Radek Krejcid3ca0632019-04-16 16:54:54 +020033 struct {
Radek Krejci241f6b52020-05-21 18:13:49 +020034 FILE *f; /**< file stream from the original file descriptor, variable is mapped to the LY_OUT_FILE's f */
Radek Krejcia5bba312020-01-09 15:41:20 +010035 int fd; /**< original file descriptor, which was not used directly because of missing vdprintf() */
Radek Krejci241f6b52020-05-21 18:13:49 +020036 } fdstream; /**< structure for LY_OUT_FDSTREAM type, which is LY_OUT_FD when vdprintf() is missing */
Radek Krejcia5bba312020-01-09 15:41:20 +010037 struct {
Radek Krejci241f6b52020-05-21 18:13:49 +020038 FILE *f; /**< file structure for LY_OUT_FILEPATH, variable is mapped to the LY_OUT_FILE's f */
Radek Krejcia5bba312020-01-09 15:41:20 +010039 char *filepath; /**< stored original filepath */
Radek Krejci241f6b52020-05-21 18:13:49 +020040 } fpath; /**< filepath structure for LY_OUT_FILEPATH */
Radek Krejcia5bba312020-01-09 15:41:20 +010041 struct {
42 char **buf; /**< storage for the pointer to the memory buffer to store the output */
Radek Krejcie7b95092019-05-15 11:03:07 +020043 size_t len; /**< number of used bytes in the buffer */
44 size_t size; /**< allocated size of the buffer */
Radek Krejci241f6b52020-05-21 18:13:49 +020045 } mem; /**< memory buffer information for LY_OUT_MEMORY type */
Radek Krejcid3ca0632019-04-16 16:54:54 +020046 struct {
Radek Krejcia5bba312020-01-09 15:41:20 +010047 ssize_t (*func)(void *arg, const void *buf, size_t count); /**< callback function */
Radek Krejcie7b95092019-05-15 11:03:07 +020048 void *arg; /**< optional argument for the callback function */
Radek Krejci241f6b52020-05-21 18:13:49 +020049 } clb; /**< printer callback for LY_OUT_CALLBACK type */
Radek Krejcie7b95092019-05-15 11:03:07 +020050 } method; /**< type-specific information about the output */
Radek Krejcid3ca0632019-04-16 16:54:54 +020051
Michal Vasko63f3d842020-07-08 10:10:14 +020052 /* LYB only */
53 char *buffered; /**< additional buffer for holes */
54 size_t buf_len; /**< number of used bytes in the additional buffer for holes */
55 size_t buf_size; /**< allocated size of the buffer for holes */
56 size_t hole_count; /**< hole counter */
Radek Krejcid3ca0632019-04-16 16:54:54 +020057
Michal Vasko63f3d842020-07-08 10:10:14 +020058 size_t printed; /**< Total number of printed bytes */
59 size_t func_printed; /**< Number of bytes printed by the last function */
Radek Krejcid3ca0632019-04-16 16:54:54 +020060};
61
Radek Krejcie7b95092019-05-15 11:03:07 +020062/**
63 * @brief Informational structure for YANG statements
64 */
Radek Krejcid3ca0632019-04-16 16:54:54 +020065struct ext_substmt_info_s {
Radek Krejcie7b95092019-05-15 11:03:07 +020066 const char *name; /**< name of the statement */
67 const char *arg; /**< name of YIN's attribute to present the statement */
Radek Krejci1deb5be2020-08-26 16:43:36 +020068 uint8_t flags; /**< various flags to clarify printing of the statement */
Radek Krejcid3ca0632019-04-16 16:54:54 +020069#define SUBST_FLAG_YIN 0x1 /**< has YIN element */
70#define SUBST_FLAG_ID 0x2 /**< the value is identifier -> no quotes */
71};
72
73/* filled in printer.c */
74extern struct ext_substmt_info_s ext_substmt_info[];
75
Radek Krejcie7b95092019-05-15 11:03:07 +020076/**
Michal Vasko7c8439f2020-08-05 13:25:19 +020077 * @brief YANG printer of the parsed module. Full YANG printer.
Radek Krejcie7b95092019-05-15 11:03:07 +020078 *
79 * @param[in] out Output specification.
Michal Vasko7c8439f2020-08-05 13:25:19 +020080 * @param[in] module Main module.
81 * @param[in] modp Parsed module to print.
Radek Krejci5536d282020-08-04 23:27:44 +020082 * @param[in] options Schema output options (see @ref schemaprinterflags).
Radek Krejcie7b95092019-05-15 11:03:07 +020083 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
Radek Krejcid3ca0632019-04-16 16:54:54 +020084 */
Radek Krejci1deb5be2020-08-26 16:43:36 +020085LY_ERR yang_print_parsed_module(struct ly_out *out, const struct lys_module *module, const struct lysp_module *modp, uint32_t options);
Radek Krejci5536d282020-08-04 23:27:44 +020086
87/**
88 * @brief Helper macros for data printers
89 */
Radek Krejci52f65552020-09-01 17:03:35 +020090#define DO_FORMAT (!(ctx->options & LY_PRINT_SHRINK))
Radek Krejci5536d282020-08-04 23:27:44 +020091#define LEVEL ctx->level /**< current level */
92#define INDENT (DO_FORMAT ? (LEVEL)*2 : 0),"" /**< indentation parameters for printer functions */
93#define LEVEL_INC LEVEL++ /**< increase indentation level */
94#define LEVEL_DEC LEVEL-- /**< decrease indentation level */
Michal Vasko7c8439f2020-08-05 13:25:19 +020095
96/**
97 * @brief YANG printer of the parsed submodule. Full YANG printer.
98 *
99 * @param[in] out Output specification.
100 * @param[in] module Main module.
101 * @param[in] submodp Parsed submodule to print.
Radek Krejci5536d282020-08-04 23:27:44 +0200102 * @param[in] options Schema output options (see @ref schemaprinterflags).
Michal Vasko7c8439f2020-08-05 13:25:19 +0200103 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
104 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200105LY_ERR yang_print_parsed_submodule(struct ly_out *out, const struct lys_module *module, const struct lysp_submodule *submodp, uint32_t options);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200106
107/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200108 * @brief YANG printer of the compiled schemas.
109 *
110 * This printer provides information about modules how they are understood by libyang.
111 * Despite the format is inspired by YANG, it is not fully compatible and should not be
112 * used as a standard YANG format.
113 *
114 * @param[in] out Output specification.
115 * @param[in] module Schema to be printed (the compiled member is used).
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800116 * @param[in] options Schema output options (see @ref schemaprinterflags).
Radek Krejcie7b95092019-05-15 11:03:07 +0200117 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
Radek Krejcid3ca0632019-04-16 16:54:54 +0200118 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200119LY_ERR yang_print_compiled(struct ly_out *out, const struct lys_module *module, uint32_t options);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800120
121/**
122 * @brief YANG printer of the compiled schema node
123 *
124 * This printer provides information about modules how they are understood by libyang.
125 * Despite the format is inspired by YANG, it is not fully compatible and should not be
126 * used as a standard YANG format.
127 *
128 * @param[in] out Output specification.
129 * @param[in] node Schema node to be printed including all its substatements.
130 * @param[in] options Schema output options (see @ref schemaprinterflags).
131 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
132 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200133LY_ERR yang_print_compiled_node(struct ly_out *out, const struct lysc_node *node, uint32_t options);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200134
Radek Krejcie7b95092019-05-15 11:03:07 +0200135/**
Michal Vasko7c8439f2020-08-05 13:25:19 +0200136 * @brief YIN printer of the parsed module. Full YIN printer.
FredGand944bdc2019-11-05 21:57:07 +0800137 *
138 * @param[in] out Output specification.
Michal Vasko7c8439f2020-08-05 13:25:19 +0200139 * @param[in] module Main module.
140 * @param[in] modp Parsed module to print.
Radek Krejci5536d282020-08-04 23:27:44 +0200141 * @param[in] options Schema output options (see @ref schemaprinterflags).
FredGand944bdc2019-11-05 21:57:07 +0800142 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
143 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200144LY_ERR yin_print_parsed_module(struct ly_out *out, const struct lys_module *module, const struct lysp_module *modp, uint32_t options);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200145
146/**
147 * @brief YIN printer of the parsed submodule. Full YIN printer.
148 *
149 * @param[in] out Output specification.
150 * @param[in] module Main module.
151 * @param[in] submodp Parsed submodule to print.
Radek Krejci5536d282020-08-04 23:27:44 +0200152 * @param[in] options Schema output options (see @ref schemaprinterflags).
Michal Vasko7c8439f2020-08-05 13:25:19 +0200153 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
154 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200155LY_ERR yin_print_parsed_submodule(struct ly_out *out, const struct lys_module *module, const struct lysp_submodule *submodp, uint32_t options);
FredGand944bdc2019-11-05 21:57:07 +0800156
157/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200158 * @brief XML printer of YANG data.
Radek Krejcie7b95092019-05-15 11:03:07 +0200159 *
160 * @param[in] out Output specification.
161 * @param[in] root The root element of the (sub)tree to print.
162 * @param[in] options [Data printer flags](@ref dataprinterflags).
163 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
164 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200165LY_ERR xml_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options);
Radek Krejcie7b95092019-05-15 11:03:07 +0200166
167/**
Radek Krejci5536d282020-08-04 23:27:44 +0200168 * @brief JSON printer of YANG data.
169 *
170 * @param[in] out Output specification.
171 * @param[in] root The root element of the (sub)tree to print.
172 * @param[in] options [Data printer flags](@ref dataprinterflags).
173 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
174 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200175LY_ERR json_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options);
Radek Krejci5536d282020-08-04 23:27:44 +0200176
177/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200178 * @brief LYB printer of YANG data.
179 *
180 * @param[in] out Output structure.
181 * @param[in] root The root element of the (sub)tree to print.
182 * @param[in] options [Data printer flags](@ref dataprinterflags).
183 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
184 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200185LY_ERR lyb_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options);
Michal Vasko60ea6352020-06-29 13:39:39 +0200186
187/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100188 * @brief Check whether the node should even be printed.
189 *
190 * @param[in] node Node to check.
191 * @param[in] options Printer options.
Radek Krejci857189e2020-09-01 13:26:36 +0200192 * @return false (no, it should not be printed) or true (yes, it is supposed to be printed)
Michal Vasko9b368d32020-02-14 13:53:31 +0100193 */
Radek Krejci857189e2020-09-01 13:26:36 +0200194ly_bool ly_should_print(const struct lyd_node *node, uint32_t options);
Michal Vasko9b368d32020-02-14 13:53:31 +0100195
196/**
Michal Vasko5233e962020-08-14 14:26:20 +0200197 * @brief Generic printer of the given format string into the specified output.
198 *
199 * Does not reset printed bytes. Adds to printed bytes.
200 *
201 * @param[in] out Output specification.
202 * @param[in] format Format string to be printed.
203 * @return LY_ERR value.
204 */
205LY_ERR ly_print_(struct ly_out *out, const char *format, ...);
206
207/**
208 * @brief Generic printer of the given string buffer into the specified output.
209 *
210 * Does not reset printed bytes. Adds to printed bytes.
211 *
212 * @param[in] out Output specification.
213 * @param[in] buf Memory buffer with the data to print.
214 * @param[in] len Length of the data to print in the @p buf.
215 * @return LY_ERR value.
216 */
217LY_ERR ly_write_(struct ly_out *out, const char *buf, size_t len);
218
219/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200220 * @brief Create a hole in the output data that will be filled later.
221 *
Michal Vasko5233e962020-08-14 14:26:20 +0200222 * Adds printed bytes.
223 *
Radek Krejcie7b95092019-05-15 11:03:07 +0200224 * @param[in] out Output specification.
225 * @param[in] len Length of the created hole.
226 * @param[out] position Position of the hole, value must be later provided to the ly_write_skipped() call.
Michal Vasko5233e962020-08-14 14:26:20 +0200227 * @return LY_ERR value.
Radek Krejcie7b95092019-05-15 11:03:07 +0200228 */
Michal Vasko5233e962020-08-14 14:26:20 +0200229LY_ERR ly_write_skip(struct ly_out *out, size_t len, size_t *position);
Radek Krejcie7b95092019-05-15 11:03:07 +0200230
231/**
232 * @brief Write data into the hole at given position.
233 *
Michal Vasko5233e962020-08-14 14:26:20 +0200234 * Does not change printed bytes.
235 *
Radek Krejcie7b95092019-05-15 11:03:07 +0200236 * @param[in] out Output specification.
237 * @param[in] position Position of the hole to fill, the value was provided by ly_write_skip().
238 * @param[in] buf Memory buffer with the data to print.
239 * @param[in] len Length of the data to print in the @p buf. Not that the length must correspond
240 * to the len value specified in the corresponding ly_write_skip() call.
Michal Vasko5233e962020-08-14 14:26:20 +0200241 * @return LY_ERR value.
Radek Krejcie7b95092019-05-15 11:03:07 +0200242 */
Michal Vasko66d99972020-06-29 13:37:42 +0200243LY_ERR ly_write_skipped(struct ly_out *out, size_t position, const char *buf, size_t len);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200244
245#endif /* LY_PRINTER_INTERNAL_H_ */