changeset 19609:e7e593e68fce

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.
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Fri, 22 Jan 2016 19:06:59 +0200
parents 01e33753b08e
children a39061958832
files 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
diffstat 10 files changed, 55 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/src/doveadm/client-connection.c	Fri Jan 22 19:08:11 2016 +0200
+++ b/src/doveadm/client-connection.c	Fri Jan 22 19:06:59 2016 +0200
@@ -151,6 +151,7 @@
 	int ret;
 
 	ctx->conn = conn;
+	o_stream_cork(conn->output);
 
 	if (ctx->v.preinit != NULL)
 		ctx->v.preinit(ctx);
@@ -173,6 +174,7 @@
 	} else {
 		o_stream_nsend(conn->output, "\n+\n", 3);
 	}
+	o_stream_uncork(conn->output);
 	pool_unref(&ctx->pool);
 }
 
@@ -517,6 +519,8 @@
 		}
 	}
 	client_connection_send_auth_handshake(conn, listen_fd);
+
+	doveadm_print_ostream = conn->output;
 	return conn;
 }
 
@@ -535,11 +539,7 @@
 		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;
-}
--- a/src/doveadm/client-connection.h	Fri Jan 22 19:08:11 2016 +0200
+++ b/src/doveadm/client-connection.h	Fri Jan 22 19:06:59 2016 +0200
@@ -23,6 +23,4 @@
 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
--- a/src/doveadm/doveadm-print-flow.c	Fri Jan 22 19:08:11 2016 +0200
+++ b/src/doveadm/doveadm-print-flow.c	Fri Jan 22 19:06:59 2016 +0200
@@ -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 @@
 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 @@
 	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 @@
 
 	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_flush(void)
 {
 	if (ctx->header_idx != 0) {
-		printf("\n");
+		o_stream_nsend(doveadm_print_ostream, "\n", 1);
 		ctx->header_idx = 0;
 	}
 }
--- a/src/doveadm/doveadm-print-pager.c	Fri Jan 22 19:08:11 2016 +0200
+++ b/src/doveadm/doveadm-print-pager.c	Fri Jan 22 19:06:59 2016 +0200
@@ -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 @@
 {
 	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 @@
 	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 @@
 
 	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_flush(void)
 {
 	if (ctx->header_idx != 0) {
-		printf("\n");
+		o_stream_nsend(doveadm_print_ostream, "\n", 1);
 		ctx->header_idx = 0;
 	}
 }
--- a/src/doveadm/doveadm-print-server.c	Fri Jan 22 19:08:11 2016 +0200
+++ b/src/doveadm/doveadm-print-server.c	Fri Jan 22 19:06:59 2016 +0200
@@ -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 @@
 
 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);
 }
--- a/src/doveadm/doveadm-print-tab.c	Fri Jan 22 19:08:11 2016 +0200
+++ b/src/doveadm/doveadm-print-tab.c	Fri Jan 22 19:06:59 2016 +0200
@@ -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 @@
 {
 	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 @@
 	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 @@
 {
 	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_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)
--- a/src/doveadm/doveadm-print.c	Fri Jan 22 19:08:11 2016 +0200
+++ b/src/doveadm/doveadm-print.c	Fri Jan 22 19:06:59 2016 +0200
@@ -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 @@
 };
 
 bool doveadm_print_hide_titles = FALSE;
+struct ostream *doveadm_print_ostream = NULL;
 
 static struct doveadm_print_context *ctx;
 
@@ -147,7 +147,8 @@
 {
 	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 @@
 	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)
--- a/src/doveadm/doveadm-print.h	Fri Jan 22 19:08:11 2016 +0200
+++ b/src/doveadm/doveadm-print.h	Fri Jan 22 19:06:59 2016 +0200
@@ -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 @@
 
 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);
 
--- a/src/doveadm/doveadm-server.h	Fri Jan 22 19:08:11 2016 +0200
+++ b/src/doveadm/doveadm-server.h	Fri Jan 22 19:06:59 2016 +0200
@@ -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;
 
--- a/src/doveadm/doveadm.c	Fri Jan 22 19:08:11 2016 +0200
+++ b/src/doveadm/doveadm.c	Fri Jan 22 19:06:59 2016 +0200
@@ -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 @@
 		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 @@
 		doveadm_unload_modules();
 		dict_drivers_unregister_builtin();
 		doveadm_print_deinit();
+		o_stream_unref(&doveadm_print_ostream);
 	}
 	doveadm_cmds_deinit();
 	master_service_deinit(&master_service);