blob: ca97eff7365395a7ed8573d30bec401c194630bc [file] [log] [blame]
Radek Krejcia5bba312020-01-09 15:41:20 +01001/**
Michal Vaskoafac7822020-10-20 14:22:26 +02002 * @file out.h
Radek Krejcia5bba312020-01-09 15:41:20 +01003 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vaskoafac7822020-10-20 14:22:26 +02004 * @brief libyang output structures and functions
Radek Krejcia5bba312020-01-09 15:41:20 +01005 *
Michal Vaskoafac7822020-10-20 14:22:26 +02006 * Copyright (c) 2015-2020 CESNET, z.s.p.o.
Radek Krejcia5bba312020-01-09 15:41:20 +01007 *
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
Michal Vaskoafac7822020-10-20 14:22:26 +020015#ifndef LY_OUT_H_
16#define LY_OUT_H_
Radek Krejcia5bba312020-01-09 15:41:20 +010017
Radek Krejci535ea9f2020-05-29 16:01:05 +020018#include <stdio.h>
Jan Kundrátc2b2f082022-06-14 15:23:12 +020019#include <sys/types.h>
20#ifdef _MSC_VER
21# define ssize_t SSIZE_T
22#endif
Radek Krejcia5bba312020-01-09 15:41:20 +010023
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include "log.h"
25
Radek Krejcica376bd2020-06-11 16:04:06 +020026#ifdef __cplusplus
27extern "C" {
28#endif
29
Radek Krejcia5bba312020-01-09 15:41:20 +010030/**
Michal Vaskoafac7822020-10-20 14:22:26 +020031 * @page howtoOutput Output Processing
Radek Krejci8678fa42020-08-18 16:07:28 +020032 *
Michal Vaskoafac7822020-10-20 14:22:26 +020033 * libyang provides a mechanism to generalize work with the outputs (and [inputs](@ref howtoInput)) of
Radek Krejci8678fa42020-08-18 16:07:28 +020034 * the different types. The ::ly_out handler can be created providing necessary information connected with the specific
35 * output type and then used throughout the printers functions. The API allows to combine output from libyang (data or schema)
36 * printers and output directly provided by the caller (via ::ly_print() or ::ly_write()).
37 *
38 * Using a generic output handler avoids need to have a set of functions for each printer functionality and results in simpler API.
39 *
aPiecekb0445f22021-06-24 11:34:07 +020040 * The API allows to alter the target of the data behind the handler by another target (of the same type). Also resetting
Radek Krejci8678fa42020-08-18 16:07:28 +020041 * a seekable output is possible with ::ly_out_reset() to re-write the output.
42 *
43 * @note
44 * This mechanism was introduced in libyang 2.0. To simplify transition from libyang 1.0 to version 2.0 and also for
45 * some simple use case where using the output handler would be an overkill, there are some basic printer functions
46 * that do not require output handler. But remember, that functionality of these function can be limited in particular cases
47 * in contrast to the functions using output handlers.
48 *
49 * Functions List
50 * --------------
51 * - ::ly_out_new_clb()
52 * - ::ly_out_new_fd()
53 * - ::ly_out_new_file()
54 * - ::ly_out_new_filepath()
55 * - ::ly_out_new_memory()
56 *
57 * - ::ly_out_clb()
58 * - ::ly_out_clb_arg()
59 * - ::ly_out_fd()
60 * - ::ly_out_file()
61 * - ::ly_out_filepath()
62 * - ::ly_out_memory()
63 *
64 * - ::ly_out_type()
65 * - ::ly_out_printed()
66 *
67 * - ::ly_out_reset()
68 * - ::ly_out_free()
69 *
70 * - ::ly_print()
71 * - ::ly_print_flush()
72 * - ::ly_write()
73 *
74 * libyang Printers List
75 * --------------------
76 * - @subpage howtoSchemaPrinters
77 * - @subpage howtoDataPrinters
78 */
79
80/**
81 * @struct ly_out
Radek Krejcia5bba312020-01-09 15:41:20 +010082 * @brief Printer output structure specifying where the data are printed.
83 */
Radek Krejci241f6b52020-05-21 18:13:49 +020084struct ly_out;
Radek Krejcia5bba312020-01-09 15:41:20 +010085
86/**
Radek Krejci52f65552020-09-01 17:03:35 +020087 * @brief Common value for data as well as schema printers to avoid formatting indentations and new lines
88 */
89#define LY_PRINT_SHRINK 0x02
90
91/**
Radek Krejcia5bba312020-01-09 15:41:20 +010092 * @brief Types of the printer's output
93 */
Radek Krejci241f6b52020-05-21 18:13:49 +020094typedef enum LY_OUT_TYPE {
95 LY_OUT_ERROR = -1, /**< error value to indicate failure of the functions returning LY_OUT_TYPE */
96 LY_OUT_FD, /**< file descriptor printer */
97 LY_OUT_FDSTREAM, /**< internal replacement for LY_OUT_FD in case vdprintf() is not available */
98 LY_OUT_FILE, /**< FILE stream printer */
99 LY_OUT_FILEPATH, /**< filepath printer */
100 LY_OUT_MEMORY, /**< memory printer */
101 LY_OUT_CALLBACK /**< callback printer */
102} LY_OUT_TYPE;
Radek Krejcia5bba312020-01-09 15:41:20 +0100103
104/**
105 * @brief Get output type of the printer handler.
106 *
107 * @param[in] out Printer handler.
108 * @return Type of the printer's output.
109 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100110LIBYANG_API_DECL LY_OUT_TYPE ly_out_type(const struct ly_out *out);
Radek Krejcia5bba312020-01-09 15:41:20 +0100111
112/**
113 * @brief Reset the output medium to write from its beginning, so the following printer function will rewrite the current data
114 * instead of appending.
115 *
116 * Note that in case the underlying output is not seekable (stream referring a pipe/FIFO/socket or the callback output type),
117 * nothing actually happens despite the function succeeds. Also note that the medium is not returned to the state it was when
Radek Krejcic5a12e12020-05-27 17:09:59 +0200118 * the handler was created. For example, file is seeked into the offset zero and truncated, the content from the time it was opened with
Radek Krejci8678fa42020-08-18 16:07:28 +0200119 * ::ly_out_new_file() is not restored.
Radek Krejcia5bba312020-01-09 15:41:20 +0100120 *
121 * @param[in] out Printer handler.
122 * @return LY_SUCCESS in case of success
123 * @return LY_ESYS in case of failure
124 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100125LIBYANG_API_DECL LY_ERR ly_out_reset(struct ly_out *out);
Radek Krejcia5bba312020-01-09 15:41:20 +0100126
127/**
Michal Vaskoce2b8742020-08-24 13:20:25 +0200128 * @brief Generic write callback for data printed by libyang.
129 *
130 * @param[in] user_data Optional caller-specific argument.
131 * @param[in] buf Data to write.
132 * @param[in] count Number of bytes to write.
133 * @return Number of printed bytes.
134 * @return Negative value in case of error.
135 */
136typedef ssize_t (*ly_write_clb)(void *user_data, const void *buf, size_t count);
137
138/**
Radek Krejcia5bba312020-01-09 15:41:20 +0100139 * @brief Create printer handler using callback printer function.
140 *
141 * @param[in] writeclb Pointer to the printer callback function writing the data (see write(2)).
Michal Vaskoce2b8742020-08-24 13:20:25 +0200142 * @param[in] user_data Optional caller-specific argument to be passed to the @p writeclb callback.
Radek Krejci84ce7b12020-06-11 17:28:25 +0200143 * @param[out] out Created printer handler supposed to be passed to different ly*_print() functions.
144 * @return LY_SUCCESS in case of success
145 * @return LY_EMEM in case allocating the @p out handler fails.
Radek Krejcia5bba312020-01-09 15:41:20 +0100146 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100147LIBYANG_API_DECL LY_ERR ly_out_new_clb(ly_write_clb writeclb, void *user_data, struct ly_out **out);
Radek Krejcia5bba312020-01-09 15:41:20 +0100148
149/**
150 * @brief Get or reset callback function associated with a callback printer handler.
151 *
152 * @param[in] out Printer handler.
Radek Krejci8678fa42020-08-18 16:07:28 +0200153 * @param[in] writeclb Optional argument providing a new printer callback function for the handler. If NULL, only the current
154 * printer callback is returned.
155 * @return Previous printer callback.
Radek Krejcia5bba312020-01-09 15:41:20 +0100156 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100157LIBYANG_API_DECL ly_write_clb ly_out_clb(struct ly_out *out, ly_write_clb writeclb);
Radek Krejcia5bba312020-01-09 15:41:20 +0100158
159/**
aPiecekb0445f22021-06-24 11:34:07 +0200160 * @brief Get or reset callback function's argument associated with a callback printer handler.
Radek Krejcia5bba312020-01-09 15:41:20 +0100161 *
162 * @param[in] out Printer handler.
163 * @param[in] arg caller-specific argument to be passed to the callback function associated with the printer handler.
164 * If NULL, only the current file descriptor value is returned.
165 * @return The previous callback argument.
166 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100167LIBYANG_API_DECL void *ly_out_clb_arg(struct ly_out *out, void *arg);
Radek Krejcia5bba312020-01-09 15:41:20 +0100168
169/**
170 * @brief Create printer handler using file descriptor.
171 *
172 * @param[in] fd File descriptor to use.
Radek Krejci84ce7b12020-06-11 17:28:25 +0200173 * @param[out] out Created printer handler supposed to be passed to different ly*_print() functions.
174 * @return LY_SUCCESS in case of success
175 * @return LY_ERR value in case of failure.
Radek Krejcia5bba312020-01-09 15:41:20 +0100176 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100177LIBYANG_API_DECL LY_ERR ly_out_new_fd(int fd, struct ly_out **out);
Radek Krejcia5bba312020-01-09 15:41:20 +0100178
179/**
180 * @brief Get or reset file descriptor printer handler.
181 *
182 * @param[in] out Printer handler.
183 * @param[in] fd Optional value of a new file descriptor for the handler. If -1, only the current file descriptor value is returned.
184 * @return Previous value of the file descriptor. Note that caller is responsible for closing the returned file descriptor in case of setting new descriptor @p fd.
185 * @return -1 in case of error when setting up the new file descriptor.
186 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100187LIBYANG_API_DECL int ly_out_fd(struct ly_out *out, int fd);
Radek Krejcia5bba312020-01-09 15:41:20 +0100188
189/**
190 * @brief Create printer handler using file stream.
191 *
192 * @param[in] f File stream to use.
Radek Krejci84ce7b12020-06-11 17:28:25 +0200193 * @param[out] out Created printer handler supposed to be passed to different ly*_print() functions.
194 * @return LY_SUCCESS in case of success
195 * @return LY_ERR value in case of failure.
Radek Krejcia5bba312020-01-09 15:41:20 +0100196 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100197LIBYANG_API_DECL LY_ERR ly_out_new_file(FILE *f, struct ly_out **out);
Radek Krejcia5bba312020-01-09 15:41:20 +0100198
199/**
200 * @brief Get or reset file stream printer handler.
201 *
202 * @param[in] out Printer handler.
203 * @param[in] f Optional new file stream for the handler. If NULL, only the current file stream is returned.
204 * @return Previous file stream of the handler. Note that caller is responsible for closing the returned stream in case of setting new stream @p f.
205 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100206LIBYANG_API_DECL FILE *ly_out_file(struct ly_out *out, FILE *f);
Radek Krejcia5bba312020-01-09 15:41:20 +0100207
208/**
209 * @brief Create printer handler using memory to dump data.
210 *
211 * @param[in] strp Pointer to store the resulting data. If it points to a pointer to an allocated buffer and
212 * @p size of the buffer is set, the buffer is used (and extended if needed) to store the printed data.
213 * @param[in] size Size of the buffer provided via @p strp. In case it is 0, the buffer for the printed data
214 * is newly allocated even if @p strp points to a pointer to an existing buffer.
Radek Krejci84ce7b12020-06-11 17:28:25 +0200215 * @param[out] out Created printer handler supposed to be passed to different ly*_print() functions.
216 * @return LY_SUCCESS in case of success
217 * @return LY_ERR value in case of failure.
Radek Krejcia5bba312020-01-09 15:41:20 +0100218 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100219LIBYANG_API_DECL LY_ERR ly_out_new_memory(char **strp, size_t size, struct ly_out **out);
Radek Krejcia5bba312020-01-09 15:41:20 +0100220
221/**
222 * @brief Get or change memory where the data are dumped.
223 *
224 * @param[in] out Printer handler.
Radek Krejci8678fa42020-08-18 16:07:28 +0200225 * @param[in] strp Optional new string pointer to store the resulting data, same rules as in ::ly_out_new_memory() are applied.
Radek Krejcia5bba312020-01-09 15:41:20 +0100226 * @param[in] size Size of the buffer provided via @p strp. In case it is 0, the buffer for the printed data
Radek Krejcibaeb8382020-05-27 16:44:53 +0200227 * is newly allocated even if @p strp points to a pointer to an existing buffer. In case the @p strp is NULL, this
228 * parameter is ignored.
Radek Krejcia5bba312020-01-09 15:41:20 +0100229 * @return Previous dumped data. Note that the caller is responsible to free the data in case of changing string pointer @p strp.
230 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100231LIBYANG_API_DECL char *ly_out_memory(struct ly_out *out, char **strp, size_t size);
Radek Krejcia5bba312020-01-09 15:41:20 +0100232
233/**
234 * @brief Create printer handler file of the given filename.
235 *
236 * @param[in] filepath Path of the file where to write data.
Radek Krejci8678fa42020-08-18 16:07:28 +0200237 * @param[out] out Created printer handler supposed to be passed to different ly*_print() functions.
Radek Krejcia5bba312020-01-09 15:41:20 +0100238 * @return NULL in case of error.
239 * @return Created printer handler supposed to be passed to different ly*_print_*() functions.
240 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100241LIBYANG_API_DECL LY_ERR ly_out_new_filepath(const char *filepath, struct ly_out **out);
Radek Krejcia5bba312020-01-09 15:41:20 +0100242
243/**
244 * @brief Get or change the filepath of the file where the printer prints the data.
245 *
246 * Note that in case of changing the filepath, the current file is closed and a new one is
247 * created/opened instead of renaming the previous file. Also note that the previous filepath
248 * string is returned only in case of not changing it's value.
249 *
250 * @param[in] out Printer handler.
251 * @param[in] filepath Optional new filepath for the handler. If and only if NULL, the current filepath string is returned.
252 * @return Previous filepath string in case the @p filepath argument is NULL.
aPiecekb0445f22021-06-24 11:34:07 +0200253 * @return NULL if changing filepath succeeds and ((void *)-1) otherwise.
Radek Krejcia5bba312020-01-09 15:41:20 +0100254 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100255LIBYANG_API_DECL const char *ly_out_filepath(struct ly_out *out, const char *filepath);
Radek Krejcia5bba312020-01-09 15:41:20 +0100256
257/**
258 * @brief Generic printer of the given format string into the specified output.
259 *
Radek Krejci8678fa42020-08-18 16:07:28 +0200260 * Alternatively, ::ly_write() can be used.
Radek Krejcia5bba312020-01-09 15:41:20 +0100261 *
262 * @param[in] out Output specification.
Michal Vasko5233e962020-08-14 14:26:20 +0200263 * @param[in] format Format string to be printed.
264 * @return LY_ERR value, get number of the printed bytes using ::ly_out_printed.
Radek Krejcia5bba312020-01-09 15:41:20 +0100265 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100266LIBYANG_API_DECL LY_ERR ly_print(struct ly_out *out, const char *format, ...);
Radek Krejcia5bba312020-01-09 15:41:20 +0100267
268/**
Radek Krejci099fd212020-05-27 18:17:35 +0200269 * @brief Flush the output from any internal buffers and clean any auxiliary data.
270 * @param[in] out Output specification.
271 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100272LIBYANG_API_DECL void ly_print_flush(struct ly_out *out);
Radek Krejci099fd212020-05-27 18:17:35 +0200273
274/**
Radek Krejcia5bba312020-01-09 15:41:20 +0100275 * @brief Generic printer of the given string buffer into the specified output.
276 *
Radek Krejci8678fa42020-08-18 16:07:28 +0200277 * Alternatively, ::ly_print() can be used.
Radek Krejcia5bba312020-01-09 15:41:20 +0100278 *
Radek Krejcia5bba312020-01-09 15:41:20 +0100279 * @param[in] out Output specification.
280 * @param[in] buf Memory buffer with the data to print.
281 * @param[in] len Length of the data to print in the @p buf.
Michal Vasko5233e962020-08-14 14:26:20 +0200282 * @return LY_ERR value, get number of the printed bytes using ::ly_out_printed.
Radek Krejcia5bba312020-01-09 15:41:20 +0100283 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100284LIBYANG_API_DECL LY_ERR ly_write(struct ly_out *out, const char *buf, size_t len);
Radek Krejcia5bba312020-01-09 15:41:20 +0100285
286/**
Michal Vasko63f3d842020-07-08 10:10:14 +0200287 * @brief Get the number of printed bytes by the last function.
288 *
289 * @param[in] out Out structure used.
290 * @return Number of printed bytes.
291 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100292LIBYANG_API_DECL size_t ly_out_printed(const struct ly_out *out);
Michal Vasko63f3d842020-07-08 10:10:14 +0200293
294/**
Radek Krejcia5bba312020-01-09 15:41:20 +0100295 * @brief Free the printer handler.
296 * @param[in] out Printer handler to free.
Radek Krejci241f6b52020-05-21 18:13:49 +0200297 * @param[in] clb_arg_destructor Freeing function for printer callback (LY_OUT_CALLBACK) argument.
298 * @param[in] destroy Flag to free allocated buffer (for LY_OUT_MEMORY) or to
299 * close stream/file descriptor (for LY_OUT_FD, LY_OUT_FDSTREAM and LY_OUT_FILE)
Radek Krejcia5bba312020-01-09 15:41:20 +0100300 */
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100301LIBYANG_API_DECL void ly_out_free(struct ly_out *out, void (*clb_arg_destructor)(void *arg), ly_bool destroy);
Radek Krejcia5bba312020-01-09 15:41:20 +0100302
Radek Krejcica376bd2020-06-11 16:04:06 +0200303#ifdef __cplusplus
304}
305#endif
306
Michal Vaskoafac7822020-10-20 14:22:26 +0200307#endif /* LY_OUT_H_ */