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 | } |
Michal Vasko | 159b887 | 2020-11-18 18:14:16 +0100 | [diff] [blame] | 436 | |
| 437 | free(out->buffered); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 438 | free(out); |
| 439 | } |
| 440 | |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 441 | static LY_ERR |
| 442 | ly_vprint_(struct ly_out *out, const char *format, va_list ap) |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 443 | { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 444 | LY_ERR ret; |
| 445 | int written = 0; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 446 | char *msg = NULL, *aux; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 447 | |
| 448 | switch (out->type) { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 449 | case LY_OUT_FD: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 450 | written = vdprintf(out->method.fd, format, ap); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 451 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 452 | case LY_OUT_FDSTREAM: |
| 453 | case LY_OUT_FILEPATH: |
| 454 | case LY_OUT_FILE: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 455 | written = vfprintf(out->method.f, 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_MEMORY: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 458 | if ((written = vasprintf(&msg, format, ap)) < 0) { |
Radek Krejci | 897ad2e | 2019-04-29 16:43:07 +0200 | [diff] [blame] | 459 | break; |
| 460 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 461 | if (out->method.mem.len + written + 1 > out->method.mem.size) { |
| 462 | 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] | 463 | if (!aux) { |
| 464 | out->method.mem.buf = NULL; |
| 465 | out->method.mem.len = 0; |
| 466 | out->method.mem.size = 0; |
| 467 | LOGMEM(NULL); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 468 | return LY_EMEM; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 469 | } |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 470 | *out->method.mem.buf = aux; |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 471 | out->method.mem.size = out->method.mem.len + written + 1; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 472 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 473 | memcpy(&(*out->method.mem.buf)[out->method.mem.len], msg, written); |
| 474 | out->method.mem.len += written; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 475 | (*out->method.mem.buf)[out->method.mem.len] = '\0'; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 476 | free(msg); |
| 477 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 478 | case LY_OUT_CALLBACK: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 479 | if ((written = vasprintf(&msg, format, ap)) < 0) { |
Radek Krejci | 897ad2e | 2019-04-29 16:43:07 +0200 | [diff] [blame] | 480 | break; |
| 481 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 482 | written = out->method.clb.func(out->method.clb.arg, msg, written); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 483 | free(msg); |
| 484 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 485 | case LY_OUT_ERROR: |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 486 | LOGINT(NULL); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 487 | return LY_EINT; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 488 | } |
| 489 | |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 490 | if (written < 0) { |
| 491 | LOGERR(NULL, LY_ESYS, "%s: writing data failed (%s).", __func__, strerror(errno)); |
| 492 | written = 0; |
| 493 | ret = LY_ESYS; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 494 | } else { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 495 | if (out->type == LY_OUT_FDSTREAM) { |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 496 | /* move the original file descriptor to the end of the output file */ |
| 497 | lseek(out->method.fdstream.fd, 0, SEEK_END); |
| 498 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 499 | ret = LY_SUCCESS; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 500 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 501 | |
| 502 | out->printed += written; |
| 503 | out->func_printed += written; |
| 504 | return ret; |
| 505 | } |
| 506 | |
| 507 | LY_ERR |
| 508 | ly_print_(struct ly_out *out, const char *format, ...) |
| 509 | { |
| 510 | LY_ERR ret; |
| 511 | va_list ap; |
| 512 | |
| 513 | va_start(ap, format); |
| 514 | ret = ly_vprint_(out, format, ap); |
| 515 | va_end(ap); |
| 516 | |
| 517 | return ret; |
| 518 | } |
| 519 | |
| 520 | API LY_ERR |
| 521 | ly_print(struct ly_out *out, const char *format, ...) |
| 522 | { |
| 523 | LY_ERR ret; |
| 524 | va_list ap; |
| 525 | |
| 526 | out->func_printed = 0; |
| 527 | |
| 528 | va_start(ap, format); |
| 529 | ret = ly_vprint_(out, format, ap); |
| 530 | va_end(ap); |
| 531 | |
| 532 | return ret; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 533 | } |
| 534 | |
Radek Krejci | 2aae375 | 2020-05-27 18:16:30 +0200 | [diff] [blame] | 535 | API void |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 536 | ly_print_flush(struct ly_out *out) |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 537 | { |
| 538 | switch (out->type) { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 539 | case LY_OUT_FDSTREAM: |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 540 | /* move the original file descriptor to the end of the output file */ |
| 541 | lseek(out->method.fdstream.fd, 0, SEEK_END); |
| 542 | fflush(out->method.fdstream.f); |
| 543 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 544 | case LY_OUT_FILEPATH: |
| 545 | case LY_OUT_FILE: |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 546 | fflush(out->method.f); |
| 547 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 548 | case LY_OUT_FD: |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 549 | fsync(out->method.fd); |
| 550 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 551 | case LY_OUT_MEMORY: |
| 552 | case LY_OUT_CALLBACK: |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 553 | /* nothing to do */ |
| 554 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 555 | case LY_OUT_ERROR: |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 556 | LOGINT(NULL); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 557 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 558 | |
| 559 | free(out->buffered); |
| 560 | out->buf_size = out->buf_len = 0; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 561 | } |
| 562 | |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 563 | LY_ERR |
| 564 | ly_write_(struct ly_out *out, const char *buf, size_t len) |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 565 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 566 | LY_ERR ret = LY_SUCCESS; |
| 567 | size_t written = 0; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 568 | |
| 569 | if (out->hole_count) { |
| 570 | /* we are buffering data after a hole */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 571 | if (out->buf_len + len > out->buf_size) { |
| 572 | out->buffered = ly_realloc(out->buffered, out->buf_len + len); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 573 | if (!out->buffered) { |
| 574 | out->buf_len = 0; |
| 575 | out->buf_size = 0; |
Radek Krejci | baeb838 | 2020-05-27 16:44:53 +0200 | [diff] [blame] | 576 | LOGMEM(NULL); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 577 | return LY_EMEM; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 578 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 579 | out->buf_size = out->buf_len + len; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 580 | } |
| 581 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 582 | memcpy(&out->buffered[out->buf_len], buf, len); |
| 583 | out->buf_len += len; |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 584 | |
| 585 | out->printed += len; |
| 586 | out->func_printed += len; |
| 587 | return LY_SUCCESS; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 588 | } |
| 589 | |
Radek Krejci | 897ad2e | 2019-04-29 16:43:07 +0200 | [diff] [blame] | 590 | repeat: |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 591 | switch (out->type) { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 592 | case LY_OUT_MEMORY: |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 593 | if (out->method.mem.len + len + 1 > out->method.mem.size) { |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 594 | *out->method.mem.buf = ly_realloc(*out->method.mem.buf, out->method.mem.len + len + 1); |
| 595 | if (!*out->method.mem.buf) { |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 596 | out->method.mem.len = 0; |
| 597 | out->method.mem.size = 0; |
Radek Krejci | baeb838 | 2020-05-27 16:44:53 +0200 | [diff] [blame] | 598 | LOGMEM(NULL); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 599 | return LY_EMEM; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 600 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 601 | out->method.mem.size = out->method.mem.len + len + 1; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 602 | } |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 603 | memcpy(&(*out->method.mem.buf)[out->method.mem.len], buf, len); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 604 | out->method.mem.len += len; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 605 | (*out->method.mem.buf)[out->method.mem.len] = '\0'; |
Radek Krejci | 897ad2e | 2019-04-29 16:43:07 +0200 | [diff] [blame] | 606 | |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 607 | written = len; |
| 608 | break; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 609 | case LY_OUT_FD: { |
| 610 | ssize_t r; |
| 611 | r = write(out->method.fd, buf, len); |
| 612 | if (r < 0) { |
| 613 | ret = LY_ESYS; |
| 614 | } else { |
| 615 | written = (size_t)r; |
| 616 | } |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 617 | break; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 618 | } |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 619 | case LY_OUT_FDSTREAM: |
| 620 | case LY_OUT_FILEPATH: |
| 621 | case LY_OUT_FILE: |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 622 | written = fwrite(buf, sizeof *buf, len, out->method.f); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 623 | if (written != len) { |
| 624 | ret = LY_ESYS; |
| 625 | } |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 626 | break; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 627 | case LY_OUT_CALLBACK: { |
| 628 | ssize_t r; |
| 629 | r = out->method.clb.func(out->method.clb.arg, buf, len); |
| 630 | if (r < 0) { |
| 631 | ret = LY_ESYS; |
| 632 | } else { |
| 633 | written = (size_t)r; |
| 634 | } |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 635 | break; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 636 | } |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 637 | case LY_OUT_ERROR: |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 638 | LOGINT(NULL); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 639 | return LY_EINT; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 640 | } |
| 641 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 642 | if (ret) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 643 | if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 644 | ret = LY_SUCCESS; |
Radek Krejci | 897ad2e | 2019-04-29 16:43:07 +0200 | [diff] [blame] | 645 | goto repeat; |
| 646 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 647 | LOGERR(NULL, LY_ESYS, "%s: writing data failed (%s).", __func__, strerror(errno)); |
| 648 | written = 0; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 649 | } else if ((size_t)written != len) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 650 | 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] | 651 | len - (size_t)written, len); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 652 | ret = LY_ESYS; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 653 | } else { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 654 | if (out->type == LY_OUT_FDSTREAM) { |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 655 | /* move the original file descriptor to the end of the output file */ |
| 656 | lseek(out->method.fdstream.fd, 0, SEEK_END); |
| 657 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 658 | ret = LY_SUCCESS; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 659 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 660 | |
| 661 | out->printed += written; |
| 662 | out->func_printed += written; |
| 663 | return ret; |
| 664 | } |
| 665 | |
| 666 | API LY_ERR |
| 667 | ly_write(struct ly_out *out, const char *buf, size_t len) |
| 668 | { |
| 669 | out->func_printed = 0; |
| 670 | |
| 671 | return ly_write_(out, buf, len); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 672 | } |
| 673 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 674 | API size_t |
| 675 | ly_out_printed(const struct ly_out *out) |
| 676 | { |
| 677 | return out->func_printed; |
| 678 | } |
| 679 | |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 680 | LY_ERR |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 681 | 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] | 682 | { |
| 683 | switch (out->type) { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 684 | case LY_OUT_MEMORY: |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 685 | if (out->method.mem.len + count > out->method.mem.size) { |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 686 | *out->method.mem.buf = ly_realloc(*out->method.mem.buf, out->method.mem.len + count); |
| 687 | if (!(*out->method.mem.buf)) { |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 688 | out->method.mem.len = 0; |
| 689 | out->method.mem.size = 0; |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 690 | LOGMEM(NULL); |
| 691 | return LY_EMEM; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 692 | } |
| 693 | out->method.mem.size = out->method.mem.len + count; |
| 694 | } |
| 695 | |
| 696 | /* save the current position */ |
| 697 | *position = out->method.mem.len; |
| 698 | |
| 699 | /* skip the memory */ |
| 700 | out->method.mem.len += count; |
| 701 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 702 | case LY_OUT_FD: |
| 703 | case LY_OUT_FDSTREAM: |
| 704 | case LY_OUT_FILEPATH: |
| 705 | case LY_OUT_FILE: |
| 706 | case LY_OUT_CALLBACK: |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 707 | /* buffer the hole */ |
| 708 | if (out->buf_len + count > out->buf_size) { |
| 709 | out->buffered = ly_realloc(out->buffered, out->buf_len + count); |
| 710 | if (!out->buffered) { |
| 711 | out->buf_len = 0; |
| 712 | out->buf_size = 0; |
Radek Krejci | baeb838 | 2020-05-27 16:44:53 +0200 | [diff] [blame] | 713 | LOGMEM(NULL); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 714 | return LY_EMEM; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 715 | } |
| 716 | out->buf_size = out->buf_len + count; |
| 717 | } |
| 718 | |
| 719 | /* save the current position */ |
| 720 | *position = out->buf_len; |
| 721 | |
| 722 | /* skip the memory */ |
| 723 | out->buf_len += count; |
| 724 | |
| 725 | /* increase hole counter */ |
| 726 | ++out->hole_count; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 727 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 728 | case LY_OUT_ERROR: |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 729 | LOGINT(NULL); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 730 | return LY_EINT; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 731 | } |
| 732 | |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 733 | /* update printed bytes counter despite we actually printed just a hole */ |
| 734 | out->printed += count; |
| 735 | out->func_printed += count; |
| 736 | return LY_SUCCESS; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 737 | } |
| 738 | |
Michal Vasko | 66d9997 | 2020-06-29 13:37:42 +0200 | [diff] [blame] | 739 | LY_ERR |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 740 | 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] | 741 | { |
Michal Vasko | 66d9997 | 2020-06-29 13:37:42 +0200 | [diff] [blame] | 742 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 743 | |
| 744 | switch (out->type) { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 745 | case LY_OUT_MEMORY: |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 746 | /* write */ |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 747 | memcpy(&(*out->method.mem.buf)[position], buf, count); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 748 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 749 | case LY_OUT_FD: |
| 750 | case LY_OUT_FDSTREAM: |
| 751 | case LY_OUT_FILEPATH: |
| 752 | case LY_OUT_FILE: |
| 753 | case LY_OUT_CALLBACK: |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 754 | if (out->buf_len < position + count) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 755 | LOGMEM(NULL); |
| 756 | return LY_EMEM; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | /* write into the hole */ |
| 760 | memcpy(&out->buffered[position], buf, count); |
| 761 | |
| 762 | /* decrease hole counter */ |
| 763 | --out->hole_count; |
| 764 | |
| 765 | if (!out->hole_count) { |
Radek Krejci | 897ad2e | 2019-04-29 16:43:07 +0200 | [diff] [blame] | 766 | /* all holes filled, we can write the buffer, |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 767 | * printed bytes counter is updated by ly_write_() */ |
| 768 | ret = ly_write_(out, out->buffered, out->buf_len); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 769 | out->buf_len = 0; |
| 770 | } |
| 771 | break; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 772 | case LY_OUT_ERROR: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 773 | LOGINT(NULL); |
| 774 | return LY_EINT; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 775 | } |
| 776 | |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 777 | if (out->type == LY_OUT_FILEPATH) { |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 778 | /* move the original file descriptor to the end of the output file */ |
| 779 | lseek(out->method.fdstream.fd, 0, SEEK_END); |
| 780 | } |
Michal Vasko | 66d9997 | 2020-06-29 13:37:42 +0200 | [diff] [blame] | 781 | return ret; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 782 | } |