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 | |
| 18 | #include "printer_schema.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 19 | #include "printer_data.h" |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 20 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 21 | /** |
| 22 | * @brief Types of the printer's output |
| 23 | */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 24 | typedef enum LYOUT_TYPE { |
| 25 | LYOUT_FD, /**< file descriptor */ |
| 26 | LYOUT_STREAM, /**< FILE stream */ |
Radek Krejci | 4a0ed4a | 2019-04-18 15:08:34 +0200 | [diff] [blame] | 27 | LYOUT_FDSTREAM, /**< FILE stream based on duplicated file descriptor */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 28 | LYOUT_MEMORY, /**< memory */ |
| 29 | LYOUT_CALLBACK /**< print via provided callback */ |
| 30 | } LYOUT_TYPE; |
| 31 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 32 | /** |
| 33 | * @brief Printer output structure specifying where the data are printed. |
| 34 | */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 35 | struct lyout { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 36 | LYOUT_TYPE type; /**< type of the output to select the output method */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 37 | union { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 38 | int fd; /**< file descriptor for LYOUT_FD type */ |
| 39 | FILE *f; /**< file structure for LYOUT_STREAM and LYOUT_FDSTREAM types */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 40 | struct { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 41 | 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 Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 45 | struct { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 46 | 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 Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 50 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 51 | 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 Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 55 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 56 | size_t printed; /**< Number of printed bytes */ |
Radek Krejci | 897ad2e | 2019-04-29 16:43:07 +0200 | [diff] [blame] | 57 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 58 | struct ly_ctx *ctx; /**< libyang context for error logging */ |
| 59 | LY_ERR status; /**< current status of the printer */ |
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 */ |
| 68 | int 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 | /** |
| 77 | * @brief macro to check current status of the printer. |
| 78 | */ |
| 79 | #define LYOUT_CHECK(LYOUT, ...) if (LYOUT->status) {return __VA_ARGS__;} |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 80 | |
| 81 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 82 | * @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 Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 87 | */ |
| 88 | LY_ERR yang_print_parsed(struct lyout *out, const struct lys_module *module); |
| 89 | |
| 90 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 91 | * @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 Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 100 | */ |
| 101 | LY_ERR yang_print_compiled(struct lyout *out, const struct lys_module *module); |
| 102 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 103 | /** |
FredGan | d944bdc | 2019-11-05 21:57:07 +0800 | [diff] [blame] | 104 | * @brief YIN printer of the parsed schemas. Full YIN printer. |
| 105 | * |
| 106 | * @param[in] out Output specification. |
| 107 | * @param[in] module Schema to be printed (the parsed member is used). |
| 108 | * @return LY_ERR value, number of the printed bytes is updated in lyout::printed. |
| 109 | */ |
| 110 | LY_ERR yin_print_parsed(struct lyout *out, const struct lys_module *module); |
| 111 | |
| 112 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 113 | * @brief XML printer of the YANG data. |
| 114 | * |
| 115 | * @param[in] out Output specification. |
| 116 | * @param[in] root The root element of the (sub)tree to print. |
| 117 | * @param[in] options [Data printer flags](@ref dataprinterflags). |
| 118 | * @return LY_ERR value, number of the printed bytes is updated in lyout::printed. |
| 119 | */ |
| 120 | LY_ERR xml_print_data(struct lyout *out, const struct lyd_node *root, int options); |
| 121 | |
| 122 | /** |
| 123 | * @brief Generic printer of the given format string into the specified output. |
| 124 | * |
| 125 | * Alternatively, ly_write() can be used. |
| 126 | * |
| 127 | * @param[in] out Output specification. |
| 128 | * @param[in] format format string to be printed. |
| 129 | * @return LY_ERR value, number of the printed bytes is updated in lyout::printed. |
| 130 | */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 131 | LY_ERR ly_print(struct lyout *out, const char *format, ...); |
| 132 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 133 | /** |
| 134 | * @brief Flush the output from any internal buffers and clean any auxiliary data. |
| 135 | * @param[in] out Output specification. |
| 136 | */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 137 | void ly_print_flush(struct lyout *out); |
| 138 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 139 | /** |
| 140 | * @brief Generic printer of the given string buffer into the specified output. |
| 141 | * |
| 142 | * Alternatively, ly_print() can be used. |
| 143 | * |
| 144 | * As an extension for printing holes (skipping some data until they are known), |
| 145 | * ly_write_skip() and ly_write_skipped() can be used. |
| 146 | * |
| 147 | * @param[in] out Output specification. |
| 148 | * @param[in] buf Memory buffer with the data to print. |
| 149 | * @param[in] len Length of the data to print in the @p buf. |
| 150 | * @return LY_ERR value, number of the printed bytes is updated in lyout::printed. |
| 151 | */ |
| 152 | LY_ERR ly_write(struct lyout *out, const char *buf, size_t len); |
| 153 | |
| 154 | /** |
| 155 | * @brief Create a hole in the output data that will be filled later. |
| 156 | * |
| 157 | * @param[in] out Output specification. |
| 158 | * @param[in] len Length of the created hole. |
| 159 | * @param[out] position Position of the hole, value must be later provided to the ly_write_skipped() call. |
| 160 | * @return LY_ERR value. The number of the printed bytes is updated in lyout::printed |
| 161 | * only in case the data are really written into the output. |
| 162 | */ |
| 163 | LY_ERR ly_write_skip(struct lyout *out, size_t len, size_t *position); |
| 164 | |
| 165 | /** |
| 166 | * @brief Write data into the hole at given position. |
| 167 | * |
| 168 | * @param[in] out Output specification. |
| 169 | * @param[in] position Position of the hole to fill, the value was provided by ly_write_skip(). |
| 170 | * @param[in] buf Memory buffer with the data to print. |
| 171 | * @param[in] len Length of the data to print in the @p buf. Not that the length must correspond |
| 172 | * to the len value specified in the corresponding ly_write_skip() call. |
| 173 | * @return LY_ERR value. The number of the printed bytes is updated in lyout::printed |
| 174 | * only in case the data are really written into the output. |
| 175 | */ |
| 176 | LY_ERR ly_write_skipped(struct lyout *out, size_t position, const char *buf, size_t len); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 177 | |
| 178 | #endif /* LY_PRINTER_INTERNAL_H_ */ |