Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1 | /** |
| 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 Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 18 | #include "printer.h" |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 19 | #include "printer_schema.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 20 | #include "printer_data.h" |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 21 | |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 22 | struct lysp_module; |
| 23 | struct lysp_submodule; |
| 24 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 25 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 26 | * @brief Printer output structure specifying where the data are printed. |
| 27 | */ |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 28 | struct ly_out { |
| 29 | LY_OUT_TYPE type; /**< type of the output to select the output method */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 30 | union { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 31 | int fd; /**< file descriptor for LY_OUT_FD type */ |
| 32 | FILE *f; /**< file structure for LY_OUT_FILE, LY_OUT_FDSTREAM and LY_OUT_FILEPATH types */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 33 | struct { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 34 | FILE *f; /**< file stream from the original file descriptor, variable is mapped to the LY_OUT_FILE's f */ |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 35 | int fd; /**< original file descriptor, which was not used directly because of missing vdprintf() */ |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 36 | } fdstream; /**< structure for LY_OUT_FDSTREAM type, which is LY_OUT_FD when vdprintf() is missing */ |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 37 | struct { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 38 | FILE *f; /**< file structure for LY_OUT_FILEPATH, variable is mapped to the LY_OUT_FILE's f */ |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 39 | char *filepath; /**< stored original filepath */ |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 40 | } fpath; /**< filepath structure for LY_OUT_FILEPATH */ |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 41 | struct { |
| 42 | char **buf; /**< storage for the pointer to the memory buffer to store the output */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 43 | size_t len; /**< number of used bytes in the buffer */ |
| 44 | size_t size; /**< allocated size of the buffer */ |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 45 | } mem; /**< memory buffer information for LY_OUT_MEMORY type */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 46 | struct { |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 47 | ssize_t (*func)(void *arg, const void *buf, size_t count); /**< callback function */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 48 | void *arg; /**< optional argument for the callback function */ |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 49 | } clb; /**< printer callback for LY_OUT_CALLBACK type */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 50 | } method; /**< type-specific information about the output */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 51 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 52 | /* LYB only */ |
| 53 | char *buffered; /**< additional buffer for holes */ |
| 54 | size_t buf_len; /**< number of used bytes in the additional buffer for holes */ |
| 55 | size_t buf_size; /**< allocated size of the buffer for holes */ |
| 56 | size_t hole_count; /**< hole counter */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 57 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 58 | size_t printed; /**< Total number of printed bytes */ |
| 59 | size_t func_printed; /**< Number of bytes printed by the last function */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 60 | }; |
| 61 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 62 | /** |
| 63 | * @brief Informational structure for YANG statements |
| 64 | */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 65 | struct ext_substmt_info_s { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 66 | const char *name; /**< name of the statement */ |
| 67 | const char *arg; /**< name of YIN's attribute to present the statement */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 68 | uint8_t flags; /**< various flags to clarify printing of the statement */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 69 | #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 */ |
| 74 | extern struct ext_substmt_info_s ext_substmt_info[]; |
| 75 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 76 | /** |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 77 | * @brief YANG printer of the parsed module. Full YANG printer. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 78 | * |
| 79 | * @param[in] out Output specification. |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 80 | * @param[in] module Main module. |
| 81 | * @param[in] modp Parsed module to print. |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 82 | * @param[in] options Schema output options (see @ref schemaprinterflags). |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 83 | * @return LY_ERR value, number of the printed bytes is updated in lyout::printed. |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 84 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 85 | LY_ERR yang_print_parsed_module(struct ly_out *out, const struct lys_module *module, const struct lysp_module *modp, uint32_t options); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * @brief Helper macros for data printers |
| 89 | */ |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 90 | #define DO_FORMAT (!(ctx->options & LY_PRINT_SHRINK)) |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 91 | #define LEVEL ctx->level /**< current level */ |
| 92 | #define INDENT (DO_FORMAT ? (LEVEL)*2 : 0),"" /**< indentation parameters for printer functions */ |
| 93 | #define LEVEL_INC LEVEL++ /**< increase indentation level */ |
| 94 | #define LEVEL_DEC LEVEL-- /**< decrease indentation level */ |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 95 | |
| 96 | /** |
| 97 | * @brief YANG printer of the parsed submodule. Full YANG printer. |
| 98 | * |
| 99 | * @param[in] out Output specification. |
| 100 | * @param[in] module Main module. |
| 101 | * @param[in] submodp Parsed submodule to print. |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 102 | * @param[in] options Schema output options (see @ref schemaprinterflags). |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 103 | * @return LY_ERR value, number of the printed bytes is updated in lyout::printed. |
| 104 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 105 | LY_ERR yang_print_parsed_submodule(struct ly_out *out, const struct lys_module *module, const struct lysp_submodule *submodp, uint32_t options); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 106 | |
| 107 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 108 | * @brief YANG printer of the compiled schemas. |
| 109 | * |
| 110 | * This printer provides information about modules how they are understood by libyang. |
| 111 | * Despite the format is inspired by YANG, it is not fully compatible and should not be |
| 112 | * used as a standard YANG format. |
| 113 | * |
| 114 | * @param[in] out Output specification. |
| 115 | * @param[in] module Schema to be printed (the compiled member is used). |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 116 | * @param[in] options Schema output options (see @ref schemaprinterflags). |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 117 | * @return LY_ERR value, number of the printed bytes is updated in lyout::printed. |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 118 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 119 | LY_ERR yang_print_compiled(struct ly_out *out, const struct lys_module *module, uint32_t options); |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 120 | |
| 121 | /** |
| 122 | * @brief YANG printer of the compiled schema node |
| 123 | * |
| 124 | * This printer provides information about modules how they are understood by libyang. |
| 125 | * Despite the format is inspired by YANG, it is not fully compatible and should not be |
| 126 | * used as a standard YANG format. |
| 127 | * |
| 128 | * @param[in] out Output specification. |
| 129 | * @param[in] node Schema node to be printed including all its substatements. |
| 130 | * @param[in] options Schema output options (see @ref schemaprinterflags). |
| 131 | * @return LY_ERR value, number of the printed bytes is updated in lyout::printed. |
| 132 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 133 | LY_ERR yang_print_compiled_node(struct ly_out *out, const struct lysc_node *node, uint32_t options); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 134 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 135 | /** |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 136 | * @brief YIN printer of the parsed module. Full YIN printer. |
FredGan | d944bdc | 2019-11-05 21:57:07 +0800 | [diff] [blame] | 137 | * |
| 138 | * @param[in] out Output specification. |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 139 | * @param[in] module Main module. |
| 140 | * @param[in] modp Parsed module to print. |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 141 | * @param[in] options Schema output options (see @ref schemaprinterflags). |
FredGan | d944bdc | 2019-11-05 21:57:07 +0800 | [diff] [blame] | 142 | * @return LY_ERR value, number of the printed bytes is updated in lyout::printed. |
| 143 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 144 | LY_ERR yin_print_parsed_module(struct ly_out *out, const struct lys_module *module, const struct lysp_module *modp, uint32_t options); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 145 | |
| 146 | /** |
| 147 | * @brief YIN printer of the parsed submodule. Full YIN printer. |
| 148 | * |
| 149 | * @param[in] out Output specification. |
| 150 | * @param[in] module Main module. |
| 151 | * @param[in] submodp Parsed submodule to print. |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 152 | * @param[in] options Schema output options (see @ref schemaprinterflags). |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 153 | * @return LY_ERR value, number of the printed bytes is updated in lyout::printed. |
| 154 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 155 | LY_ERR yin_print_parsed_submodule(struct ly_out *out, const struct lys_module *module, const struct lysp_submodule *submodp, uint32_t options); |
FredGan | d944bdc | 2019-11-05 21:57:07 +0800 | [diff] [blame] | 156 | |
| 157 | /** |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 158 | * @brief XML printer of YANG data. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 159 | * |
| 160 | * @param[in] out Output specification. |
| 161 | * @param[in] root The root element of the (sub)tree to print. |
| 162 | * @param[in] options [Data printer flags](@ref dataprinterflags). |
| 163 | * @return LY_ERR value, number of the printed bytes is updated in lyout::printed. |
| 164 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 165 | LY_ERR xml_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 166 | |
| 167 | /** |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 168 | * @brief JSON printer of YANG data. |
| 169 | * |
| 170 | * @param[in] out Output specification. |
| 171 | * @param[in] root The root element of the (sub)tree to print. |
| 172 | * @param[in] options [Data printer flags](@ref dataprinterflags). |
| 173 | * @return LY_ERR value, number of the printed bytes is updated in lyout::printed. |
| 174 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 175 | LY_ERR json_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options); |
Radek Krejci | 5536d28 | 2020-08-04 23:27:44 +0200 | [diff] [blame] | 176 | |
| 177 | /** |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 178 | * @brief LYB printer of YANG data. |
| 179 | * |
| 180 | * @param[in] out Output structure. |
| 181 | * @param[in] root The root element of the (sub)tree to print. |
| 182 | * @param[in] options [Data printer flags](@ref dataprinterflags). |
| 183 | * @return LY_ERR value, number of the printed bytes is updated in lyout::printed. |
| 184 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 185 | LY_ERR lyb_print_data(struct ly_out *out, const struct lyd_node *root, uint32_t options); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 186 | |
| 187 | /** |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 188 | * @brief Check whether the node should even be printed. |
| 189 | * |
| 190 | * @param[in] node Node to check. |
| 191 | * @param[in] options Printer options. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 192 | * @return false (no, it should not be printed) or true (yes, it is supposed to be printed) |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 193 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 194 | ly_bool ly_should_print(const struct lyd_node *node, uint32_t options); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 195 | |
| 196 | /** |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 197 | * @brief Generic printer of the given format string into the specified output. |
| 198 | * |
| 199 | * Does not reset printed bytes. Adds to printed bytes. |
| 200 | * |
| 201 | * @param[in] out Output specification. |
| 202 | * @param[in] format Format string to be printed. |
| 203 | * @return LY_ERR value. |
| 204 | */ |
| 205 | LY_ERR ly_print_(struct ly_out *out, const char *format, ...); |
| 206 | |
| 207 | /** |
| 208 | * @brief Generic printer of the given string buffer into the specified output. |
| 209 | * |
| 210 | * Does not reset printed bytes. Adds to printed bytes. |
| 211 | * |
| 212 | * @param[in] out Output specification. |
| 213 | * @param[in] buf Memory buffer with the data to print. |
| 214 | * @param[in] len Length of the data to print in the @p buf. |
| 215 | * @return LY_ERR value. |
| 216 | */ |
| 217 | LY_ERR ly_write_(struct ly_out *out, const char *buf, size_t len); |
| 218 | |
| 219 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 220 | * @brief Create a hole in the output data that will be filled later. |
| 221 | * |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 222 | * Adds printed bytes. |
| 223 | * |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 224 | * @param[in] out Output specification. |
| 225 | * @param[in] len Length of the created hole. |
| 226 | * @param[out] position Position of the hole, value must be later provided to the ly_write_skipped() call. |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 227 | * @return LY_ERR value. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 228 | */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 229 | LY_ERR ly_write_skip(struct ly_out *out, size_t len, size_t *position); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 230 | |
| 231 | /** |
| 232 | * @brief Write data into the hole at given position. |
| 233 | * |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 234 | * Does not change printed bytes. |
| 235 | * |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 236 | * @param[in] out Output specification. |
| 237 | * @param[in] position Position of the hole to fill, the value was provided by ly_write_skip(). |
| 238 | * @param[in] buf Memory buffer with the data to print. |
| 239 | * @param[in] len Length of the data to print in the @p buf. Not that the length must correspond |
| 240 | * to the len value specified in the corresponding ly_write_skip() call. |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 241 | * @return LY_ERR value. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 242 | */ |
Michal Vasko | 66d9997 | 2020-06-29 13:37:42 +0200 | [diff] [blame] | 243 | LY_ERR ly_write_skipped(struct ly_out *out, size_t position, const char *buf, size_t len); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 244 | |
| 245 | #endif /* LY_PRINTER_INTERNAL_H_ */ |