Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file printer.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 4 | * @brief Generic libyang printers functions. |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 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 | #include "common.h" |
| 16 | |
| 17 | #include <errno.h> |
| 18 | #include <stdarg.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 21 | #include <string.h> |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 22 | #include <sys/types.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 23 | #include <unistd.h> |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 24 | #include <assert.h> |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 25 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 26 | #include "log.h" |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 27 | #include "printer_internal.h" |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 28 | #include "plugins_types.h" |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * @brief informational structure shared by printers |
| 32 | */ |
| 33 | struct ext_substmt_info_s ext_substmt_info[] = { |
| 34 | {NULL, NULL, 0}, /**< LYEXT_SUBSTMT_SELF */ |
| 35 | {"argument", "name", SUBST_FLAG_ID}, /**< LYEXT_SUBSTMT_ARGUMENT */ |
| 36 | {"base", "name", SUBST_FLAG_ID}, /**< LYEXT_SUBSTMT_BASE */ |
| 37 | {"belongs-to", "module", SUBST_FLAG_ID}, /**< LYEXT_SUBSTMT_BELONGSTO */ |
| 38 | {"contact", "text", SUBST_FLAG_YIN}, /**< LYEXT_SUBSTMT_CONTACT */ |
| 39 | {"default", "value", 0}, /**< LYEXT_SUBSTMT_DEFAULT */ |
| 40 | {"description", "text", SUBST_FLAG_YIN}, /**< LYEXT_SUBSTMT_DESCRIPTION */ |
| 41 | {"error-app-tag", "value", 0}, /**< LYEXT_SUBSTMT_ERRTAG */ |
| 42 | {"error-message", "value", SUBST_FLAG_YIN}, /**< LYEXT_SUBSTMT_ERRMSG */ |
| 43 | {"key", "value", 0}, /**< LYEXT_SUBSTMT_KEY */ |
| 44 | {"namespace", "uri", 0}, /**< LYEXT_SUBSTMT_NAMESPACE */ |
| 45 | {"organization", "text", SUBST_FLAG_YIN}, /**< LYEXT_SUBSTMT_ORGANIZATION */ |
| 46 | {"path", "value", 0}, /**< LYEXT_SUBSTMT_PATH */ |
| 47 | {"prefix", "value", SUBST_FLAG_ID}, /**< LYEXT_SUBSTMT_PREFIX */ |
| 48 | {"presence", "value", 0}, /**< LYEXT_SUBSTMT_PRESENCE */ |
| 49 | {"reference", "text", SUBST_FLAG_YIN}, /**< LYEXT_SUBSTMT_REFERENCE */ |
| 50 | {"revision-date", "date", SUBST_FLAG_ID}, /**< LYEXT_SUBSTMT_REVISIONDATE */ |
| 51 | {"units", "name", 0}, /**< LYEXT_SUBSTMT_UNITS */ |
| 52 | {"value", "value", SUBST_FLAG_ID}, /**< LYEXT_SUBSTMT_VALUE */ |
| 53 | {"yang-version", "value", SUBST_FLAG_ID}, /**< LYEXT_SUBSTMT_VERSION */ |
| 54 | {"modifier", "value", SUBST_FLAG_ID}, /**< LYEXT_SUBSTMT_MODIFIER */ |
| 55 | {"require-instance", "value", SUBST_FLAG_ID}, /**< LYEXT_SUBSTMT_REQINST */ |
| 56 | {"yin-element", "value", SUBST_FLAG_ID}, /**< LYEXT_SUBSTMT_YINELEM */ |
| 57 | {"config", "value", SUBST_FLAG_ID}, /**< LYEXT_SUBSTMT_CONFIG */ |
| 58 | {"mandatory", "value", SUBST_FLAG_ID}, /**< LYEXT_SUBSTMT_MANDATORY */ |
| 59 | {"ordered-by", "value", SUBST_FLAG_ID}, /**< LYEXT_SUBSTMT_ORDEREDBY */ |
| 60 | {"status", "value", SUBST_FLAG_ID}, /**< LYEXT_SUBSTMT_STATUS */ |
| 61 | {"fraction-digits", "value", SUBST_FLAG_ID}, /**< LYEXT_SUBSTMT_DIGITS */ |
| 62 | {"max-elements", "value", SUBST_FLAG_ID}, /**< LYEXT_SUBSTMT_MAX */ |
| 63 | {"min-elements", "value", SUBST_FLAG_ID}, /**< LYEXT_SUBSTMT_MIN */ |
| 64 | {"position", "value", SUBST_FLAG_ID}, /**< LYEXT_SUBSTMT_POSITION */ |
| 65 | {"unique", "tag", 0}, /**< LYEXT_SUBSTMT_UNIQUE */ |
| 66 | }; |
| 67 | |
Michal Vasko | 6f4cbb6 | 2020-02-28 11:15:47 +0100 | [diff] [blame] | 68 | int |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 69 | ly_is_default(const struct lyd_node *node) |
| 70 | { |
| 71 | const struct lysc_node_leaf *leaf; |
| 72 | const struct lysc_node_leaflist *llist; |
| 73 | const struct lyd_node_term *term; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 74 | LY_ARRAY_SIZE_TYPE u; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 75 | |
| 76 | assert(node->schema->nodetype & LYD_NODE_TERM); |
| 77 | term = (const struct lyd_node_term *)node; |
| 78 | |
| 79 | if (node->schema->nodetype == LYS_LEAF) { |
| 80 | leaf = (const struct lysc_node_leaf *)node->schema; |
| 81 | if (!leaf->dflt) { |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | /* compare with the default value */ |
| 86 | if (leaf->type->plugin->compare(&term->value, leaf->dflt)) { |
| 87 | return 0; |
| 88 | } |
| 89 | } else { |
| 90 | llist = (const struct lysc_node_leaflist *)node->schema; |
| 91 | if (!llist->dflts) { |
| 92 | return 0; |
| 93 | } |
| 94 | |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 95 | LY_ARRAY_FOR(llist->dflts, u) { |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 96 | /* compare with each possible default value */ |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 97 | if (llist->type->plugin->compare(&term->value, llist->dflts[u])) { |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 98 | return 0; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return 1; |
| 104 | } |
| 105 | |
| 106 | int |
| 107 | ly_should_print(const struct lyd_node *node, int options) |
| 108 | { |
| 109 | const struct lyd_node *next, *elem; |
| 110 | |
| 111 | if (options & LYDP_WD_TRIM) { |
| 112 | /* do not print default nodes */ |
| 113 | if (node->flags & LYD_DEFAULT) { |
| 114 | /* implicit default node/NP container with only default nodes */ |
| 115 | return 0; |
| 116 | } else if (node->schema->nodetype & LYD_NODE_TERM) { |
| 117 | if (ly_is_default(node)) { |
| 118 | /* explicit default node */ |
| 119 | return 0; |
| 120 | } |
| 121 | } |
| 122 | } else if ((node->flags & LYD_DEFAULT) && !(options & LYDP_WD_MASK) && !(node->schema->flags & LYS_CONFIG_R)) { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 123 | /* LYDP_WD_EXPLICIT |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 124 | * - print only if it contains status data in its subtree */ |
| 125 | LYD_TREE_DFS_BEGIN(node, next, elem) { |
| 126 | if (elem->schema->flags & LYS_CONFIG_R) { |
| 127 | return 1; |
| 128 | } |
| 129 | LYD_TREE_DFS_END(node, next, elem) |
| 130 | } |
| 131 | return 0; |
| 132 | } else if ((node->flags & LYD_DEFAULT) && (node->schema->nodetype == LYS_CONTAINER) && !(options & LYDP_KEEPEMPTYCONT)) { |
| 133 | /* avoid empty default containers */ |
| 134 | LYD_TREE_DFS_BEGIN(node, next, elem) { |
| 135 | if (elem->schema->nodetype != LYS_CONTAINER) { |
| 136 | return 1; |
| 137 | } |
| 138 | assert(elem->flags & LYD_DEFAULT); |
| 139 | LYD_TREE_DFS_END(node, next, elem) |
| 140 | } |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | return 1; |
| 145 | } |
| 146 | |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 147 | API LY_OUT_TYPE |
| 148 | ly_out_type(const struct ly_out *out) |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 149 | { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 150 | LY_CHECK_ARG_RET(NULL, out, LY_OUT_ERROR); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 151 | return out->type; |
| 152 | } |
| 153 | |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 154 | API struct ly_out * |
| 155 | ly_out_new_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg) |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 156 | { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 157 | struct ly_out *out; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 158 | |
| 159 | out = calloc(1, sizeof *out); |
| 160 | LY_CHECK_ERR_RET(!out, LOGMEM(NULL), NULL); |
| 161 | |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 162 | out->type = LY_OUT_CALLBACK; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 163 | out->method.clb.func = writeclb; |
| 164 | out->method.clb.arg = arg; |
| 165 | |
| 166 | return out; |
| 167 | } |
| 168 | |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 169 | API ssize_t (*ly_out_clb(struct ly_out *out, ssize_t (*writeclb)(void *arg, const void *buf, size_t count)))(void *arg, const void *buf, size_t count) |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 170 | { |
| 171 | void *prev_clb; |
| 172 | |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 173 | LY_CHECK_ARG_RET(NULL, out, out->type == LY_OUT_CALLBACK, NULL); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 174 | |
| 175 | prev_clb = out->method.clb.func; |
| 176 | |
| 177 | if (writeclb) { |
| 178 | out->method.clb.func = writeclb; |
| 179 | } |
| 180 | |
| 181 | return prev_clb; |
| 182 | } |
| 183 | |
| 184 | API void * |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 185 | ly_out_clb_arg(struct ly_out *out, void *arg) |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 186 | { |
| 187 | void *prev_arg; |
| 188 | |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 189 | LY_CHECK_ARG_RET(NULL, out, out->type == LY_OUT_CALLBACK, NULL); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 190 | |
| 191 | prev_arg = out->method.clb.arg; |
| 192 | |
| 193 | if (arg) { |
| 194 | out->method.clb.arg = arg; |
| 195 | } |
| 196 | |
| 197 | return prev_arg; |
| 198 | } |
| 199 | |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 200 | API struct ly_out * |
| 201 | ly_out_new_fd(int fd) |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 202 | { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 203 | struct ly_out *out; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 204 | |
| 205 | out = calloc(1, sizeof *out); |
| 206 | LY_CHECK_ERR_RET(!out, LOGMEM(NULL), NULL); |
| 207 | |
| 208 | #ifdef HAVE_VDPRINTF |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 209 | out->type = LY_OUT_FD; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 210 | out->method.fd = fd; |
| 211 | #else |
| 212 | /* Without vdfprintf(), change the printing method to printing to a FILE stream. |
| 213 | * To preserve the original file descriptor, duplicate it and use it to open file stream. */ |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 214 | out->type = LY_OUT_FDSTREAM; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 215 | out->method.fdstream.fd = fd; |
| 216 | |
| 217 | fd = dup(out->method.fdstream.fd); |
| 218 | if (fd < 0) { |
| 219 | LOGERR(NULL, LY_ESYS, "Unable to duplicate provided file descriptor (%d) for printing the output (%s).", |
| 220 | out->method.fdstream.fd, strerror(errno)); |
| 221 | free(out); |
| 222 | return NULL; |
| 223 | } |
| 224 | out->method.fdstream.f = fdopen(fd, "a"); |
| 225 | if (!out->method.fdstream.f) { |
| 226 | LOGERR(NULL, LY_ESYS, "Unable to open provided file descriptor (%d) for printing the output (%s).", |
| 227 | out->method.fdstream.fd, strerror(errno)); |
| 228 | free(out); |
| 229 | fclose(fd); |
| 230 | return NULL; |
| 231 | } |
| 232 | #endif |
| 233 | |
| 234 | return out; |
| 235 | } |
| 236 | |
| 237 | API int |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 238 | ly_out_fd(struct ly_out *out, int fd) |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 239 | { |
| 240 | int prev_fd; |
| 241 | |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 242 | LY_CHECK_ARG_RET(NULL, out, out->type <= LY_OUT_FDSTREAM, -1); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 243 | |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 244 | if (out->type == LY_OUT_FDSTREAM) { |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 245 | prev_fd = out->method.fdstream.fd; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 246 | } else { /* LY_OUT_FD */ |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 247 | prev_fd = out->method.fd; |
| 248 | } |
| 249 | |
| 250 | if (fd != -1) { |
| 251 | /* replace output stream */ |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 252 | if (out->type == LY_OUT_FDSTREAM) { |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 253 | int streamfd; |
| 254 | FILE *stream; |
| 255 | |
| 256 | streamfd = dup(fd); |
| 257 | if (streamfd < 0) { |
| 258 | LOGERR(NULL, LY_ESYS, "Unable to duplicate provided file descriptor (%d) for printing the output (%s).", fd, strerror(errno)); |
| 259 | return -1; |
| 260 | } |
| 261 | stream = fdopen(streamfd, "a"); |
| 262 | if (!stream) { |
| 263 | LOGERR(NULL, LY_ESYS, "Unable to open provided file descriptor (%d) for printing the output (%s).", fd, strerror(errno)); |
| 264 | close(streamfd); |
| 265 | return -1; |
| 266 | } |
| 267 | /* close only the internally created stream, file descriptor is returned and supposed to be closed by the caller */ |
| 268 | fclose(out->method.fdstream.f); |
| 269 | out->method.fdstream.f = stream; |
| 270 | out->method.fdstream.fd = streamfd; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 271 | } else { /* LY_OUT_FD */ |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 272 | out->method.fd = fd; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | return prev_fd; |
| 277 | } |
| 278 | |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 279 | API struct ly_out * |
| 280 | ly_out_new_file(FILE *f) |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 281 | { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 282 | struct ly_out *out; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 283 | |
| 284 | out = calloc(1, sizeof *out); |
| 285 | LY_CHECK_ERR_RET(!out, LOGMEM(NULL), NULL); |
| 286 | |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 287 | out->type = LY_OUT_FILE; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 288 | out->method.f = f; |
| 289 | |
| 290 | return out; |
| 291 | } |
| 292 | |
| 293 | API FILE * |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 294 | ly_out_file(struct ly_out *out, FILE *f) |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 295 | { |
| 296 | FILE *prev_f; |
| 297 | |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 298 | LY_CHECK_ARG_RET(NULL, out, out->type == LY_OUT_FILE, NULL); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 299 | |
| 300 | prev_f = out->method.f; |
| 301 | |
| 302 | if (f) { |
| 303 | out->method.f = f; |
| 304 | } |
| 305 | |
| 306 | return prev_f; |
| 307 | } |
| 308 | |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 309 | API struct ly_out * |
| 310 | ly_out_new_memory(char **strp, size_t size) |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 311 | { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 312 | struct ly_out *out; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 313 | |
| 314 | out = calloc(1, sizeof *out); |
| 315 | LY_CHECK_ERR_RET(!out, LOGMEM(NULL), NULL); |
| 316 | |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 317 | out->type = LY_OUT_MEMORY; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 318 | out->method.mem.buf = strp; |
| 319 | if (!size) { |
| 320 | /* buffer is supposed to be allocated */ |
| 321 | *strp = NULL; |
| 322 | } else if (*strp) { |
| 323 | /* there is already buffer to use */ |
| 324 | out->method.mem.size = size; |
| 325 | } |
| 326 | |
| 327 | return out; |
| 328 | } |
| 329 | |
| 330 | char * |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 331 | ly_out_memory(struct ly_out *out, char **strp, size_t size) |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 332 | { |
| 333 | char *data; |
| 334 | |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 335 | LY_CHECK_ARG_RET(NULL, out, out->type == LY_OUT_MEMORY, NULL); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 336 | |
| 337 | data = *out->method.mem.buf; |
| 338 | |
| 339 | if (strp) { |
| 340 | out->method.mem.buf = strp; |
| 341 | out->method.mem.len = out->method.mem.size = 0; |
| 342 | out->printed = 0; |
| 343 | if (!size) { |
| 344 | /* buffer is supposed to be allocated */ |
| 345 | *strp = NULL; |
| 346 | } else if (*strp) { |
| 347 | /* there is already buffer to use */ |
| 348 | out->method.mem.size = size; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | return data; |
| 353 | } |
| 354 | |
| 355 | API LY_ERR |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 356 | ly_out_reset(struct ly_out *out) |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 357 | { |
| 358 | LY_CHECK_ARG_RET(NULL, out, LY_EINVAL); |
| 359 | |
| 360 | switch(out->type) { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 361 | case LY_OUT_ERROR: |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 362 | LOGINT(NULL); |
| 363 | return LY_EINT; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 364 | case LY_OUT_FD: |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 365 | if ((lseek(out->method.fd, 0, SEEK_SET) == -1) && errno != ESPIPE) { |
| 366 | LOGERR(NULL, LY_ESYS, "Seeking output file descriptor failed (%s).", strerror(errno)); |
| 367 | return LY_ESYS; |
| 368 | } |
| 369 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 370 | case LY_OUT_FDSTREAM: |
| 371 | case LY_OUT_FILE: |
| 372 | case LY_OUT_FILEPATH: |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 373 | if ((fseek(out->method.f, 0, SEEK_SET) == -1) && errno != ESPIPE) { |
| 374 | LOGERR(NULL, LY_ESYS, "Seeking output file stream failed (%s).", strerror(errno)); |
| 375 | return LY_ESYS; |
| 376 | } |
| 377 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 378 | case LY_OUT_MEMORY: |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 379 | out->printed = 0; |
| 380 | out->method.mem.len = 0; |
| 381 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 382 | case LY_OUT_CALLBACK: |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 383 | /* nothing to do (not seekable) */ |
| 384 | break; |
| 385 | } |
| 386 | |
| 387 | return LY_SUCCESS; |
| 388 | } |
| 389 | |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 390 | API struct ly_out * |
| 391 | ly_out_new_filepath(const char *filepath) |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 392 | { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 393 | struct ly_out *out; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 394 | |
| 395 | out = calloc(1, sizeof *out); |
| 396 | LY_CHECK_ERR_RET(!out, LOGMEM(NULL), NULL); |
| 397 | |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 398 | out->type = LY_OUT_FILEPATH; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 399 | out->method.fpath.f = fopen(filepath, "w"); |
| 400 | if (!out->method.fpath.f) { |
| 401 | LOGERR(NULL, LY_ESYS, "Failed to open file \"%s\" (%s).", filepath, strerror(errno)); |
| 402 | return NULL; |
| 403 | } |
| 404 | out->method.fpath.filepath = strdup(filepath); |
| 405 | return out; |
| 406 | } |
| 407 | |
| 408 | API const char * |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 409 | ly_out_filepath(struct ly_out *out, const char *filepath) |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 410 | { |
| 411 | FILE *f; |
| 412 | |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 413 | LY_CHECK_ARG_RET(NULL, out, out->type == LY_OUT_FILEPATH, filepath ? NULL : ((void *)-1)); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 414 | |
| 415 | if (!filepath) { |
| 416 | return out->method.fpath.filepath; |
| 417 | } |
| 418 | |
| 419 | /* replace filepath */ |
| 420 | f = out->method.fpath.f; |
| 421 | out->method.fpath.f = fopen(filepath, "w"); |
| 422 | if (!out->method.fpath.f) { |
| 423 | LOGERR(NULL, LY_ESYS, "Failed to open file \"%s\" (%s).", filepath, strerror(errno)); |
| 424 | out->method.fpath.f = f; |
| 425 | return ((void *)-1); |
| 426 | } |
| 427 | fclose(f); |
| 428 | free(out->method.fpath.filepath); |
| 429 | out->method.fpath.filepath = strdup(filepath); |
| 430 | |
| 431 | return NULL; |
| 432 | } |
| 433 | |
| 434 | API void |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 435 | ly_out_free(struct ly_out *out, void (*clb_arg_destructor)(void *arg), int destroy) |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 436 | { |
| 437 | if (!out) { |
| 438 | return; |
| 439 | } |
| 440 | |
| 441 | switch (out->type) { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 442 | case LY_OUT_CALLBACK: |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 443 | if (clb_arg_destructor) { |
| 444 | clb_arg_destructor(out->method.clb.arg); |
| 445 | } |
| 446 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 447 | case LY_OUT_FDSTREAM: |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 448 | fclose(out->method.fdstream.f); |
| 449 | if (destroy) { |
| 450 | close(out->method.fdstream.fd); |
| 451 | } |
| 452 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 453 | case LY_OUT_FD: |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 454 | if (destroy) { |
| 455 | close(out->method.fd); |
| 456 | } |
| 457 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 458 | case LY_OUT_FILE: |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 459 | if (destroy) { |
| 460 | fclose(out->method.f); |
| 461 | } |
| 462 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 463 | case LY_OUT_MEMORY: |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 464 | if (destroy) { |
| 465 | free(*out->method.mem.buf); |
| 466 | } |
| 467 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 468 | case LY_OUT_FILEPATH: |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 469 | free(out->method.fpath.filepath); |
| 470 | if (destroy) { |
| 471 | fclose(out->method.fpath.f); |
| 472 | } |
| 473 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 474 | case LY_OUT_ERROR: |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 475 | LOGINT(NULL); |
| 476 | } |
| 477 | free(out); |
| 478 | } |
| 479 | |
| 480 | API LY_ERR |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 481 | ly_print(struct ly_out *out, const char *format, ...) |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 482 | { |
| 483 | int count = 0; |
| 484 | char *msg = NULL, *aux; |
| 485 | va_list ap; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 486 | |
Radek Krejci | 56cc087 | 2019-04-30 09:22:27 +0200 | [diff] [blame] | 487 | LYOUT_CHECK(out, out->status); |
| 488 | |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 489 | va_start(ap, format); |
| 490 | |
| 491 | switch (out->type) { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 492 | case LY_OUT_FD: |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 493 | #ifdef HAVE_VDPRINTF |
| 494 | count = vdprintf(out->method.fd, format, ap); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 495 | break; |
Radek Krejci | 4a0ed4a | 2019-04-18 15:08:34 +0200 | [diff] [blame] | 496 | #else |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 497 | /* never should be here since ly_out_fd() is supposed to set type to LY_OUT_FDSTREAM in case vdprintf() is missing */ |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 498 | LOGINT(NULL); |
| 499 | return LY_EINT; |
Radek Krejci | 4a0ed4a | 2019-04-18 15:08:34 +0200 | [diff] [blame] | 500 | #endif |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 501 | case LY_OUT_FDSTREAM: |
| 502 | case LY_OUT_FILEPATH: |
| 503 | case LY_OUT_FILE: |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 504 | count = vfprintf(out->method.f, format, ap); |
| 505 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 506 | case LY_OUT_MEMORY: |
Radek Krejci | 897ad2e | 2019-04-29 16:43:07 +0200 | [diff] [blame] | 507 | if ((count = vasprintf(&msg, format, ap)) < 0) { |
| 508 | break; |
| 509 | } |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 510 | if (out->method.mem.len + count + 1 > out->method.mem.size) { |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 511 | aux = ly_realloc(*out->method.mem.buf, out->method.mem.len + count + 1); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 512 | if (!aux) { |
| 513 | out->method.mem.buf = NULL; |
| 514 | out->method.mem.len = 0; |
| 515 | out->method.mem.size = 0; |
| 516 | LOGMEM(NULL); |
| 517 | va_end(ap); |
Radek Krejci | 4a0ed4a | 2019-04-18 15:08:34 +0200 | [diff] [blame] | 518 | return LY_EMEM; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 519 | } |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 520 | *out->method.mem.buf = aux; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 521 | out->method.mem.size = out->method.mem.len + count + 1; |
| 522 | } |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 523 | memcpy(&(*out->method.mem.buf)[out->method.mem.len], msg, count); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 524 | out->method.mem.len += count; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 525 | (*out->method.mem.buf)[out->method.mem.len] = '\0'; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 526 | free(msg); |
| 527 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 528 | case LY_OUT_CALLBACK: |
Radek Krejci | 897ad2e | 2019-04-29 16:43:07 +0200 | [diff] [blame] | 529 | if ((count = vasprintf(&msg, format, ap)) < 0) { |
| 530 | break; |
| 531 | } |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 532 | count = out->method.clb.func(out->method.clb.arg, msg, count); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 533 | free(msg); |
| 534 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 535 | case LY_OUT_ERROR: |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 536 | LOGINT(NULL); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | va_end(ap); |
| 540 | |
| 541 | if (count < 0) { |
Radek Krejci | 897ad2e | 2019-04-29 16:43:07 +0200 | [diff] [blame] | 542 | LOGERR(out->ctx, LY_ESYS, "%s: writing data failed (%s).", __func__, strerror(errno)); |
Radek Krejci | 56cc087 | 2019-04-30 09:22:27 +0200 | [diff] [blame] | 543 | out->status = LY_ESYS; |
Radek Krejci | 897ad2e | 2019-04-29 16:43:07 +0200 | [diff] [blame] | 544 | return LY_ESYS; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 545 | } else { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 546 | if (out->type == LY_OUT_FDSTREAM) { |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 547 | /* move the original file descriptor to the end of the output file */ |
| 548 | lseek(out->method.fdstream.fd, 0, SEEK_END); |
| 549 | } |
Radek Krejci | 897ad2e | 2019-04-29 16:43:07 +0200 | [diff] [blame] | 550 | out->printed += count; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 551 | return LY_SUCCESS; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | void |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 556 | ly_print_flush(struct ly_out *out) |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 557 | { |
| 558 | switch (out->type) { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 559 | case LY_OUT_FDSTREAM: |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 560 | /* move the original file descriptor to the end of the output file */ |
| 561 | lseek(out->method.fdstream.fd, 0, SEEK_END); |
| 562 | fflush(out->method.fdstream.f); |
| 563 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 564 | case LY_OUT_FILEPATH: |
| 565 | case LY_OUT_FILE: |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 566 | fflush(out->method.f); |
| 567 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 568 | case LY_OUT_FD: |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 569 | fsync(out->method.fd); |
| 570 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 571 | case LY_OUT_MEMORY: |
| 572 | case LY_OUT_CALLBACK: |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 573 | /* nothing to do */ |
| 574 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 575 | case LY_OUT_ERROR: |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 576 | LOGINT(NULL); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 577 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 578 | |
| 579 | free(out->buffered); |
| 580 | out->buf_size = out->buf_len = 0; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 581 | } |
| 582 | |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 583 | API LY_ERR |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 584 | ly_write(struct ly_out *out, const char *buf, size_t len) |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 585 | { |
| 586 | int written = 0; |
| 587 | |
Radek Krejci | 56cc087 | 2019-04-30 09:22:27 +0200 | [diff] [blame] | 588 | LYOUT_CHECK(out, out->status); |
| 589 | |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 590 | if (out->hole_count) { |
| 591 | /* we are buffering data after a hole */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 592 | if (out->buf_len + len > out->buf_size) { |
| 593 | out->buffered = ly_realloc(out->buffered, out->buf_len + len); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 594 | if (!out->buffered) { |
| 595 | out->buf_len = 0; |
| 596 | out->buf_size = 0; |
| 597 | LOGMEM_RET(NULL); |
| 598 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 599 | out->buf_size = out->buf_len + len; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 600 | } |
| 601 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 602 | memcpy(&out->buffered[out->buf_len], buf, len); |
| 603 | out->buf_len += len; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 604 | return LY_SUCCESS; |
| 605 | } |
| 606 | |
Radek Krejci | 897ad2e | 2019-04-29 16:43:07 +0200 | [diff] [blame] | 607 | repeat: |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 608 | switch (out->type) { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 609 | case LY_OUT_MEMORY: |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 610 | if (out->method.mem.len + len + 1 > out->method.mem.size) { |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 611 | *out->method.mem.buf = ly_realloc(*out->method.mem.buf, out->method.mem.len + len + 1); |
| 612 | if (!*out->method.mem.buf) { |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 613 | out->method.mem.len = 0; |
| 614 | out->method.mem.size = 0; |
| 615 | LOGMEM_RET(NULL); |
| 616 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 617 | out->method.mem.size = out->method.mem.len + len + 1; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 618 | } |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 619 | memcpy(&(*out->method.mem.buf)[out->method.mem.len], buf, len); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 620 | out->method.mem.len += len; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 621 | (*out->method.mem.buf)[out->method.mem.len] = '\0'; |
Radek Krejci | 897ad2e | 2019-04-29 16:43:07 +0200 | [diff] [blame] | 622 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 623 | out->printed += len; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 624 | return LY_SUCCESS; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 625 | case LY_OUT_FD: |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 626 | written = write(out->method.fd, buf, len); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 627 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 628 | case LY_OUT_FDSTREAM: |
| 629 | case LY_OUT_FILEPATH: |
| 630 | case LY_OUT_FILE: |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 631 | written = fwrite(buf, sizeof *buf, len, out->method.f); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 632 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 633 | case LY_OUT_CALLBACK: |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 634 | written = out->method.clb.func(out->method.clb.arg, buf, len); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 635 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 636 | case LY_OUT_ERROR: |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 637 | LOGINT(NULL); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | if (written < 0) { |
Radek Krejci | 897ad2e | 2019-04-29 16:43:07 +0200 | [diff] [blame] | 641 | if (errno == EAGAIN || errno == EWOULDBLOCK) { |
| 642 | goto repeat; |
| 643 | } |
| 644 | LOGERR(out->ctx, LY_ESYS, "%s: writing data failed (%s).", __func__, strerror(errno)); |
Radek Krejci | 56cc087 | 2019-04-30 09:22:27 +0200 | [diff] [blame] | 645 | out->status = LY_ESYS; |
Radek Krejci | 897ad2e | 2019-04-29 16:43:07 +0200 | [diff] [blame] | 646 | return LY_ESYS; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 647 | } else if ((size_t)written != len) { |
| 648 | LOGERR(out->ctx, LY_ESYS, "%s: writing data failed (unable to write %u from %u data).", __func__, len - (size_t)written, len); |
Radek Krejci | 56cc087 | 2019-04-30 09:22:27 +0200 | [diff] [blame] | 649 | out->status = LY_ESYS; |
Radek Krejci | 897ad2e | 2019-04-29 16:43:07 +0200 | [diff] [blame] | 650 | return LY_ESYS; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 651 | } else { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 652 | if (out->type == LY_OUT_FDSTREAM) { |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 653 | /* move the original file descriptor to the end of the output file */ |
| 654 | lseek(out->method.fdstream.fd, 0, SEEK_END); |
| 655 | } |
Radek Krejci | 897ad2e | 2019-04-29 16:43:07 +0200 | [diff] [blame] | 656 | out->printed += written; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 657 | return LY_SUCCESS; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | LY_ERR |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 662 | ly_write_skip(struct ly_out *out, size_t count, size_t *position) |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 663 | { |
Radek Krejci | 56cc087 | 2019-04-30 09:22:27 +0200 | [diff] [blame] | 664 | LYOUT_CHECK(out, out->status); |
| 665 | |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 666 | switch (out->type) { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 667 | case LY_OUT_MEMORY: |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 668 | if (out->method.mem.len + count > out->method.mem.size) { |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 669 | *out->method.mem.buf = ly_realloc(*out->method.mem.buf, out->method.mem.len + count); |
| 670 | if (!(*out->method.mem.buf)) { |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 671 | out->method.mem.len = 0; |
| 672 | out->method.mem.size = 0; |
Radek Krejci | 56cc087 | 2019-04-30 09:22:27 +0200 | [diff] [blame] | 673 | out->status = LY_ESYS; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 674 | LOGMEM_RET(NULL); |
| 675 | } |
| 676 | out->method.mem.size = out->method.mem.len + count; |
| 677 | } |
| 678 | |
| 679 | /* save the current position */ |
| 680 | *position = out->method.mem.len; |
| 681 | |
| 682 | /* skip the memory */ |
| 683 | out->method.mem.len += count; |
Radek Krejci | 897ad2e | 2019-04-29 16:43:07 +0200 | [diff] [blame] | 684 | |
| 685 | /* update printed bytes counter despite we actually printed just a hole */ |
| 686 | out->printed += count; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 687 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 688 | case LY_OUT_FD: |
| 689 | case LY_OUT_FDSTREAM: |
| 690 | case LY_OUT_FILEPATH: |
| 691 | case LY_OUT_FILE: |
| 692 | case LY_OUT_CALLBACK: |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 693 | /* buffer the hole */ |
| 694 | if (out->buf_len + count > out->buf_size) { |
| 695 | out->buffered = ly_realloc(out->buffered, out->buf_len + count); |
| 696 | if (!out->buffered) { |
| 697 | out->buf_len = 0; |
| 698 | out->buf_size = 0; |
Radek Krejci | 56cc087 | 2019-04-30 09:22:27 +0200 | [diff] [blame] | 699 | out->status = LY_ESYS; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 700 | LOGMEM_RET(NULL); |
| 701 | } |
| 702 | out->buf_size = out->buf_len + count; |
| 703 | } |
| 704 | |
| 705 | /* save the current position */ |
| 706 | *position = out->buf_len; |
| 707 | |
| 708 | /* skip the memory */ |
| 709 | out->buf_len += count; |
| 710 | |
| 711 | /* increase hole counter */ |
| 712 | ++out->hole_count; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 713 | |
| 714 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 715 | case LY_OUT_ERROR: |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 716 | LOGINT(NULL); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | return LY_SUCCESS; |
| 720 | } |
| 721 | |
| 722 | LY_ERR |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 723 | ly_write_skipped(struct ly_out *out, size_t position, const char *buf, size_t count) |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 724 | { |
| 725 | LY_ERR ret = LY_SUCCESS; |
| 726 | |
Radek Krejci | 56cc087 | 2019-04-30 09:22:27 +0200 | [diff] [blame] | 727 | LYOUT_CHECK(out, out->status); |
| 728 | |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 729 | switch (out->type) { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 730 | case LY_OUT_MEMORY: |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 731 | /* write */ |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 732 | memcpy(&(*out->method.mem.buf)[position], buf, count); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 733 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 734 | case LY_OUT_FD: |
| 735 | case LY_OUT_FDSTREAM: |
| 736 | case LY_OUT_FILEPATH: |
| 737 | case LY_OUT_FILE: |
| 738 | case LY_OUT_CALLBACK: |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 739 | if (out->buf_len < position + count) { |
Radek Krejci | 56cc087 | 2019-04-30 09:22:27 +0200 | [diff] [blame] | 740 | out->status = LY_ESYS; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 741 | LOGMEM_RET(NULL); |
| 742 | } |
| 743 | |
| 744 | /* write into the hole */ |
| 745 | memcpy(&out->buffered[position], buf, count); |
| 746 | |
| 747 | /* decrease hole counter */ |
| 748 | --out->hole_count; |
| 749 | |
| 750 | if (!out->hole_count) { |
Radek Krejci | 897ad2e | 2019-04-29 16:43:07 +0200 | [diff] [blame] | 751 | /* all holes filled, we can write the buffer, |
| 752 | * printed bytes counter is updated by ly_write() */ |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 753 | ret = ly_write(out, out->buffered, out->buf_len); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 754 | out->buf_len = 0; |
| 755 | } |
| 756 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 757 | case LY_OUT_ERROR: |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 758 | LOGINT(NULL); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 759 | } |
| 760 | |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame^] | 761 | if (out->type == LY_OUT_FILEPATH) { |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 762 | /* move the original file descriptor to the end of the output file */ |
| 763 | lseek(out->method.fdstream.fd, 0, SEEK_END); |
| 764 | } |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 765 | return ret; |
| 766 | } |