]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
doveadm: Changed most print formatters to write to ostream.
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Fri, 22 Jan 2016 17:06:59 +0000 (19:06 +0200)
committerTimo Sirainen <timo.sirainen@dovecot.fi>
Fri, 22 Jan 2016 17:38:11 +0000 (19:38 +0200)
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.

src/doveadm/client-connection.c
src/doveadm/client-connection.h
src/doveadm/doveadm-print-flow.c
src/doveadm/doveadm-print-pager.c
src/doveadm/doveadm-print-server.c
src/doveadm/doveadm-print-tab.c
src/doveadm/doveadm-print.c
src/doveadm/doveadm-print.h
src/doveadm/doveadm-server.h
src/doveadm/doveadm.c

index eca98740a848744d5e22f127e3c8dea307395d64..096424aec025faff79b025a24a6b3e5297de6417 100644 (file)
@@ -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;
-}
index f8f2ceb7bc445e01d64d855132d82bacda0a8080..a1d134cb71d0bd7ab23febfb914453d5b5c0a0ae 100644 (file)
@@ -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
index 291adf16698e0a48b2f1288e035243589787a286..9b0c51f38b16138c3a93bd0523eea06240b45d35 100644 (file)
@@ -2,10 +2,9 @@
 
 #include "lib.h"
 #include "array.h"
+#include "ostream.h"
 #include "doveadm-print-private.h"
 
-#include <stdio.h>
-
 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;
        }
 }
index d929b354d9230d2d150781861ad06d45732096d1..f792933b0c7ac6bed0c1ea38f5773fbc90c44cd3 100644 (file)
@@ -2,10 +2,9 @@
 
 #include "lib.h"
 #include "array.h"
+#include "ostream.h"
 #include "doveadm-print-private.h"
 
-#include <stdio.h>
-
 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;
        }
 }
index 431cdf6fc8e81376383764fc5c4d19f85901f977..89d6ab940318fff5e1008e406cd2a393443be620 100644 (file)
@@ -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);
 }
index 20e30607a5abf9eb2ca5cd53566fc36a0e3519c0..e6b241172b7c4902c3cb31cc37b72a2c82a43572 100644 (file)
@@ -2,10 +2,9 @@
 
 #include "lib.h"
 #include "array.h"
+#include "ostream.h"
 #include "doveadm-print-private.h"
 
-#include <stdio.h>
-
 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)
index fa22cfb9cfabbc659dfddf556e74a794d76b3f70..5f239148e67ae793b76624e681b9d0c923b9d1bc 100644 (file)
@@ -3,10 +3,9 @@
 #include "lib.h"
 #include "array.h"
 #include "istream.h"
+#include "ostream.h"
 #include "doveadm-print-private.h"
 
-#include <stdio.h>
-
 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)
index de0917f8cde69494d59fce94948c60d5b386266c..78c9b57e2aac2657d0ec9fe672254d6c9034436b 100644 (file)
@@ -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);
 
index d68616cbc9f58e9ed4b28018d7c7472c5f61395b..ec479537f9fa8cc98b0825650ca5b644d04d29ee 100644 (file)
@@ -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;
 
index 78903d41ea6628b10714932258b03a869f92231e..7a85a310f909f012c7b9dbddeebe4c7509f1ccba 100644 (file)
@@ -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);