blob: 7d0e312df92172ecd2e16bf32a7a1f30f34f85c3 [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/**
104 * @brief XML printer of the YANG data.
105 *
106 * @param[in] out Output specification.
107 * @param[in] root The root element of the (sub)tree to print.
108 * @param[in] options [Data printer flags](@ref dataprinterflags).
109 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
110 */
111LY_ERR xml_print_data(struct lyout *out, const struct lyd_node *root, int options);
112
113/**
114 * @brief Generic printer of the given format string into the specified output.
115 *
116 * Alternatively, ly_write() can be used.
117 *
118 * @param[in] out Output specification.
119 * @param[in] format format string to be printed.
120 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
121 */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200122LY_ERR ly_print(struct lyout *out, const char *format, ...);
123
Radek Krejcie7b95092019-05-15 11:03:07 +0200124/**
125 * @brief Flush the output from any internal buffers and clean any auxiliary data.
126 * @param[in] out Output specification.
127 */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200128void ly_print_flush(struct lyout *out);
129
Radek Krejcie7b95092019-05-15 11:03:07 +0200130/**
131 * @brief Generic printer of the given string buffer into the specified output.
132 *
133 * Alternatively, ly_print() can be used.
134 *
135 * As an extension for printing holes (skipping some data until they are known),
136 * ly_write_skip() and ly_write_skipped() can be used.
137 *
138 * @param[in] out Output specification.
139 * @param[in] buf Memory buffer with the data to print.
140 * @param[in] len Length of the data to print in the @p buf.
141 * @return LY_ERR value, number of the printed bytes is updated in lyout::printed.
142 */
143LY_ERR ly_write(struct lyout *out, const char *buf, size_t len);
144
145/**
146 * @brief Create a hole in the output data that will be filled later.
147 *
148 * @param[in] out Output specification.
149 * @param[in] len Length of the created hole.
150 * @param[out] position Position of the hole, value must be later provided to the ly_write_skipped() call.
151 * @return LY_ERR value. The number of the printed bytes is updated in lyout::printed
152 * only in case the data are really written into the output.
153 */
154LY_ERR ly_write_skip(struct lyout *out, size_t len, size_t *position);
155
156/**
157 * @brief Write data into the hole at given position.
158 *
159 * @param[in] out Output specification.
160 * @param[in] position Position of the hole to fill, the value was provided by ly_write_skip().
161 * @param[in] buf Memory buffer with the data to print.
162 * @param[in] len Length of the data to print in the @p buf. Not that the length must correspond
163 * to the len value specified in the corresponding ly_write_skip() call.
164 * @return LY_ERR value. The number of the printed bytes is updated in lyout::printed
165 * only in case the data are really written into the output.
166 */
167LY_ERR ly_write_skipped(struct lyout *out, size_t position, const char *buf, size_t len);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200168
169#endif /* LY_PRINTER_INTERNAL_H_ */