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 | /** |
| 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 | */ |
| 111 | LY_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 Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 122 | LY_ERR ly_print(struct lyout *out, const char *format, ...); |
| 123 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 124 | /** |
| 125 | * @brief Flush the output from any internal buffers and clean any auxiliary data. |
| 126 | * @param[in] out Output specification. |
| 127 | */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 128 | void ly_print_flush(struct lyout *out); |
| 129 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 130 | /** |
| 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 | */ |
| 143 | LY_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 | */ |
| 154 | LY_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 | */ |
| 167 | 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] | 168 | |
| 169 | #endif /* LY_PRINTER_INTERNAL_H_ */ |