blob: c3f2605e0a4cc680d5bfddba004f329995307c82 [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 Krejcid3ca0632019-04-16 16:54:54 +020019#include "printer_schema.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020020#include "printer_data.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 Krejci897ad2e2019-04-29 16:43:07 +020060
Michal Vasko44685da2020-03-17 15:38:06 +010061 const struct ly_ctx *ctx; /**< libyang context for error logging */
Radek Krejcie7b95092019-05-15 11:03:07 +020062 LY_ERR status; /**< current status of the printer */
Radek Krejcid3ca0632019-04-16 16:54:54 +020063};
64
Radek Krejcie7b95092019-05-15 11:03:07 +020065/**
66 * @brief Informational structure for YANG statements
67 */
Radek Krejcid3ca0632019-04-16 16:54:54 +020068struct ext_substmt_info_s {
Radek Krejcie7b95092019-05-15 11:03:07 +020069 const char *name; /**< name of the statement */
70 const char *arg; /**< name of YIN's attribute to present the statement */
71 int flags; /**< various flags to clarify printing of the statement */
Radek Krejcid3ca0632019-04-16 16:54:54 +020072#define SUBST_FLAG_YIN 0x1 /**< has YIN element */
73#define SUBST_FLAG_ID 0x2 /**< the value is identifier -> no quotes */
74};
75
76/* filled in printer.c */
77extern struct ext_substmt_info_s ext_substmt_info[];
78
Radek Krejcie7b95092019-05-15 11:03:07 +020079/**
80 * @brief macro to check current status of the printer.
81 */
82#define LYOUT_CHECK(LYOUT, ...) if (LYOUT->status) {return __VA_ARGS__;}
Radek Krejcid3ca0632019-04-16 16:54:54 +020083
84/**
Michal Vasko7c8439f2020-08-05 13:25:19 +020085 * @brief YANG printer of the parsed module. Full YANG printer.
Radek Krejcie7b95092019-05-15 11:03:07 +020086 *
87 * @param[in] out Output specification.
Michal Vasko7c8439f2020-08-05 13:25:19 +020088 * @param[in] module Main module.
89 * @param[in] modp Parsed module to print.
Radek Krejcie7b95092019-05-15 11:03:07 +020090 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
Radek Krejcid3ca0632019-04-16 16:54:54 +020091 */
Michal Vasko7c8439f2020-08-05 13:25:19 +020092LY_ERR yang_print_parsed_module(struct ly_out *out, const struct lys_module *module, const struct lysp_module *modp);
93
94/**
95 * @brief YANG printer of the parsed submodule. Full YANG printer.
96 *
97 * @param[in] out Output specification.
98 * @param[in] module Main module.
99 * @param[in] submodp Parsed submodule to print.
100 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
101 */
102LY_ERR yang_print_parsed_submodule(struct ly_out *out, const struct lys_module *module, const struct lysp_submodule *submodp);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200103
104/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200105 * @brief YANG printer of the compiled schemas.
106 *
107 * This printer provides information about modules how they are understood by libyang.
108 * Despite the format is inspired by YANG, it is not fully compatible and should not be
109 * used as a standard YANG format.
110 *
111 * @param[in] out Output specification.
112 * @param[in] module Schema to be printed (the compiled member is used).
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800113 * @param[in] options Schema output options (see @ref schemaprinterflags).
Radek Krejcie7b95092019-05-15 11:03:07 +0200114 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
Radek Krejcid3ca0632019-04-16 16:54:54 +0200115 */
Radek Krejci241f6b52020-05-21 18:13:49 +0200116LY_ERR yang_print_compiled(struct ly_out *out, const struct lys_module *module, int options);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800117
118/**
119 * @brief YANG printer of the compiled schema node
120 *
121 * This printer provides information about modules how they are understood by libyang.
122 * Despite the format is inspired by YANG, it is not fully compatible and should not be
123 * used as a standard YANG format.
124 *
125 * @param[in] out Output specification.
126 * @param[in] node Schema node to be printed including all its substatements.
127 * @param[in] options Schema output options (see @ref schemaprinterflags).
128 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
129 */
Radek Krejci241f6b52020-05-21 18:13:49 +0200130LY_ERR yang_print_compiled_node(struct ly_out *out, const struct lysc_node *node, int options);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200131
Radek Krejcie7b95092019-05-15 11:03:07 +0200132/**
Michal Vasko7c8439f2020-08-05 13:25:19 +0200133 * @brief YIN printer of the parsed module. Full YIN printer.
FredGand944bdc2019-11-05 21:57:07 +0800134 *
135 * @param[in] out Output specification.
Michal Vasko7c8439f2020-08-05 13:25:19 +0200136 * @param[in] module Main module.
137 * @param[in] modp Parsed module to print.
FredGand944bdc2019-11-05 21:57:07 +0800138 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
139 */
Michal Vasko7c8439f2020-08-05 13:25:19 +0200140LY_ERR yin_print_parsed_module(struct ly_out *out, const struct lys_module *module, const struct lysp_module *modp);
141
142/**
143 * @brief YIN printer of the parsed submodule. Full YIN printer.
144 *
145 * @param[in] out Output specification.
146 * @param[in] module Main module.
147 * @param[in] submodp Parsed submodule to print.
148 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
149 */
150LY_ERR yin_print_parsed_submodule(struct ly_out *out, const struct lys_module *module, const struct lysp_submodule *submodp);
FredGand944bdc2019-11-05 21:57:07 +0800151
152/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200153 * @brief XML printer of YANG data.
Radek Krejcie7b95092019-05-15 11:03:07 +0200154 *
155 * @param[in] out Output specification.
156 * @param[in] root The root element of the (sub)tree to print.
157 * @param[in] options [Data printer flags](@ref dataprinterflags).
158 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
159 */
Radek Krejci241f6b52020-05-21 18:13:49 +0200160LY_ERR xml_print_data(struct ly_out *out, const struct lyd_node *root, int options);
Radek Krejcie7b95092019-05-15 11:03:07 +0200161
162/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200163 * @brief LYB printer of YANG data.
164 *
165 * @param[in] out Output structure.
166 * @param[in] root The root element of the (sub)tree to print.
167 * @param[in] options [Data printer flags](@ref dataprinterflags).
168 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
169 */
170LY_ERR lyb_print_data(struct ly_out *out, const struct lyd_node *root, int options);
171
172/**
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100173 * @brief Check whether a node value equals to its default one.
174 *
175 * @param[in] node Term node to test.
176 * @return 0 if no,
177 * @return non-zero if yes.
178 */
179int ly_is_default(const struct lyd_node *node);
180
181/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100182 * @brief Check whether the node should even be printed.
183 *
184 * @param[in] node Node to check.
185 * @param[in] options Printer options.
186 * @return 0 if no.
187 * @return non-zero if yes.
188 */
189int ly_should_print(const struct lyd_node *node, int options);
190
191/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200192 * @brief Create a hole in the output data that will be filled later.
193 *
194 * @param[in] out Output specification.
195 * @param[in] len Length of the created hole.
196 * @param[out] position Position of the hole, value must be later provided to the ly_write_skipped() call.
Radek Krejcibaeb8382020-05-27 16:44:53 +0200197 * @return The number of bytes prepared for write. The number of the printed bytes is updated in lyout::printed
Radek Krejcie7b95092019-05-15 11:03:07 +0200198 * only in case the data are really written into the output.
Radek Krejcibaeb8382020-05-27 16:44:53 +0200199 * @return Negative value in case of error, absolute value of the return code maps to LY_ERR value.
Radek Krejcie7b95092019-05-15 11:03:07 +0200200 */
Radek Krejcibaeb8382020-05-27 16:44:53 +0200201ssize_t ly_write_skip(struct ly_out *out, size_t len, size_t *position);
Radek Krejcie7b95092019-05-15 11:03:07 +0200202
203/**
204 * @brief Write data into the hole at given position.
205 *
206 * @param[in] out Output specification.
207 * @param[in] position Position of the hole to fill, the value was provided by ly_write_skip().
208 * @param[in] buf Memory buffer with the data to print.
209 * @param[in] len Length of the data to print in the @p buf. Not that the length must correspond
210 * to the len value specified in the corresponding ly_write_skip() call.
Michal Vasko66d99972020-06-29 13:37:42 +0200211 * @return LY_SUCCESS on success.
212 * @return LY_ERR value in case of error.
Radek Krejcie7b95092019-05-15 11:03:07 +0200213 */
Michal Vasko66d99972020-06-29 13:37:42 +0200214LY_ERR ly_write_skipped(struct ly_out *out, size_t position, const char *buf, size_t len);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200215
216#endif /* LY_PRINTER_INTERNAL_H_ */