From: Timo Sirainen Date: Fri, 22 Jan 2016 17:06:59 +0000 (+0200) Subject: doveadm: Changed most print formatters to write to ostream. X-Git-Tag: 2.2.22.rc1~311 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=57591c5ea045b6829ebcfed9b145d719d63f935a;p=thirdparty%2Fdovecot%2Fcore.git doveadm: Changed most print formatters to write to ostream. This allows all the formatters to be used when sending replies to the upcoming doveadm HTTP server responses. The "table" formatter wasn't modified, because it writes to both stdout and stderr. --- diff --git a/src/doveadm/client-connection.c b/src/doveadm/client-connection.c index eca98740a8..096424aec0 100644 --- a/src/doveadm/client-connection.c +++ b/src/doveadm/client-connection.c @@ -151,6 +151,7 @@ doveadm_mail_cmd_server_run(struct client_connection *conn, int ret; ctx->conn = conn; + o_stream_cork(conn->output); if (ctx->v.preinit != NULL) ctx->v.preinit(ctx); @@ -173,6 +174,7 @@ doveadm_mail_cmd_server_run(struct client_connection *conn, } else { o_stream_nsend(conn->output, "\n+\n", 3); } + o_stream_uncork(conn->output); pool_unref(&ctx->pool); } @@ -517,6 +519,8 @@ client_connection_create(int fd, int listen_fd, bool ssl) } } client_connection_send_auth_handshake(conn, listen_fd); + + doveadm_print_ostream = conn->output; return conn; } @@ -535,11 +539,7 @@ void client_connection_destroy(struct client_connection **_conn) i_error("close(client) failed: %m"); pool_unref(&conn->pool); + doveadm_print_ostream = NULL; doveadm_client = NULL; master_service_client_connection_destroyed(master_service); } - -struct ostream *client_connection_get_output(struct client_connection *conn) -{ - return conn->output; -} diff --git a/src/doveadm/client-connection.h b/src/doveadm/client-connection.h index f8f2ceb7bc..a1d134cb71 100644 --- a/src/doveadm/client-connection.h +++ b/src/doveadm/client-connection.h @@ -23,6 +23,4 @@ struct client_connection * client_connection_create(int fd, int listen_fd, bool ssl); void client_connection_destroy(struct client_connection **conn); -struct ostream *client_connection_get_output(struct client_connection *conn); - #endif diff --git a/src/doveadm/doveadm-print-flow.c b/src/doveadm/doveadm-print-flow.c index 291adf1669..9b0c51f38b 100644 --- a/src/doveadm/doveadm-print-flow.c +++ b/src/doveadm/doveadm-print-flow.c @@ -2,10 +2,9 @@ #include "lib.h" #include "array.h" +#include "ostream.h" #include "doveadm-print-private.h" -#include - struct doveadm_print_flow_header { const char *title; enum doveadm_print_header_flags flags; @@ -34,10 +33,18 @@ doveadm_print_flow_header(const struct doveadm_print_header *hdr) static void flow_next_hdr(void) { if (++ctx->header_idx < array_count(&ctx->headers)) - printf(" "); + o_stream_nsend(doveadm_print_ostream, " ", 1); else { ctx->header_idx = 0; - printf("\n"); + o_stream_nsend(doveadm_print_ostream, "\n", 1); + } +} + +static void doveadm_print_flow_print_heder(const struct doveadm_print_flow_header *hdr) +{ + if ((hdr->flags & DOVEADM_PRINT_HEADER_FLAG_HIDE_TITLE) == 0) { + o_stream_nsend_str(doveadm_print_ostream, hdr->title); + o_stream_nsend(doveadm_print_ostream, "=", 1); } } @@ -46,9 +53,8 @@ static void doveadm_print_flow_print(const char *value) const struct doveadm_print_flow_header *hdr = array_idx(&ctx->headers, ctx->header_idx); - if ((hdr->flags & DOVEADM_PRINT_HEADER_FLAG_HIDE_TITLE) == 0) - printf("%s=", hdr->title); - printf("%s", value); + doveadm_print_flow_print_heder(hdr); + o_stream_nsend_str(doveadm_print_ostream, value); flow_next_hdr(); } @@ -60,10 +66,9 @@ doveadm_print_flow_print_stream(const unsigned char *value, size_t size) if (!ctx->streaming) { ctx->streaming = TRUE; - if ((hdr->flags & DOVEADM_PRINT_HEADER_FLAG_HIDE_TITLE) == 0) - printf("%s=", hdr->title); + doveadm_print_flow_print_heder(hdr); } - fwrite(value, 1, size, stdout); + o_stream_nsend(doveadm_print_ostream, value, size); if (size == 0) { flow_next_hdr(); ctx->streaming = FALSE; @@ -83,7 +88,7 @@ static void doveadm_print_flow_init(void) static void doveadm_print_flow_flush(void) { if (ctx->header_idx != 0) { - printf("\n"); + o_stream_nsend(doveadm_print_ostream, "\n", 1); ctx->header_idx = 0; } } diff --git a/src/doveadm/doveadm-print-pager.c b/src/doveadm/doveadm-print-pager.c index d929b354d9..f792933b0c 100644 --- a/src/doveadm/doveadm-print-pager.c +++ b/src/doveadm/doveadm-print-pager.c @@ -2,10 +2,9 @@ #include "lib.h" #include "array.h" +#include "ostream.h" #include "doveadm-print-private.h" -#include - struct doveadm_print_pager_header { const char *title; }; @@ -33,7 +32,7 @@ static void pager_next_hdr(void) { if (++ctx->header_idx == array_count(&ctx->headers)) { ctx->header_idx = 0; - printf("\f\n"); + o_stream_nsend(doveadm_print_ostream, "\f\n", 2); } } @@ -42,7 +41,10 @@ static void doveadm_print_pager_print(const char *value) const struct doveadm_print_pager_header *hdr = array_idx(&ctx->headers, ctx->header_idx); - printf("%s: %s\n", hdr->title, value); + o_stream_nsend_str(doveadm_print_ostream, hdr->title); + o_stream_nsend(doveadm_print_ostream, ": ", 2); + o_stream_nsend_str(doveadm_print_ostream, value); + o_stream_nsend(doveadm_print_ostream, "\n", 1); pager_next_hdr(); } @@ -54,9 +56,10 @@ doveadm_print_pager_print_stream(const unsigned char *value, size_t size) if (!ctx->streaming) { ctx->streaming = TRUE; - printf("%s:\n", hdr->title); + o_stream_nsend_str(doveadm_print_ostream, hdr->title); + o_stream_nsend(doveadm_print_ostream, ":\n", 2); } - fwrite(value, 1, size, stdout); + o_stream_nsend(doveadm_print_ostream, value, size); if (size == 0) { pager_next_hdr(); ctx->streaming = FALSE; @@ -76,7 +79,7 @@ static void doveadm_print_pager_init(void) static void doveadm_print_pager_flush(void) { if (ctx->header_idx != 0) { - printf("\n"); + o_stream_nsend(doveadm_print_ostream, "\n", 1); ctx->header_idx = 0; } } diff --git a/src/doveadm/doveadm-print-server.c b/src/doveadm/doveadm-print-server.c index 431cdf6fc8..89d6ab9403 100644 --- a/src/doveadm/doveadm-print-server.c +++ b/src/doveadm/doveadm-print-server.c @@ -6,7 +6,6 @@ #include "strescape.h" #include "ostream.h" #include "client-connection.h" -#include "doveadm-server.h" #include "doveadm-print-private.h" struct doveadm_print_server_context { @@ -65,10 +64,7 @@ doveadm_print_server_print_stream(const unsigned char *value, size_t size) static void doveadm_print_server_flush(void) { - if (doveadm_client == NULL) - return; - - o_stream_nsend(client_connection_get_output(doveadm_client), + o_stream_nsend(doveadm_print_ostream, str_data(ctx.str), str_len(ctx.str)); str_truncate(ctx.str, 0); } diff --git a/src/doveadm/doveadm-print-tab.c b/src/doveadm/doveadm-print-tab.c index 20e30607a5..e6b241172b 100644 --- a/src/doveadm/doveadm-print-tab.c +++ b/src/doveadm/doveadm-print-tab.c @@ -2,10 +2,9 @@ #include "lib.h" #include "array.h" +#include "ostream.h" #include "doveadm-print-private.h" -#include - struct doveadm_print_tab_context { unsigned int header_idx, header_count; @@ -18,7 +17,7 @@ static void doveadm_print_tab_flush_header(void) { if (!ctx.header_written) { if (!doveadm_print_hide_titles) - printf("\n"); + o_stream_nsend(doveadm_print_ostream, "\n", 1); ctx.header_written = TRUE; } } @@ -29,8 +28,8 @@ doveadm_print_tab_header(const struct doveadm_print_header *hdr) ctx.header_count++; if (!doveadm_print_hide_titles) { if (ctx.header_count > 1) - printf("\t"); - printf("%s", hdr->title); + o_stream_nsend(doveadm_print_ostream, "\t", 1); + o_stream_nsend_str(doveadm_print_ostream, hdr->title); } } @@ -38,12 +37,12 @@ static void doveadm_print_tab_print(const char *value) { doveadm_print_tab_flush_header(); if (ctx.header_idx > 0) - printf("\t"); - printf("%s", value); + o_stream_nsend(doveadm_print_ostream, "\t", 1); + o_stream_nsend_str(doveadm_print_ostream, value); if (++ctx.header_idx == ctx.header_count) { ctx.header_idx = 0; - printf("\n"); + o_stream_nsend(doveadm_print_ostream, "\n", 1); } } @@ -56,8 +55,8 @@ doveadm_print_tab_print_stream(const unsigned char *value, size_t size) } doveadm_print_tab_flush_header(); if (ctx.header_idx > 0) - printf("\t"); - fwrite(value, 1, size, stdout); + o_stream_nsend(doveadm_print_ostream, "\t", 1); + o_stream_nsend(doveadm_print_ostream, value, size); } static void doveadm_print_tab_flush(void) diff --git a/src/doveadm/doveadm-print.c b/src/doveadm/doveadm-print.c index fa22cfb9cf..5f239148e6 100644 --- a/src/doveadm/doveadm-print.c +++ b/src/doveadm/doveadm-print.c @@ -3,10 +3,9 @@ #include "lib.h" #include "array.h" #include "istream.h" +#include "ostream.h" #include "doveadm-print-private.h" -#include - struct doveadm_print_header_context { const char *key; char *sticky_value; @@ -23,6 +22,7 @@ struct doveadm_print_context { }; bool doveadm_print_hide_titles = FALSE; +struct ostream *doveadm_print_ostream = NULL; static struct doveadm_print_context *ctx; @@ -147,7 +147,8 @@ void doveadm_print_flush(void) { if (ctx != NULL && ctx->v->flush != NULL) ctx->v->flush(); - fflush(stdout); + o_stream_uncork(doveadm_print_ostream); + o_stream_cork(doveadm_print_ostream); } void doveadm_print_unstick_headers(void) @@ -194,8 +195,10 @@ void doveadm_print_deinit(void) if (ctx == NULL) return; - if (ctx->v->flush != NULL) + if (ctx->v->flush != NULL && doveadm_print_ostream != NULL) { ctx->v->flush(); + o_stream_uncork(doveadm_print_ostream); + } if (ctx->v->deinit != NULL) ctx->v->deinit(); array_foreach_modifiable(&ctx->headers, hdr) diff --git a/src/doveadm/doveadm-print.h b/src/doveadm/doveadm-print.h index de0917f8cd..78c9b57e2a 100644 --- a/src/doveadm/doveadm-print.h +++ b/src/doveadm/doveadm-print.h @@ -4,6 +4,7 @@ #define DOVEADM_PRINT_TYPE_TAB "tab" #define DOVEADM_PRINT_TYPE_FLOW "flow" #define DOVEADM_PRINT_TYPE_TABLE "table" +#define DOVEADM_PRINT_TYPE_SERVER "server" enum doveadm_print_header_flags { DOVEADM_PRINT_HEADER_FLAG_RIGHT_JUSTIFY = 0x01, @@ -14,6 +15,8 @@ enum doveadm_print_header_flags { extern const struct doveadm_print_vfuncs *doveadm_print_vfuncs_all[]; extern bool doveadm_print_hide_titles; +/* points to either stdout or to doveadm-server's TCP connection */ +extern struct ostream *doveadm_print_ostream; bool doveadm_print_is_initialized(void); diff --git a/src/doveadm/doveadm-server.h b/src/doveadm/doveadm-server.h index d68616cbc9..ec479537f9 100644 --- a/src/doveadm/doveadm-server.h +++ b/src/doveadm/doveadm-server.h @@ -1,8 +1,6 @@ #ifndef DOVEADM_SERVER_H #define DOVEADM_SERVER_H -#define DOVEADM_PRINT_TYPE_SERVER "server" - extern struct client_connection *doveadm_client; extern struct doveadm_print_vfuncs doveadm_print_server_vfuncs; diff --git a/src/doveadm/doveadm.c b/src/doveadm/doveadm.c index 78903d41ea..7a85a310f9 100644 --- a/src/doveadm/doveadm.c +++ b/src/doveadm/doveadm.c @@ -3,6 +3,7 @@ #include "lib.h" #include "array.h" #include "str.h" +#include "ostream.h" #include "env-util.h" #include "execv-const.h" #include "dict.h" @@ -316,6 +317,8 @@ int main(int argc, char *argv[]) quick_init = TRUE; } else { quick_init = FALSE; + doveadm_print_ostream = o_stream_create_fd(STDOUT_FILENO, 0, FALSE); + o_stream_set_no_error_handling(doveadm_print_ostream, TRUE); doveadm_dump_init(); doveadm_mail_init(); dict_drivers_register_builtin(); @@ -355,6 +358,7 @@ int main(int argc, char *argv[]) doveadm_unload_modules(); dict_drivers_unregister_builtin(); doveadm_print_deinit(); + o_stream_unref(&doveadm_print_ostream); } doveadm_cmds_deinit(); master_service_deinit(&master_service);