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