blob: 973eb20c0c988e167b209321375934f25bd1c3a1 [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
18#include "printer_schema.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include "printer_data.h"
Radek Krejcid3ca0632019-04-16 16:54:54 +020020
Radek Krejcie7b95092019-05-15 11:03:07 +020021/**
22 * @brief Types of the printer's output
23 */
Radek Krejcid3ca0632019-04-16 16:54:54 +020024typedef enum LYOUT_TYPE {
25 LYOUT_FD, /**< file descriptor */
26 LYOUT_STREAM, /**< FILE stream */
Radek Krejci4a0ed4a2019-04-18 15:08:34 +020027 LYOUT_FDSTREAM, /**< FILE stream based on duplicated file descriptor */
Radek Krejcid3ca0632019-04-16 16:54:54 +020028 LYOUT_MEMORY, /**< memory */
29 LYOUT_CALLBACK /**< print via provided callback */
30} LYOUT_TYPE;
31
Radek Krejcie7b95092019-05-15 11:03:07 +020032/**
33 * @brief Printer output structure specifying where the data are printed.
34 */
Radek Krejcid3ca0632019-04-16 16:54:54 +020035struct lyout {
Radek Krejcie7b95092019-05-15 11:03:07 +020036 LYOUT_TYPE type; /**< type of the output to select the output method */
Radek Krejcid3ca0632019-04-16 16:54:54 +020037 union {
Radek Krejcie7b95092019-05-15 11:03:07 +020038 int fd; /**< file descriptor for LYOUT_FD type */
39 FILE *f; /**< file structure for LYOUT_STREAM and LYOUT_FDSTREAM types */
Radek Krejcid3ca0632019-04-16 16:54:54 +020040 struct {
Radek Krejcie7b95092019-05-15 11:03:07 +020041 char *buf; /**< pointer to the memory buffer to store the output */
42 size_t len; /**< number of used bytes in the buffer */
43 size_t size; /**< allocated size of the buffer */
44 } mem; /**< memory buffer information for LYOUT_MEMORY type */
Radek Krejcid3ca0632019-04-16 16:54:54 +020045 struct {
Radek Krejcie7b95092019-05-15 11:03:07 +020046 ssize_t (*f)(void *arg, const void *buf, size_t count); /**< callback function */
47 void *arg; /**< optional argument for the callback function */
48 } clb; /**< printer callback for LYOUT_CALLBACK type */
49 } method; /**< type-specific information about the output */
Radek Krejcid3ca0632019-04-16 16:54:54 +020050
Radek Krejcie7b95092019-05-15 11:03:07 +020051 char *buffered; /**< additional buffer for holes, used only for LYB data format */
52 size_t buf_len; /**< number of used bytes in the additional buffer for holes, used only for LYB data format */
53 size_t buf_size; /**< allocated size of the buffer for holes, used only for LYB data format */
54 size_t hole_count; /**< hole counter, used only for LYB data format */
Radek Krejcid3ca0632019-04-16 16:54:54 +020055
Radek Krejcie7b95092019-05-15 11:03:07 +020056 size_t printed; /**< Number of printed bytes */
Radek Krejci897ad2e2019-04-29 16:43:07 +020057
Radek Krejcie7b95092019-05-15 11:03:07 +020058 struct ly_ctx *ctx; /**< libyang context for error logging */
59 LY_ERR status; /**< current status of the printer */
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 */
68 int 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/**
77 * @brief macro to check current status of the printer.
78 */
79#define LYOUT_CHECK(LYOUT, ...) if (LYOUT->status) {return __VA_ARGS__;}
Radek Krejcid3ca0632019-04-16 16:54:54 +020080
81/**
Radek Krejcie7b95092019-05-15 11:03:07 +020082 * @brief YANG printer of the parsed schemas. Full YANG printer.
83 *
84 * @param[in] out Output specification.
85 * @param[in] module Schema to be printed (the parsed member is used).
86 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
Radek Krejcid3ca0632019-04-16 16:54:54 +020087 */
88LY_ERR yang_print_parsed(struct lyout *out, const struct lys_module *module);
89
90/**
Radek Krejcie7b95092019-05-15 11:03:07 +020091 * @brief YANG printer of the compiled schemas.
92 *
93 * This printer provides information about modules how they are understood by libyang.
94 * Despite the format is inspired by YANG, it is not fully compatible and should not be
95 * used as a standard YANG format.
96 *
97 * @param[in] out Output specification.
98 * @param[in] module Schema to be printed (the compiled member is used).
99 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
Radek Krejcid3ca0632019-04-16 16:54:54 +0200100 */
101LY_ERR yang_print_compiled(struct lyout *out, const struct lys_module *module);
102
Radek Krejcie7b95092019-05-15 11:03:07 +0200103/**
FredGand944bdc2019-11-05 21:57:07 +0800104 * @brief YIN printer of the parsed schemas. Full YIN printer.
105 *
106 * @param[in] out Output specification.
107 * @param[in] module Schema to be printed (the parsed member is used).
108 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
109 */
110LY_ERR yin_print_parsed(struct lyout *out, const struct lys_module *module);
111
112/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200113 * @brief XML printer of the YANG data.
114 *
115 * @param[in] out Output specification.
116 * @param[in] root The root element of the (sub)tree to print.
117 * @param[in] options [Data printer flags](@ref dataprinterflags).
118 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
119 */
120LY_ERR xml_print_data(struct lyout *out, const struct lyd_node *root, int options);
121
122/**
123 * @brief Generic printer of the given format string into the specified output.
124 *
125 * Alternatively, ly_write() can be used.
126 *
127 * @param[in] out Output specification.
128 * @param[in] format format string to be printed.
129 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
130 */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200131LY_ERR ly_print(struct lyout *out, const char *format, ...);
132
Radek Krejcie7b95092019-05-15 11:03:07 +0200133/**
134 * @brief Flush the output from any internal buffers and clean any auxiliary data.
135 * @param[in] out Output specification.
136 */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200137void ly_print_flush(struct lyout *out);
138
Radek Krejcie7b95092019-05-15 11:03:07 +0200139/**
140 * @brief Generic printer of the given string buffer into the specified output.
141 *
142 * Alternatively, ly_print() can be used.
143 *
144 * As an extension for printing holes (skipping some data until they are known),
145 * ly_write_skip() and ly_write_skipped() can be used.
146 *
147 * @param[in] out Output specification.
148 * @param[in] buf Memory buffer with the data to print.
149 * @param[in] len Length of the data to print in the @p buf.
150 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
151 */
152LY_ERR ly_write(struct lyout *out, const char *buf, size_t len);
153
154/**
155 * @brief Create a hole in the output data that will be filled later.
156 *
157 * @param[in] out Output specification.
158 * @param[in] len Length of the created hole.
159 * @param[out] position Position of the hole, value must be later provided to the ly_write_skipped() call.
160 * @return LY_ERR value. The number of the printed bytes is updated in lyout::printed
161 * only in case the data are really written into the output.
162 */
163LY_ERR ly_write_skip(struct lyout *out, size_t len, size_t *position);
164
165/**
166 * @brief Write data into the hole at given position.
167 *
168 * @param[in] out Output specification.
169 * @param[in] position Position of the hole to fill, the value was provided by ly_write_skip().
170 * @param[in] buf Memory buffer with the data to print.
171 * @param[in] len Length of the data to print in the @p buf. Not that the length must correspond
172 * to the len value specified in the corresponding ly_write_skip() call.
173 * @return LY_ERR value. The number of the printed bytes is updated in lyout::printed
174 * only in case the data are really written into the output.
175 */
176LY_ERR ly_write_skipped(struct lyout *out, size_t position, const char *buf, size_t len);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200177
178#endif /* LY_PRINTER_INTERNAL_H_ */