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