blob: 6c4487429c2bb3d55e877d55d8e2af8e376f568a [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
Radek Krejcie7b95092019-05-15 11:03:07 +020022/**
Radek Krejcie7b95092019-05-15 11:03:07 +020023 * @brief Printer output structure specifying where the data are printed.
24 */
Radek Krejci241f6b52020-05-21 18:13:49 +020025struct ly_out {
26 LY_OUT_TYPE type; /**< type of the output to select the output method */
Radek Krejcid3ca0632019-04-16 16:54:54 +020027 union {
Radek Krejci241f6b52020-05-21 18:13:49 +020028 int fd; /**< file descriptor for LY_OUT_FD type */
29 FILE *f; /**< file structure for LY_OUT_FILE, LY_OUT_FDSTREAM and LY_OUT_FILEPATH types */
Radek Krejcid3ca0632019-04-16 16:54:54 +020030 struct {
Radek Krejci241f6b52020-05-21 18:13:49 +020031 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 +010032 int fd; /**< original file descriptor, which was not used directly because of missing vdprintf() */
Radek Krejci241f6b52020-05-21 18:13:49 +020033 } fdstream; /**< structure for LY_OUT_FDSTREAM type, which is LY_OUT_FD when vdprintf() is missing */
Radek Krejcia5bba312020-01-09 15:41:20 +010034 struct {
Radek Krejci241f6b52020-05-21 18:13:49 +020035 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 +010036 char *filepath; /**< stored original filepath */
Radek Krejci241f6b52020-05-21 18:13:49 +020037 } fpath; /**< filepath structure for LY_OUT_FILEPATH */
Radek Krejcia5bba312020-01-09 15:41:20 +010038 struct {
39 char **buf; /**< storage for the pointer to the memory buffer to store the output */
Radek Krejcie7b95092019-05-15 11:03:07 +020040 size_t len; /**< number of used bytes in the buffer */
41 size_t size; /**< allocated size of the buffer */
Radek Krejci241f6b52020-05-21 18:13:49 +020042 } mem; /**< memory buffer information for LY_OUT_MEMORY type */
Radek Krejcid3ca0632019-04-16 16:54:54 +020043 struct {
Radek Krejcia5bba312020-01-09 15:41:20 +010044 ssize_t (*func)(void *arg, const void *buf, size_t count); /**< callback function */
Radek Krejcie7b95092019-05-15 11:03:07 +020045 void *arg; /**< optional argument for the callback function */
Radek Krejci241f6b52020-05-21 18:13:49 +020046 } clb; /**< printer callback for LY_OUT_CALLBACK type */
Radek Krejcie7b95092019-05-15 11:03:07 +020047 } method; /**< type-specific information about the output */
Radek Krejcid3ca0632019-04-16 16:54:54 +020048
Michal Vasko63f3d842020-07-08 10:10:14 +020049 /* LYB only */
50 char *buffered; /**< additional buffer for holes */
51 size_t buf_len; /**< number of used bytes in the additional buffer for holes */
52 size_t buf_size; /**< allocated size of the buffer for holes */
53 size_t hole_count; /**< hole counter */
Radek Krejcid3ca0632019-04-16 16:54:54 +020054
Michal Vasko63f3d842020-07-08 10:10:14 +020055 size_t printed; /**< Total number of printed bytes */
56 size_t func_printed; /**< Number of bytes printed by the last function */
Radek Krejci897ad2e2019-04-29 16:43:07 +020057
Michal Vasko44685da2020-03-17 15:38:06 +010058 const struct ly_ctx *ctx; /**< libyang context for error logging */
Radek Krejcie7b95092019-05-15 11:03:07 +020059 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 */
Radek Krejci241f6b52020-05-21 18:13:49 +020088LY_ERR yang_print_parsed(struct ly_out *out, const struct lys_module *module);
Radek Krejcid3ca0632019-04-16 16:54:54 +020089
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).
Radek Krejcid8c0f5e2019-11-17 12:18:34 +080099 * @param[in] options Schema output options (see @ref schemaprinterflags).
Radek Krejcie7b95092019-05-15 11:03:07 +0200100 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
Radek Krejcid3ca0632019-04-16 16:54:54 +0200101 */
Radek Krejci241f6b52020-05-21 18:13:49 +0200102LY_ERR yang_print_compiled(struct ly_out *out, const struct lys_module *module, int options);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800103
104/**
105 * @brief YANG printer of the compiled schema node
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] node Schema node to be printed including all its substatements.
113 * @param[in] options Schema output options (see @ref schemaprinterflags).
114 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
115 */
Radek Krejci241f6b52020-05-21 18:13:49 +0200116LY_ERR yang_print_compiled_node(struct ly_out *out, const struct lysc_node *node, int options);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200117
Radek Krejcie7b95092019-05-15 11:03:07 +0200118/**
FredGand944bdc2019-11-05 21:57:07 +0800119 * @brief YIN printer of the parsed schemas. Full YIN printer.
120 *
121 * @param[in] out Output specification.
122 * @param[in] module Schema to be printed (the parsed member is used).
123 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
124 */
Radek Krejci241f6b52020-05-21 18:13:49 +0200125LY_ERR yin_print_parsed(struct ly_out *out, const struct lys_module *module);
FredGand944bdc2019-11-05 21:57:07 +0800126
127/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200128 * @brief XML printer of YANG data.
Radek Krejcie7b95092019-05-15 11:03:07 +0200129 *
130 * @param[in] out Output specification.
131 * @param[in] root The root element of the (sub)tree to print.
132 * @param[in] options [Data printer flags](@ref dataprinterflags).
133 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
134 */
Radek Krejci241f6b52020-05-21 18:13:49 +0200135LY_ERR xml_print_data(struct ly_out *out, const struct lyd_node *root, int options);
Radek Krejcie7b95092019-05-15 11:03:07 +0200136
137/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200138 * @brief LYB printer of YANG data.
139 *
140 * @param[in] out Output structure.
141 * @param[in] root The root element of the (sub)tree to print.
142 * @param[in] options [Data printer flags](@ref dataprinterflags).
143 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
144 */
145LY_ERR lyb_print_data(struct ly_out *out, const struct lyd_node *root, int options);
146
147/**
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100148 * @brief Check whether a node value equals to its default one.
149 *
150 * @param[in] node Term node to test.
151 * @return 0 if no,
152 * @return non-zero if yes.
153 */
154int ly_is_default(const struct lyd_node *node);
155
156/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100157 * @brief Check whether the node should even be printed.
158 *
159 * @param[in] node Node to check.
160 * @param[in] options Printer options.
161 * @return 0 if no.
162 * @return non-zero if yes.
163 */
164int ly_should_print(const struct lyd_node *node, int options);
165
166/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200167 * @brief Create a hole in the output data that will be filled later.
168 *
169 * @param[in] out Output specification.
170 * @param[in] len Length of the created hole.
171 * @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 +0200172 * @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 +0200173 * only in case the data are really written into the output.
Radek Krejcibaeb8382020-05-27 16:44:53 +0200174 * @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 +0200175 */
Radek Krejcibaeb8382020-05-27 16:44:53 +0200176ssize_t ly_write_skip(struct ly_out *out, size_t len, size_t *position);
Radek Krejcie7b95092019-05-15 11:03:07 +0200177
178/**
179 * @brief Write data into the hole at given position.
180 *
181 * @param[in] out Output specification.
182 * @param[in] position Position of the hole to fill, the value was provided by ly_write_skip().
183 * @param[in] buf Memory buffer with the data to print.
184 * @param[in] len Length of the data to print in the @p buf. Not that the length must correspond
185 * to the len value specified in the corresponding ly_write_skip() call.
Michal Vasko66d99972020-06-29 13:37:42 +0200186 * @return LY_SUCCESS on success.
187 * @return LY_ERR value in case of error.
Radek Krejcie7b95092019-05-15 11:03:07 +0200188 */
Michal Vasko66d99972020-06-29 13:37:42 +0200189LY_ERR ly_write_skipped(struct ly_out *out, size_t position, const char *buf, size_t len);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200190
191#endif /* LY_PRINTER_INTERNAL_H_ */