blob: 35102b5ad51e1d21dbdf8fa0a69162cc501b1c27 [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
Radek Krejcie7b95092019-05-15 11:03:07 +020049 char *buffered; /**< additional buffer for holes, used only for LYB data format */
50 size_t buf_len; /**< number of used bytes in the additional buffer for holes, used only for LYB data format */
51 size_t buf_size; /**< allocated size of the buffer for holes, used only for LYB data format */
52 size_t hole_count; /**< hole counter, used only for LYB data format */
Radek Krejcid3ca0632019-04-16 16:54:54 +020053
Radek Krejcie7b95092019-05-15 11:03:07 +020054 size_t printed; /**< Number of printed bytes */
Radek Krejci897ad2e2019-04-29 16:43:07 +020055
Michal Vasko44685da2020-03-17 15:38:06 +010056 const struct ly_ctx *ctx; /**< libyang context for error logging */
Radek Krejcie7b95092019-05-15 11:03:07 +020057 LY_ERR status; /**< current status of the printer */
Radek Krejcid3ca0632019-04-16 16:54:54 +020058};
59
Radek Krejcie7b95092019-05-15 11:03:07 +020060/**
61 * @brief Informational structure for YANG statements
62 */
Radek Krejcid3ca0632019-04-16 16:54:54 +020063struct ext_substmt_info_s {
Radek Krejcie7b95092019-05-15 11:03:07 +020064 const char *name; /**< name of the statement */
65 const char *arg; /**< name of YIN's attribute to present the statement */
66 int flags; /**< various flags to clarify printing of the statement */
Radek Krejcid3ca0632019-04-16 16:54:54 +020067#define SUBST_FLAG_YIN 0x1 /**< has YIN element */
68#define SUBST_FLAG_ID 0x2 /**< the value is identifier -> no quotes */
69};
70
71/* filled in printer.c */
72extern struct ext_substmt_info_s ext_substmt_info[];
73
Radek Krejcie7b95092019-05-15 11:03:07 +020074/**
75 * @brief macro to check current status of the printer.
76 */
77#define LYOUT_CHECK(LYOUT, ...) if (LYOUT->status) {return __VA_ARGS__;}
Radek Krejcid3ca0632019-04-16 16:54:54 +020078
79/**
Radek Krejcie7b95092019-05-15 11:03:07 +020080 * @brief YANG printer of the parsed schemas. Full YANG printer.
81 *
82 * @param[in] out Output specification.
83 * @param[in] module Schema to be printed (the parsed member is used).
84 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
Radek Krejcid3ca0632019-04-16 16:54:54 +020085 */
Radek Krejci241f6b52020-05-21 18:13:49 +020086LY_ERR yang_print_parsed(struct ly_out *out, const struct lys_module *module);
Radek Krejcid3ca0632019-04-16 16:54:54 +020087
88/**
Radek Krejcie7b95092019-05-15 11:03:07 +020089 * @brief YANG printer of the compiled schemas.
90 *
91 * This printer provides information about modules how they are understood by libyang.
92 * Despite the format is inspired by YANG, it is not fully compatible and should not be
93 * used as a standard YANG format.
94 *
95 * @param[in] out Output specification.
96 * @param[in] module Schema to be printed (the compiled member is used).
Radek Krejcid8c0f5e2019-11-17 12:18:34 +080097 * @param[in] options Schema output options (see @ref schemaprinterflags).
Radek Krejcie7b95092019-05-15 11:03:07 +020098 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
Radek Krejcid3ca0632019-04-16 16:54:54 +020099 */
Radek Krejci241f6b52020-05-21 18:13:49 +0200100LY_ERR yang_print_compiled(struct ly_out *out, const struct lys_module *module, int options);
Radek Krejcid8c0f5e2019-11-17 12:18:34 +0800101
102/**
103 * @brief YANG printer of the compiled schema node
104 *
105 * This printer provides information about modules how they are understood by libyang.
106 * Despite the format is inspired by YANG, it is not fully compatible and should not be
107 * used as a standard YANG format.
108 *
109 * @param[in] out Output specification.
110 * @param[in] node Schema node to be printed including all its substatements.
111 * @param[in] options Schema output options (see @ref schemaprinterflags).
112 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
113 */
Radek Krejci241f6b52020-05-21 18:13:49 +0200114LY_ERR yang_print_compiled_node(struct ly_out *out, const struct lysc_node *node, int options);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200115
Radek Krejcie7b95092019-05-15 11:03:07 +0200116/**
FredGand944bdc2019-11-05 21:57:07 +0800117 * @brief YIN printer of the parsed schemas. Full YIN printer.
118 *
119 * @param[in] out Output specification.
120 * @param[in] module Schema to be printed (the parsed member is used).
121 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
122 */
Radek Krejci241f6b52020-05-21 18:13:49 +0200123LY_ERR yin_print_parsed(struct ly_out *out, const struct lys_module *module);
FredGand944bdc2019-11-05 21:57:07 +0800124
125/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200126 * @brief XML printer of the YANG data.
127 *
128 * @param[in] out Output specification.
129 * @param[in] root The root element of the (sub)tree to print.
130 * @param[in] options [Data printer flags](@ref dataprinterflags).
131 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
132 */
Radek Krejci241f6b52020-05-21 18:13:49 +0200133LY_ERR xml_print_data(struct ly_out *out, const struct lyd_node *root, int options);
Radek Krejcie7b95092019-05-15 11:03:07 +0200134
135/**
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100136 * @brief Check whether a node value equals to its default one.
137 *
138 * @param[in] node Term node to test.
139 * @return 0 if no,
140 * @return non-zero if yes.
141 */
142int ly_is_default(const struct lyd_node *node);
143
144/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100145 * @brief Check whether the node should even be printed.
146 *
147 * @param[in] node Node to check.
148 * @param[in] options Printer options.
149 * @return 0 if no.
150 * @return non-zero if yes.
151 */
152int ly_should_print(const struct lyd_node *node, int options);
153
154/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200155 * @brief Flush the output from any internal buffers and clean any auxiliary data.
156 * @param[in] out Output specification.
157 */
Radek Krejci241f6b52020-05-21 18:13:49 +0200158void ly_print_flush(struct ly_out *out);
Radek Krejcie7b95092019-05-15 11:03:07 +0200159
160/**
161 * @brief Create a hole in the output data that will be filled later.
162 *
163 * @param[in] out Output specification.
164 * @param[in] len Length of the created hole.
165 * @param[out] position Position of the hole, value must be later provided to the ly_write_skipped() call.
166 * @return LY_ERR value. The number of the printed bytes is updated in lyout::printed
167 * only in case the data are really written into the output.
168 */
Radek Krejci241f6b52020-05-21 18:13:49 +0200169LY_ERR ly_write_skip(struct ly_out *out, size_t len, size_t *position);
Radek Krejcie7b95092019-05-15 11:03:07 +0200170
171/**
172 * @brief Write data into the hole at given position.
173 *
174 * @param[in] out Output specification.
175 * @param[in] position Position of the hole to fill, the value was provided by ly_write_skip().
176 * @param[in] buf Memory buffer with the data to print.
177 * @param[in] len Length of the data to print in the @p buf. Not that the length must correspond
178 * to the len value specified in the corresponding ly_write_skip() call.
179 * @return LY_ERR value. The number of the printed bytes is updated in lyout::printed
180 * only in case the data are really written into the output.
181 */
Radek Krejci241f6b52020-05-21 18:13:49 +0200182LY_ERR ly_write_skipped(struct ly_out *out, size_t position, const char *buf, size_t len);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200183
184#endif /* LY_PRINTER_INTERNAL_H_ */