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