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