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 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 22 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 23 | * @brief Printer output structure specifying where the data are printed. |
| 24 | */ |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 25 | struct ly_out { |
| 26 | 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] | 27 | union { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 28 | 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 Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 30 | struct { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 31 | 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] | 32 | 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] | 33 | } 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] | 34 | struct { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 35 | 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] | 36 | char *filepath; /**< stored original filepath */ |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 37 | } fpath; /**< filepath structure for LY_OUT_FILEPATH */ |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 38 | struct { |
| 39 | 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] | 40 | size_t len; /**< number of used bytes in the buffer */ |
| 41 | size_t size; /**< allocated size of the buffer */ |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 42 | } mem; /**< memory buffer information for LY_OUT_MEMORY type */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 43 | struct { |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 44 | 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] | 45 | void *arg; /**< optional argument for the callback function */ |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 46 | } clb; /**< printer callback for LY_OUT_CALLBACK type */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 47 | } method; /**< type-specific information about the output */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 48 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 49 | 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 Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 53 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 54 | size_t printed; /**< Number of printed bytes */ |
Radek Krejci | 897ad2e | 2019-04-29 16:43:07 +0200 | [diff] [blame] | 55 | |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 56 | const struct ly_ctx *ctx; /**< libyang context for error logging */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 57 | LY_ERR status; /**< current status of the printer */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 58 | }; |
| 59 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 60 | /** |
| 61 | * @brief Informational structure for YANG statements |
| 62 | */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 63 | struct ext_substmt_info_s { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 64 | 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 Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 67 | #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 */ |
| 72 | extern struct ext_substmt_info_s ext_substmt_info[]; |
| 73 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 74 | /** |
| 75 | * @brief macro to check current status of the printer. |
| 76 | */ |
| 77 | #define LYOUT_CHECK(LYOUT, ...) if (LYOUT->status) {return __VA_ARGS__;} |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 78 | |
| 79 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 80 | * @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 Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 85 | */ |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 86 | LY_ERR yang_print_parsed(struct ly_out *out, const struct lys_module *module); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 87 | |
| 88 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 89 | * @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 Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 97 | * @param[in] options Schema output options (see @ref schemaprinterflags). |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 98 | * @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] | 99 | */ |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 100 | LY_ERR yang_print_compiled(struct ly_out *out, const struct lys_module *module, int options); |
Radek Krejci | d8c0f5e | 2019-11-17 12:18:34 +0800 | [diff] [blame] | 101 | |
| 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 Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 114 | LY_ERR yang_print_compiled_node(struct ly_out *out, const struct lysc_node *node, int options); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 115 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 116 | /** |
FredGan | d944bdc | 2019-11-05 21:57:07 +0800 | [diff] [blame] | 117 | * @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 Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 123 | LY_ERR yin_print_parsed(struct ly_out *out, const struct lys_module *module); |
FredGan | d944bdc | 2019-11-05 21:57:07 +0800 | [diff] [blame] | 124 | |
| 125 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 126 | * @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 Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 133 | LY_ERR xml_print_data(struct ly_out *out, const struct lyd_node *root, int options); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 134 | |
| 135 | /** |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 136 | * @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 | */ |
| 142 | int ly_is_default(const struct lyd_node *node); |
| 143 | |
| 144 | /** |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 145 | * @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 | */ |
| 152 | int ly_should_print(const struct lyd_node *node, int options); |
| 153 | |
| 154 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 155 | * @brief Flush the output from any internal buffers and clean any auxiliary data. |
| 156 | * @param[in] out Output specification. |
| 157 | */ |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 158 | void ly_print_flush(struct ly_out *out); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 159 | |
| 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 Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 169 | 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] | 170 | |
| 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 Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 182 | 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] | 183 | |
| 184 | #endif /* LY_PRINTER_INTERNAL_H_ */ |